【蓝桥杯单片机进阶强化-03】PCF8591与24C02综合应用-智能照明自动控制器
内容来自小蜜蜂老师
前面已经讲解了PCF8591 和AT24C02的基本使用原理有需要可以看看这两
篇博文
通过该题目的练习更熟悉了PCF8591 IIC读取数据程序的编写,和AT24C02
IIC 读操作和写操作程序的编程。在练习过程中更重要的一点是锻炼了我们
编程的逻辑 相信在后面的学习中对题目的理解和功能的实现会有更好的思考
#include "reg52.h"
#include "iic.h"
sbit L1 = P0^0;
sbit L2 = P0^1;
sbit L3 = P0^2;
sbit L4 = P0^3;
sbit L5 = P0^4;
sbit L6 = P0^5;
sbit L7 = P0^6;
sbit L8 = P0^7;
unsigned char Rd1 = 0;
unsigned char Rd1_old = 0;
unsigned char Level = 0;
unsigned char Level_old = 0;
sbit S4 = P3^3;
sbit S5 = P3^2;
unsigned char code SMG_Duan[] = {0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,
0x80,0x90,0x88,0x83,0xC6,0xA1,0x86,0x8E,0xbf};
void Delay(unsigned int t)
{
while(t--);
}
void Init_74HC138(unsigned char n)
{
switch(n)
{
case 4:P2 = P2| 0x1f; P2 = 0x80; break;
case 5:P2 = P2| 0x1f; P2 = 0xa0; break;
case 6:P2 = P2| 0x1f; P2 = 0xc0; break;
case 7:P2 = P2| 0x1f; P2 = 0xe0; break;
case 0:P2 = P2| 0X1f; P2 = 0x00; break;
}
}
void InitSystem(void)
{
Init_74HC138(5);
P0 = 0x00;
Init_74HC138(4);
P0 = 0xff;
}
void SMG_DisplayBit(unsigned char pos ,unsigned char dat)
{
Init_74HC138(6);
P0 = 0x01 << pos ;
Init_74HC138(7);
P0 = dat;
}
void SMG_CLOSEALL(unsigned char dat)
{
Init_74HC138(6);
P0 = 0xff;
Init_74HC138(7);
P0 = dat;
}
void Read_Rd1_Data()
{
IIC_Start();
IIC_SendByte(0x90);
IIC_WaitAck();
IIC_SendByte(0x01);
IIC_WaitAck();
IIC_Stop();
IIC_Start();
IIC_SendByte(0x91);
IIC_WaitAck();
Rd1 = IIC_RecByte();
IIC_SendAck(1);
IIC_Stop();
}
void Led_Level_Tackle(void)
{
Init_74HC138(4);
Read_Rd1_Data();
if(Rd1 > 200 && Rd1 <= 250)
{
P0 = 0xfe;
Level = 5;
}
else if(Rd1 > 150 && Rd1 <= 200)
{
P0 = 0xfc;
Level = 4;
}
else if(Rd1 > 100 && Rd1 <= 150)
{
P0 = 0xf0;
Level = 3;
}
else if(Rd1 > 50 && Rd1 <= 100)
{
P0 = 0xc0;
Level = 2;
}
else if(Rd1 > 0 && Rd1 <= 50)
{
P0 = 0x00;
Level = 1;
}
}
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_WaitAck();
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 ;
}
void SMG_Dispaly_New_Data(void)
{
SMG_DisplayBit(0,SMG_Duan[16]);
Delay(200);
SMG_DisplayBit(1,SMG_Duan[Level]);
Delay(200);
SMG_DisplayBit(2,SMG_Duan[16]);
Delay(200);
SMG_DisplayBit(3,0xff);
Delay(200);
SMG_DisplayBit(4,0xff);
Delay(200);
SMG_DisplayBit(5,SMG_Duan[Rd1/100]);
Delay(200);
SMG_DisplayBit(6,SMG_Duan[Rd1/10%10]);
Delay(200);
SMG_DisplayBit(7,SMG_Duan[Rd1%10]);
Delay(200);
}
void SMG_Dispaly_Old_Data(void)
{
SMG_DisplayBit(0,SMG_Duan[16]);
Delay(200);
SMG_DisplayBit(1,SMG_Duan[Level_old]);
Delay(200);
SMG_DisplayBit(2,SMG_Duan[16]);
Delay(200);
SMG_DisplayBit(3,0xff);
Delay(200);
SMG_DisplayBit(4,0xff);
Delay(200);
SMG_DisplayBit(5,SMG_Duan[Rd1_old/100]);
Delay(200);
SMG_DisplayBit(6,SMG_Duan[Rd1_old/10%10]);
Delay(200);
SMG_DisplayBit(7,SMG_Duan[Rd1_old%10]);
Delay(200);
}
void Key_Tackle(void)
{
if(S4 == 0)
{
Delay(20);
if(S4 == 0)
{
Write_24C02(0x01,Level);
Delay(200);
Write_24C02(0x02,Rd1);
while(S4 == 0)
{
SMG_Dispaly_New_Data();
}
}
}
if(S5 == 0)
{
Delay(20);
if(S5 == 0)
{
Level_old = Read_24C02(0x01);
Delay(200);
Rd1_old = Read_24C02(0x02);
while(S5 == 0)
{
SMG_Dispaly_Old_Data();
}
}
}
}
void main(void)
{
InitSystem();
while(1)
{
Led_Level_Tackle();
SMG_Dispaly_New_Data();
Key_Tackle();
}
}
路漫漫其修远兮 加油
|