2400波特率 发送接收测试都OK ,占用一个定时器
- void timer_init()//定时器初始化
- {
- _ctm0c0 = 0b00000000; //16000000/4 =4000,000
- _ctm0c1 = 0xc1;
- _ctm0al = 0xa0;
- _ctm0ah = 0x01;
- _ctma0f=0;
- _ctma0e=1;
- _ct0on = 1;
- _ct0pau = 0; //
- _emi = 1;
- }
- void __attribute((interrupt(0x14))) ctm0(void)
- {
- _ctma0f=0;
- if(RX==0&&Rx_start_bit==0)
- {
- Rx_start_bit=1;
- RX_start_Receive=1;
- j=0;
- RI=0;
- }
- if(RX_start_Receive==1)
- {
- i_jia++;
- if(i_jia>=4)
- {
- i_jia=0;
- if(j<10)
- {
- Output >>=1;
- if(RX)
- {
- Output|=0x80; //先收低位
- }
- j++;
- }
- if(RX==1&&flah6==1){flah6=0;RX_start_Receive=0;Rx_start_bit=0;RI=1;}
- if(j==9){j=0;receiveByte=Output;flah6=1;}
- }
- }
- if(moni_uart_start==1) ///发送串口数据
- {
- t++;
- if(t>=4)
- {
- t=0;
- if(t1>=8){i=0;moni_uart_start=0;TX=1;t=0;t1=0;jump_first_bit=0;}
- if(jump_first_bit==1)
- {
- if(i<9)//发送8位数据位
- {
- if(send_buff&0x01) //先传低位
- {
- TX=1;
- }
- else
- {
- TX=0;
- }
- i++;
- send_buff=send_buff>>1;
- }
- t1++;
- }
- if(send_flrst_bit==0) {i=0;send_flrst_bit=1;TX=0;jump_first_bit=1;send_buff=moni_send_dat;} //发送启始位
- }
- }
- }