This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

TM4C123的UART的接收问题,UARTgets()函数一直跳不出来,停止位是0吗?



如题!

主程序

int main(void)
{
//
// Set the clocking to run directly from the crystal.
//配置系统时钟16M,不分频
//
SysCtlClockSet(SYSCTL_SYSDIV_1 | SYSCTL_USE_OSC | SYSCTL_OSC_MAIN |
SYSCTL_XTAL_16MHZ);

//
// Initialize the UART and write status.
//串口初始化以及写状态
//
ConfigureUART();
UARTEnable(GPIO_PORTA_BASE);
//
// Enable the peripherals used by this example.
//使能定时器1时钟
//
SysCtlPeripheralEnable(SYSCTL_PERIPH_TIMER1);
//
// Enable the peripherals used by this example.
//使能定时器2时钟
//
SysCtlPeripheralEnable(SYSCTL_PERIPH_TIMER0); //
// For this example T1CCP1 is used with port B pin 5.
// The actual port and pins used may be different on your part, consult
// the data sheet for more information.
// GPIO port B needs to be enabled so these pins can be used.
// TODO: change this to whichever GPIO port you are using.
//T1CCP1只能使用PB5口,详见pin_map.h,不同的板子另说
//
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOB);
//
// Configure the GPIO pin muxing for the Timer/CCP function.
// This is only necessary if your part supports GPIO pin function muxing.
// Study the data sheet to see which functions are allocated per pin.
// TODO: change this to select the port/pin you are using
//管脚配置
//
GPIOPinConfigure(GPIO_PB5_T1CCP1);
GPIOPinConfigure(GPIO_PB6_T0CCP0);
//
// Configure the ccp settings for CCP pin. This function also gives
// control of these pins to the SSI hardware. Consult the data sheet to
// see which functions are allocated per pin.
// TODO: change this to select the port/pin you are using.
//管脚类型设置为定时器外设
//
GPIOPinTypeTimer(GPIO_PORTB_BASE, GPIO_PIN_5);
GPIOPinTypeTimer(GPIO_PORTB_BASE, GPIO_PIN_6);
//
//串口打印
//
UARTprintf("16-Bit Timer PWM ->");
UARTprintf("\n Timer = Timer1B");
UARTprintf("\n Mode = PWM");
UARTprintf("\n Duty Cycle = 可调\n");
UARTprintf("\nGenerating PWM on CCP1 (PB5) -> ");
UARTprintf("\n Timer = Timer0A");
UARTprintf("\n Mode = CAP_COUNT");
//
// Configure the two 32-bit periodic timers.
//配置定时器1:分立工作|B:PWM
//
TimerConfigure(TIMER1_BASE,TIMER_CFG_SPLIT_PAIR|TIMER_CFG_B_PWM);
TimerConfigure(TIMER0_BASE,TIMER_CFG_SPLIT_PAIR|TIMER_CFG_A_CAP_COUNT);
TimerControlEvent(TIMER0_BASE, TIMER_A, TIMER_EVENT_POS_EDGE);
//
//定时器预置值设定,设定占空比为50000/t
//
TimerLoadSet(TIMER1_BASE, TIMER_B, 50000);
//
//定时器预置值设定
//
TimerLoadSet(TIMER0_BASE, TIMER_A, 50000);
//
//定时器匹配值设定
//
TimerMatchSet(TIMER0_BASE,TIMER_A,0);
//
//Set the Timer1B match value to load value / 10*t.
//
TimerMatchSet(TIMER1_BASE, TIMER_B,TimerLoadGet(TIMER1_BASE, TIMER_B)/10 *t);
//
//使能定时器中断
//
IntEnable(INT_TIMER0A);
//
//定时器中断使能为捕获比较
//
TimerIntEnable(TIMER0_BASE, TIMER_CAPA_MATCH);
//
// Enable the timers.
//使能定时器
//
TimerEnable(TIMER1_BASE, TIMER_B);
TimerEnable(TIMER0_BASE, TIMER_A);
//
// Loop forever while the timers run.
//
while(1)
{
if(t>=1)t--;//调节占空比从0到1,十分之一步进
else t=10;
//
// Print out indication on the console that the program is running.
//
PrintRunningDots();
//
//Set the Timer1B match value to load value /10* t.
//
TimerMatchSet(TIMER1_BASE, TIMER_B,TimerLoadGet(TIMER1_BASE, TIMER_B)/10*t);
//
//Get the Timer1B match value.
//
timerload=TimerMatchGet(TIMER1_BASE, TIMER_B);
//
//打印定时器比较值,PWM波占空比
//UARTgetc();
sysctlvol=SysCtlClockGet();
edgecount=TimerValueGet(TIMER0_BASE,TIMER_A);
UARTgets(cp,0);
UARTprintf("\nsysctlvol:\%u\n",sysctlvol);
UARTprintf("\ntimerload:\%u\n",timerload);
UARTprintf("\n 占空比:\%u%%\n",100-t*10);
UARTprintf("\n 中断次数:\%u\n",g_ui32IntCount);
UARTprintf("\n 脉冲计数:\%u\n",edgecount);
//UARTprintf("\nuartgets:\%u\n",*cp);

}
}

  • 怎样可以停止接收,跳出函数继续执行???

  • 如果需要直接跳出来,可以使用函数UARTCharGetNonBlocking。

    关于UARTCharGetNonBlocking和UARTCharGet的详细区别,请阅读TivaWare目录下doc文件夹中SW-TM4C-DRL-UG-2.1.0.12573.pdf文件的564页。

  • 我试过用UARTCharGetNonBlocking,但是它返回的值有点看不懂,我用串口调试助手发送字母,可是程序接收后打印出来是数字

  • 直接看源码吧:

    int32_t
    UARTCharGetNonBlocking(uint32_t ui32Base)
    {
    //
    // Check the arguments.
    //
    ASSERT(_UARTBaseValid(ui32Base));

    //
    // See if there are any characters in the receive FIFO.
    //
    if(!(HWREG(ui32Base + UART_O_FR) & UART_FR_RXFE))
    {
    //
    // Read and return the next character.
    //
    return(HWREG(ui32Base + UART_O_DR));
    }
    else
    {
    //
    // There are no characters, so return a failure.
    //
    return(-1);
    }
    }

    如果UART接收FIFO有数据,则返回值为FIFO中的数据;

    如果UART接收FIFO为空,则返回-1.

    对应的,如果使用UARTCharGet时:

    int32_t
    UARTCharGet(uint32_t ui32Base)
    {
    //
    // Check the arguments.
    //
    ASSERT(_UARTBaseValid(ui32Base));

    //
    // Wait until a char is available.
    //
    while(HWREG(ui32Base + UART_O_FR) & UART_FR_RXFE)
    {
    }

    //
    // Now get the char.
    //
    return(HWREG(ui32Base + UART_O_DR));
    }

    如果UART接收FIFO有数据,则返回值为FIFO中的数据;

    如果UART接收FIFO为空,则程序会一直停下来等待,知道FIFO中收到数据.