#include <C8051F120.h>
#include <stdio.h>
#include <string.h>
unsigned char code string_change[]={0X43,0X44,0X20,0X77,0X6A,0X79,0X0D,0XFF};
#define uint unsigned int
#define BAUDRATE 115200
#define SYSTEMCLOCK 24500000
uchar rbyte;
unsigned char flag_rece=0 ;
unsigned char flag_send=0 ;
void Device_Init();
void Delay_ms(uint time);
void PORT_Init();
void SCK_Init();
void UART_SendS(uchar code *p);
void UART_SendB(uchar rbyte);
void UART0_Init();
void main()
{
Device_Init();
Delay_ms(200);
UART_SendS(string_change);
// UART_ReceB();
Delay_ms(200);
UART_SendB(rbyte);
while(1);
}
void Device_Init()
{
WDTCN = 0xDE;
WDTCN = 0xAD;
SCK_Init();
PORT_Init();
UART0_Init();
EA = 1;
SFRPAGE = UART0_PAGE ;
}
void SCK_Init()
{
char SFRPAGE_SAVE = SFRPAGE;
SFRPAGE = CONFIG_PAGE;
OSCICN = 0x83;
SFRPAGE = SFRPAGE_SAVE;
}
void PORT_Init()
{
char SFRPAGE_SAVE = SFRPAGE;
SFRPAGE = CONFIG_PAGE;
XBR0 = 0x04;
XBR2 = 0x44;
P0MDOUT = 0x03;
SFRPAGE=SFRPAGE_SAVE;
}
void UART0_Init()
{
char SFRPAGE_SAVE = SFRPAGE;
SFRPAGE = CONFIG_PAGE;
SCON0 = 0x50; // SCON1: mode 0, 8-bit UART, enable RX
SSTA0=0X10; //禁止波特率二分频
SFRPAGE = TIMER01_PAGE;
TMOD &= ~0xF0;
TMOD |= 0x20; // TMOD: timer 1, mode 2, 8-bit reload
if (SYSTEMCLOCK/BAUDRATE/16/256 < 1) {
TH1 = -(SYSTEMCLOCK/BAUDRATE/16);
CKCON |= 0x10; // T1M = 1; SCA1:0 = xx
} else if (SYSTEMCLOCK/BAUDRATE/16/256 < 4) {
TH1 = -(SYSTEMCLOCK/BAUDRATE/16/4);
CKCON &= ~0x13; // Clear all T1 related bits
CKCON |= 0x01; // T1M = 0; SCA1:0 = 01
} else if (SYSTEMCLOCK/BAUDRATE/16/256 < 12) {
TH1 = -(SYSTEMCLOCK/BAUDRATE/16/12);
CKCON &= ~0x13; // T1M = 0; SCA1:0 = 00
} else {
TH1 = -(SYSTEMCLOCK/BAUDRATE/16/48);
CKCON &= ~0x13; // Clear all T1 related bits
CKCON |= 0x02; // T1M = 0; SCA1:0 = 10
}
TL1 = TH1; // init Timer1
TR1 = 1; // START Timer1
ES0=1;
}
void UART0_Interrupt(void)
{
if (RI0 == 1)
{
RI0 = 0; //清接收标志位
rbyte = SBUF0; // Read a character from UART
flag_rece=1;
}
if (TI0 == 1) // Check if transmit flag is set
{
TI0=0;
flag_send=1;
}
}
void Delay_ms(uint time)
{
uint i, j;
for(i=1;i<=time;i++)
for(j=1;j<=300;j++);
}
void UART_SendS(uchar code *p)
{
while(*p != 0xFF)
{
UART_SendB(*p);
p++;
}
}
void UART_SendB(uchar rbyte)
{
TI0 = 0;
SBUF0 = 0x12;
while(!TI0);
TI0 = 0 ;
}
在debug的时候运行到UART_SendB()的时候就运行不下去了。 |