/*
程序功能:电风扇智控系统+Nokia5110显示+红外遥控
Members:何志明,韩爽
Datetime:Saturday,December 27,2014
*/
#include<reg52.h>
#include"Nokia.h"
#define uint unsigned int
#define uchar unsigned char
#define T 100 //脉冲周期,100ms
#define M (T/2) //半个周期
#define Min_speed 48 //最小速度
#define Max_speed 100 //最大速度
#define pwm_div 10 //pwm间隔
sbit pwm=P1^0; //电机控制端
sbit k0=P3^4; //功能键
sbit k1=P3^5; //档位
sbit k2=P3^6; //进入定时功能设置模式
sbit k3=P3^7; //确定并启动定时
sbit P3_2=P3^2; //INT0
sbit P3_3=P3^3; //INT1
sbit speaker=P1^1; //蜂鸣器
uchar num,num0,num1;
uchar k1_num,k2_num;
uchar speed,rate,gear; //全局速度
uchar minute,second,second1,min,sec,via;
uint ms,ms1;
bit flag; //定时开启标志
uchar watch=60,a,b=6,c; //定时变量,默认一小时,最长三小时(可任意设置)
/*--------------ms级延时函数------------------*/
void delay_1ms(uchar d) //误差 0us
{
uchar a,b,c;
for(c=d;c>0;c--)
for(b=142;b>0;b--)
for(a=2;a>0;a--);
}
/*----------------pwm电机调速-------------------*/
void Motor_pwm(uchar speed)
{
if(num0<=speed)
{
pwm=1;
}
else if(num0<=T)
{
pwm=0;
}
else{num0=0;}
}
/*-----------------外部中断初始化--------------*/ void Int01_Init()
{
pwm=0; //关电机
EA=1;
EX0=1;
//EX1=1;
IT0=0;
//IT1=0;
}
/*-----------------定时器初始化----------------*/ void Timer_Init()
{
TMOD=0x01;
TH0=0xfe;
TL0=0x33; //11.0592,定时1ms EA=1;
ET0=1;
TR0=1;
}
void speaker_out();
void Timer_start();
void Set_Timer();
void key_scan();
void Motor_display();
/*----------------------主函数---------------------*/ void main()
{ pwm=0;
LCD_Init(); //液晶屏初始化 Turn_on_language(); //开机问候语 //Power_off_language(); //关机提示语 Dis_Init(); //固定字符显示
Int01_Init();
Timer_Init();
while(1)
{
P3_2=k0; //电平转移 INT0 //P3_3=k2; //电平转移 INT1 Motor_display(); //Nokia液晶显示 }
}
/*------------------外部中断0---------------------*/ void Int0_() interrupt 0
{
speaker=0; //提示音
if(k0==0) //模式中断
{
delay(5);
if(k0==0)
{
while(!k0);
num++;
}
}
if(num<6)
{ TR0=1;}
if(num==2)
{
//k1_num=0;
}
if(num==3)
{
ms1=0;
second1=0; //计数单元清零
speed=58;
}
if(num==4)
{
num=0;
pwm=0; //关电机
speed=0;
TR0=0; //关T0
}
speaker=1; //关闭蜂鸣器
}
/*------------------定时器0产生pwm-----------------*/
void Timer0() interrupt 1
{
TR0=0;
TH0=0xfe;
TL0=0x33;
num0++;
key_scan(); //按键扫描 TR0=1;
}
/*---------------------蜂鸣器滴滴声-----------------------*/ void speaker_out()
{
speaker=~speaker;
delay_1ms(1);
speaker=0;
delay_1ms(200);
speaker=1;
delay_1ms(100);
}
/*---------------------Motor_display-----------------------*/ void Motor_display()
{
rate=speed;
if(rate==100)
{
rate=99;
}
Display5110(62,2,Digit,6,2,rate/10);
Display5110(68,2,Digit,6,2,rate%10); //speed Display5110(30,2,Digit,6,2,gear); //挡位 if(num==1)
{ gear=k1_num;
Display5110(36,0,Chinese,12,2,6);
Display5110(48,0,Chinese,12,2,7); //正常 }
if(num==2)
{ gear=4;
Display5110(36,0,Chinese,12,2,8);
Display5110(48,0,Chinese,12,2,9); //自然 }
if(num==3)
{ gear=5;
Display5110(36,0,Chinese,12,2,10);
Display5110(48,0,Chinese,12,2,11); //睡眠
}
if(k2_num>0&&k2_num<4)
{
Display5110(46,4,Digit,6,2,a);
Display5110(52,4,Digit,6,2,b);
Display5110(58,4,Digit,6,2,c);
}
if(k2_num==4)
{ P0=0x0f;
Display5110(34,4,Mark1,6,2,7); //on
Display5110(40,4,Mark1,6,2,8);
a=min/100;
b=min%100/10;
c=min%10;
Display5110(46,4,Digit,6,2,a);
Display5110(52,4,Digit,6,2,b);
Display5110(58,4,Digit,6,2,c);
Display5110(70,4,Digit,6,2,sec/10);
Display5110(76,4,Digit,6,2,sec%10);
if(sec==1)
{
delay(500); //!此处因多次中断返回而有延时,需消抖 min--;
}
sec=60-second; //倒计时
}
}
/*-----------------------按键扫描换挡----------------------*/
void key_scan()
{
Set_timer(); //K2被按下,则进入定时时间设置 switch(num)
{
case 1: /*................正常风````````^^^1^^^^^*/
if(k1==0)
{
delay(5);
if(k1==0)
{
while(!k1);
k1_num++;
if(k1_num==4)
{k1_num=0;}
}
}
switch(k1_num)
{
case 1:speed=52; //低速档
Motor_pwm(speed);P0=0xfe;//工作状态及程序流指示灯(下同) break;
case 2:speed=70; //中速档
Motor_pwm(speed); P0=0xfd;
break;
case 3:speed=100; //高速档
Motor_pwm(speed); P0=0xfb;
break;
default:speed=0; P0=0x00; //关闭电机
Motor_pwm(speed);
break;
}
break;
case 2://Motor_pwm(100); /*................自然风````````^^^2^^^^^*/ ms1++;
if(ms1==1000)
{
ms1=0;
second1++;
}
if(second1<6) //转5秒
{
speed=70;
Motor_pwm(speed);P0=0xef;
}
else if(second1<16) //停10秒
{
speed=0; P0=0xdf;
Motor_pwm(speed);
}
else{second1=0;}
break;
case 3://Motor_pwm(56); /*................睡眠风````````^^^3^^^^^*/
ms1++;
if(ms1==1000)
{ P0=0xbf;
ms1=0;
speed-=4;
second1++;
//delay(500);
}
if(second1<=10)
{
Motor_pwm(speed); P0=0x7f;
}
else
{
second1=0;
speed=58;
}
break;
// case 4:Set_timer(); //K2被按下,则进入定时时间设置 // break;
// case 5:flag=1; /*-------------确定并启动定时-----------*/ // speaker=0;
// Timer_start();
// speaker=1;
// break;
}
}
/*-------------------------------设置定时时间-------------------------------*/ void Set_Timer()
{
if(k2==0)
{
delay(5);
if(k2==0)
{
while(!k2);
k2_num++;
if(k2_num==5) //
{
k2_num=0;
}
}
}
switch(k2_num)
{
case 1: //百位a设定 0-1 (k3) if(k3==0)
{
delay(5); if(k3==0) {
while(!k3); a++; if(a>=2) {
a=0; }
}
}
break;
case 2: if(k3==0)
{
delay(5); if(k3==0) {
while(!k3); b++; if(b>=9) {
b=0; }
}
}
break;
case 3: if(k3==0)
{
delay(5); if(k3==0) {
while(!k3); c++; if(c>=10) {
c=0; }
}
}
watch=a*100+b*10+c; min=watch-1; //十位b设定 0-8 //个位c设定 0-9 //设定好定时关机时间(分)
break;
case 4:flag=1; /*-------------确定并启动定时-----------*/ Timer_start();
break;
}
}
/*-------------------------------启动定时并倒计时------------------------*/ void Timer_start()
{
ms++;
if(ms==1000)
{
ms=0;
second++;
if(second==60)
{
second=0;
minute++;
bbs.99jianzhu.com内容:建筑图纸、PDF/word 流程,表格,案例,最新,施工方案、工程书籍、建筑论文、合同表格、标准规范、CAD图纸等内容。