找回密码
 立即注册
查看: 333|回复: 3

用的8G1K08A单片机为什么通过串口1发送指令时COM1.RX_Cnt的值不变

[复制链接]

4

主题

4

回帖

54

积分

注册会员

积分
54
发表于 2024-6-17 09:53:02 | 显示全部楼层 |阅读模式
  1. #include "intrins.h"
  2. #include "ws2812.h"
  3. #include        "GPIO.h"
  4. #include        "delay.h"
  5. #include "UART.h"
  6. unsigned char s[8][3];
  7. unsigned char c[8][3];
  8. #define GREEN 0
  9. #define RED 1
  10. #define BLUE 2
  11. unsigned char comm_buffer[5];
  12. /*************        ±¾µØ±äÁ¿ÉùÃ÷        **************/
  13. //u8        idata RX1_Buffer[RX1_Lenth];        //½ÓÊÕ»º³å
  14. u8        TX1_Cnt;        //·¢ËͼÆÊý
  15. bit        B_TX1_Busy;        //·¢ËÍæ±êÖ¾
  16. u8        RX1_Cnt;        //½ÓÊÕ¼ÆÊý
  17. bit Rec_On;
  18. bit Rec_Ok;
  19. /******************* IOÅäÖú¯Êý *******************/
  20. void        GPIO_config(void)
  21. {
  22.         GPIO_InitTypeDef        GPIO_InitStructure;                //½á¹¹¶¨Òå
  23.        
  24.         GPIO_InitStructure.Pin  = GPIO_Pin_5 ;                //Ö¸¶¨Òª³õʼ»¯µÄIO, GPIO_Pin_0 ~ GPIO_Pin_7
  25.         GPIO_InitStructure.Mode = GPIO_PullUp;        //Ö¸¶¨IOµÄÊäÈë»òÊä³ö·½Ê½,GPIO_PullUp,GPIO_HighZ,GPIO_OUT_OD,GPIO_OUT_PP
  26.         GPIO_Inilize(GPIO_P5,&GPIO_InitStructure);        //³õʼ»¯
  27. }
  28. void UartInit(void)        //9600bps@22.1184MHz
  29. {
  30.         SCON = 0x50;               
  31.         AUXR |= 0x40;               
  32.         AUXR &= 0xFE;               
  33.         TMOD &= 0x0F;               
  34.         TL1 = 0xC0;                       
  35.         TH1 = 0xFD;                       
  36.         ET1 = 0;                       
  37.         TR1 = 1;                       
  38. }
  39. void        UART_config(void)
  40. {
  41.         COMx_InitDefine                COMx_InitStructure;                                        //½á¹¹¶¨Òå
  42.         COMx_InitStructure.UART_Mode      = UART_8bit_BRTx;        //ģʽ, UART_ShiftRight,UART_8bit_BRTx,UART_9bit,UART_9bit_BRTx
  43.         COMx_InitStructure.UART_BRT_Use   = BRT_Timer1;                        //Ñ¡Ôñ²¨ÌØÂÊ·¢ÉúÆ÷, BRT_Timer1, BRT_Timer2 (×¢Òâ: ´®¿Ú2¹Ì¶¨Ê¹ÓÃBRT_Timer2)
  44.         COMx_InitStructure.UART_BaudRate  = 9600ul;                        //²¨ÌØÂÊ, Ò»°ã 110 ~ 115200
  45.         COMx_InitStructure.UART_RxEnable  = ENABLE;                                //½ÓÊÕÔÊÐí,   ENABLE»òDISABLE
  46.         COMx_InitStructure.BaudRateDouble = DISABLE;                        //²¨ÌØÂʼӱ¶, ENABLE»òDISABLE
  47.         COMx_InitStructure.UART_Interrupt = ENABLE;                                //ÖжÏÔÊÐí,   ENABLE»òDISABLE
  48.         COMx_InitStructure.UART_Priority    = Priority_0;                        //Ö¸¶¨ÖжÏÓÅÏȼ¶(µÍµ½¸ß) Priority_0,Priority_1,Priority_2,Priority_3
  49.         COMx_InitStructure.UART_P_SW      = UART1_SW_P30_P31;        //Çл»¶Ë¿Ú,   UART1_SW_P30_P31,UART1_SW_P36_P37,UART1_SW_P16_P17,UART1_SW_P43_P44
  50.         UART_Configuration(UART1, &COMx_InitStructure);                //³õʼ»¯´®¿Ú1 UART1,UART2,UART3,UART4
  51.        
  52. }
  53. void GPI_Init(void)
  54. {
  55.         P0M0 = 0x00;
  56.         P0M1 = 0x00;
  57.         P1M0 = 0x00;
  58.         P1M1 = 0x00;
  59.         P2M0 = 0x00;
  60.         P2M1 = 0x00;
  61.         P3M0 = 0x00;
  62.         P3M1 = 0x00;
  63.         P4M0 = 0x00;
  64.         P4M1 = 0x00;
  65.         P5M0 = 0x00;
  66.         P5M1 = 0x00;
  67. }
  68. void UartSend_Data(unsigned char dat)
  69. {
  70.         SBUF = dat;  // °ÑÒª·¢Ë͵ÄÊý¾Ý¸øSBUF¼Ä´æÆ÷
  71.         while(!TI);  // µÈ´ýÊý¾Ý·¢ËÍÍê³É
  72.         TI = 0;      // Çå³ý·¢ËÍÍê³É±ê־λ
  73. }
  74. void changeOneColorRed(unsigned char i,unsigned char b){
  75.   s[i][GREEN]=0;
  76.         s[i][RED]=(b<<1)*1;
  77.         s[i][BLUE]=0;
  78.   SendOneFrame(&s[0][0]);
  79. }
  80. void changeOneColorGreen(unsigned char i,unsigned char b){
  81.   s[i][GREEN]=(b<<1)*1;
  82.         s[i][RED]=0;
  83.         s[i][BLUE]=0;
  84.   SendOneFrame(&s[0][0]);
  85. }
  86. void changeOneColorBlue(unsigned char i,unsigned char b){
  87.   s[i][GREEN]=0;
  88.         s[i][RED]=0;
  89.         s[i][BLUE]=(b<<1)*1;
  90.   SendOneFrame(&s[0][0]);
  91. }
  92. void main(void)
  93. {
  94.         u8 i;
  95.         int j;
  96.         B_TX1_Busy = 0;
  97.         RX1_Cnt = 0;
  98.         Rec_On = 0;
  99.   Rec_Ok = 0;
  100.                 EAXSFR();                /* À©Õ¹¼Ä´æÆ÷·ÃÎÊʹÄÜ */
  101.         GPIO_config();
  102.         UartInit();
  103.         UART_config();
  104.         EA = 1;
  105.        
  106.         for(i=0;i<8;i++){
  107.                 s[i][0]=20;
  108.                 s[i][1]=1;
  109.                 s[i][2]=1;
  110.         }
  111.         SendOneFrame(&s[0][0]);
  112.        
  113.         delay_ms(500);
  114.         delay_ms(500);
  115.         delay_ms(500);
  116.        
  117.                 for(i=0;i<8;i++){
  118.                 s[i][0]=0;
  119.                 s[i][1]=0;
  120.                 s[i][2]=0;
  121.         }
  122.        
  123.         SendCloseFrame();
  124.        
  125.         while (1)
  126.         {
  127.                 delay_ms(1);
  128.         if (Rec_On==1 && j<=4) {
  129.                                                         comm_buffer[j] = RX1_Buffer[i];
  130.                                                         j++;                                                               
  131.                                                 }       
  132.                
  133.                         if (COM1.RX_Cnt ==5)   //light No. ok
  134.                         {
  135.                                 if(RX1_Buffer[RX1_Cnt-4]== 0x18){
  136.                                         SendCloseFrame();
  137.                                 } else if(RX1_Buffer[RX1_Cnt-4]== 0x19){
  138.                                         SendOneFrame(&s[0][0]);                                       
  139.                                 } else if(RX1_Buffer[RX1_Cnt-3] == 0x0A){
  140.                                   changeOneColorRed(RX1_Buffer[RX1_Cnt-4],RX1_Buffer[RX1_Cnt-2]);
  141.                           } else if(RX1_Buffer[RX1_Cnt-3] == 0x0B){
  142.                             changeOneColorGreen(RX1_Buffer[RX1_Cnt-4],RX1_Buffer[RX1_Cnt-2]);
  143.                           } else if(RX1_Buffer[RX1_Cnt-3] == 0x0C){
  144.                             changeOneColorBlue(RX1_Buffer[RX1_Cnt-4],RX1_Buffer[RX1_Cnt-2]);
  145.                           }
  146.                                 UartSend_Data('O');
  147.                                 UartSend_Data('K');
  148.                         } else {
  149.                           //UartSend_Data('F');
  150.                                 //UartSend_Data('A');
  151.                         }
  152.                         RX1_Cnt = 0;
  153.                         Rec_Ok = 0;
  154.        
  155. }
  156. }
复制代码


UART.C

3.54 KB, 下载次数: 46

UART.h

3.79 KB, 下载次数: 50

回复

使用道具 举报 送花

4

主题

4

回帖

54

积分

注册会员

积分
54
发表于 2024-6-17 09:59:58 | 显示全部楼层
解决了,没加中断函数
回复 支持 反对

使用道具 举报 送花

4

主题

4

回帖

54

积分

注册会员

积分
54
发表于 2024-6-17 10:06:12 | 显示全部楼层
还有个问题怎么读取如 fc fc 01 0e 88 0e 00 中任意指令符来判断执行哪个逻辑
回复 支持 反对

使用道具 举报 送花

  • 打卡等级:以坛为家II
  • 打卡总天数:428
  • 最近打卡:2025-05-07 00:13:45
已绑定手机

19

主题

3194

回帖

4948

积分

论坛元老

积分
4948
发表于 2024-6-17 13:12:36 来自手机 | 显示全部楼层
数据结构加Switch判断
回复 支持 反对

使用道具 举报 送花

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

本版积分规则

QQ|手机版|小黑屋|深圳国芯人工智能有限公司 ( 粤ICP备2022108929号-2 )

GMT+8, 2025-5-7 11:58 , Processed in 0.157371 second(s), 71 queries .

Powered by Discuz! X3.5

© 2001-2025 Discuz! Team.

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