#include
#include
#define unit unsigned int
#define uchar unsigned char
/*界说字型字位口*/
#define DIGPORT XBYTE [0x8000]
#define WORDPORT XBYTE [0x4000]
/*界说P1口各管脚*/
sbit K0=P1^0;
sbit K1=P1^1;
sbit K2=P1^2;
sbit BEEP=P1^3;
sbit L0=P1^4;
sbit L1=P1^5;
sbit L2=P1^6;
sbit L3=P1^7;
uchar data BUFFER[2]={0,0}; /*显现缓冲区(依次为低位,高位)*/
uchar data CLOCK[4]={0,0,0,0}; /*寄存时钟时刻(依次为百分秒,秒,分和时位)*/
uchar data SECOND[2]={0,0}; /*寄存秒表时刻(依次为百分秒,秒)*/
uchar data REVERSE[4]={0,0,0,0}; /*寄存倒计时时刻(依次为百分秒,秒,分和时位)*/
/*界说运转状况*/
uchar data STATE=0;
/*STATE=0,秒表*/
/*STATE=1,倒计时分调整*/
/*STATE=2,倒计时小时调整*/
/*STATE=3,倒计时运转*/
/*STATE=4 or 7,时钟运转*/
/*STATE=5,时钟分调整*/
/*STATE=6,时钟时调整*/
bit bdata SND=0; /*秒表START*/
bit bdata RST=0; /*秒表RESET*/
bit bdata STP=0; /*倒计时STOP*/
/*数码管显现编码”0″-”9″,”A”,”-“*/
uchar code TABLE[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x40};
void display(void)
{
uchar i; /*显现缓冲区首址*/
uchar delay; /*显现延时*/
uchar disp; /*显现内容*/
uchar digit; /*界说数码管显现位*/
digit=0x08;
for (i=0;i<2;i++) /*秒表,显现百分秒和秒*/
{
if (BUFFER[i]>100) disp=BUFFER[i]%10+10;
else disp=BUFFER[i]%10;
DIGPORT=digit;
WORDPORT=TABLE[disp];
for (delay=0;delay<=200;delay++);
digit=digit>>1;
disp=BUFFER[i]/10;
DIGPORT=digit;
WORDPORT=TABLE[disp];
for (delay=0;delay<=200;delay++);
digit=digit>>1;
}
}
void timer0(void) interrupt 1 using 1
{
/*重置守时初值*/
TH0=-5000/256;
TL0=-5000%256;
/*时钟处理*/
CLOCK[0]=CLOCK[0]+1;
if ((CLOCK[0]==50) && (STATE>=4))
{
L0=!L0;
}
if (CLOCK[0]==100)
{
if (STATE>=4) L0=!L0;
CLOCK[0]=0;
CLOCK[1]=CLOCK[1]+1;
if (CLOCK[1]==60)
{
CLOCK[1]=0;
CLOCK[2]=CLOCK[2]+1;
if (CLOCK[2]==60)
{
CLOCK[2]=0;
CLOCK[3]=CLOCK[3]+1;
if (CLOCK[3]==24)
{
CLOCK[3]=0;
}
}
}
}
}
void timer2(void) interrupt 5 using 3
{
TF2=0;
TH2=-5000/256;
TL2=-5000%256;
/*秒表处理*/
if (STATE==0)
{
SECOND[0]=SECOND[0]+1;
if (SECOND[0]==50)
{
L1=!L1;
}
if (SECOND[0]==100)
{
L1=!L1;
SECOND[0]=0;
SECOND[1]=SECOND[1]+1;
if (SECOND[1]==100)
{
SECOND[1]=0;
}
}
}
/*倒计时处理*/
if (STATE==3)
{
REVERSE[0]=REVERSE[0]+1;
if (REVERSE[0]==50)
{
L2=!L2;
}
if (REVERSE[0]==100)
{
L2=!L2;
REVERSE[0]=0;
REVERSE[1]=REVERSE[1]+1;
if (REVERSE[1]==60)
{
REVERSE[1]=00;
REVERSE[2]=REVERSE[2]-1;
if ((REVERSE[2]==0) && (REVERSE[3]==0))
{
TR2=0;
STP=1;
BEEP=0;
}
else if (REVERSE[2]==0xff)
{
REVERSE[2]=59;
REVERSE[3]=REVERSE[3]-1;
}
}
}
}
}
void intsvr1(void) interrupt 2 using 2
{
if (STATE==5)
{
CLOCK[2]=CLOCK[2]+1;
if (CLOCK[2]==60) CLOCK[2]=0;
}
if (STATE==6)
{
CLOCK[3]=CLOCK[3]+1;
if (CLOCK[3]==24) CLOCK[3]=0;
}
}
void timer1(void) interrupt 3 using 3
{
if (STATE==0)
{
if (RST)
{
SECOND[0]=0;
SECOND[1]=0;
L1=1;
RST=0;
}
else
{
if (SND) TR2=1;
else
{
L1=1;
TR2=0;
RST=1;
}
SND=!SND;
}
}
else if (STATE==1)
{
REVERSE[2]=REVERSE[2]+1;
if (REVERSE[2]==60) REVERSE[2]=0;
}
else if (STATE==2)
{
REVERSE[3]=REVERSE[3]+1;
if (REVERSE[3]==24) REVERSE[3]=0;
}
else
{
if (STP)
{
BEEP=1;
L2=1;
STP=0;
}
else TR2=0;
}
}
void main(void)
{
uchar stabak; /*上一次程序运转的状况*/
P1=0x0f;
EA=1; IT1=1; ET0=1;
TMOD=0x61; /*T1方法2计数,T0方法1计时*/
TH0=-5000/256; TL0=-5000%256;
TH1=0xff; TL1=0xff;
T2CON=0; TH2=-5000/256; TL2=-5000%256;
K0=1; K1=1; K2=1;
STATE=P1&0x07;
stabak=STATE;
for (;;)
{
switch (STATE)
{
case 0: /*STATE=0,秒表*/
{
EX1=0; ET1=1; ET2=1; TR1=1; SND=1;
L1=1; L0=0; L2=0;
SECOND[0]=0;
SECOND[1]=0;
} break;
case 1: /*STATE=1,倒计时分调整*/
{
EX1=0; ET1=1; ET2=1; TR1=1; TR2=0;
L2=1; L0=0; L1=0;
REVERSE[0]=0;
REVERSE[1]=0;
} break;
case 2: /*STATE=2,倒计时小时调整*/
{
EX1=0; ET1=1; ET2=1; TR1=1; TR2=0;
L2=1; L0=0; L1=0;
REVERSE[0]=0;
REVERSE[1]=0;
} break;
case 3: /*STATE=3,倒计时运转*/
{
EX1=0; ET1=1; ET2=1; TR1=1;
if ((REVERSE[2]==0)&&(REVERSE[3]==0)) L2=1;
else TR2=1;
L0=0; L1=0;
} break;
case 5: /*STATE=5,时钟分调整*/
{
IE1=0; EX1=1; ET1=0; ET2=1; TR0=0;
L0=1; L1=0; L2=0;
} break;
case 6:/*STATE=6,时钟时调整*/
{
IE1=0; EX1=1; ET1=0; ET2=1; TR0=0;
L0=1; L1=0; L2=0;
} break;
default: /*STATE=4 or 7,时钟运转*/
{
EX1=0; ET1=0; ET2=1; TR0=1;
L1=0; L2=0;
}
}
while (STATE==stabak)
{
switch (STATE)
{
case 0: /*STATE=0,秒表*/
{
BUFFER[0]=SECOND[0];
BUFFER[1]=SECOND[1];
} break;
case 1: /*STATE=1,倒计时分调整*/
{
BUFFER[0]=REVERSE[2];
BUFFER[1]=101;/*A-*/
} break;
case 2: /*STATE=2,倒计时小时调整*/
{
BUFFER[0]=110;/*-A*/
BUFFER[1]=REVERSE[3];
} break;
case 3: /*STATE=3,倒计时运转*/
{
BUFFER[0]=REVERSE[2];
BUFFER[1]=REVERSE[3];
} break;
case 5: /*STATE=5,时钟分调整*/
{
BUFFER[0]=CLOCK[2];
BUFFER[1]=101;/*A-*/
} break;
case 6: /*STATE=6,时钟时调整*/
{
BUFFER[0]=110;/*-A*/
BUFFER[1]=CLOCK[3];
} break;
default: /*STATE=4 or 7,时钟运转*/
{
BUFFER[0]=CLOCK[2];
BUFFER[1]=CLOCK[3];
}
}
display();
K0=1; K1=1; K2=1;
STATE=P1&0x07;
}
stabak=STATE;
}
}
DS1820温度丈量程序
#include
#include
#include
#include
#include
#define uchar unsigned char
#define uint unsigned int
#define dsyawh XBYTE[0x0600] // 界说双端口RAM中的存储单元地址;
#define dsyawl XBYTE[0x0100]
#define dspitchh XBYTE[0x0200]
#define dspitchl XBYTE[0x0300]
#define dsrollh XBYTE[0x0400]
#define dsrolll XBYTE[0x0500]
sbit P1_0=P1^0;
sbit P1_2=P1^2;
uchar idata stringyaw[6];
uchar idata stringpitch[6];
uchar idata stringroll[6];
uint k, i,j;
uint yaw0,yaw0flag;
uint record,number;
uint iyaw, ipitch, iroll;
uint numbercoma;
uint len1,len2,len3;
uint numyaw,numroll,numpitch;
//主函数,初始化串口和变量
main()
{
TMOD=0x20;
TL1=0xfd;
TH1=0xfd;
SCON=0x50;
PCON=0x00;
IE=0x90;
TR1=1;
P1_0=0;
k=2;
dsyawh=0;
dsyawl=0;
dspitchh=0;
dspitchl=0;
dsrollh=0;
dsrolll=0;
yaw0flag=0;
i=0;
P1_2=1;
while(1);
}
// 因为选用的是专用的通讯单片机,能够运用中止方法进行串行通讯,进步通讯质量。
serial () interrupt 4 using 1
{
RI=0; //软件铲除中止标志位
//判别是否收到HPR格局句子的榜首个字符”$”,其数值为0x24,假如收到,则开端记载数据,
//并设置记载标志record=1。*/
if (SBUF==0x24)
{
record=1;
i=0;
iyaw=0; //记载航向数据字符数量的变量
ipitch=0; //记载俯仰数据字符数量的变量
iroll=0; //记载横滚数据字符数量的变量
numbercoma=0; // 记载逗号数量的变量
}
if (record==1) // 开端处理HPR中的数据信息
{
//使用HPR数据中的逗号距离,区别接收到的字符处于何种姿势数据中。
if(SBUF==0x2c) {numbercoma++;}
// 榜首个逗号之后的字符归于航向数据,拜见HRP句子
if (numbercoma==1)
{
stringyaw[iyaw]=SBUF; // 将字符放入航向字符串”stringyaw[ ]”中
iyaw++;
}
// 第三个逗号之后的字符归于俯仰数据
if (numbercoma==3)
{
stringpitch[ipitch]=SBUF; // 将字符放入俯仰字符串”stringpitch[ ]”中
ipitch++;
}
// 第五个逗号之后的字符归于滚转数据
if (numbercoma==5)
{
stringroll[iroll]=SBUF; // 将字符放入横滚字符串”stringroll[ ]”中
iroll++;
}
//判别是否收到HPR格局句子的字符”*”,假如收到,则完毕记载,开端处理。
if (SBUF==*)
{
stringyaw[iyaw]=\0; // 给出完毕航向数据字符串的完毕符
stringpitch[ipitch]=\0;
stringroll[iroll]=\0;
// 开端处理航向数据的相关字符,转化为数据格局
j=0;
// 将航向字符用sprintf()函数连接成字符串,并舍掉最终一个逗号
len1=sprintf(stringyaw,”%s”,stringyaw+1);
//用atoi()函数将字符串转化为数据格局
if (len1!=0)
{
numyaw=atoi(stringyaw);
}
// 开端处理俯仰数据的相关字符,转化为数据格局
j=0;
len2=sprintf(stringpitch,”%s”,stringpitch+1);
if (len2!=0)
{
numpitch=atoi(stringpitch);
}
// 开端处理滚转数据的相关字符,转化为数据格局
j=0;
len3=sprintf(stringroll,”%s”,stringroll+1);
if (len3!=0)
{
numroll=atoi(stringroll);
}
// 回来初始值;
numyaw=0;
numroll=0;
numpitch=0;
numbercoma=0;
record=0;
ipitch=0;
iroll=0;
iyaw=0;
}
}
}