这段代码有什么问题(我正在尝试在 tm4c123gh6pm 中使用 UART2)

问题描述 投票:0回答:0
#include "tm4c123gh6pm.h"
#include<string.h>
#include<stdint.h>

void RGBLED_Init(void){
    
SYSCTL_RCGCGPIO_R |= 0x20;   //enabling the clock of port f
while ((SYSCTL_RCGCGPIO_R & 0x20)== 0);  // delay
GPIO_PORTF_LOCK_R =  0x4C4F434B;
GPIO_PORTF_CR_R |= 0x0E;
GPIO_PORTF_AMSEL_R &= ~0x0E;
GPIO_PORTF_PCTL_R  &= ~0x0000FFF0;
GPIO_PORTF_AFSEL_R &= ~0x0E;
GPIO_PORTF_DIR_R |= 0x0E;
GPIO_PORTF_DEN_R  |= 0x0E;
GPIO_PORTF_DATA_R  &= ~0x0E;
}


void rgb_output(unsigned char data)
{
  //GPIO_PORTF_DATA_R &= ~(0x0E); // reset pf3,pf2,pf1
    GPIO_PORTF_DATA_R |= data; 
}


void rgb_reset()
{
  GPIO_PORTF_DATA_R &= ~(0x0E); // reset pf3,pf2,pf1
}


void uart_init(unsigned clk,unsigned baudrate)
{
      unsigned BRD;
    
      SYSCTL_RCGCUART_R |= 0X04; // activate UART2
      while((SYSCTL_RCGCUART_R & 0XFF) == 0);
      SYSCTL_RCGCGPIO_R |= 0X08; //  activate port D
      while((SYSCTL_RCGCGPIO_R & 0X08) == 0);
    
    
      UART2_CTL_R &= ~(0X0001);   // disable UART
      BRD = ((clk<<2) + (baudrate<<1))/baudrate; // SET BAUD RATE DIVISOR
      UART2_IBRD_R = BRD >> 6;
      UART2_FBRD_R = BRD&63;
    
        GPIO_PORTD_AFSEL_R |= 0XC0; // enable alt function PD6, PD7
      GPIO_PORTD_PCTL_R = (GPIO_PORTD_PCTL_R & 0X00FFFFFF) | 0X11000000; // configure uart for pa0,pa1
      
      UART2_LCRH_R = 0X0070; // 8-bit word length, endable Fifo
      UART2_CTL_R = 0X0301;  // enable RXE,TXE AND UART
    
      GPIO_PORTD_DEN_R |= 0XC0;  // enable digital IO on PD6,PD7
      GPIO_PORTD_AMSEL_R &= ~0XC0; // disable analog function on PD6, PD7
         
}
char uart_inchar(void)
{
    while((UART2_FR_R & 0X10) != 0);
    return (char)(UART2_DR_R & 0XFF);
}

void uart_outchar(char data)
{
    while((UART2_FR_R & 0X0020) != 0);
  UART2_DR_R = data;
}
void uart_outstring(char *pt)
{
    while(*pt){
        uart_outchar(*pt);
        pt++;
    }
}

void uart_instring(char *command,int len)
{
    char character;
    int i = 0;
    for(;i<len;i++)
    {
        character = uart_inchar();
        if(character!='\r')
        {
            command[i] = character;
          uart_outchar(command[i]);
        }
        else
            break;
    }
    
}
const int len = 200;
char command[len]={0};
char c;
int main(void)
{
    
    RGBLED_Init();
    uart_init(16000000,9600);
    for(;;)
    {
        uart_outstring("Enter : \n");
           uart_instring(command,len);
         c = command[0];
          if(c=='r')
            rgb_output(0x02);
            else if(c=='b')
            rgb_output(0x04);
            else if(c=='g')
            rgb_output(0x08);
            else
                rgb_reset();
            
            memset(command,0,len);
            uart_outstring("\n");
        }
    }

这是使用 UART2(PD6,PD7)的串行窗口控制 TM4C123GH6PM 中的 LED(红色、蓝色和绿色)(PF1、PF2、PF3)的代码,但每当我收到此错误enter image description here(在图片中)代码到达一行对来自 uart2 或端口 D 的寄存器进行读取或写入操作,我正在使用 keil 编译器

谢谢

我将 uart0(PA0,PA1) 用于相同的代码(当然更改值)并且它有效所以 uart2 的问题是什么enter image description here

c arm embedded microcontroller
© www.soinside.com 2019 - 2024. All rights reserved.