蓝桥杯单片机必备知识-----(3)pcf8591–ADC
IIC.H中需要添加的代码
unsigned char init_pcf8591(void);
unsigned char read_pcf8591(void);
IIC.C中需要添加的代码
void init_pcf8591(void)
{
IIC_Start();
IIC_SendByte(0x90);
IIC_WaitAck();
IIC_SendByte(0x03);
IIC_WaitAck();
IIC_Stop();
}
unsigned char adc_pcf8591(void)
{
unsigned char temp;
IIC_Start();
IIC_SendByte(0x90);
IIC_WaitAck();
IIC_SendByte(add);
IIC_WaitAck();
IIC_Stop();
IIC_Start();
IIC_SendByte(0x91);
IIC_WaitAck();
temp=IIC_RecByte();
IIC_Stop();
return temp;
}
主函数
void main()
{
cls_buzz();
AUXR = 0x80;
TMOD = 0x30;
TL0 = 0xcd;
TH0 = 0xd4;
TF0 = 0;
TR0 = 1;
ET0 = 1;
EA = 1;
init_pcf8591();
while(1)
{
if(adc_flag)
{
adc_flag = 0;
adc_value = read_pcf8591();
}
}
}
<