#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; //模擬串口發送
/*收發顯示數據相關*/
bit Uart1_Sendbusy = 0 ; //標記是否發送完成
uchar G_count;
unsigned char display; //數碼管顯示數據
uchar flag;
bit flg_1ms; //1ms的標志
uchar duanxuan[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71}; //顯示0-f
/**********************
函數名稱:Timer0
功能描述:定時器0的中斷響應函數 ,定時100us
***********************/
void Timer0() interrupt 1
{
TH0=(65535-1000)/256; //定時器初始值
TL0=(65535-1000)%256;
G_count++;
if(G_count==10) //中斷10次對應1ms
{
G_count=0;
flg_1ms=1;
}
flag++;
if(flag==2)
flag=0;
switch(flag)
{
case 0: P2=0x00;P0=duanxuan[display/16];break;
case 1: P2=0x01;P0=duanxuan[display%16];break;
}
}
void GPIO()
{
P3M1 = 0x00; P3M0 = 0x00; //準雙向口
P2M1 = 0x00; P2M0 = 0xff;
P1M1 = 0x00; P1M0 = 0x00; //開漏模式
// P0M1 = 0x00; P0M0 = 0xff; //推完模式
}
void Uart2_Init(void) //19200bps@11.0592MHz
{
S2CON = 0x50; //8位數據,可變波特率
AUXR |= 0x04; //定時器2時鐘為Fosc,即1T
T2L = 0x70; //設定定時初值
T2H = 0xFF; //設定定時初值
IE2 &= 0xFB; //禁止定時器2中斷
// IE2 |= 0x01; //串口2中斷允許位
AUXR |= 0x10; //啟動定時器2
}
void Timer5_Init(void) //1毫秒@11.0592MHz
{
AUXR |= 0x04; //定時器時鐘1T模式
T2L = 0xCD; //設置定時初值
T2H = 0xD4; //設置定時初值
AUXR |= 0x10; //定時器2開始計時
}
void Send_Ascii(unsigned char dat)
{
// P_TXD = 0;
while(Uart1_Sendbusy);
Uart1_Sendbusy=1;
S2BUF=dat;
}
void main()
{
GPIO();
Timer5_Init();
Uart2_Init();
EA = 1;
// Send_Ascii(0x12);
while(1)
{
Send_Ascii(0x12);
}
}
void Uart2_isr() interrupt 8
{
if (S2CON & 0x02) //發送中斷
{
S2CON &= ~0x02;
Uart1_Sendbusy = 0;
// P_TXD = 1;
}
if(S2CON & 0x01)
{
S2CON &= ~0x01;
display = S2BUF;
}
}