找回密碼
 立即注冊

QQ登錄

只需一步,快速開始

搜索
查看: 3307|回復: 1
收起左側(cè)

51單片機串口通信程序,支持自定義協(xié)議

[復制鏈接]
ID:280056 發(fā)表于 2019-12-5 17:40 | 顯示全部樓層 |閱讀模式
51串口通信,支持自定義協(xié)議,代碼注釋非常全,求黑幣
單片機源程序如下:
  1. #include"reg52.h" /*51串口通信by沸騰的冰塊/不拼不活*/
  2. typedef unsigned int u16;
  3. typedef unsigned char u8;
  4. #define Data_SIZE 7     //存入數(shù)組RevBuf數(shù)據(jù)長度==uart_Data_SIZE-2
  5. #define uart_Data_SIZE 5//協(xié)議實際長度(一個漢字占兩位)
  6. u8 RevBuf[Data_SIZE];       //數(shù)據(jù)接收緩沖區(qū)
  7. u8 indata[uart_Data_SIZE];  //防數(shù)據(jù)抵消緩沖區(qū)
  8. u16 data_count=0;   //數(shù)據(jù)長度,串口中斷自增
  9. u16 temp_length=0;  //數(shù)據(jù)長度,協(xié)議長度中轉(zhuǎn)
  10. u8 flished_flag=0;  //數(shù)據(jù)接收符合要求標志
  11. u8 uart_flished_flag=0;  //串口成功標志
  12. u8 i=0;//延時用
  13. u16 temp=0,count=0;//temp緩存,count用于判斷是否接受到數(shù)據(jù)
  14. sbit LED1=P1^0;sbit k1=P2^0;
  15. sbit LED2=P1^1;sbit k2=P2^1;
  16. sbit LED3=P1^2;sbit k3=P2^2;
  17. sbit LED4=P1^3;sbit k4=P2^3;
  18. sbit LED5=P1^4;sbit k5=P2^4;
  19. sbit LED6=P1^5;sbit k6=P2^5;
  20. sbit LED7=P1^6;sbit k7=P2^6;
  21. sbit LED8=P1^7;sbit k8=P2^7;
  22. void InitUART(void)
  23. {
  24.         TMOD = 0x20;
  25.         SCON = 0x50;
  26.         TH1 = 0xFA;//在此修改波特率
  27.         TL1 = TH1;
  28.         PCON = 0x80;
  29.         EA = 1;
  30.         ES = 1;
  31.         TR1 = 1;
  32. }
  33. void delay(u16 i)
  34. {
  35.         while(i--);        
  36. }
  37. void keypros()//按鍵處理
  38. {
  39.         if(k1==0)
  40.         {        
  41.                 delay(1000);
  42.                 if(k1==0)
  43.                 {
  44.                         LED1=~LED1;
  45.                 }
  46.                 while(!k1);
  47.         }
  48.         if(k2==0)
  49.         {        
  50.                 delay(1000);
  51.                 if(k2==0)
  52.                 {
  53.                         LED2=~LED2;
  54.                 }
  55.                 while(!k2);
  56.         }
  57.         if(k3==0)
  58.         {        
  59.                 delay(1000);
  60.                 if(k3==0)
  61.                 {
  62.                         LED3=~LED3;
  63.                 }
  64.                 while(!k3);
  65.         }
  66.         if(k4==0)
  67.         {        
  68.                 delay(1000);
  69.                 if(k4==0)
  70.                 {
  71.                         LED4=~LED4;
  72.                 }
  73.                 while(!k4);
  74.         }
  75.         if(k5==0)
  76.         {        
  77.                 delay(1000);
  78.                 if(k5==0)
  79.                 {
  80.                         LED5=~LED5;
  81.                 }
  82.                 while(!k5);
  83.         }
  84.         if(k6==0)
  85.         {        
  86.                 delay(1000);
  87.                 if(k6==0)
  88.                 {
  89.                         LED6=~LED6;
  90.                 }
  91.                 while(!k6);
  92.         }
  93.         if(k7==0)
  94.         {        
  95.                 delay(1000);
  96.                 if(k7==0)
  97.                 {
  98.                         LED7=~LED7;
  99.                 }
  100.                 while(!k7);
  101.         }
  102.         if(k8==0)
  103.         {        
  104.                 delay(1000);
  105.                 if(k8==0)
  106.                 {
  107.                         LED8=~LED8;
  108.                 }
  109.                 while(!k8);
  110.         }
  111. }
  112. void UartSendByte(unsigned char c)//發(fā)送一個字符
  113. {
  114.         SBUF = c;
  115.         while(!TI);
  116.         TI = 0;
  117. }
  118. void UartSendString(u8 *string)//發(fā)送一個字符串
  119. {
  120.         while(*string)
  121.         {
  122.                  UartSendByte(*string++);
  123.         }
  124. }
  125. void main(void)
  126. {
  127.         InitUART();
  128.         UartSendString("Uart_test  is  OK \r\n");
  129.         while(1)
  130.   {
  131.                 keypros();
  132.                 if(count<data_count)
  133.                 {
  134.                         count=data_count;
  135.                         if(temp!='\n') //判斷是否接收到結(jié)束符
  136.                         {
  137.                                 RevBuf[data_count-1]=temp;// 否,就存到RevBuf【】數(shù)組中
  138.                                 flished_flag=0;//未接收完
  139.                         }
  140.                         else
  141.                         {
  142.                                 temp_length=data_count;//是,記錄其數(shù)據(jù)長度
  143.                                 data_count=count=0;
  144.                                 flished_flag=1;//接收完
  145.                         }
  146.                 }
  147.                 if(temp_length == Data_SIZE&&flished_flag==1)   //判斷數(shù)據(jù)長度是否滿足要求,是否接收完。
  148.                 {
  149.                         for(i=0;i<uart_Data_SIZE;i++)
  150.                         {
  151.                           indata[i]=RevBuf[i];  //緩沖區(qū)
  152.                         }
  153.                         uart_flished_flag=1;    //數(shù)據(jù)接收成功標志
  154.                         flished_flag=0;
  155.                 }
  156.                 if(uart_flished_flag==1)  //數(shù)據(jù)接收完整成功
  157.                 {
  158.                         UartSendString("你發(fā)送的是\r\n");
  159.                         for(i=0;i<uart_Data_SIZE;i++)
  160.                         {
  161.                                 SBUF = indata[i];
  162.                                 while(!TI);
  163.                                 TI=0;
  164.                         }
  165.                         UartSendString("\r\n");
  166.                         uart_flished_flag=0;
  167.                         if(indata[1]=='n')//發(fā)送:on :1
  168.                                 switch(indata[4])
  169.                                 {
  170.                                         case'1':LED1=0;break;
  171.                                         case'2':LED2=0;break;
  172.                                         case'3':LED3=0;break;
  173.                                         case'4':LED4=0;break;
  174.                                         case'5':LED5=0;break;
  175.                                         case'6':LED6=0;break;
  176.                                         case'7':LED7=0;break;
  177.                                         case'8':LED8=0;break;
  178.                                 }
  179.                                 if(indata[1]=='f')//發(fā)送:off:1
  180.                                 switch(indata[4])
  181.                                 {
  182.                                         case'1':LED1=1;break;
  183.                                         case'2':LED2=1;break;
  184.                                         case'3':LED3=1;break;
  185.                                         case'4':LED4=1;break;
  186.                                         case'5':LED5=1;break;
  187.                                         case'6':LED6=1;break;
  188.                                         case'7':LED7=1;break;
  189.                                         case'8':LED8=1;break;
  190.                                 }
  191.                 }
  192.   }
  193. }
  194. void UARTInterrupt(void) interrupt 4
  195. {
  196.         if(RI)
  197.         {
  198.                 temp=SBUF;
  199.                 data_count++;
  200.                 RI=0;
  201.         }
  202. }
復制代碼


51串口通信帶協(xié)議.rar

1.41 KB, 下載次數(shù): 21, 下載積分: 黑幣 -5

評分

參與人數(shù) 1黑幣 +50 收起 理由
admin + 50 共享資料的黑幣獎勵!

查看全部評分

回復

使用道具 舉報

ID:280056 發(fā)表于 2019-12-7 10:11 | 顯示全部樓層
  1. #include"reg52.h" /*51串口通信高級版by沸騰的冰塊/不拼不活*/
  2. typedef unsigned int u16;
  3. typedef unsigned char u8;
  4. #define Data_SIZE 7     //存入數(shù)組RevBuf數(shù)據(jù)長度==協(xié)議長度-2
  5. u8 RevBuf[Data_SIZE];   //數(shù)據(jù)接收緩沖區(qū)
  6. u8 indata[2*Data_SIZE]; //防數(shù)據(jù)抵消緩沖區(qū)
  7. u16 data_count=0;   //數(shù)據(jù)長度,串口中斷自增
  8. u16 temp_length=0;  //數(shù)據(jù)長度,協(xié)議長度中轉(zhuǎn)
  9. u8 flished_flag=0;  //數(shù)據(jù)接收符合要求標志
  10. u8 uart_flished_flag=0;  //串口成功標志
  11. u8 i=0;//延時用,循環(huán)用
  12. u8 sign;//協(xié)議開頭標志位
  13. u16 temp=0,count=0;//temp緩存,count用于判斷是否接受到數(shù)據(jù)
  14. sbit LED1=P1^0;sbit k1=P2^0;
  15. sbit LED2=P1^1;sbit k2=P2^1;
  16. sbit LED3=P1^2;sbit k3=P2^2;
  17. sbit LED4=P1^3;sbit k4=P2^3;
  18. sbit LED5=P1^4;sbit k5=P2^4;
  19. sbit LED6=P1^5;sbit k6=P2^5;
  20. sbit LED7=P1^6;sbit k7=P2^6;
  21. sbit LED8=P1^7;sbit k8=P2^7;
  22. void InitUART(void)
  23. {
  24.         TMOD = 0x20;
  25.         SCON = 0x50;
  26.         TH1 = 0xFA;//在此修改波特率
  27.         TL1 = TH1;
  28.         PCON = 0x80;
  29.         EA = 1;
  30.         ES = 1;
  31.         TR1 = 1;
  32. }
  33. void delay(u16 i)
  34. {
  35.         while(i--);       
  36. }
  37. void keypros()//按鍵處理
  38. {
  39.         if(k1==0)
  40.         {       
  41.                 delay(1000);
  42.                 if(k1==0)
  43.                 {
  44.                         LED1=~LED1;
  45.                 }
  46.                 while(!k1);
  47.         }
  48.         if(k2==0)
  49.         {       
  50.                 delay(1000);
  51.                 if(k2==0)
  52.                 {
  53.                         LED2=~LED2;
  54.                 }
  55.                 while(!k2);
  56.         }
  57.         if(k3==0)
  58.         {       
  59.                 delay(1000);
  60.                 if(k3==0)
  61.                 {
  62.                         LED3=~LED3;
  63.                 }
  64.                 while(!k3);
  65.         }
  66.         if(k4==0)
  67.         {       
  68.                 delay(1000);
  69.                 if(k4==0)
  70.                 {
  71.                         LED4=~LED4;
  72.                 }
  73.                 while(!k4);
  74.         }
  75.         if(k5==0)
  76.         {       
  77.                 delay(1000);
  78.                 if(k5==0)
  79.                 {
  80.                         LED5=~LED5;
  81.                 }
  82.                 while(!k5);
  83.         }
  84.         if(k6==0)
  85.         {       
  86.                 delay(1000);
  87.                 if(k6==0)
  88.                 {
  89.                         LED6=~LED6;
  90.                 }
  91.                 while(!k6);
  92.         }
  93.         if(k7==0)
  94.         {       
  95.                 delay(1000);
  96.                 if(k7==0)
  97.                 {
  98.                         LED7=~LED7;
  99.                 }
  100.                 while(!k7);
  101.         }
  102.         if(k8==0)
  103.         {       
  104.                 delay(1000);
  105.                 if(k8==0)
  106.                 {
  107.                         LED8=~LED8;
  108.                 }
  109.                 while(!k8);
  110.         }
  111. }
  112. void UartSendByte(unsigned char c)//發(fā)送一個字符
  113. {
  114.         SBUF = c;
  115.         while(!TI);
  116.         TI = 0;
  117. }
  118. void UartSendString(u8 *string)//發(fā)送一個字符串
  119. {
  120.         while(*string)
  121.         {
  122.                  UartSendByte(*string++);
  123.         }
  124. }
  125. void main(void)
  126. {
  127.         InitUART();
  128.         UartSendString("Uart_test  is  OK \r\n");
  129.         while(1)
  130.   {
  131.                 keypros();
  132.                 if(count<data_count)
  133.                 {
  134.                         count=data_count;
  135.                         if(temp!='\n') //判斷是否接收到結(jié)束符
  136.                         {
  137.                                 RevBuf[data_count-1]=temp;//否,存到RevBuf【】數(shù)組中
  138.                                 flished_flag=0;//未接收完
  139.                         }
  140.                         else
  141.                         {
  142.                                 temp_length=data_count;//是,記錄其數(shù)據(jù)長度
  143.                                 data_count=count=0;
  144.                                 flished_flag=1;//接收完
  145.                         }
  146.                 }
  147.                 if(temp_length >= Data_SIZE&&flished_flag==1) //判斷數(shù)據(jù)長度是否滿足要求,是否接收完。
  148.                 {
  149.                         for(i=0;i<(temp_length-2);i++)
  150.                         {
  151.                           indata[i]=RevBuf[i];  //緩沖區(qū)
  152.                         }
  153.                         uart_flished_flag=1;    //數(shù)據(jù)接收成功標志
  154.                         flished_flag=0;
  155.                 }
  156.                 if(uart_flished_flag==1)  //數(shù)據(jù)接收完整成功
  157.                 {
  158.                         UartSendString("你發(fā)送的是\r\n");
  159.                         for(i=0;i<(temp_length-2);i++)
  160.                         {
  161.                                 SBUF = indata[i];
  162.                                 while(!TI);
  163.                                 TI=0;
  164.                         }
  165.                         UartSendString("\r\n");
  166.                         uart_flished_flag=0;
  167.                         for(i=0;i<(temp_length-2);i++)//定位協(xié)議頭
  168.                         {
  169.                                 if(indata[i]=='o')
  170.                                 {
  171.                                         sign=i;break;
  172.                                 }
  173.                         }
  174.                         if(indata[sign]=='o'&&indata[sign+1]=='n')//發(fā)送:on :1
  175.                                 switch(indata[sign+4])
  176.                                 {
  177.                                         case'1':LED1=0;break;
  178.                                         case'2':LED2=0;break;
  179.                                         case'3':LED3=0;break;
  180.                                         case'4':LED4=0;break;
  181.                                         case'5':LED5=0;break;
  182.                                         case'6':LED6=0;break;
  183.                                         case'7':LED7=0;break;
  184.                                         case'8':LED8=0;break;
  185.                                 }
  186.                                 if(indata[sign]=='o'&&indata[sign+1]=='f'&&indata[sign+2]=='f')//發(fā)送:off:1
  187.                                 switch(indata[sign+4])
  188.                                 {
  189.                                         case'1':LED1=1;break;
  190.                                         case'2':LED2=1;break;
  191.                                         case'3':LED3=1;break;
  192.                                         case'4':LED4=1;break;
  193.                                         case'5':LED5=1;break;
  194.                                         case'6':LED6=1;break;
  195.                                         case'7':LED7=1;break;
  196.                                         case'8':LED8=1;break;
  197.                                 }
  198.                 }
  199.   }
  200. }
  201. void UARTInterrupt(void) interrupt 4
  202. {
  203.         if(RI)
  204.         {
  205.                 temp=SBUF;
  206.                 data_count++;
  207.                 RI=0;
  208.         }
  209. }
復制代碼
回復

使用道具 舉報

您需要登錄后才可以回帖 登錄 | 立即注冊

本版積分規(guī)則

手機版|小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術交流QQ群281945664

Powered by 單片機教程網(wǎng)

快速回復 返回頂部 返回列表