目的:通过串口调试助手给C8051F340的IC发送数据,再读到串口调试助手显示出来(用到中断),以下程序
#include <c8051f340.h>
#include <stdio.h>
#define SYSCLK 12000000
#define BAUDRATE 9600
void SYSCLK_Init (void);
void UART0_Init (void);
void PORT_Init (void);
void
timer2_Init (int);
#define UART_BUFFERSIZE 64
unsigned char UART_Buffer[UART_BUFFERSIZE];
unsigned char UART_Buffer_Size = 0;
unsigned char UART_Input_First = 0;
unsigned char UART_Output_First = 0;
unsigned char TX_Ready =1;
static char Byte;
void main (void)
{
PCA0MD &= ~0x40;
PORT_Init();
SYSCLK_Init ();
UART0_Init();
EA = 1;
while(1)
{
if(TX_Ready == 1 && UART_Buffer_Size != 0 && Byte == 13)
{
TX_Ready = 0;
TI0 = 1;
}
}
}
void PORT_Init (void)
{
P0MDOUT |= 0x10;
XBR0 = 0x01;
XBR1 = 0x40;
}
void SYSCLK_Init (void)
{
OSCICN |= 0x03;
RSTSRC = 0x04;
}
void UART0_Init (void)
{
SCON0 = 0x10;
if (SYSCLK/BAUDRATE/2/256 < 1)
{
TH1 = -(SYSCLK/BAUDRATE/2);
CKCON &= ~0x0B; // T1M = 1; SCA1:0 = xx
CKCON |= 0x08;
}
else if (SYSCLK/BAUDRATE/2/256 < 4)
{
TH1 = -(SYSCLK/BAUDRATE/2/4);
CKCON &= ~0x0B; // T1M = 0; SCA1:0 = 01
CKCON |= 0x01;
}
else if (SYSCLK/BAUDRATE/2/256 < 12)
{
TH1 = -(SYSCLK/BAUDRATE/2/12);
CKCON &= ~0x0B; // T1M = 0; SCA1:0 = 00
}
else
{
TH1 = -(SYSCLK/BAUDRATE/2/48);
CKCON &= ~0x0B; // T1M = 0; SCA1:0 = 10
CKCON |= 0x02;
}
TL1 = TH1;
TMOD &= ~0xf0;
TMOD |= 0x20;
TR1 = 1;
TX_Ready = 1;
IP |= 0x10;
ES0 = 1;
}
void UART0_Interrupt (void) interrupt 4
{
if (RI0 == 1)
{
if( UART_Buffer_Size == 0)
{
UART_Input_First = 0;
}
RI0 = 0;
Byte = SBUF0;
if (UART_Buffer_Size < UART_BUFFERSIZE)
{
UART_Buffer[UART_Input_First] = Byte;
UART_Buffer_Size++;
UART_Input_First++;
}
}
if (TI0 == 1)
{
TI0 = 0;
if (UART_Buffer_Size != 0)
{
if ( UART_Buffer_Size == UART_Input_First ) {
UART_Output_First = 0; }
Byte = UART_Buffer[UART_Output_First];
if ((Byte >= 0x61) && (Byte <= 0x7A))
{
Byte -= 32;
}
SBUF0 = Byte;
UART_Output_First++;
UART_Buffer_Size--;
}
else
{
UART_Buffer_Size = 0;
TX_Ready = 1;
}
}
}
但是以上程序出现问题,发送bbbb,读出的HEX值为
9F 89 89 35
或EB 62 62 62 0D
请教各位大神,我这程序哪错了?我检查了很久都没找出错误,泪崩中。
一周热门 更多>