一,第一场
#include "reg52.h"
#include "smgshow.h"
#include "PCF8591.h"
#include "AT24C02.h"
#include "Timer.h"
sbit R1=P3^2;
sbit R2=P3^3;
sbit C1=P3^5;
sbit C2=P3^4;
int Va,Vp,dig;
bit Sta_Va;
int Old_Va;
int New_Va;
void LED3();
void Read_Vrb2()
{
unsigned char dat;
float v;
dat=PCF8591_RB2();
v=5*dat/256.0*100;
Va=v;
}
unsigned char sta=1;
void SMGWork()
{
switch(sta)
{
case 1://数据显示界面
Display_SMG(5,SMGdotduanma[Va/100]);
Display_SMG(6,SMGnotdotduanma[Va/10%10]);
Display_SMG(7,SMGnotdotduanma[Va%10]);
Display_SMG(0,0xc1);
break;
case 2://参数设置界面
Display_SMG(5,SMGdotduanma[Vp/100]);
Display_SMG(6,SMGnotdotduanma[Vp/10%10]);
Display_SMG(7,SMGnotdotduanma[Vp%10]);
Display_SMG(0,0x8c);
break;
case 3://计数显示界面
Display_SMG(6,SMGnotdotduanma[dig/10%10]);
Display_SMG(7,SMGnotdotduanma[dig%10]);
Display_SMG(0,0Xc8);
break;
}
}
unsigned char defau_Key=0;
void Scan_KBD()
{
//扫描按键12
R2=0;
R1=C1=C2=1;
if(C1==0)
{
Delay(500);
if(C1==0)
{
defau_Key=0;//错误按键计数清零
if(sta==1)
{
sta=2;
}
else if(sta==2)
{
sta=3;
Write_24C02(0,Vp/10);//将电压参数存储进24C02
}
else
{
sta=1;
}
while(C1==0)
{
SMGWork();
}
}
}
//扫描S16
R2=0;
R1=C1=C2=1;
if(C2==0)
{
Delay(500);
if(C2==0)
{
if(sta!=2)
{
defau_Key=defau_Key+1;
}
if(sta==2)//加0.5只能在电压参数界面生效
{
Vp=Vp+50;
defau_Key=0;
if(Vp>500)//电压参数界面大于5.00
{
Vp=0;
}
}
}
while(C2==0)
{
SMGWork();
}
}
//扫描S17
R1=0;
R2=C1=C2=1;
if(C2==0)
{
Delay(500);
if(C2==0)
{
if(sta!=2)
{
defau_Key=defau_Key+1;
}
if(sta==2)
{
Vp=Vp-50;//减0.5只能在电压参数界面生效
defau_Key=0;
if(Vp<0)
{
Vp=500;
}
}
}
while(C2==0)
{
SMGWork();
}
}
//扫描S13
R1=0;
R2=C1=C2=1;
if(C1==0)
{
Delay(500);
if(C1==0)
{
if(sta!=3)
{
defau_Key=defau_Key+1;//计数清0只能在计数参数界面生效
}
if(sta==3)
{
dig=0;//计数清0
defau_Key=0;
}
}
while(C1==0)
{
SMGWork();
}
}
LED3();//按键错误超过3次,LED3点亮
}
unsigned char State_LED=0xff;
unsigned char count=0;
void Service_T0() interrupt 1
{
TH1=(0-50000)/256;//定时50ms
TL1=(0-50000)%256;
count=count+1;
if((count==100) && (Vp>Va))//只点亮LED1
{
State_LED=(State_LED & ~(0x01));
HC573(4,State_LED);
count=0;
}
if(Vp<=Va)//只熄灭LED1
{
State_LED=State_LED | 0x01;
HC573(4,State_LED);
}
if(Sta_Va==0)
{
Old_Va=Va;
Sta_Va=1;
}
else
{
if(Old_Va>Vp && Vp>Va)
{
dig =dig+1;
if(dig>100)
{
dig=0;
}
if(dig % 2==0)//奇数熄灭
{
State_LED |= 0x02;
HC573(4,State_LED);
}
else
{
State_LED &= ~0x02;
HC573(4,State_LED);
}
}
Old_Va=Va;
}
}
void LED3()
{
if(defau_Key>=3)//点亮LED3
{
State_LED &= ~0x04;
HC573(4,State_LED);
}
else//熄灭LED3
{
State_LED |= 0x04;
HC573(4,State_LED);
}
}
void Ini_system()
{
HC573(4,0xff);
HC573(5,0x00);
Display_ALL(0xff);
Vp=Read_24C02(0)*10;
}
void main()
{
Ini_system();
Ini_Timer0();
while(1)
{
Read_Vrb2();
SMGWork();
Scan_KBD();
LED3();
}
}
#include "reg52.h"
void Delay(int t)
{
while(t--);
}
void HC573(unsigned char channel, unsigned char dat)
{
P0=dat;
switch(channel)
{
case 4:
P2=(P2 & 0x1f) | 0x80;//选通Y4C,LED
break;
case 5:
P2=(P2 & 0x1f) | 0xa0;//选通Y5C,蜂鸣器,继电器
break;
case 6:
P2=(P2 & 0x1f) | 0xc0;//选通Y6C,数码管位选
break;
case 7:
P2=(P2 & 0x1f) | 0xe0;//选通Y7C,数码管段码
break;
}
P2=(P2 & 0x1f) | 0x00;
}
void Display_SMG(unsigned char pos,unsigned char val)
{
HC573(6,0x01<<pos);
HC573(7,val);
Delay(500);
// HC573(6,0x01<<pos);
HC573(7,0xff);//消隐
}
void Display_ALL(unsigned char dat)
{
HC573(6,0xff);
HC573(7,dat);
}
#include "iic.h"
#include "reg52.h"
unsigned char PCF8591_RB2()
{
unsigned char dat;
IIC_Start();
IIC_SendByte(0x90);
IIC_WaitAck();
IIC_SendByte(0x03);
IIC_WaitAck();
//需要延时
IIC_Start();
IIC_SendByte(0x91);
IIC_WaitAck();
dat=IIC_RecByte();
IIC_SendAck(1);
IIC_Stop();
return dat;
}
/*
程序说明: IIC总线驱动程序
软件环境: Keil uVision 4.10
硬件环境: CT107单片机综合实训平台 8051,12MHz
日 期: 2011-8-9
*/
#include "reg52.h"
#include "intrins.h"
#define DELAY_TIME 5
#define SlaveAddrW 0xA0
#define SlaveAddrR 0xA1
//总线引脚定义
sbit SDA = P2^1; /* 数据线 */
sbit SCL = P2^0; /* 时钟线 */
void IIC_Delay(unsigned char i)
{
do{_nop_();}
while(i--);
}
//总线启动条件
void IIC_Start(void)
{
SDA = 1;
SCL = 1;
IIC_Delay(DELAY_TIME);
SDA = 0;
IIC_Delay(DELAY_TIME);
SCL = 0;
}
//总线停止条件
void IIC_Stop(void)
{
SDA = 0;
SCL = 1;
IIC_Delay(DELAY_TIME);
SDA = 1;
IIC_Delay(DELAY_TIME);
}
//发送应答
void IIC_SendAck(bit ackbit)
{
SCL = 0;
SDA = ackbit; // 0:应答,1:非应答
IIC_Delay(DELAY_TIME);
SCL = 1;
IIC_Delay(DELAY_TIME);
SCL = 0;
SDA = 1;
IIC_Delay(DELAY_TIME);
}
//等待应答
bit IIC_WaitAck(void)
{
bit ackbit;
SCL = 1;
IIC_Delay(DELAY_TIME);
ackbit = SDA;
SCL = 0;
IIC_Delay(DELAY_TIME);
return ackbit;
}
//通过I2C总线发送数据
void IIC_SendByte(unsigned char byt)
{
unsigned char i;
for(i=0; i<8; i++)
{
SCL = 0;
IIC_Delay(DELAY_TIME);
if(byt & 0x80) SDA = 1;
else SDA = 0;
IIC_Delay(DELAY_TIME);
SCL = 1;
byt <<= 1;
IIC_Delay(DELAY_TIME);
}
SCL = 0;
}
//从I2C总线上接收数据
unsigned char IIC_RecByte(void)
{
unsigned char i, da;
for(i=0; i<8; i++)
{
SCL = 1;
IIC_Delay(DELAY_TIME);
da <<= 1;
if(SDA) da |= 1;
SCL = 0;
IIC_Delay(DELAY_TIME);
}
return da;
}
#include "reg52.h"
#include "iic.h"
void Write_24C02(unsigned char addr,unsigned char dat)
{
IIC_Start();
IIC_SendByte(0xa0);
IIC_WaitAck();
IIC_SendByte(addr);
IIC_WaitAck();
IIC_SendByte(dat);
IIC_SendAck(1);
IIC_Stop();
}
unsigned char Read_24C02(unsigned char addr)
{
unsigned char temp;
IIC_Start();
IIC_SendByte(0xa0);
IIC_WaitAck();
IIC_SendByte(addr);
IIC_WaitAck();
IIC_Start();
IIC_SendByte(0xa1);
IIC_WaitAck();
temp=IIC_RecByte();
IIC_SendAck(1);
IIC_Stop();
return temp;
}
#include "reg52.h"
void Ini_Timer0()//定时器0初始化函数
{
TR0=1;
TMOD=0x01;
TH1=(0-50000)/256;//定时50ms
TL1=(0-50000)%256;
ET0=1;
EA=1;
}
二,第二场
#include "reg52.h"
#include "smgshow.h"
#include "ds18b20.h"
#include "pcf8591.h"
sbit S4=P3^3;
sbit S5=P3^2;
sbit S6=P3^1;
sbit S7=P3^0;
unsigned char sta=1;
int T=0;
int Tmax=21;
int Tmin=20;
int Tmin2;
int Tmax2;
void Read_Temp()
{
Conv_18B20();
Delay(1000);//等待转化时间
T=Read_18B20();
}
void SMGWork()
{
switch(sta)
{
case 1:
Display_SMG(0,0xc6);
Display_SMG(6,SMGnotdotduanma[T/10%10]);
Display_SMG(7,SMGnotdotduanma[T%10]);
break;
case 2:
Display_SMG(0,0x8c);
Display_SMG(3,SMGnotdotduanma[Tmax/10%10]);
Display_SMG(4,SMGnotdotduanma[Tmax%10]);
Display_SMG(6,SMGnotdotduanma[Tmin/10%10]);
Display_SMG(7,SMGnotdotduanma[Tmin%10]);
break;
}
}
bit sta_S5=0;
unsigned char de_key=0;//错误按键次数
void Scan_key()
{
//实现S4功能切换界面
if(S4==0)
{
Delay(500);
{
if(S4==0)
{
de_key=0;
if(sta==1)
{
Tmin2=Tmin;
Tmax2=Tmax;
sta=2;
}
else
{
if(Tmin>Tmax)
{
Tmin=Tmin2;
Tmax=Tmax2;
}
sta=1;
}
while(S4==0)
{
SMGWork();
}
}
}
}
//实现S5的功能
if(S5==0)
{
Delay(500);
if(S5==0)
{
if(sta!=2)
{
de_key=de_key+1;
}
if(sta_S5==0)//sta_S5=0为Tmax,sta_S5=1为Tmin
{
sta_S5=1;
}
else
{
sta_S5=0;
}
while(S5==0)
{
SMGWork();
}
}
}
//实现S6的功能
if(S6==0)
{
Delay(500);
if(S6==0)
{
if(sta!=2)
{
de_key=de_key+1;
}
if(sta==2)//只在参数界面又效
{
if(sta_S5==0)
{
Tmax=Tmax+1;
if(Tmax>100)
{
Tmax=0;
}
}
if(sta_S5==1)
{
Tmin=Tmin+1;
if(Tmin>100)
{
Tmin=0;
}
}
}
while(S6==0)
{
SMGWork();
}
}
}
//实现S7的功能
if(S7==0)
{
Delay(500);
if(S7==0)
{
if(sta!=2)
{
de_key=de_key+1;
}
if(sta==2)
{
if(sta_S5==0)
{
Tmax=Tmax-1;
if(Tmax<0)
{
Tmax=99;
}
}
if(sta_S5==1)
{
Tmin=Tmin-1;
if(Tmin<0)
{
Tmin=99;
}
}
}
while(S7==0)
{
SMGWork();
}
}
}
}
unsigned char sta_led=0xff;
void DAC_LED()
{
if(T>Tmax)
{
DAC_output(204);
sta_led &= ~0x01;
HC573(4,sta_led);
}
else
{
sta_led |=0x01;
HC573(4,sta_led);
}
//
if(Tmin<=T && T<=Tmax)
{
DAC_output(153);
sta_led &= ~0x02;
HC573(4,sta_led);
}
else
{
sta_led |=0x02;
HC573(4,sta_led);
}
if(T<Tmin)
{
DAC_output(102);
sta_led &= ~0x04;
HC573(4,sta_led);
}
else
{
sta_led |= 0x04;
HC573(4,sta_led);
}
}
void Ini_system()
{
HC573(4,0xff);
HC573(5,0x00);
Display_ALL(0xff);
}
void LED4()
{
if(de_key>=1)
{
sta_led &= ~0x08;
HC573(4,sta_led);
}
else
{
sta_led |= 0x08;
HC573(4,sta_led);
}
}
void main()
{
Ini_system();
while(1)
{
DAC_LED();
Read_Temp();
Scan_key();
SMGWork();
LED4();
}
}
#include "reg52.h"
#include "onewire.h"
void Conv_18B20()//开启温度转化
{
init_ds18b20();
Write_DS18B20(0xcc);
Write_DS18B20(0x44);
}
float Read_18B20()
{
unsigned char LSB;
unsigned char MSB;
int t;
float T;
init_ds18b20();
Write_DS18B20(0xcc);
Write_DS18B20(0xbe);
LSB=Read_DS18B20();
MSB=Read_DS18B20();
t=(MSB << 8) | LSB;
T=t/16.0;
return T;
}
/*
程序说明: IIC总线驱动程序
软件环境: Keil uVision 4.10
硬件环境: CT107单片机综合实训平台 8051,12MHz
日 期: 2011-8-9
*/
#include "reg52.h"
#include "intrins.h"
#define DELAY_TIME 5
#define SlaveAddrW 0xA0
#define SlaveAddrR 0xA1
//总线引脚定义
sbit SDA = P2^1; /* 数据线 */
sbit SCL = P2^0; /* 时钟线 */
void IIC_Delay(unsigned char i)
{
do{_nop_();}
while(i--);
}
//总线启动条件
void IIC_Start(void)
{
SDA = 1;
SCL = 1;
IIC_Delay(DELAY_TIME);
SDA = 0;
IIC_Delay(DELAY_TIME);
SCL = 0;
}
//总线停止条件
void IIC_Stop(void)
{
SDA = 0;
SCL = 1;
IIC_Delay(DELAY_TIME);
SDA = 1;
IIC_Delay(DELAY_TIME);
}
//发送应答
void IIC_SendAck(bit ackbit)
{
SCL = 0;
SDA = ackbit; // 0:应答,1:非应答
IIC_Delay(DELAY_TIME);
SCL = 1;
IIC_Delay(DELAY_TIME);
SCL = 0;
SDA = 1;
IIC_Delay(DELAY_TIME);
}
//等待应答
bit IIC_WaitAck(void)
{
bit ackbit;
SCL = 1;
IIC_Delay(DELAY_TIME);
ackbit = SDA;
SCL = 0;
IIC_Delay(DELAY_TIME);
return ackbit;
}
//通过I2C总线发送数据
void IIC_SendByte(unsigned char byt)
{
unsigned char i;
for(i=0; i<8; i++)
{
SCL = 0;
IIC_Delay(DELAY_TIME);
if(byt & 0x80) SDA = 1;
else SDA = 0;
IIC_Delay(DELAY_TIME);
SCL = 1;
byt <<= 1;
IIC_Delay(DELAY_TIME);
}
SCL = 0;
}
//从I2C总线上接收数据
//unsigned char IIC_RecByte(void)
//{
// unsigned char i, da;
// for(i=0; i<8; i++)
// {
// SCL = 1;
// IIC_Delay(DELAY_TIME);
// da <<= 1;
// if(SDA) da |= 1;
// SCL = 0;
// IIC_Delay(DELAY_TIME);
// }
// return da;
//}
#include "iic.h"
#include "reg52.h"
void DAC_output(unsigned char dat)
{
IIC_Start();
IIC_SendByte(0x90);
IIC_WaitAck();
IIC_SendByte(0x40);
IIC_Start();
IIC_SendByte(0x91);
IIC_WaitAck();
IIC_SendByte(dat);
IIC_SendAck(1);
IIC_Stop();
}
/*
程序说明: 单总线驱动程序
软件环境: Keil uVision 4.10
硬件环境: CT107单片机综合实训平台(外部晶振12MHz) STC89C52RC单片机
日 期: 2011-8-9
*/
#include "reg52.h"
sbit DQ = P1^4; //单总线接口
//单总线延时函数
void Delay_OneWire(unsigned int t) //STC89C52RC
{
while(t--);
}
//通过单总线向DS18B20写一个字节
void Write_DS18B20(unsigned char dat)
{
unsigned char i;
for(i=0;i<8;i++)
{
DQ = 0;
DQ = dat&0x01;
Delay_OneWire(50);
DQ = 1;
dat >>= 1;
}
Delay_OneWire(50);
}
//从DS18B20读取一个字节
unsigned char Read_DS18B20(void)
{
unsigned char i;
unsigned char dat;
for(i=0;i<8;i++)
{
DQ = 0;
dat >>= 1;
DQ = 1;
if(DQ)
{
dat |= 0x80;
}
Delay_OneWire(50);
}
return dat;
}
//DS18B20设备初始化
bit init_ds18b20(void)
{
bit initflag = 0;
DQ = 1;
Delay_OneWire(120);
DQ = 0;
Delay_OneWire(800);
DQ = 1;
Delay_OneWire(100);
initflag = DQ;
Delay_OneWire(50);
return initflag;
}
#include "reg52.h"
void Delay(int t)
{
while(t--);
}
void HC573(unsigned char channel, unsigned char dat)
{
P0=dat;
switch(channel)
{
case 4:
P2=(P2 & 0x1f) | 0x80;//选通Y4C,LED
break;
case 5:
P2=(P2 & 0x1f) | 0xa0;//选通Y5C,蜂鸣器,继电器
break;
case 6:
P2=(P2 & 0x1f) | 0xc0;//选通Y6C,数码管位选
break;
case 7:
P2=(P2 & 0x1f) | 0xe0;//选通Y7C,数码管段码
break;
}
P2=(P2 & 0x1f) | 0x00;
}
void Display_SMG(unsigned char pos,unsigned char val)
{
HC573(6,0x01<<pos);
HC573(7,val);
Delay(500);
// HC573(6,0x01<<pos);
HC573(7,0xff);//消隐
}
void Display_ALL(unsigned char dat)
{
HC573(6,0xff);
HC573(7,dat);
}
三,总结
把每个元件模块化,跟着题目一步一步来。