STC15L100的芯片与ZGB相连,做一个简单的串口发送数据,发送规定的数据,外部中断3进行控制,来一个外部中断,发送一次数据程序,下板之后,下板成功,链接串口助手,按下中断,第一组DATA数据正确,延时后,发送DATA2的数据,DATA2的数据变成了每一个数都延时一次,在发出下一个数,DATA2应该不延时,求大神看看程序,11.0592MHZ的内部晶振,9600波特率。
- #include <reg51.h>
- char code DATA[19]={0x02, 0x6c, 0x62, 0x03, 0x03, 0x03, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xc1}; //显靶
- char code DATA2[19]={0x03, 0x6c, 0x62, 0x03, 0x03, 0x03, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xc1}; //影靶
- //char *ch;
- unsigned char i,j;
- //-----------------------------------------
- //define baudrate const
- //BAUD = 65536 - FOSC/3/BAUDRATE/M (1T:M=1; 12T:M=12)
- //NOTE: (FOSC/3/BAUDRATE) must be greater then 98, (RECOMMEND GREATER THEN 110)
- #define BAUD 0xFE80 // 9600bps @ 11.0592MHz
- sfr AUXR = 0x8E;
- sfr INT_CLKO = 0x8f;
- sbit RXB = P3^0; //define UART TX/RX port
- sbit TXB = P3^1;
- sbit T3 = P3^5;
- sbit led = P3^4;
- typedef bit BOOL;
- typedef unsigned char BYTE;
- typedef unsigned int WORD;
- BYTE TBUF,RBUF;//字节
- BYTE TDAT,RDAT;
- BYTE TCNT,RCNT;
- BYTE TBIT,RBIT;
- BOOL TING,RING;
- BOOL TEND,REND;
- void UART_INIT();
- void uart1_TX();
- BYTE r;
- BYTE buf[16];
- unsigned int k=0;
- //-----------------------------------------
- //initial UART module variable
- void delay(unsigned int n)
- {
- unsigned int w=0;
- while(n--)
- for(w=0;w<114;w++);
- }
- void UART_INIT()
- {
- TING = 0;
- RING = 0;
- TEND = 1;
- REND = 0;
- TCNT = 0;
- RCNT = 0;
- }
- void exint3() interrupt 11
- {
- while(T3==0);
- delay(10);
- while(T3==0)
- k = 1;
- i = 0;
- j = 0;
- led = 0;
- }
- void uart3i()
- {
- if(i<19)
- {
- TEND = 0; //发送完一个字符清一次
- TBUF = DATA[i];//发送缓冲器 进行赋值
- TING = 1;
- i++;
- }
- }
- void uart3j()
- {
- if(j<19)
- {
- TEND = 0; //发送完一个字符清一次
- TBUF = DATA2[j];//发送缓冲器 进行赋值
- TING = 1;
- j++;
- }
- }
- void main()
- {
- TMOD = 0x00; //timer0 in 16-bit auto reload mode定时器0在16位自动重新加载模式下
- AUXR = 0x80; //timer0 working at 1T mode定时器0在1T模式下工作
- TL0 = BAUD;
- TH0 = BAUD>>8; //initial timer0 and set reload value初始计时器0并设置重新加载值
- TR0 = 1; //tiemr0 start runningtimer0 tiemr0开始运行
- ET0 = 1; //enable timer0 interrupt使timer0中断
- PT0 = 1; //improve timer0 interrupt priority提高定时器0中断优先级
- EA = 1; //总中断
- led = 0;
- INT_CLKO |= 0x20;
- UART_INIT();
- while (1)
- { //user's function
- if (REND)
- {
- REND = 0;
- buf[r++ & 0x0f] = RBUF; //接收缓冲器 高四位清0低四位赋值
- }
- if(k == 1)
- {
- if (TEND)
- {
- {
- uart3i();
- }
- if(i==19)
- delay(1000);
- {
- uart3j();
- if(j==19)
- {
- k = 0;
- }
- }
- }
- }
- }
- }
- //-----------------------------------------
- //Timer interrupt routine for UART计时器中断串口
- void tm0() interrupt 1 using 1
- { //****************接收数据******************/
- if (RING) //正在接受数据
- {
- if (--RCNT == 0)
- {
- RCNT = 3; //reset send baudrate counter 复位发送波特率计数器
- if (--RBIT == 0)
- {
- RBUF = RDAT; //save the data to RDAT数据保存到RBUF中
- RING = 0; //stop receive 停止接受
- REND = 1; //set receive completed flag设置接收完成标志
- }
- else
- {
- RDAT >>= 1; //接收到的单bit数据暂存
- if (RXB) RDAT |= 0x80; //shift RX data to RX buffer 将RX数据转移到RX缓冲区
- }
- }
- }
- else if (!RXB)//判断开始位RXB=0
- {
- RING = 1; //set start receive flag 设置开始接收标志
- RCNT = 4; //initial receive baudrate counter 初始接收波特率计数器
- RBIT = 9; //initial receive bit number (8 data bits + 1 stop bit) 初始接收位(8位数据位1停止位)
- }
- //*******************发送********************/
- if (--TCNT == 0)
- {
- TCNT = 3; //reset send baudrate counter 复位发送波特率计数器
- if (TING) //judge whether sending 判断是否发送
- {
- if (TBIT == 0)
- {
- TXB = 0; //send start bit 发送起始位
- TDAT = TBUF; //load data from TBUF to TDAT 数据从TBUF加载到TDAT
- TBIT = 9; //initial send bit number (8 data bits + 1 stop bit)初始发送位(8位数据位1停止位)
- }
- else
- {
- TDAT >>= 1; //shift data to CY将 数据右移1位,低位送给CY
- if (--TBIT == 0)
- {
- TXB = 1; //发送停止位数据
- TING = 0; //stop send发送停止位
- TEND = 1; //set send completed flag设置发送完成标志
- }
- else
- {
- TXB = CY; //write CY to TX port 发送单bit数据
- }
- }
- }
- }
- }
复制代码 |