#include<c8051f120.h>
#define systemclock 11059200
#define baudrate 9600
sbit P0_6=P0^6;
sbit P2_3=P2^3;
void OSCILLATOR_init()
{
int i;
char SFRPAGE_SAVE=SFRPAGE;
SFRPAGE=CONFIG_PAGE;
OSCICN=0x83;
OSCXCN=0x67;
for (i = 0; i < 256; i++);
while (!(OSCXCN & 0x80));
SFRPAGE=LEGACY_PAGE;
FLSCL|=0x30;
if(systemclock<=25000000)
{
FLSCL&=~0x30;
}
else if(systemclock<=50000000)
{
FLSCL&=~0x20;
}
else if(systemclock<=75000000)
{
FLSCL&=~0x10;
}
else
{
FLSCL&=~0x00;
}
SFRPAGE = CONFIG_PAGE;
CLKSEL=0x01;
SFRPAGE = SFRPAGE_SAVE;
}
void port_init()
{
char SFRPAGE_SAVE=SFRPAGE;
SFRPAGE=CONFIG_PAGE;
XBR0=0x04;
XBR1=0x00;
XBR2=0x44;
P0MDOUT=0xf5;
P1MDOUT=0xff;
P2MDOUT=0xff;
P3MDOUT=0xff;
SFRPAGE = SFRPAGE_SAVE;
}
void uart1_init()
{
char SFRPAGE_SAVE=SFRPAGE;
SFRPAGE=
tiMER01_PAGE;
TCON=0x40;
TMOD=0x20;
CKCON=0x10;
TR1=1;
SFRPAGE=SFRPAGE_SAVE;
SFRPAGE=UART1_PAGE;
SCON1=0x00;
TI1=0;
TH1=256-(systemclock/(2*baudrate));
TL1=TH1;
SFRPAGE = SFRPAGE_SAVE;
}
void senddata(unsigned char i)
{
//P0_6=0;
SBUF1=i;
while(TI1==0);
TI1=0;
}
void main()
{
char SFRPAGE_SAVE = SFRPAGE;
SFRPAGE = CONFIG_PAGE;
WDTCN=0xde;
WDTCN=0xad;
OSCILLATOR_init();
port_init();
uart1_init();
EA=1;
SFRPAGE = SFRPAGE_SAVE;
while(1)
{
int t,x;
SFRPAGE = UART1_PAGE;
senddata(0x55);
for(t=0;t<300;t++)
for(x=0;x<400;x++);
SFRPAGE = SFRPAGE_SAVE;
}
}
#include"c8051f020.h"
#define BaudRate_1200 1 //1*1200
#define BaudRate_2400 2 //2*1200
#define BaudRate_4800 4 //4*1200
#define BaudRate_9600 8 //8*1200
#define BaudRate_19200 16 //16*1200
#define BaudRate_38400 32 //32*1200
#define BaudRate_57600 48 //48*1200
#define BaudRate_115200 96 //96*1200
#define SYSCLK 22118400
bit flag;
unsigned char data_recieved;
void SYSCLK_Init (void);
void PORT_Init (void);
void Uart0_Init(unsigned char BaudRate); //Uart0初始化
void Uart0_SendByte(unsigned char value);
void Uart0_SendString(unsigned char *pt);
void main()
{
WDTCN=0xDE; //禁止看门狗定时器
WDTCN=0xAD;
SYSCLK_Init ();
PORT_Init ();
Uart0_Init(BaudRate_115200);
flag=0;
Uart0_SendString("uart!");
while(1)
{
if(flag)
{
flag=0;
Uart0_SendByte(data_recieved);
}
if(RI0) //查询法接收
{
RI0=0;
data_recieved=SBUF0;
flag=1;
}
}
}
void SYSCLK_Init (void)
{
int i; // 延时计数器
OSCXCN = 0x67; // 使用外部22.1184MHz晶振
for(i=0; i<256; i++); // 等待1ms
while(!(OSCXCN & 0x80)); // 等待外部振荡器稳定
OSCICN = 0x88; // 切换到外部晶振
}
void PORT_Init (void)
{
XBR0 = 0x04; //设置XBR0
XBR1 = 0x00; //设置XBR1
XBR2 = 0x40; //打开交叉开关和弱上拉
// XBR0 = 0x8f; //设置XBR0
// XBR1 = 0x1e; //设置XBR1
// XBR2 = 0x44; //打开交叉开关和弱上拉
// P0MDOUT = 0x34; //设置P0口输出
P0MDOUT = 0xfe; //设置P0口输出
}
void Uart0_Init(unsigned char BaudRate) //Uart0初始化
{
CKCON=0x10; // 定时器Timer1使用系统时钟作为时间基准
TMOD=0x20; // TMOD: 定时器1, 方式2, 8位重载
SCON0=0X50;
PCON |= 0x80; // SMOD00 = 1
T2CON=0x00; //选择定时器1溢出作为波特率时钟源
TH1 = -(SYSCLK/1200/(BaudRate*16)); // 设置定时器Timer1重载值
TI0 = 0; // Indicate TX0 ready
RI0 = 0; // Indicate RX0 ready
ES0=1;
EA=0;
ET1=0;
TR1=1;
}
void Uart0_SendByte(unsigned char value)
{
SBUF0=value; // 发送数据到uart0
while(!TI0); // 等待发送1个字节数据完成
TI0=0; // 清除中断标志
}
void Uart0_SendString(unsigned char *pt)
{
while(*pt)
{
Uart0_SendByte(*pt++);
}
}
void ISR_Uart0(void) interrupt 4
{
if(RI0)
{
data_recieved=SBUF0;
RI0=0;
flag=1;
}
// TI0=0;
}
一周热门 更多>