主函数:
//第七届模拟风扇
#include
#include
#include
sbit s7=P3^0;
sbit s6=P3^1;
sbit s5=P3^2;
sbit s4=P3^3;
unsigned char flag_yemian,flag_mode;
unsigned char tt,ttt,work_t,num;
unsigned char wendu;
unsigned char shijian[]={
0,0};
unsigned char seg[]={
0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0xbf,0xff,0xc6};
unsigned char yi,er,san,si,wu,liu,qi,ba;
void delayms(int ms);
void allinit();
void display_12(yi,er);
void display_34(san,si);
void display_56(wu,liu);
void display_78(qi,ba);
void Timer0Init(void);
void Timer1Init(void);
void key_scan();
unsigned char ds18b20_get();
void main(){
allinit();
Timer1Init();
Timer0Init();
EA=1;ET0=1;ET1=1;
yi=10;er=10;san=10;si=10;wu=10;liu=10;qi=10;ba=10;
while(1){
key_scan();
wendu=ds18b20_get();
//界面选择
//工作模式页面
if(flag_yemian==0){
//模式
if(shijian[0]==0&&shijian[1]==0){
P2=0X80;P0=0XFF;P2=P2&0x1f;
}
else{
if(flag_mode+1==1){
P2=0X80;P0=0XFE;P2=P2&0X1F;
}
else if(flag_mode+1==2){
P2=0X80;P0=0XFD;P2=P2&0X1F;
}
else if(flag_mode+1==3){
P2=0X80;P0=0XFB;P2=P2&0X1F;
}
}
yi=10;er=flag_mode+1;san=10;si=11;wu=shijian[1]/10;liu=shijian[1]%10;qi=shijian[0]/10;ba=shijian[0]%10;
}
//温度显示页面
else if(flag_yemian==1){
yi=10;er=4;san=10;si=11;wu=11;liu=wendu/10;qi=wendu%10;ba=12;
}
display_12(yi,er);
display_34(san,si);
display_56(wu,liu);
display_78(qi,ba);
}
}
定时器0、1
//定时器0控制计时
void time0() interrupt 1{
tt++;
if(tt==200){
//都为0不工作
if(shijian[0]==0&&shijian[1]==0){
shijian[0]=0;
shijian[1]=0;
P2=0X80;P0=0XFF;P2=P2&0x1f;
P34=0;
}
//否则倒计时
else{
tt=0;
num++;
shijian[0]--;
if(num==60){
num=0;
if(shijian[1]>0){
shijian[1]--;
shijian[0]=59;
}
else if (shijian[1]==0)
shijian[0]=59;
}
}
}
}
//定时器1,用于模式下PWM输出
void time1() interrupt 3{
ttt++;
if(flag_mode+1==1){
if(ttt<3)
P34=1;
else
P34=0;
}
else if(flag_mode+1==2){
if(ttt<4)
P34=1;
else
P34=0;
}
else if(flag_mode+1==3){
if(ttt<7)
P34=1;
else
P34=0;
}
if(ttt==10)
ttt=0;
}
按键及温度获取
//按键
void key_scan(){
//s7控制页面切换
if(s7==0){
delayms(5);
if(s7==0){
if(flag_yemian==0)
flag_yemian=1;
else
flag_yemian=0;
}
while(!s7);
}
//s6控制定时关闭
if(s6==0){
delayms(5);
if(s6==0){
shijian[0]=0;
shijian[1]=0;
P34=0;
}
while(!s6);
}
//s5控制定时加减
if(s5==0){
delayms(5);
if(s5==0){
shijian[1]++;
if(shijian[1]==2)
shijian[1]=0;
}
while(!s5);
if(shijian[0]>0)
shijian[1]=shijian[1]+1;
tt=0;num=0;shijian[1]--;shijian[0]=59;
}
//s4控制模式
if(s4==0){
delayms(5);
if(s4==0){
flag_mode++;
if(flag_mode==3)
flag_mode=0;
}
while(!s4);
}
}
//温度获取
unsigned char ds18b20_get(){
unsigned char temp,high,low;
init_ds18b20();
Write_DS18B20(0xcc);
Write_DS18B20(0x44);
delayms(1);
init_ds18b20();
Write_DS18B20(0xcc);
Write_DS18B20(0xbe);
low=Read_DS18B20();
high=Read_DS18B20();
init_ds18b20();
temp=high<<4;
temp=temp|(low>>4);
return temp;
}
初始化设置
//定时器0初始化,定时5ms
void Timer0Init(void){
AUXR |= 0x80;
TMOD &= 0xF0;
TL0 = 0x00;
TH0 = 0x28;
TF0 = 0;
TR0 = 1;
}
//定时器1初始化,定时100us
void Timer1Init(void)
{
AUXR |= 0x40;
TMOD &= 0x0F;
TL1 = 0xAE;
TH1 = 0xFB;
TF1 = 0;
TR1 = 1;
}
//延时
void delayms(int ms){
int q,w;
for(q=0;q<ms;q++)
for(w=0;w<845;w++);
}
//初始化
void allinit(){
P2=0XA0;P0=0X00;
P2=0XC0;P0=0XFF;P2=0XE0;P0=0XFF;
P2=0X80;P0=0XFF;
}
//数码管
void display_12(yi,er){
P2=0XC0;P0=0X01;P2=0XE0;P0=seg[yi];
delayms(1);
P2=0XC0;P0=0X02;P2=0XE0;P0=seg[er];
delayms(1);
}
void display_34(san,si){
P2=0XC0;P0=0X04;P2=0XE0;P0=seg[san];
delayms(1);
P2=0XC0;P0=0X08;P2=0XE0;P0=seg[si];
delayms(1);
}
void display_56(wu,liu){
P2=0XC0;P0=0X10;P2=0XE0;P0=seg[wu];
delayms(1);
P2=0XC0;P0=0X20;P2=0XE0;P0=seg[liu];
delayms(1);
}
void display_78(qi,ba){
P2=0XC0;P0=0X40;P2=0XE0;P0=seg[qi];
delayms(1);
P2=0XC0;P0=0X80;P2=0XE0;P0=seg[ba];
delayms(1);
P2=0XC0;P0=0XFF;P2=0XE0;P0=0XFF;
delayms(1);
}