用的8G1K08A单片机为什么通过串口1发送指令时COM1.RX_Cnt的值不变
#include "intrins.h"#include "ws2812.h"
#include "GPIO.h"
#include "delay.h"
#include "UART.h"
unsigned char s;
unsigned char c;
#define GREEN 0
#define RED 1
#define BLUE 2
unsigned char comm_buffer;
/************* ±¾µØ±äÁ¿ÉùÃ÷ **************/
//u8 idata RX1_Buffer; //½ÓÊÕ»º³å
u8 TX1_Cnt; //·¢ËͼÆÊý
bit B_TX1_Busy; //·¢ËÍæ±êÖ¾
u8 RX1_Cnt; //½ÓÊÕ¼ÆÊý
bit Rec_On;
bit Rec_Ok;
/******************* IOÅäÖú¯Êý *******************/
void GPIO_config(void)
{
GPIO_InitTypeDef GPIO_InitStructure; //½á¹¹¶¨Òå
GPIO_InitStructure.Pin= GPIO_Pin_5 ; //Ö¸¶¨Òª³õʼ»¯µÄIO, GPIO_Pin_0 ~ GPIO_Pin_7
GPIO_InitStructure.Mode = GPIO_PullUp; //Ö¸¶¨IOµÄÊäÈë»òÊä³ö·½Ê½,GPIO_PullUp,GPIO_HighZ,GPIO_OUT_OD,GPIO_OUT_PP
GPIO_Inilize(GPIO_P5,&GPIO_InitStructure); //³õʼ»¯
}
void UartInit(void) //9600bps@22.1184MHz
{
SCON = 0x50;
AUXR |= 0x40;
AUXR &= 0xFE;
TMOD &= 0x0F;
TL1 = 0xC0;
TH1 = 0xFD;
ET1 = 0;
TR1 = 1;
}
void UART_config(void)
{
COMx_InitDefine COMx_InitStructure; //½á¹¹¶¨Òå
COMx_InitStructure.UART_Mode = UART_8bit_BRTx; //ģʽ, UART_ShiftRight,UART_8bit_BRTx,UART_9bit,UART_9bit_BRTx
COMx_InitStructure.UART_BRT_Use = BRT_Timer1; //Ñ¡Ôñ²¨ÌØÂÊ·¢ÉúÆ÷, BRT_Timer1, BRT_Timer2 (×¢Òâ: ´®¿Ú2¹Ì¶¨Ê¹ÓÃBRT_Timer2)
COMx_InitStructure.UART_BaudRate= 9600ul; //²¨ÌØÂÊ, Ò»°ã 110 ~ 115200
COMx_InitStructure.UART_RxEnable= ENABLE; //½ÓÊÕÔÊÐí, ENABLE»òDISABLE
COMx_InitStructure.BaudRateDouble = DISABLE; //²¨ÌØÂʼӱ¶, ENABLE»òDISABLE
COMx_InitStructure.UART_Interrupt = ENABLE; //ÖжÏÔÊÐí, ENABLE»òDISABLE
COMx_InitStructure.UART_Priority = Priority_0; //Ö¸¶¨ÖжÏÓÅÏȼ¶(µÍµ½¸ß) Priority_0,Priority_1,Priority_2,Priority_3
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
UART_Configuration(UART1, &COMx_InitStructure); //³õʼ»¯´®¿Ú1 UART1,UART2,UART3,UART4
}
void GPI_Init(void)
{
P0M0 = 0x00;
P0M1 = 0x00;
P1M0 = 0x00;
P1M1 = 0x00;
P2M0 = 0x00;
P2M1 = 0x00;
P3M0 = 0x00;
P3M1 = 0x00;
P4M0 = 0x00;
P4M1 = 0x00;
P5M0 = 0x00;
P5M1 = 0x00;
}
void UartSend_Data(unsigned char dat)
{
SBUF = dat;// °ÑÒª·¢Ë͵ÄÊý¾Ý¸øSBUF¼Ä´æÆ÷
while(!TI);// µÈ´ýÊý¾Ý·¢ËÍÍê³É
TI = 0; // Çå³ý·¢ËÍÍê³É±ê־λ
}
void changeOneColorRed(unsigned char i,unsigned char b){
s=0;
s=(b<<1)*1;
s=0;
SendOneFrame(&s);
}
void changeOneColorGreen(unsigned char i,unsigned char b){
s=(b<<1)*1;
s=0;
s=0;
SendOneFrame(&s);
}
void changeOneColorBlue(unsigned char i,unsigned char b){
s=0;
s=0;
s=(b<<1)*1;
SendOneFrame(&s);
}
void main(void)
{
u8 i;
int j;
B_TX1_Busy = 0;
RX1_Cnt = 0;
Rec_On = 0;
Rec_Ok = 0;
EAXSFR(); /* À©Õ¹¼Ä´æÆ÷·ÃÎÊʹÄÜ */
GPIO_config();
UartInit();
UART_config();
EA = 1;
for(i=0;i<8;i++){
s=20;
s=1;
s=1;
}
SendOneFrame(&s);
delay_ms(500);
delay_ms(500);
delay_ms(500);
for(i=0;i<8;i++){
s=0;
s=0;
s=0;
}
SendCloseFrame();
while (1)
{
delay_ms(1);
if (Rec_On==1 && j<=4) {
comm_buffer = RX1_Buffer;
j++;
}
if (COM1.RX_Cnt ==5) //light No. ok
{
if(RX1_Buffer== 0x18){
SendCloseFrame();
} else if(RX1_Buffer== 0x19){
SendOneFrame(&s);
} else if(RX1_Buffer == 0x0A){
changeOneColorRed(RX1_Buffer,RX1_Buffer);
} else if(RX1_Buffer == 0x0B){
changeOneColorGreen(RX1_Buffer,RX1_Buffer);
} else if(RX1_Buffer == 0x0C){
changeOneColorBlue(RX1_Buffer,RX1_Buffer);
}
UartSend_Data('O');
UartSend_Data('K');
} else {
//UartSend_Data('F');
//UartSend_Data('A');
}
RX1_Cnt = 0;
Rec_Ok = 0;
}
}
解决了,没加中断函数
还有个问题怎么读取如 fc fc 01 0e 88 0e 00 中任意指令符来判断执行哪个逻辑 数据结构加Switch判断
页:
[1]