#include"STC15F2K60S2.h"
#include "string.h"
#define uchar unsigned char
#define Machine_Focs 11059200L //晶振頻率
#define BAUD1 19200 //波特率
sbit Key3 = P1^7;
sbit Key1 = P3^2;
sbit Key2 = P3^3;
sbit P_RXD=P1^0; //模擬串口接收
sbit P_TXD=P1^1; //模擬串口發送
#define stringLenth 16
bit Uart1_Sendbusy = 0; //標記符號
unsigned char uartSbuf[17];
unsigned char aciiCount;
unsigned char display;
void GPIO()
{
P3M1 = 0x00; P3M0 = 0x00; //準雙向口
P2M1 = 0x00; P2M0 = 0xff;
P1M1 = 0x00; P1M0 = 0x00; //開漏模式
// P0M1 = 0x00; P0M0 = 0xff; //推完模式
}
void Uart1_Init(void)
{
SCON = 0x50; //8位數據,可變波特率
AUXR |= 0x40; //定時器1時鐘為Fosc,即1T
AUXR &= 0xFE; //串口1選擇定時器1為波特率發生器
TMOD &= 0x0F; //設定定時器1為16位自動重裝方式
TL1 = 0x70; //設定定時初值
TH1 = 0xFF; //設定定時初值
ET1 = 0; //禁止定時器1中斷
TR1 = 1; //啟動定時器1
ES=1; //串口中斷允許位
}
void Timer0_Init() //1ms@11.0592MHZ
{
AUXR |= 0x80; //定時器時鐘1T模式
TMOD &= 0xF0; //設置定時器模式
TL0 = 0xCD; //設置定時初值
TH0 = 0xD4; //設置定時初值
TF0 = 0; //清除TF0標志
TR0 = 1; //定時器0開始計時
}
/******************
STC單片機的串口發送字節的函數
*******************/
void Send_Ascii(unsigned char dat)
{
P_TXD = 0;
while(Uart1_Sendbusy);
Uart1_Sendbusy=1;
SBUF=dat;
}
void main()
{
GPIO();
Timer0_Init();
Uart1_Init();
EA = 1;
// Send_Ascii(0x12);
while(1)
{
Send_Ascii(0x12);
}
}
void Uart_isr()interrupt 4
{
ES=0;//關閉中斷
if (RI)
{
RI=0;
P_RXD = 0;
uartSbuf[aciiCount]=SBUF;
aciiCount++;
if (aciiCount > stringLenth) //根據各個模式截取合適長度的字符串
{
aciiCount=0;
P_RXD = 1;
}
}
if (TI)
{
TI = 0;
Uart1_Sendbusy = 0;
P_TXD = 1;
}
ES=1;
}