//第十届电压频率采集N555
#include
#include
#include
sbit s7=P3^0;
sbit s6=P3^1;
sbit s5=P3^2;
sbit s4=P3^3;
sbit led1=P0^0;
sbit led2=P0^1;
sbit led3=P0^2;
sbit led4=P0^3;
sbit led5=P0^4;
sbit pinlv_flag=P3^4;
unsigned char t,pinlv,pinlv_temp;
unsigned char flag_yemian,flag_dac,flag_seg,flag_led;
int dianya,dac_out=200;
unsigned char yi,er,san,si,wu,liu,qi,ba;
unsigned char seg[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0x40,0x79,0x24,0x30,0x19,0x12,0x02,0x78,0x00,0x10,0xbf,0xff,0x8e,0xc1};
void allinit();
void display_12(yi,er);
void display_34(san,si);
void display_56(wu,liu);
void display_78(qi,ba);
void delayms(int ms);
void key_scan();
void the_led();
unsigned char iic_read();
void main(){
allinit();
TMOD=0x16;TH0=0xFF; TL0=0xFF; TH1=0x28; TL1=0x00; ET0=1; ET1=1; EA=1;
TR0=1; TR1=1;
yi=10;er=10;san=10;si=10;wu=10;liu=10;qi=10;ba=10;
while(1){
dianya=iic_read();
dianya=dianya*1.96;
//判断界面
if(flag_yemian==0){
yi=23;er=21;san=21;si=21;wu=21;liu=dac_out/100+10;qi=dac_out%100/10;ba=dac_out%10;
}
else if(flag_yemian==1){
if(pinlv<100){
yi=22;er=21;san=21;si=21;wu=pinlv%100/10;liu=pinlv%10;qi=0;ba=0;
}
else if(pinlv<10){
yi=22;er=21;san=21;si=21;wu=21;liu=pinlv%10;qi=0;ba=0;
}
else {
yi=22;er=21;san=21;si=pinlv/100;wu=pinlv%100/10;liu=pinlv%10;qi=0;ba=0;
}
}
key_scan();
//判断DAC
if(flag_dac==1)dac_out=dianya;
else dac_out=200;
//判断小灯
if(flag_led==0)the_led();
else if(flag_led==1){
P2=0X80;P0=0XFF;
}
//判断数码管
if(flag_seg==0){
display_12(yi,er);
display_34(san,si);
display_56(wu,liu);
display_78(qi,ba);
}
else if(flag_seg==1){
P2=0XC0;P0=0XFF;P2=0XE0;P0=0XFF;
}
}
}
按键及小灯
//按键功能控制
void key_scan(){
//s7灭数码管
if(s7==0){
delayms(1);
if(s7==0){
if(flag_seg==0)
flag_seg=1;
else
flag_seg=0;
}
while(!s7);
}
//s6灭灯
else if(s6==0){
delayms(1);
if(s6==0){
if(flag_led==0)
flag_led=1;
else if(flag_led==1)
flag_led=0;
}
while(!s6);
}
//s5转换dac输出
else if(s5==0){
delayms(1);
if(s5==0){
if(flag_dac==0)flag_dac=1;
else flag_dac=0;
}
while(!s5);
}
//s4转换显示界面
else if(s4==0){
delayms(1);
if(s4==0){
if(flag_yemian==0)
flag_yemian=1;
else
flag_yemian=0;
}
while(!s4);
}
}
//小灯闪烁控制
void the_led(){
P2=0X80;
if(flag_yemian==0){
led1=0;led2=1;
if(dac_out<150)led3=1;
else if(dac_out<250)led3=0;
else if(dac_out<350)led3=1;
else led3=0;
if(pinlv<10) led4=1;
else if(pinlv<50) led4=0;
else if(pinlv<100) led4=1;
else led4=0;
if(flag_dac==0)led5=0;
else led5=1;
}
else{
led1=1;led2=0;
if(dac_out<150)led3=1;
else if(dac_out<250)led3=0;
else if(dac_out<350)led3=1;
else led3=0;
if(pinlv<10) led4=1;
else if(pinlv<50) led4=0;
else if(pinlv<100) led4=1;
else led4=0;
if(flag_dac==0)led5=0;
else led5=1;
}
}
N555计数及初始化函数等
//计数
void time0(void) interrupt 1
{
pinlv_temp++;
}
//定时
void time1() interrupt 3
{
TH1=0x28; TL1=0x00;
t++;
if(t==20){
pinlv=pinlv_temp;
pinlv_temp=0;
t=0;
}
}
//获取电压
unsigned char iic_read(){
unsigned char temp;
IIC_Start();
IIC_SendByte(0x90);
IIC_WaitAck();
IIC_SendByte(0x03);
IIC_WaitAck();
IIC_Stop();
IIC_Start();
IIC_SendByte(0x91);
IIC_WaitAck();
temp=IIC_RecByte();
IIC_Stop();
return temp;
}
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);
}
void delayms(int ms){
int q,w;
for(q=0;q<ms;q++)
for(w=0;w<845;w++);
}