本文主要是介绍小蜜蜂老师【基于PCF8951的电压信号探测器】,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
关于PCF8951模块,有一点需要指出,
IIC_SendByte(0x03);和IIC_SendByte(0x43);
0x43是打开了模拟输出,如果用到DAC最好写0x43,不然可能误关开关
如果只需要进行DAC的话,写0x03就ok了
什么时候用到ADC呢,要把电压值显示在数码管上的时候
什么时候用到DAC呢,要有模拟电压输出的时候,这个是用万用表进行测量的
#include"reg52.h"
#include"iic.h"sbit S4=P3^3;
sbit S5=P3^2;
sbit S6=P3^1;unsigned char code SMG_duanma[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0xbf,0xff};
unsigned int adc_value=0;
float adc_volt=0;
unsigned int smg_volt=0;
unsigned int para=300;
unsigned char stat=0;
unsigned char num=0;
unsigned int count=0;
unsigned char stat_led=0xff;
unsigned char led_f=0;
unsigned char count_k=0;void HC138(unsigned char m)
{switch(m){case 4:P2=(P2&0x1f)|0x80;break;case 5:P2=(P2&0x1f)|0xa0;break;case 6:P2=(P2&0x1f)|0xc0;break;case 7:P2=(P2&0x1f)|0xe0;break;case 0:P2=(P2&0x1f)|0x00;break;}P2=(P2&0x1f)|0x00;
}void SMG_bit(unsigned char pos,unsigned char dat)
{P0=(0x01<<pos);HC138(6);P0=dat;HC138(7);
}void SMG_off()
{P0=0xff;HC138(6);P0=0xff;HC138(7);
}void SMG_delay(unsigned char y)
{while(y--);
}void SMG_display()
{if(stat==0){SMG_bit(0,0xc1);SMG_delay(200);SMG_bit(0,0xff);SMG_delay(200);SMG_bit(5,SMG_duanma[smg_volt/100]);SMG_delay(200);SMG_bit(5,0xff);SMG_delay(200);SMG_bit(6,SMG_duanma[(smg_volt/10)%10]);SMG_delay(200);SMG_bit(6,0xff);SMG_delay(200);SMG_bit(7,SMG_duanma[smg_volt%10]);SMG_delay(200);SMG_bit(7,0xff);SMG_delay(200);SMG_off();SMG_delay(200);}else if(stat==1){SMG_bit(0,0x8c);SMG_delay(200);SMG_bit(0,0xff);SMG_delay(200);SMG_bit(5,SMG_duanma[para/100]);SMG_delay(200);SMG_bit(5,0xff);SMG_delay(200);SMG_bit(6,SMG_duanma[(para/10)%10]);SMG_delay(200);SMG_bit(6,0xff);SMG_delay(200);SMG_bit(7,SMG_duanma[para%10]);SMG_delay(200);SMG_bit(7,0xff);SMG_delay(200);SMG_off();SMG_delay(200);}else if(stat==2){SMG_bit(0,0x92);SMG_delay(200);SMG_bit(0,0xff);SMG_delay(200);if(num>9){SMG_bit(6,SMG_duanma[num/10]);SMG_delay(200);SMG_bit(7,0xff);SMG_delay(200);}SMG_bit(7,SMG_duanma[num%10]);SMG_delay(200);SMG_bit(7,0xff);SMG_delay(200);SMG_off();SMG_delay(200);}
}void InitTimer0()
{TMOD=0x01;TH0=(65535-50000)/256;TL0=(65535-50000)%256;EA=1;TR0=1;ET0=1;
}void serviceTimer0() interrupt 1
{TH0=(65535-50000)/256;TL0=(65535-50000)%256;if(count_k==1){count++;if(count%20==0){if(num!=99){num++;}if(led_f==1){if((stat_led&0x80)==0x80){stat_led&=~0x80;}else{stat_led|=0x80;}}}if(count>120){stat_led&=~0x07;led_f=1;}else if(count>80){stat_led&=~0x03;}else if(count>40){stat_led&=~0x01;}else{stat_led=0xff;}}
}unsigned char ReadPCF88951()
{unsigned char tmp;IIC_Start();IIC_SendByte(0x90);IIC_WaitAck();IIC_SendByte(0x03);IIC_WaitAck();IIC_Stop();SMG_display();IIC_Start();IIC_SendByte(0x91);IIC_WaitAck();tmp=IIC_RecByte();IIC_SendAck(1);IIC_Stop();return tmp;
}void analysize_volt()
{unsigned char i;adc_value=0;for(i=0;i<3;i++){adc_value+=ReadPCF88951();}adc_value=adc_value/3;adc_volt=adc_value*(5.0/255);smg_volt=adc_volt*100;if(smg_volt<para){if(count_k==0){count_k=1;}}else{count_k=0;led_f=0;count=0;num=0;stat_led|=0xff;}SMG_display();P0=stat_led;HC138(4);
}void key_delay(unsigned char p)
{while(p--);
}void keyboard()
{if(S4==0){key_delay(200);if(S4==0){while(S4==0){analysize_volt();}if(stat==0){stat=1;}else if(stat==1){stat=2;}else if(stat==2){stat=0;}}}if(S5==0){key_delay(200);if(S5==0){while(S5==0){analysize_volt();}if(stat==1){if(para!=0){para=para-50;}else{para=500;}}}}if(S6==0){key_delay(200);if(S6==0){while(S6==0){analysize_volt();}if(stat==1){if(para!=500){para=para+50;}else{para=0;}}}}
}void Initsystem()
{P0=0xff;HC138(4);P0=0x00;HC138(5);SMG_off();
}void main()
{Initsystem();InitTimer0();while(1){analysize_volt();keyboard();}
}
这篇关于小蜜蜂老师【基于PCF8951的电压信号探测器】的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!