741342386 发表于 2019-10-24 18:49:42

STC15W201S 串口接收数据T0定时器死掉

本帖最后由 741342386 于 2019-10-24 18:57 编辑

配置: T2做串口波特率发生器9600
      T0做定时计数器         10ms
现象: P55上有个LED定时器设置1S闪烁间隔, 串口不接受数据时一切正常,串口可以正常发送数据,P55上LED正常以1S间隔闪烁.
      用串口助手向单片机发送数据( 例子: 0xaa 0xbb 0xcc ), 串口只能接收到第一个数据 0xaa紧接着P55上LED停止闪烁, 此时发送任何数据单片机都无法接收,LED也不会再闪烁
代码:

#include "STC15W4K32S4.h"
#include <INTRINS.H>

#define u8 unsigned char
#define u16 unsigned int
#define u32 unsigned long

sbit Led= P5^5;
bit        B_TX1_Busy;
volatile u16 Test = 0;

void Set_my_IO( void )
{
        P3_Mode_PullUp( PIN_0 );//P3.0( RXD )
        P3_Mode_OUT_PP( PIN_1 | PIN_2 | PIN_3 );//P3.1( TXD ) P3.2 P3.3
        P5_Mode_OUT_PP( PIN_4 | PIN_5 );
       
        Led = 0;
}

void UART1_config( void )
{
        AUXR |= 0x01;                //S1 BRT Use Timer2;
        AUXR &= ~(1<<4);        //Timer stop
        AUXR &= ~(1<<3);        //Timer2 set As Timer
        AUXR |=(1<<2);        //Timer2 set as 1T mode
        T2H = 0xFE;
        T2L = 0xE0;
        IE2&= ~(1<<2);       
        AUXR |=(1<<4);        //Timer run enable
        SCON = (SCON & 0x3f) | (1<<6);       
        ES= 1;       
        REN = 1;       
        B_TX1_Busy= 0;
}
void Timer0Init(void)//10ms
{
        TR0 = 0;               
        ET0 = 0;       
        AUXR |= T0x12;       
        TMOD &= ~T0_C_T;       
        TMOD &= 0xfc;               
        TL0 = 0x00;               
        TH0 = 0x28;
        PT0 = 0;       
        INT_CLKO &= ~T0CLKO;       
        ET0 = 1;               
        TR0 = 1;               
        EA = 1;
}

void UART1_Send_Byte(u8 puts)
{
        B_TX1_Busy = 1;               
        SBUF = puts;               
        delay_short(10);
        while(B_TX1_Busy);       
}

void main( void )
{
        Set_my_IO();
        UART1_config();
        Timer0Init();
        EA = 1;
        while(1)
        {
                ;
        }
}

void Timer0_Routine(void)        interrupt 1
{
        Test ++;       
        if( Test > 100 )
        {       
                Test = 0;
                if( Led == 1 ){ Led = 0; }
                else          { Led = 1; }
        }
}

void UART1_int (void) interrupt 4
{
        if(RI)
        {
                RI = 0;
                UART1_Send_Byte( SBUF );
        }
        if(TI)
        {
                TI = 0;
                B_TX1_Busy = 0;
        }
}

lcw_swust 发表于 2019-10-24 19:50:56

本帖最后由 lcw_swust 于 2019-10-24 20:01 编辑

你在串口中断里发送数据,但是发送时有个死循环等待标志,那个标志又需要进入中断才清0,所以死掉了。
while(B_TX1_Busy)
改为
while(B_TX1_Busy&&(TI==0));
TI=0;

741342386 发表于 2019-10-24 20:19:42

本帖最后由 741342386 于 2019-10-24 20:21 编辑

lcw_swust 发表于 2019-10-24 19:50
你在串口中断里发送数据,但是发送时有个死循环等待标志,那个标志又需要进入中断才清0,所以死掉了。
whi ...

感激不尽可以了,主要一开始没把发送放在接收中断里的. 因为一接收数据就死我就想看看到底收到几个数
页: [1]
查看完整版本: STC15W201S 串口接收数据T0定时器死掉