找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 4474|回复: 2
收起左侧

LCD1602+STC15W408AS单片机串行调试工具代码

[复制链接]
ID:326783 发表于 2018-11-24 14:56 | 显示全部楼层 |阅读模式
制作出来的实物图如下:
IMG_2962[1].jpg IMG_2964[1].jpg IMG_2966[1].jpg

用STC15W408as+LCD1602做了个串口收发,可以设置波特率、设定发送的字节个数、设定要发送的字节、及收发模式,进行串口调试时,可以不用电脑了,很方便不是么。

单片机源程序如下:
  1. #include "STC15W408AS.H"
  2. #include "Lcd.h"
  3. #include "key.h"
  4. #include "E2prom.h"
  5. #include "Serive_usart.h"

  6. uchar b_value=0;
  7. uchar Ser_state=0;
  8. bit byte_bit=0;
  9. uchar Data_Len=1;
  10. uchar Menu_Count=0;
  11. uchar KEY_Value=0, Arr_point=0, old_apoint=0;
  12. uchar Mu4_L=0, Mu4_H=0;
  13. uchar idata Send_Buffer[10]={0};
  14. uchar idata Rec_Buffer[10]={0};                /*设立接收数组*/
  15. uchar R_Count=0;
  16. bit R_Flag=0;
  17. uint R_outtime=0;
  18. bit R_okFlag=0;
  19. uchar code tab1[]={" Serial_Receive "};
  20. uchar code tab2[]={"BAND =          "};
  21. uchar code tab4[]={"[+][-]      [En]"};
  22. uchar code tab5[6][5]= {{"1200 "},
  23.                         {"2400 "},
  24.                         {"4800 "},
  25.                         {"9600 "},
  26.                         {"14400"},
  27.                         {"19200"}};
  28. uchar code tab6[5][10]= {{"1:Set_Baud"},    /*开机选择画面0-选择波特率*/
  29.                         {"2:Set_Len "},
  30.                           {"3:Set_SeBy"},    /*1-选择要发送的字节*/
  31.                           {"4:Set_Mode"},    /*2-选择模式*/
  32.                           {"5: Start  "}};    /*3-是否开始*/
  33. uchar code tab7[2][7]= {{"Send   "},
  34.                        {"Receive"}};
  35. uchar code tab8[13]= {"Set Send Len="};
  36. uchar code tab9[16]= {"S=0 R=1 sta= 0  "};
  37. uchar code tab10[16]={"[<][>][+][^][En]"};
  38. uchar code tab11[8]={"NO. : 0x"};
  39. uchar code tab12[16]={"[S][R]      [En]"};
  40. uchar code tab13[7]={"[S][En]"};
  41. uchar code tab14[4]={"[En]"};
  42. uchar code tab15[16]={"                "};
  43. uchar code tab16[12]={"            "};

  44. void Mu1(void);
  45. void Mu2(void);
  46. void Mu3(void);
  47. void Mu4(void);
  48. void Mu5(void);
  49. void Mu6(void);
  50. void Data_ind(uchar point);
  51. void Usart_Ser();

  52. void main()
  53. {
  54.     key_Init();
  55.     init_lcd();
  56.     Usart_Init();
  57.     Clear_display();
  58.     delay(1000);
  59.     chrt_disp(tab1,0x80,16);
  60.     Disp_Init_Draw();
  61.     delay (100);
  62.     Arr_point=Band_Init();
  63.     old_apoint=Arr_point;
  64.     delay(100);
  65.     Clear_display();
  66.     while(1)
  67.     {
  68.         switch(Menu_Count)
  69.         {
  70.             case 0:
  71.             {
  72.                 Mu1();
  73.             }
  74.             break;
  75.             case 1:
  76.             {
  77.                 Mu2();
  78.             }
  79.             break;
  80.             case 2:
  81.             {
  82.                 Mu3();
  83.             }
  84.             break;
  85.             case 3:
  86.             {
  87.                  Mu4();
  88.             }
  89.             break;
  90.             case 4:
  91.             {
  92.                 Mu5();
  93.             }
  94.             break;
  95.             case 5:
  96.             {
  97.                  Mu6();
  98.             }
  99.             break;
  100.             default:
  101.             {
  102.                  Menu_Count=0;   
  103.             }
  104.         }
  105.     }
  106. }
  107. void Mu1(void)
  108. {
  109.     uchar Mscr1_Flag=0;
  110.     chrt_disp(tab6[0+b_value],0x80,10);
  111.     chrt_disp(tab4,0xc0,16);
  112.     do
  113.     {
  114.         KEY_Value=Key_Scan();
  115.         if(KEY_Value == 0x3c)
  116.         {
  117.             Mscr1_Flag=1;
  118.             b_value++;
  119.             if(b_value==5)
  120.             {
  121.                 b_value=0;
  122.             }        
  123.         }
  124.         else if(KEY_Value == 0x5c)
  125.         {
  126.             Mscr1_Flag=1;
  127.             if(b_value==0)
  128.             {
  129.                 b_value=4;
  130.             }
  131.             else
  132.             {
  133.                   b_value--;
  134.             }   
  135.         }
  136.         else
  137.         {
  138.             if(Mscr1_Flag)
  139.             {
  140.                    chrt_disp(tab6[0+b_value],0x80,10);
  141.                  Mscr1_Flag=0;
  142.             }   
  143.         }   
  144.     }while(KEY_Value != 0x78);
  145.     Menu_Count=b_value+1;
  146.     Clear_display();
  147.     delay(100);
  148. }
  149. void Mu2(void)
  150. {
  151.     uchar M2scr_Flag=0;
  152.     chrt_disp(tab2,0x80,16);
  153.     chrt_disp(tab5[0+Arr_point],0x87,5);
  154.     chrt_disp(tab4,0xc0,16);
  155.      do
  156.     {
  157.         KEY_Value=Key_Scan();
  158.         if(KEY_Value == 0x3c)
  159.         {
  160.             M2scr_Flag=1;
  161.             Arr_point++;
  162.             if(Arr_point==6)
  163.             {
  164.                 Arr_point=0;
  165.             }        
  166.         }
  167.         else if(KEY_Value==0x5c)
  168.         {
  169.             M2scr_Flag=1;
  170.             if(Arr_point==0)
  171.             {
  172.                 Arr_point=5;
  173.             }
  174.             else
  175.             {
  176.                 Arr_point--;
  177.             }
  178.         }
  179.         else
  180.         {
  181.              if(M2scr_Flag)
  182.             {
  183.                 M2scr_Flag=0;
  184.                 chrt_disp(tab5[0+Arr_point],0x87,5);
  185.             }
  186.         }
  187.     }while(KEY_Value != 0x78);
  188.     if(old_apoint != Arr_point)
  189.     {
  190.          IapEraseSector(IAP_ADDRESS);
  191.         IapProgramByte(IAP_ADDRESS, Arr_point);
  192.     }
  193.     Set_Baud(Arr_point);
  194.     Menu_Count=0;
  195.     Clear_display();
  196.     delay(100);
  197. }
  198. void Mu3(void)
  199. {
  200.     uchar Scr_Flag=0;
  201.     uchar GB_point=0;
  202.     chrt_disp(tab8,0x80,13);
  203.     Mum_disp(0x8e,Data_Len ,1);
  204.     chrt_disp(tab4,0xc0,16);
  205.     do
  206.     {
  207.          KEY_Value=Key_Scan();
  208.         if(KEY_Value == 0x3c)    /*按下加键*/
  209.         {
  210.             Scr_Flag=1;
  211.             Data_Len++;
  212.             if(Data_Len==0x0a)
  213.             {
  214.                 Data_Len=1;
  215.             }
  216.         }
  217.         else if(KEY_Value == 0x5c)    /*按下减键*/
  218.         {
  219.             Scr_Flag=1;
  220.             if(Data_Len==1)
  221.             {
  222.                  Data_Len=9;
  223.             }
  224.             else
  225.             {
  226.                 Data_Len--;
  227.             }
  228.         }
  229.         else
  230.         {
  231.             ;
  232.         }
  233.         if(Scr_Flag)
  234.         {
  235.              Scr_Flag=0;
  236.             Mum_disp(0x8e,Data_Len,1);
  237.         }   
  238.     }while(KEY_Value != 0x78);                    /*取得要发送字节长度值*/
  239.     Menu_Count=0;
  240.     Clear_display();
  241.     delay(100);   
  242. }
  243. void Mu4(void)
  244. {
  245.     uchar temp4=1, M4scr=0, M4scr1=0, temp41, temp42;
  246.     temp41=Send_Buffer[temp4-1]>>4;        /*取得高4位*/
  247.     temp42=Send_Buffer[temp4-1]&0x0f;    /*取得低4位*/
  248.     Data_ind(temp4-1);
  249.     chrt_disp(tab11,0x83,8);
  250.     Mum_disp(0x86, temp4,1);
  251.     Mum_disp(0x8b, Mu4_H,0);
  252.     Mum_disp(0x8c, Mu4_L,0);
  253.     chrt_disp(tab10,0xc0,16);
  254.     do
  255.     {
  256.         KEY_Value=Key_Scan();
  257.         if(KEY_Value==0x3c)
  258.         {
  259.             M4scr=1;
  260.             if(temp4==1)
  261.             {
  262.                 temp4 = Data_Len;
  263.             }
  264.             else
  265.             {
  266.                 temp4--;
  267.             }
  268.         }
  269.         else if(KEY_Value==0x5c)
  270.         {
  271.             M4scr=1;
  272.             temp4++;
  273.             if(temp4>Data_Len)
  274.             {
  275.                  temp4=1;
  276.             }
  277.             
  278.         }
  279.         else if(KEY_Value==0x6c)
  280.         {
  281.             M4scr1=1;
  282.             if(byte_bit)
  283.             {
  284.                  temp41++;
  285.                 if(temp41>=16)
  286.                 {
  287.                     temp41=0;
  288.                 }   
  289.             }
  290.             else
  291.             {
  292.                 temp42++;
  293.                 if(temp42>=16)
  294.                 {
  295.                     temp42=0;
  296.                 }   
  297.             }
  298.         }
  299.         else if(KEY_Value==0x74)
  300.         {
  301.             byte_bit = !byte_bit;
  302.         }
  303.         else
  304.         {
  305.             if(M4scr)
  306.             {
  307.                 temp41=Send_Buffer[temp4-1]>>4;
  308.                 temp42=Send_Buffer[temp4-1]&0x0f;
  309.                  Mum_disp(0x86, temp4,1);
  310.                 Data_ind(temp4-1);
  311.                 Mum_disp(0x8b, Mu4_H,0);
  312.                 Mum_disp(0x8c, Mu4_L,0);
  313.                 M4scr=0;
  314.             }
  315.             if(M4scr1)
  316.             {
  317.                  M4scr1=0;
  318.                  Send_Buffer[temp4-1]=(temp41<<4) | temp42;
  319.                  Data_ind(temp4-1);
  320.                  Mum_disp(0x86, temp4,1);
  321.                  Mum_disp(0x8b, Mu4_H,0);
  322.                  Mum_disp(0x8c, Mu4_L,0);
  323.                  
  324.             }
  325.         }
  326.     }while(KEY_Value != 0x78);     /*不但要回车,还要输入字节个数等于设定个数*/
  327.     Menu_Count=0;
  328.     Clear_display();
  329.     delay(100);        
  330. }
  331. void Data_ind(uchar point)
  332. {
  333.     Mu4_H=Send_Buffer[point]>>4;
  334.     if(Mu4_H >= 0x0a)
  335.     {
  336.         Mu4_H += 0x37;
  337.     }
  338.     else
  339.     {
  340.         Mu4_H += 0x30;
  341.     }
  342.     Mu4_L=Send_Buffer[point] & 0x0f;
  343.     if(Mu4_L >= 0x0a)
  344.     {
  345.         Mu4_L += 0x37;
  346.     }
  347.     else
  348.     {
  349.         Mu4_L += 0x30;
  350.     }        
  351. }
  352. void Mu5(void)
  353. {
  354.     uchar M5scr=0;
  355.      chrt_disp(tab9,0x80,16);
  356.     chrt_disp(tab12,0xc0,16);
  357.     do
  358.     {
  359.         KEY_Value=Key_Scan();
  360.         if(KEY_Value==0x3c)
  361.         {
  362.             M5scr=1;
  363.               Ser_state=0;
  364.         }
  365.         else if(KEY_Value==0x5c)
  366.         {
  367.             M5scr=1;
  368.             Ser_state=1;
  369.         }
  370.         else
  371.         {
  372.             if(M5scr)
  373.             {
  374.                 M5scr=0;
  375.                 Mum_disp(0x8d, Ser_state,1);
  376.             }
  377.         }   
  378.     }while(KEY_Value != 0x78);
  379.     Menu_Count=0;
  380.     Clear_display();
  381.     delay(100);
  382. }
  383. void Mu6(void)
  384. {
  385.     uchar i, temp1;
  386.     ES=1;
  387.     if(Ser_state)
  388.     {
  389.         chrt_disp(tab14,0xcc,16);
  390.         do
  391.         {
  392.             if(R_Flag)
  393.             {
  394.                 R_outtime--;
  395.                 if(R_outtime ==0)
  396.                 {
  397.                      R_Flag=0;
  398.                     R_okFlag=1;
  399.                 }   
  400.             }   
  401.             KEY_Value=Key_Scan();
  402.             if(R_okFlag)                    /*如果有接收的数据*/
  403.             {
  404. //                Clear_display();
  405. //                delay(10);
  406.                 chrt_disp(tab16,0xc0,12);
  407.                 chrt_disp(tab15,0x80,16);
  408.                 for(i=0; i<R_Count; i++)
  409.                 {
  410.                     if(i>=8)
  411.                     {
  412.                         
  413.                         temp1=Rec_Buffer[i] >> 4;
  414.                         if(temp1 > 9)
  415.                         {
  416.                             temp1+=0x37;
  417.                         }
  418.                         else
  419.                         {
  420.                             temp1+=0x30;
  421.                         }
  422.                         Mum_disp(0xc0+(i-8)*2, temp1,0);
  423.                         temp1=Rec_Buffer[i] & 0x0f;
  424.                         if(temp1 > 9)
  425.                         {
  426.                             temp1+=0x37;
  427.                         }
  428.                         else
  429.                         {
  430.                             temp1+=0x30;
  431.                         }
  432.                         Mum_disp(0xc0+(i-8)*2+1, temp1,0);
  433.                         //R_okFlag=0;
  434.                     }
  435.                     else
  436.                     {
  437.                         temp1=Rec_Buffer[i] >> 4;
  438.                         if(temp1 > 9)
  439.                         {
  440.                             temp1+=0x37;
  441.                         }
  442.                         else
  443.                         {
  444.                              temp1+=0x30;
  445.                         }
  446.                         Mum_disp(0x80+2*i, temp1,0);
  447.                         temp1=Rec_Buffer[i] & 0x0f;
  448.                         if(temp1 > 9)
  449.                         {
  450.                             temp1+=0x37;
  451.                         }
  452.                         else
  453.                         {
  454.                              temp1+=0x30;
  455.                         }
  456.                         Mum_disp(0x80+2*i+1, temp1,0);
  457.                         //R_okFlag=0;
  458.                     }   
  459.                 }
  460.                 R_Count=0;
  461.                 R_okFlag=0;
  462.             }
  463.         }while(KEY_Value != 0x78);   
  464.     }
  465.     else
  466.     {
  467.         chrt_disp(tab13,0xc9,7);
  468.         do
  469.         {
  470.             KEY_Value=Key_Scan();
  471.             if(KEY_Value==0x74)
  472.             {
  473.                 for(i=0;i<Data_Len;i++)
  474.                 {
  475.                       Send_Byte(Send_Buffer[i]);
  476.                 }   
  477.             }   
  478.          }while(KEY_Value != 0x78);
  479.     }
  480.     ES=0;
  481.        Menu_Count=0;
  482.     Clear_display();
  483.     delay(100);
  484. }
  485. void Usart_Ser() interrupt 4 using 1
  486. {
  487.     if(RI)
  488.     {
  489.         RI=0;
  490.         Rec_Buffer[R_Count]=SBUF;
  491.         R_Count++;
  492.         if(!R_Flag)
  493.         {
  494.             R_Flag=1;
  495.         }
  496.         R_outtime=1000;
  497.     }
  498.     if(TI)
  499.     {
  500.         TI=0;
  501.     }
  502. }

复制代码

评分

参与人数 1黑币 +50 收起 理由
admin + 50 共享资料的黑币奖励!

查看全部评分

回复

使用道具 举报

ID:585455 发表于 2020-9-23 09:29 | 显示全部楼层
沒有庫函數,無法編譯,非常可惜。
回复

使用道具 举报

ID:1067504 发表于 2024-3-31 18:48 来自手机 | 显示全部楼层
开源代码发出来就好了……
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表