【Realplay】 PCF8591模块 ADDA转换模块 模数数模转换送杜邦线.zip
(625.18 KB, 下载次数: 16)
毕业设计1.rar
(15.14 MB, 下载次数: 48)
家庭智能灯控系统,人走断电,根据环境调光,强制开关
不多说上程序
//主程序//
#include <REGX51.H>
#define uchar unsigned char
#define uint unsigned int
uint cp1,cp2,cp3,cp4,cp5,sec=0,hour=0,min=0,a,b=16,pwm,sec1=0;
uint intensity,infrard,AD1,AD2;
#include <AD.c>
#include <1602.c>
#include <key.c>
sbit LED=P2^1;
#include <display.c>
void timer_isr()interrupt 1
{
TL0 = (65535-1000)%256;
TH0 = (65535-1000)/256;
cp1++;
cp3++;
if(cp3 > AD2) pwm =1;
if(cp3 <= AD2) pwm =0;
if(cp3 >= 25) cp3=0;
if(cp1>=200)
{
cp1=0;
cp2++;
if(cp2 == 1)
{
infrard = read_pcf8591(0x40);
}
if(cp2 == 4)
{
cp2=0;
sec++;
intensity = read_pcf8591(0x42);//光强
AD1= intensity*0.39;
AD2= intensity/10;
}
}
if(sec>=60)
{
min++;
sec=0;
}
if(min>=60)
{
min=0;
hour++;
}
if(hour>=24) hour=0;
if(mode == 0)
{
if(infrard >= 150)LED = pwm;
else LED = 1;
}
if(mode == 1)
{
LED = 1;
}
}
void timer_init()
{
TMOD |= 0x01;
TL0 = (65535-1000)%256;
TH0 = (65535-1000)/256;
TR0 = 1;
ET0 = 1;
EA = 1;
}
void main()
{
// uchar i;
timer_init();
init_24c04();
init_1602();
sec=0;
hour=0;
min=0;
while(1)
{
key();
display();
}
}
//pcf8591AD转换//
#include<intrins.h>
sbit scl = P1^1;
sbit sda = P1^0;
/*************起始信号**************/
//工作条件sda 产生下降沿 scl 为高电平;
void star_24c04 ()
{
sda = 1;
scl = 1;
sda = 0;
scl = 0; //时钟信号复位
}
/*************终止信号**************/
//终止条件sda数据信号产生下降沿 scl 时钟信号为高电平
void stop_24c04()
{
sda = 0;
scl = 1;
sda = 1;
}
/**************应答信号**************/
void ack_24c04()
{
uchar i= 255;
scl = 1;
while(sda && i--);
scl = 0;
}
/**************I2C总线初始化*********/
void init_24c04(void)
{
sda = 1;
scl = 1;
}
/**************读取一个字节**************/
uchar read_onebyte_24c04(void)
{
uchar i ,dat;
sda = 1;//释放总线
for(i = 0;i < 8;i++)
{
scl = 1;
dat = dat<<1;
if(sda)dat = dat|0x01;//先读取高位,放到dat 的低位;
scl = 0;
}
sda = 1;
scl = 0;
return (dat);
}
/*************写入一个字节**************/
void write_onebyte_24c04(uchar dat)
{
uchar i;
for(i = 0;i < 8;i++)
{
sda = (bit)(dat & 0x80);
dat = dat << 1;
scl = 1;
scl = 0;
}
sda = 1;
scl = 0;
}
//读地址、数据函数
//0x40表示0号通道;0x41表示1号通道;
uchar read_pcf8591(uchar sh)
{
uchar temp;
star_24c04(); //发出开始信号
write_onebyte_24c04(0x90);//PCF8591地址 相当于握手信号
ack_24c04(); //响应
write_onebyte_24c04(sh);//0x40表示0号通道;//0x43 滑变 //0x40 光敏 //0x41 热敏
ack_24c04(); //响应
star_24c04(); //再次发出开始信号
write_onebyte_24c04(0x91);//发出读取对应8951模块AD的信号
ack_24c04(); //响应
temp=read_onebyte_24c04();//读取数据
stop_24c04(); //停止
return(temp); //返回数据
}
//1602时序//
#define uchar unsigned char
#define uint unsigned int
sbit RS = P2^3;//数据/命令
sbit RW = P2^4;//读/写
sbit E = P2^5;//使能
uchar i;
uchar num[] = {"0123456789"};
void delayus(uint x) //延时函数
{
while(x--);
}
void write_com(uchar com)//写命令
{
RW = 0;
RS = 0;
E = 1;
P0 = com;
delayus(100);
E = 0;
}
void write_data(uchar da)//写入数据
{
RW = 0;
RS = 1;
E = 1;
P0 = da;
delayus(100);
E = 0;
}
void init_1602() //LCD1602 初始化
{
write_com(0x3c);//设定数据总线的个数4/8,显示一行/两行
write_com(0x0c);// //光标不显示
write_com(0x06);// //光标随字符右移
}
//void LCD_clr1602() //LCD1602 清屏
//{
// write_com(0x01); // 对字符串清0
// write_com(0x02); //对光标清0
//}
void goto_xy(uchar y,uchar x) //定位显示位置
{
if(y == 1)
write_com(x + 0x80); //定位第一行
else
write_com(x + 0x80 + 0x40); //定位第二行
}
void display_num(unsigned char x) //显示数字
{
write_data(num[x / 10]);
write_data(num[x % 10]);
}
void display_num1(unsigned int x) //显示数字
{
write_data(num[x / 100 % 10]);
write_data(num[x / 10 % 10]);
write_data(num[x % 10]);
}
void display_string(uchar *p) //显示字符
{
while(*p)
{
write_data(*p);
p++;
}
}
//display//
void display()
{
goto_xy(1,0);
display_string("time ");
goto_xy(1,6);
display_num(hour);
goto_xy(1,8);
display_string(":");
goto_xy(1,9);
display_num(min);
goto_xy(1,11);
display_string(":");
goto_xy(1,12);
display_num(sec);
goto_xy(2,0);
display_string("Lx:");
goto_xy(2,3);
display_num1(AD1);
goto_xy(2,6);
display_string("%");
goto_xy(2,8);
if(infrard > 150)display_string("OPEN");
else display_string("OFF ");
}
|