在用串口调试的过程中,串口调试助手显示的数据,一会是对的,一会是错的,还有今天调试是好的,结果第二天再重新运行程序就出错了,现附上程序代码,求大神们指正。用IAR编译的
#include <nxp/iolpc2148.h>
#include <stdio.h>
#include <intrinsics.h>
typedef unsigned char uint8; /* defined for unsigned 8-bits integer variable 无符号8位整型变量 */
typedef signed char int8; /* defined for signed 8-bits integer variable 有符号8位整型变量 */
typedef unsigned short uint16; /* defined for unsigned 16-bits integer variable 无符号16位整型变量 */
typedef signed short int16; /* defined for signed 16-bits integer variable 有符号16位整型变量 */
typedef unsigned int uint32; /* defined for unsigned 32-bits integer variable 无符号32位整型变量 */
typedef signed int int32; /* defined for signed 32-bits integer variable 有符号32位整型变量 */
typedef float fp32; /* single precision floating point variable (32bits) 单精度浮点数(32位长度) */
typedef double fp64; /* double precision floating point variable (64bits) 双精度浮点数(64位长度) */
//系统时钟宏定义
#define XTALFREQ 12000000 //XTAL frequency in Hz
#define PCLKFREQ (XTALFREQ/4) //pclk must always be XTALFREQ/4?
//函数宏定义
#define SetBit(x,y); ( (x) |= (1<<y) )
#define ClrBit(x,y); ( (x) &= (~(1<<y)) )
void UARTO_SendBuf();
typedef struct UartMode
{
uint8 datab;
uint8 stopb;
uint8 parity;
}UARTMODE;
uint8 rcv_buf[8];
volatile uint8 rcv_new;
void UART0IntHandler(void)
{
uint8 i;
if((U0IIR&0X0F)==0x04)
{
for(i=0;i<8;i++)
{
rcv_buf[i]= U0RBR;
}
rcv_new=1;
}
VICVectAddr=0x00;
}
void UARTO_SendByte(uint8 dat)
{
U0THR=dat;
}
void UARTO_SendBuf()
{
uint8 i;
for(i=0;i<8;i++)
UARTO_SendByte(rcv_buf[i]);
while(U0LSR&0X20);
}
void uart_int(void)
{
//配置UART1的寄存器
/*Configure UART1 to 9600 buad, 8 bit, 1 stop, no parity */
U0FCR = 1; // Enable FIFOs whether used or not
SetBit(U0LCR,7); // U1LCR = 0X80-enable access to divisor
// latch bit, necessary for setting baud rate
U0LCR |= 3; // Eight bits U1LCR = 0X83
ClrBit(U0LCR,3); // No parity
ClrBit(U0LCR,2); // One stop bit
U0DLL = PCLKFREQ / (9600 * 16);
U0DLM = (PCLKFREQ / (9600 * 16)) >> 8;
ClrBit(U0LCR,7);
}
int main(void)
{
rcv_new=0;
PINSEL0=0X05;
uart_int();
U0FCR = 0x81;
U0IER = 0x01;
__enable_interrupt();
VICIntSelect = 0x00;
VICVectCntl0 = 0x20|0x06;
VICVectAddr0 = (uint32)UART0IntHandler;
VICIntEnable = 1<<0x06;
while(1)
{
if(rcv_new==1)
{
rcv_new==0;
UARTO_SendBuf();
}
}
}
//irq 中断入口
#pragma vector=IRQV
__irq __arm void irq_handler (void)
{
void (*interrupt_function)();
unsigned int vector;
vector = VICVectAddr; //获得中断向量.
interrupt_function = (void(*)())vector;
if(interrupt_function != NULL)
{
interrupt_function(); //调用向量中断函数.
}
else
{
VICVectAddr = 0; //清除VIC中的中断.
}
}
//fiq中断入口
__fiq __arm void fiq_handler (void)
{
while(1);
}
|