使用LM4F232H5QC这个片子的Uart2(PD6,PD7)的时候发现MCU可以接收到PC端的数据,但是无法发送数据,用示波器测了下PD7这个管脚,发现MCU发送数据时该管脚上无波形输出,MCU收数据的时候是有波形的。
使用GPIO将PD7这个管脚拉高拉低都没有问题,但是配置成Uart7就无法输出Uart数据,这是怎么回事儿呢?
代码:
系统初始化
Void main()
{ //Task_Params taskParams;
/* IO init s*/
ROM_FPULazyStackingEnable();
ROM_SysCtlClockSet(SYSCTL_SYSDIV_4 | SYSCTL_USE_PLL | SYSCTL_XTAL_16MHZ |
SYSCTL_OSC_MAIN);
ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOH);
ROM_GPIOPinTypeGPIOOutput(GPIO_PORTH_BASE, GPIO_PIN_2);
GPIOPinWrite(GPIO_PORTH_BASE, GPIO_PIN_2, 0x04);
ConfigureUART2();
UARTprintf("****************************************************\n");
UARTprintf("***********XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX**********\n");
UARTprintf("****************************************************\n");
/* IO init e*/
WatchDogInit(); /* enable watchdog1 */
BIOS_start(); /* enable interrupts and start SYS/BIOS */
}
void ConfigureUART2(void)
{
MAP_SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOD);
MAP_SysCtlPeripheralEnable(SYSCTL_PERIPH_UART2);
MAP_GPIOPinConfigure(GPIO_PD6_U2RX);
MAP_GPIOPinConfigure(GPIO_PD7_U2TX);
MAP_GPIOPinTypeUART(GPIO_PORTD_BASE, GPIO_PIN_6 | GPIO_PIN_7);
MAP_UARTConfigSetExpClk(UART2_BASE, ROM_SysCtlClockGet(), 115200,(UART_CONFIG_WLEN_8 | UART_CONFIG_STOP_ONE | UART_CONFIG_PAR_NONE));
MAP_UARTIntDisable(UART2_BASE, 0xFFFFFFFF);
MAP_UARTIntEnable(UART2_BASE, UART_INT_RX | UART_INT_RT);
MAP_IntEnable(INT_UART2);
MAP_UARTEnable(UART2_BASE);
}
/*Data Receive*/
void DebugIntHandler(UArg arg)
{
Type_sWord aubRet;
UARTIntClear(UART2_BASE, UART_INT_RX | UART_INT_RT);
UARTRxErrorClear(UART2_BASE);
aubRet=UARTCharGetNonBlocking(UART2_BASE);
if(aubRet!=-1)
{
UARTprintf("****************** UART2 ISR ok *********************\n");
//UARTprintf("%c",aubRet);
wvdDebugLogRcvIntData(aubRet);
}
}
/*Data Transimit*/
void UARTprintf(const Type_uByte *pcString, …)
{
va_list vaArgP;
//
// Start the varargs processing.
//
va_start(vaArgP, pcString);
UARTvprintf(pcString, vaArgP);
//
// We're finished with the varargs now.
//
va_end(vaArgP);
}
void
UARTvprintf(const Type_uByte *pcString, va_list vaArgP)
{
Type_sWord ui32Idx, ui32Value, ui32Pos, ui32Count, ui32Base, ui32Neg;
Type_uByte *pcStr, pcBuf[16], cFill;
//
// Loop while there are more characters in the string.
//
while(*pcString)
{
//
// Find the first non-% character, or the end of the string.
//
for(ui32Idx = 0;
(pcString[ui32Idx] != '%') && (pcString[ui32Idx] != '\0');
ui32Idx++)
{
}
//
// Write this portion of the string.
//
UARTwrite(pcString, ui32Idx);
//
// Skip the portion of the string that was written.
//
pcString += ui32Idx;
//
// See if the next character is a %.
//
if(*pcString == '%')
{
//
// Skip the %.
//
pcString++;
//
// Set the digit count to zero, and the fill character to space
// (in other words, to the defaults).
//
ui32Count = 0;
cFill = ' ';
//
// It may be necessary to get back here to process more characters.
// Goto's aren't pretty, but effective. I feel extremely dirty for
// using not one but two of the beasts.
//
again:
//
// Determine how to handle the next character.
//
switch(*pcString++)
{
//
// Handle the digit characters.
//
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
{
//
// If this is a zero, and it is the first digit, then the
// fill character is a zero instead of a space.
//
if((pcString[-1] == '0') && (ui32Count == 0))
{
cFill = '0';
}
//
// Update the digit count.
//
ui32Count *= 10;
ui32Count += pcString[-1] – '0';
//
// Get the next character.
//
goto again;
}
//
// Handle the %c command.
//
case 'c':
{
//
// Get the value from the varargs.
//
ui32Value = va_arg(vaArgP, Type_uWord);
//
// Print out the character.
//
UARTwrite((Type_uByte *)&ui32Value, 1);
//
// This command has been handled.
//
break;
}
//
// Handle the %d and %i commands.
//
case 'd':
case 'i':
{
//
// Get the value from the varargs.
//
ui32Value = va_arg(vaArgP, Type_uWord);
//
// Reset the buffer position.
//
ui32Pos = 0;
//
// If the value is negative, make it positive and indicate
// that a minus sign is needed.
//
if((Type_sWord)ui32Value < 0)
{
//
// Make the value positive.
//
ui32Value = -(Type_sWord)ui32Value;
//
// Indicate that the value is negative.
//
ui32Neg = 1;
}
else
{
//
// Indicate that the value is positive so that a minus
// sign isn't inserted.
//
ui32Neg = 0;
}
//
// Set the base to 10.
//
ui32Base = 10;
//
// Convert the value to ASCII.
//
goto convert;
}
//
// Handle the %s command.
//
case 's':
{
//
// Get the string pointer from the varargs.
//
pcStr = va_arg(vaArgP, Type_uByte *);
//
// Determine the length of the string.
//
for(ui32Idx = 0; pcStr[ui32Idx] != '\0'; ui32Idx++)
{
}
//
// Write the string.
//
UARTwrite(pcStr, ui32Idx);
//
// Write any required padding spaces
//
if(ui32Count > ui32Idx)
{
ui32Count -= ui32Idx;
while(ui32Count–)
{
UARTwrite(" ", 1);
}
}
//
// This command has been handled.
//
break;
}
//
// Handle the %u command.
//
case 'u':
{
//
// Get the value from the varargs.
//
ui32Value = va_arg(vaArgP, Type_uWord);
//
// Reset the buffer position.
//
ui32Pos = 0;
//
// Set the base to 10.
//
ui32Base = 10;
//
// Indicate that the value is positive so that a minus sign
// isn't inserted.
//
ui32Neg = 0;
//
// Convert the value to ASCII.
//
goto convert;
}
//
// Handle the %x and %X commands. Note that they are treated
// identically; in other words, %X will use lower case letters
// for a-f instead of the upper case letters it should use. We
// also alias %p to %x.
//
case 'x':
case 'X':
case 'p':
{
//
// Get the value from the varargs.
//
ui32Value = va_arg(vaArgP, Type_uWord);
//
// Reset the buffer position.
//
ui32Pos = 0;
//
// Set the base to 16.
//
ui32Base = 16;
//
// Indicate that the value is positive so that a minus sign
// isn't inserted.
//
ui32Neg = 0;
//
// Determine the number of digits in the string version of
// the value.
//
convert:
for(ui32Idx = 1;
(((ui32Idx * ui32Base) <= ui32Value) &&
(((ui32Idx * ui32Base) / ui32Base) == ui32Idx));
ui32Idx *= ui32Base, ui32Count–)
{
}
//
// If the value is negative, reduce the count of padding
// characters needed.
//
if(ui32Neg)
{
ui32Count–;
}
//
// If the value is negative and the value is padded with
// zeros, then place the minus sign before the padding.
//
if(ui32Neg && (cFill == '0'))
{
//
// Place the minus sign in the output buffer.
//
pcBuf[ui32Pos++] = '-';
//
// The minus sign has been placed, so turn off the
// negative flag.
//
ui32Neg = 0;
}
//
// Provide additional padding at the beginning of the
// string conversion if needed.
//
if((ui32Count > 1) && (ui32Count < 16))
{
for(ui32Count–; ui32Count; ui32Count–)
{
pcBuf[ui32Pos++] = cFill;
}
}
//
// If the value is negative, then place the minus sign
// before the number.
//
if(ui32Neg)
{
//
// Place the minus sign in the output buffer.
//
pcBuf[ui32Pos++] = '-';
}
//
// Convert the value into a string.
//
for(; ui32Idx; ui32Idx /= ui32Base)
{
pcBuf[ui32Pos++] =
g_pcHex[(ui32Value / ui32Idx) % ui32Base];
}
//
// Write the string.
//
UARTwrite(pcBuf, ui32Pos);
//
// This command has been handled.
//
break;
}
//
// Handle the %% command.
//
case '%':
{
//
// Simply write a single %.
//
UARTwrite(pcString – 1, 1);
//
// This command has been handled.
//
break;
}
//
// Handle all other commands.
//
default:
{
//
// Indicate an error.
//
UARTwrite("ERROR", 5);
//
// This command has been handled.
//
break;
}
}
}
}
}
int UARTwrite(const Type_uByte *pcBuf, Type_uWord ui32Len)
{
unsigned int uIdx;
//
// Send the characters
//
for(uIdx = 0; uIdx < ui32Len; uIdx++)
{
//
// If the character to the UART is \n, then add a \r before it so that
// \n is translated to \n\r in the output.
//
if(pcBuf[uIdx] == '\n')
{
MAP_UARTCharPut(UART2_BASE, '\r');
}
//
// Send the character to the UART output.
//
MAP_UARTCharPut(UART2_BASE, pcBuf[uIdx]);
}
//
// Return the number of characters written.
//
return(uIdx);
}
Triton Zhang:
LM4F232H5QC的PD7端口默认为NMI功能,在使能PD.7为UART功能时,需要切换PD.7端口为数字UART口的TX功能.
在切换时需哟输入UNLOCK密码:
HWREG(GPIO_PORTD_BASE + GPIO_O_LOCK) = GPIO_LOCK_KEY_DD;
HWREG(GPIO_PORTD_BASE + GPIO_O_CR) = 0x80;
GPIOPinTypeGPIOOutput(GPIO_PORTD_BASE,0x80);
MAP_GPIOPinConfigure(GPIO_PD7_U2TX);
HWREG(GPIO_PORTD_BASE + GPIO_O_LOCK) = GPIO_LOCK_KEY_DD;
HWREG(GPIO_PORTD_BASE + GPIO_O_CR) = 0x00;
HWREG(GPIO_PORTD_BASE + GPIO_O_LOCK) = 0X00;
zhentao yang:
回复 Triton Zhang:
我把这段程序代码加了进去,但是执行到第二句
HWREG(GPIO_PORTD_BASE + GPIO_O_CR) = 0x80;
这句的时候就出现了异常,这是为什么呢?