STM8 模拟串口程序

<strong>本程序使用IO口线模拟串口的接收(半双工模式),下面是程序的设计思路:</strong>

 

1、接收

默认串口的数据格式为 一位起始位+8位数据位+停止位,由于起始位为低电平,停止位为高电平。因此,RX线要使能外部中断,中断方式为下降沿中断。只需要在中断程序中使能定时器,并且要在main函数之前对定时器进行初始化,设置定时器的预装值,这里设置的预装值就是设置串口的波特率,另外还要使能定时器的更新中断,在定时器的中断处理程序中,按位接收串口发送过来的数据。当接收到停止位时,失能定时器,然后对定时器进行清零处理

下面是外部中断处理函数:

 

INTERRUPT_HANDLER(EXTI_PORTA_IRQHandler, 3)
{
    if(EXTI_GetExtIntSensitivity(EXTI_PORT_GPIOA)==EXTI_SENSITIVITY_FALL_LOW 
        && emuart_struct.statu==EMUART_WAIT)
    {
        GPIO_Init(GPIOA, GPIO_PIN_3, GPIO_MODE_IN_FL_NO_IT); //关闭PA.3中断 
        emuart_struct.statu=EMUART_RECV;        
        TIM2_Cmd(ENABLE); //Delay(0X1FF); //启动定时器        
    }
}
 

 

 

2、发送

原理同接收一样,在发送函数中使能定时器,并且将RX口线拉低。在定时器更新中断处理函数中按位发送数据,当数据发送完毕后将RX线拉高,并且失能定时器,然后对定时器进行清零处理。

下面是定时器中断处理函数:

 

INTERRUPT_HANDLER(TIM2_UPD_OVF_BRK_IRQHandler, 13)
 { 
   static unsigned char recvdata=0;
   unsigned char bitemp=0;
   unsigned char readbit;
   if(TIM2_GetITStatus(TIM2_IT_UPDATE)==SET)
   {
        TIM2_ClearITPendingBit(TIM2_IT_UPDATE);
      
        if(emuart_struct.statu==EMUART_RECV)            //读取数据
        {
            emuart_struct.bitRecved++; 
            if(emuart_struct.bitRecved>=1 && emuart_struct.bitRecved<=8) 
            {
                if(GPIO_ReadInputPin(GPIOA, GPIO_PIN_3) == 0X08)
                {
                    readbit=(unsigned char)(1<<(emuart_struct.bitRecved-1));
                }
                else
                {
                    readbit=(unsigned char)(0<<(emuart_struct.bitRecved-1));
                }                 
                recvdata|=readbit;                                                 
            }
            else if(emuart_struct.bitRecved==9)
            {            
                emuart_struct.bitRecved=0;
                emuart_struct.recvData=recvdata;
                emuart_struct.isRead=ISNOTREAD;
                emuart_struct.statu=EMUART_WAIT;
                recvdata=0;
                TIM2_Cmd(DISABLE);                      //关闭定时器              
                TIM2->CNTRH=0;//TIM2->ARRH;             //定时器清零                            
                TIM2->CNTRL=0;//TIM2->ARRL;
                GPIO_Init(GPIOA, GPIO_PIN_3, GPIO_MODE_IN_FL_IT);       //打开外部中断
            }
         }

         else if(emuart_struct.statu==EMUART_SEND)       //发送数据
         {        
            emuart_struct.bitSended++;
            if(emuart_struct.bitSended>=1 && emuart_struct.bitSended<=8)         
            {       
                 bitemp=(emuart_struct.sendData>>(emuart_struct.bitSended-1))&0x01;
                 if(bitemp==1)
                 {
                     GPIO_WriteHigh(GPIOB, GPIO_PIN_4);
                 }
                 else if(bitemp==0)
                 {
                     GPIO_WriteLow(GPIOB, GPIO_PIN_4);
                 }        
            }
            else if(emuart_struct.bitSended==9)         //发送或接收数据完毕
            {
                emuart_struct.bitSended=0;
                GPIO_WriteHigh(GPIOB,GPIO_PIN_4);
                emuart_struct.statu=EMUART_WAIT;
                emuart_struct.isSend=ISEND;
                TIM2_Cmd(DISABLE);   //关闭定时器  
                TIM2->CNTRH=0;       //定时器清零                            
                TIM2->CNTRL=0;                      
            } 
         }   
    }
 }
 

源码下载链接:https://download.csdn.net/download/qq_18309083/9642319

 

 

 

 

 

 

 

 

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值