stm8系列--usart收发+遇到问题

 

#define _maxbuf 100

typedef struct
{
 u8 rcbuf[_maxbuf]; //MODBUS接收缓冲区
 u16 timout;//MODbus的数据断续时间    
 u8 recount;//MODbus端口已经收到的数据个数
 u8  reflag;//收到一帧数据的标志
 u8 Sendbuf[_maxbuf]; //MODbus发送缓冲区    
 
}MYUSART;
View Code

 

void main(void)
{

    Init_UART1();
    Init_Timer4();

    enableInterrupts(); 

    while (1)
    {
        if(myusart.reflag>0)
        {
            Com_Handle();//收到什么回复什么;        
            myusart.recount=0;
            myusart.reflag=0;
        }    
    }  
}
MAIN

 

void Init_Timer4(void)
{
    TIM4_TimeBaseInit(TIM4_PRESCALER_128, 0x0f);  //1MS

  TIM4_ClearFlag(TIM4_FLAG_UPDATE);

    TIM4_ITConfig(TIM4_IT_UPDATE, ENABLE);
    TIM4_Cmd(ENABLE);
}
Init_Timer4

 

MYUSART myusart;

void Init_UART1(void)
{
    UART1_DeInit();
    UART1_Init((u32)9600, UART1_WORDLENGTH_8D, UART1_STOPBITS_1, UART1_PARITY_NO, UART1_SYNCMODE_CLOCK_DISABLE, UART1_MODE_TXRX_ENABLE);
    UART1_ITConfig(UART1_IT_RXNE_OR, ENABLE);
}

void UART1_SendData8_Buff(uint8_t* data,uint8_t len)
{
    uint8_t i = 0;
    for(i=0;i<len;i++)
    {
        while(( UART1_GetFlagStatus(UART1_FLAG_TXE)==RESET));
      UART1_SendData8(data[i]);    
    }
}

void Com_Handle(void)//发送什么回复什么
{
    UART1_SendData8_Buff(myusart.rcbuf,myusart.recount);
}
Init_UART1

 

 INTERRUPT_HANDLER(UART1_RX_IRQHandler, 18)
 {
    /* In order to detect unexpected events during development,
       it is recommended to set a breakpoint on the following instruction.
    */
#if 1
        if( myusart.reflag==0)
        {
            myusart.timout=10;
            if(myusart.recount<_maxbuf)
            {
                myusart.rcbuf[myusart.recount++]=UART1_ReceiveData8();
            }
        }
#endif
        UART1_ClearITPendingBit(UART1_IT_RXNE);
        
 }
USART中断18

 

int time_i=0;

 INTERRUPT_HANDLER(TIM4_UPD_OVF_IRQHandler, 23)
{
  /* In order to detect unexpected events during development,
     it is recommended to set a breakpoint on the following instruction.
  */
    #if 1
    if(myusart.timout>0)
    {
        if(--myusart.timout==0)
        {
            myusart.reflag=1; //收到一帧数据
        }
    }
    #endif
    time_i++;
    if(time_i>=300)
    {
        time_i=0;
        flag_send=1;
    }
    //300MS
    TIM4_ClearITPendingBit(TIM4_IT_UPDATE);
}
TIM4中断23

 

 

 

 https://blog.csdn.net/taoynkkx/article/details/46821953?spm=1001.2101.3001.6650.5&utm_medium=distribute.pc_relevant.none-task-blog-2%7Edefault%7EBlogOpenSearchComplete%7ERate-5-46821953-blog-71158962.235%5Ev43%5Epc_blog_bottom_relevance_base8&depth_1-utm_source=distribute.pc_relevant.none-task-blog-2%7Edefault%7EBlogOpenSearchComplete%7ERate-5-46821953-blog-71158962.235%5Ev43%5Epc_blog_bottom_relevance_base8&utm_relevant_index=10

posted @ 2024-05-29 11:22  不折不扣  阅读(51)  评论(0)    收藏  举报