#include<c8051f020.h>
#include <STRING.H>
#include <stdlib.h>
#include <stdio.h>
sbit UARTRX=P0^1;
sbit UARTTX=P0^0;
unsigned char MM,flag;
void SYS_INIT()
{
unsigned char i;
WDTCN=0XDE;
WDTCN=0XAD; //禁止看门狗
OSCXCN=0X67; //开启外部晶体,22.184MHZ
OSCICN=0X9C; //时钟丢失允许0x84,选择内部时钟2MHZ
for(i=0;i<200;i++);
while(((OSCXCN&0X80)!=0X80)); //查询外部晶振是否稳定
OSCICN=0X88; //选择外部时钟
EMI0CF=0XF0; //只使用内部RAM,总线非复用,P4-P7
PCON=0X80; //UART设置SMOD=1
EA=1;
}
void PORT_INIT()
{
XBR0=0X05; //UART0允许,SMBUS(IIC)允许
XBR1=0;
XBR2=0XC0; //弱上拉全局禁止,交叉开关允许
P1MDIN=0XFF; //设置P1端口输 入方式为数字
P0MDOUT=0;
P1MDOUT=0;
P2MDOUT=0;
P3MDOUT=0; //0为漏极开路输出,1为弱上拉
P74OUT=0X00; /*P5为键盘,漏极开路
7 6 5 4 3 2 1 0
P7H P7L P6H P6L P5H P5L P4H P4L */
P0=0XFF;P1=0XFF;P2=0XFE;P3=0X9F;//RGB灯熄灭
P4=0XFF;P5=0XFF;P6=0XFF;P7=0XFF;
}
void UART_INIT() //工作方式1,SMOD=1
{
CKCON=0X68; //定时器均使用系统时钟,不分频,0110 1000 T1用于波特率发声器需12分频
TMOD=0X20; //8位重装定时器1,为串口提供波特率
TCON=0X00; //T1、T0,外部中断屏蔽
TL1=0XF4;
TH1=0XF4; //22.1184MHz,波特率默认9600
//9600 TH1=0XF4
//4800 TH1=0XE8
ES0=1; //2400 TH1=0XD0
//IE=0X90; //1200 TH1=0XA0
SCON0=0X50; //接收禁止,允许接收为0X50 0100 0000
SADEN0=0X00; //UART0从地址无关
TR1=1; //打开波特率发生器
}
void UART_Send_Byte(unsigned char mydata)
{
SBUF0=mydata;
while(!
ti0);
TI0=0;
}
/*void UART_Send_Str(unsigned char s[])
{
unsigned char i;
for(i=0;i<strlen(s)-1;i++)
UART_Send_Byte(s[i]);
}*/
void main()
{
SYS_INIT(); //系统初始化
PORT_INIT(); //端口初始化
UART_INIT();
while(1)
{
if(flag==1)
{
UART_Send_Byte(0X12);
}
}
}
void UART0_ISR() interrupt 4 //定时器0
{
if(!RI0) //接收
{
flag=1;
}
else
{
MM=SBUF0;
while(!RI0)
RI0=0;
}
}
一周热门 更多>