stm8系列--usart收發

不折不扣發表於2024-05-29

stm8系列--usart收發
void main(void)
{

    Init_UART1();
    Init_Timer4();

    enableInterrupts(); 

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

stm8系列--usart收發
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

stm8系列--usart收發
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

stm8系列--usart收發
 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

stm8系列--usart收發
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

相關文章