【Realplay】 PCF8591模塊 ADDA轉(zhuǎn)換模塊 模數(shù)數(shù)模轉(zhuǎn)換送杜邦線.zip
(625.18 KB, 下載次數(shù): 16)
2018-7-11 16:20 上傳
點擊文件名下載附件
下載積分: 黑幣 -5
畢業(yè)設(shè)計1.rar
(15.14 MB, 下載次數(shù): 48)
2018-7-11 16:20 上傳
點擊文件名下載附件
下載積分: 黑幣 -5
家庭智能燈控系統(tǒng),人走斷電,根據(jù)環(huán)境調(diào)光,強制開關(guān)
不多說上程序
//主程序//
#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轉(zhuǎn)換//
#include<intrins.h>
sbit scl = P1^1;
sbit sda = P1^0;
/*************起始信號**************/
//工作條件sda 產(chǎn)生下降沿 scl 為高電平;
void star_24c04 ()
{
sda = 1;
scl = 1;
sda = 0;
scl = 0; //時鐘信號復(fù)位
}
/*************終止信號**************/
//終止條件sda數(shù)據(jù)信號產(chǎn)生下降沿 scl 時鐘信號為高電平
void stop_24c04()
{
sda = 0;
scl = 1;
sda = 1;
}
/**************應(yīng)答信號**************/
void ack_24c04()
{
uchar i= 255;
scl = 1;
while(sda && i--);
scl = 0;
}
/**************I2C總線初始化*********/
void init_24c04(void)
{
sda = 1;
scl = 1;
}
/**************讀取一個字節(jié)**************/
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);
}
/*************寫入一個字節(jié)**************/
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;
}
//讀地址、數(shù)據(jù)函數(shù)
//0x40表示0號通道;0x41表示1號通道;
uchar read_pcf8591(uchar sh)
{
uchar temp;
star_24c04(); //發(fā)出開始信號
write_onebyte_24c04(0x90);//PCF8591地址 相當于握手信號
ack_24c04(); //響應(yīng)
write_onebyte_24c04(sh);//0x40表示0號通道;//0x43 滑變 //0x40 光敏 //0x41 熱敏
ack_24c04(); //響應(yīng)
star_24c04(); //再次發(fā)出開始信號
write_onebyte_24c04(0x91);//發(fā)出讀取對應(yīng)8951模塊AD的信號
ack_24c04(); //響應(yīng)
temp=read_onebyte_24c04();//讀取數(shù)據(jù)
stop_24c04(); //停止
return(temp); //返回數(shù)據(jù)
}
//1602時序//
#define uchar unsigned char
#define uint unsigned int
sbit RS = P2^3;//數(shù)據(jù)/命令
sbit RW = P2^4;//讀/寫
sbit E = P2^5;//使能
uchar i;
uchar num[] = {"0123456789"};
void delayus(uint x) //延時函數(shù)
{
while(x--);
}
void write_com(uchar com)//寫命令
{
RW = 0;
RS = 0;
E = 1;
P0 = com;
delayus(100);
E = 0;
}
void write_data(uchar da)//寫入數(shù)據(jù)
{
RW = 0;
RS = 1;
E = 1;
P0 = da;
delayus(100);
E = 0;
}
void init_1602() //LCD1602 初始化
{
write_com(0x3c);//設(shè)定數(shù)據(jù)總線的個數(shù)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) //顯示數(shù)字
{
write_data(num[x / 10]);
write_data(num[x % 10]);
}
void display_num1(unsigned int x) //顯示數(shù)字
{
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 ");
}
|