標(biāo)題:
自制的的燈網(wǎng)絡(luò)系統(tǒng)單片機(jī)程序
[打印本頁(yè)]
作者:
123wer煩
時(shí)間:
2019-5-23 16:16
標(biāo)題:
自制的的燈網(wǎng)絡(luò)系統(tǒng)單片機(jī)程序
這是自己初步做的一個(gè)燈網(wǎng)絡(luò)系統(tǒng)
具體內(nèi)容在附件中
希望可以為廣大好友提供參考。
單片機(jī)源程序如下:
/*******************************************************************************
* 實(shí)驗(yàn)名 : 萬(wàn)年歷實(shí)驗(yàn)
* 使用的IO :
* 實(shí)驗(yàn)效果 :1602顯示時(shí)鐘,按K3進(jìn)入時(shí)鐘設(shè)置,按K1選擇設(shè)置的時(shí)分秒日月,按K2選擇
*選擇設(shè)置加1。
* 注意
按下s5按鍵進(jìn)入操作模式
s3按下為選擇改變數(shù)值
s2按下加數(shù)值 :
*******************************************************************************/
#include<reg51.h>
#include"lcd.h"
#include"ds1302.h"
sbit K1=P3^1;
sbit K2=P3^0;
sbit K3=P3^2;
sbit K4=P3^3;
sbit led8=P1^7;
sbit led2=P1^1;
sbit sda=P2^0;
sbit scl=P2^1;
sbit beep=P2^3;
uchar led_num=100; //用來(lái)判斷亮度
uchar out_light;
uchar IRtime; //儲(chǔ)存檢測(cè)紅外高低電平持續(xù)時(shí)間
uchar IRcord[4]; //儲(chǔ)存解碼后的4個(gè)字節(jié)數(shù)據(jù)
uchar IRdata[33]; //包含起始碼在內(nèi)的33位數(shù)據(jù)
bit IRpro_ok; //解碼后4個(gè)字節(jié)數(shù)據(jù)接收完成標(biāo)志位
bit IRok; //33位數(shù)據(jù)接收完成標(biāo)志
void Int1Configuration();
void delay();
void Display_led();
void delay1(uint i);
void LcdDisplay();
unsigned char SetState,SetPlace;
void Delay10ms(void); //誤差 0us
void display_led();
/*********************************DA AD部分***********************************/
void start()
{
scl=1;
_nop_();
sda=1;
delay();
sda=0;
delay();
scl=0;
_nop_();
}
void stop()
{
scl=1;
_nop_();
sda=0;
delay();
sda=1;
delay();
}
void respons()
{
scl=1;
_nop_();
sda=0;
delay();
scl=0;
_nop_();
}
void norespons()
{
scl=1;
_nop_();
sda=1;
delay();
}
void init()
{
scl=1;
_nop_();
sda=1;
_nop_();
}
void write_byte(uchar dat)
{
uchar i,temp;
temp=dat;
for(i=0;i<8;i++)
{
scl=0;
_nop_();
if((temp&0x80)==0x80)
sda=1; //寫1
else
sda=0; //寫0
scl=1;
_nop_();
temp<<=1;
}
scl=0;
_nop_();
}
uchar read_byte()
{
uchar dat,i;
sda=1;
_nop_();
scl=0;
_nop_();
for(i=0;i<8;i++)
{
scl=1;
_nop_();
if(sda)
dat|=0x01;
if(i<7)
dat<<=1;
scl=0;
_nop_();
}
return dat;
}
void DAC_write(uchar dat)
{
start();
write_byte(0x90);
respons();
write_byte(0x40);
respons();
write_byte(dat);
respons();
stop();
}
uchar ADC_read(uchar com)
{
uchar dat;
dat=com;
start();
write_byte(0x90);
respons();
write_byte(com);
respons();
start();
write_byte(0x91);
respons();
dat=read_byte();
norespons(); //100
stop();
return dat;
}
void CHG_light_out()
{
init();
out_light=ADC_read(0x42);
if(out_light>=0&&out_light<=50)//強(qiáng)光亮
{
led_num=100;
}
if(out_light>50&&out_light<=100)//強(qiáng)平常光亮
{
led_num=120;
}
if(out_light>100&&out_light<=200)//平常光亮
{
led_num=130;
}
if(out_light>200) //弱平常光亮
{
led_num=150;
}
}
void beep_play()
{
beep=0;
delay1(50);
beep=1;
}
void Display_led()
{
uchar i;
init();
if(TIME[2]==0x12)
{
DAC_write(led_num);
if(IRcord[2]==0x0c)
{
led_num=led_num+10;
beep_play();
if(led_num==160)
{
led_num==100;
}
IRcord[2]=0;
}
}
else
{
DAC_write(0);
}
}
/************************************紅外通訊部分*******************************/
void init_IR()
{
TMOD=0x02; //定時(shí)器0工作模式二,8位自動(dòng)重裝
TL0=TH0=0; //定時(shí)器0溢出一次時(shí)間為256個(gè)機(jī)器周期
EA=1; //開啟總中斷
ET0=1; //開啟定時(shí)器0中斷
TR0=1; //啟動(dòng)定時(shí)器0
IT0=1; //外部中斷1跳邊沿觸發(fā)
EX0=1; //開啟外部中斷1
}
//把提取的33次脈寬進(jìn)行解碼 NEC協(xié)議
void IRcordpro()
{
uchar i;//i是用于計(jì)數(shù)處理4個(gè)字節(jié)
uchar j;//j用于計(jì)數(shù)處理1個(gè)字節(jié)的8位數(shù)據(jù)
uchar k;//k用于計(jì)數(shù)處理33次脈寬
k = 1;//從第一位脈寬開始處理,丟掉起始碼
for(i = 0; i < 4; i++)
{
for(j = 0; j < 8; j++)
{
//如果脈寬大于數(shù)據(jù)0標(biāo)準(zhǔn)的1125us那么就判定為數(shù)據(jù)1
if(IRdata[k] > 5) IRcord[i] |= 0x80;//寫1
//只能右移7次,如果右移8次則會(huì)把第一位數(shù)據(jù)移出去
if(j < 7) IRcord[i] >>= 1;
k++; //處理下一次脈寬
}
}
IRpro_ok = 1;//解碼完成
}
void menu()
{
if(IRcord[2]==0x47)
{
IRcord[2]=0;
beep_play();
SetState=~SetState;
SetPlace=0;
Ds1302Init();
}
}
/*******************************************************************************/
/*******************************************************************************
* 函數(shù)名 : main
* 函數(shù)功能 : 主函數(shù)
* 輸入 : 無(wú)
* 輸出 : 無(wú)
*******************************************************************************/
void main()
{
unsigned char i;
Int1Configuration();
LcdInit();
init_IR();
Ds1302Init();
CHG_light_out();
while(1)
{
LcdWriteCom(0x80);
LcdWriteCom(0x0f);
if(IRok)//判斷33次脈寬是否提取完成
{
IRcordpro();//根據(jù)脈寬解碼出4個(gè)字節(jié)的數(shù)據(jù)
IRok = 0;//清零脈寬檢查完成標(biāo)志位等待下一次脈寬檢查
if(IRpro_ok)//判斷解碼是否完成
{
if(IRcord[2]==0x40)
{
//led8=0;
}
}
}
Display_led();
menu();
if(SetState==0)
{
Ds1302ReadTime();
}
else
{
if(IRcord[2]==0x44)
{
SetPlace++;
beep_play();
if(SetPlace>=7)
SetPlace=0;
IRcord[2]=0;
}
if(IRcord[2]==0x40)
{
TIME[SetPlace]++;
if((TIME[SetPlace]&0x0f)>9) //換成BCD碼。
{
TIME[SetPlace]=TIME[SetPlace]+6;
}
if((TIME[SetPlace]>=0x60)&&(SetPlace<2)) //分秒只能到59
{
TIME[SetPlace]=0;
}
if((TIME[SetPlace]>=0x24)&&(SetPlace==2)) //小時(shí)只能到23
{
TIME[SetPlace]=0;
}
if((TIME[SetPlace]>=0x32)&&(SetPlace==3)) //日只能到31
{
TIME[SetPlace]=0;
}
if((TIME[SetPlace]>=0x13)&&(SetPlace==4)) //月只能到12
{
TIME[SetPlace]=0;
}
if((TIME[SetPlace]>=0x8)&&(SetPlace==5)) //周只能到7
{
TIME[SetPlace]=1;
}
// if(SetPlace==5) //月只能到12
// {
// TIME[SetPlace]=;
// }
beep_play();
IRcord[2]=0;
}
}
LcdDisplay();
}
}
/*******************************************************************************
* 函數(shù)名 : LcdDisplay()
* 函數(shù)功能 : 顯示函數(shù)
* 輸入 : 無(wú)
* 輸出 : 無(wú)
*******************************************************************************/
void LcdDisplay()
{
LcdWriteCom(0x80+0X40);
LcdWriteData('0'+TIME[2]/16); //時(shí)
LcdWriteData('0'+(TIME[2]&0x0f));
LcdWriteData('-');
LcdWriteData('0'+TIME[1]/16); //分
LcdWriteData('0'+(TIME[1]&0x0f));
LcdWriteData('-');
LcdWriteData('0'+TIME[0]/16); //秒
LcdWriteData('0'+(TIME[0]&0x0f));
LcdWriteCom(0x80);
LcdWriteData('2');
LcdWriteData('0');
LcdWriteData('0'+TIME[6]/16); //年
LcdWriteData('0'+(TIME[6]&0x0f));
LcdWriteData('-');
LcdWriteData('0'+TIME[4]/16); //月
LcdWriteData('0'+(TIME[4]&0x0f));
LcdWriteData('-');
LcdWriteData('0'+TIME[3]/16); //日
LcdWriteData('0'+(TIME[3]&0x0f));
LcdWriteCom(0x8D);
LcdWriteData('0'+(TIME[5]&0x07)); //星期
}
/*******************************************************************************
* 函數(shù)名 : Int0Configuration()
* 函數(shù)功能 : 配置外部中斷0
* 輸入 : 無(wú)
* 輸出 : 無(wú)
*******************************************************************************/
void Int1Configuration()
{
//設(shè)置INT1
IT1=1;//跳變沿出發(fā)方式(下降沿)
EX1=1;//打開INT1的中斷允許。
EA=1;//打開總中斷
}
/*******************************************************************************
* 函數(shù)名 : Int0()
* 函數(shù)功能 : 外部中斷0 中斷函數(shù)
* 輸入 : 無(wú)
* 輸出 : 無(wú)
*******************************************************************************/
void Int1() interrupt 2
{
// Delay10ms();
//if(K3==0)
beep_play();
SetState=~SetState;
SetPlace=0;
Ds1302Init();
}
//定時(shí)器0中斷,每中斷一次需要256*1.085us = 277.76us
void time0() interrupt 1
{
……………………
…………限于本文篇幅 余下代碼請(qǐng)從51黑下載附件…………
復(fù)制代碼
所有資料51hei提供下載:
燈網(wǎng)絡(luò)系統(tǒng).zip
(54.55 KB, 下載次數(shù): 5)
2019-5-23 16:14 上傳
點(diǎn)擊文件名下載附件
下載積分: 黑幣 -5
作者:
admin
時(shí)間:
2019-5-24 05:46
本帖需要重新編輯補(bǔ)全電路原理圖,源碼,詳細(xì)說(shuō)明與圖片即可獲得100+黑幣(帖子下方有編輯按鈕)
歡迎光臨 (http://www.torrancerestoration.com/bbs/)
Powered by Discuz! X3.1