用51寫的一個萬年歷程序,包含了DS1302,oled,紅外遙控,很好用。
0.png (8.92 KB, 下載次數: 82)
下載附件
2017-12-22 02:07 上傳
紅外遙控接收頭 接 P3.3
DS3231 SDA 接 P0.0
DS3231 SCL 接 P0.1
OLED12864 SCL 接 P1.3
OLED12864 SCL 接 P1.3
可根據具體需要自行更改
效果圖:
04.png (226.21 KB, 下載次數: 50)
下載附件
2017-12-22 02:08 上傳
05.png (548.28 KB, 下載次數: 55)
下載附件
2017-12-22 02:08 上傳
01.png (244.44 KB, 下載次數: 71)
下載附件
2017-12-22 02:08 上傳
02.png (229.99 KB, 下載次數: 60)
下載附件
2017-12-22 02:08 上傳
03.png (214.03 KB, 下載次數: 66)
下載附件
2017-12-22 02:08 上傳
原理圖:
OLED 原理圖.jpg (40.55 KB, 下載次數: 55)
下載附件
2017-12-22 02:08 上傳
單片機源程序如下:
- /*****************************************************************************************************
- ********************************************頭文件、宏定義********************************************
- *****************************************************************************************************/
- #include <reg52.h> //oled_12864_IIC連接+DS3231時鐘模塊+紅外遙控和按鍵+STC90C516RD+
- #include <intrins.h> //年 月 日 時 分 秒 星期 溫度顯示 溫度來源DS3231,64S刷新一次
- #define uint unsigned int //年誤差+- 2min
- #define uchar unsigned char
- /*****************************************************************************************************
- **********************************************按鍵定義************************************************
- *****************************************************************************************************/
- sbit key1=P0^4; //設置
- sbit key2=P0^5; //加
- sbit key3=P0^6; //減
- sbit key4=P0^7; //確定
- sbit laba=P3^7; //喇叭
- /*****************************************************************************************************
- ********************************************紅外遙控定義**********************************************
- *****************************************************************************************************/
- #define Imax 14000//此處為晶振為11.0592時的取值, //時間計算 //紅外接口P3.3
- #define Imin 8000 //如用其它頻率的晶振時,
- #define Inum1 1450//要改變相應的取值。
- #define Inum3 3000
- #define Inum2 700
- uchar f;
- uchar IrOK;
- unsigned long m,Tc;
- uchar Im[4]={0x00,0x00,0x00,0x00};
- /*****************************************************************************************************
- *******************************************DS3231變量定義*********************************************
- *****************************************************************************************************/
- sbit DS3231_IIC_SDA=P0^0;
- sbit DS3231_IIC_SCL=P0^1;
- uchar RTC_Data[8];
- uchar RTC_Data1[8];
- uchar RTC_Set_ID;
- code uchar rtc_address[8]={0x00,0x01,0x02,0x04,0x05,0x03,0x06,0x11};//秒分時日月周年 最低位讀寫位
- /*****************************************************************************************************
- *******************************************OLED12864定義**********************************************
- *****************************************************************************************************/
- sbit oled_SCL=P1^3; //串行時鐘
- sbit oled_SDA=P1^2; //串行數據
- uchar display_buff[16]; //時間顯示數組
- /*****************************************************************************************************
- *********************************************變量定義*************************************************
- *****************************************************************************************************/
- bit T0_Flag; //定時器T01ms標志位
- bit laba_flag; //蜂鳴器標志位
- bit Open_Flag; //蜂鳴器標志位
- bit RTC_Save_Flag; //設置時間標志位
- bit shezhi_display_flag;
- void delayms(uint x); //延時函數
- void display1(); //顯示函數
- void display2();
- void OLED_12864_Write_commomand(uint dat); //命令
- void OLED_12864_Write_data(uint dat); //數據
- void OLED_12864_Init(); //初始化
- void OLED_Fill(uchar bmp_dat); //屏幕填充
- void OLED_SetPos(uchar x, uchar y); //起始坐標
- void OLED_8x16Str(uchar x, uchar y, uchar ch); //8*16的點陣
- void OLED_16x16CN(uchar x, uchar y, uchar N); //16*16的點陣
- void OLED_16x32(uchar x, uchar y, uchar ch);
- void Time_Fun(); //時基、走時函數
- void DS3231_Read_time(); //讀取時間
- void DS3231_write_time(); //寫入時間
- void KeyDataProcess(); //鍵值處理函數
- void DS3231_Init(); //時間模塊初始化
- /*****************************************************************************************************
- ******************************************OLED12864點陣數組*******************************************
- *****************************************************************************************************/
- uchar code F16x16[]=
- {
- 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
- 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,//"不顯示",0
-
- 0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x00,
- 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,//"一",1
-
- …………
- …………限于本文篇幅 余下代碼請從51黑下載附件…………
- 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,//不顯示,21
- };
- /*****************************************************************************************************
- ********************************************初始化時間************************************************
- *****************************************************************************************************/
- void Ram_init()
- {
- DS3231_Read_time(); //讀取 秒分時日月周年
- if((!RTC_Data[6])&&(!RTC_Data[5])&&(!RTC_Data[4])&&(!RTC_Data[3])&&(!RTC_Data[2])&&(!RTC_Data[1])&&(!RTC_Data[0]))
- {
- RTC_Data[0]=0; //秒 //秒分時日月周年 最低位讀寫位
- RTC_Data[1]=0; //分
- RTC_Data[2]=12; //時
- RTC_Data[3]=3; //日
- RTC_Data[4]=11; //月
- RTC_Data[5]=2; //周
- RTC_Data[6]=15; //年
- DS3231_write_time(); //寫入 秒分時日月周年
- }
- RTC_Set_ID=0;
- }
- /*****************************************************************************************************
- ************************************************主函數************************************************
- *****************************************************************************************************/
- void main()
- {
- EA=1; //開總中斷
- IT1=1; //下降沿有效
- EX1=1; //外部中斷開
- ET1=1; //打開允許開關
- TMOD=0x11; //定時/計數器 工作于方式1
- TH1=0xff; //50ms定時常數
- TL1=256-131;
- TR1=1; //啟動定時/計數器 中斷
- TH0=0; //賦初值
- TL0=0;
- TR0=1; //開始計時
- shezhi_display_flag=0;
- delayms(10);
- OLED_12864_Init();
- delayms(10);
- display1(); //顯示
- display2(); //顯示
- Ram_init(); //初始化時間
- DS3231_Init(); //模塊初始化
- laba=0;
- while(1)
- {
- if(T0_Flag)
- {
- T0_Flag=0;
- Time_Fun();
- }
- }
- }
- /*****************************************************************************************************
- *********************************************時基、走時函數*******************************************
- *****************************************************************************************************/
- void Time_Fun()
- {
- static uchar T10Ms_Cnt=0,T200Ms_Cnt=0;
- static uchar Buzz_On_Cnt=0,RTC_Save_Cnt=0;
-
- T10Ms_Cnt++;
- if(T10Ms_Cnt>=5)
- {
- T10Ms_Cnt=0;
- KeyDataProcess();
- if(laba_flag)
- {
- Buzz_On_Cnt++;
- if(Buzz_On_Cnt>=10)
- {
- laba_flag=0;
- Buzz_On_Cnt=0;
- }
- }
- if(RTC_Save_Flag)
- {
- RTC_Save_Cnt++;
- if(RTC_Save_Cnt>=50)
- {
- RTC_Save_Cnt=0;
- RTC_Save_Flag=0;
- RTC_Set_ID=0;
- DS3231_write_time();
- }
- }
- }
- T200Ms_Cnt++;
- if(T200Ms_Cnt>=100&&shezhi_display_flag==0)
- {
- T200Ms_Cnt=0;
- if(!RTC_Set_ID)
- DS3231_Read_time();
- display1();
- display2();
- }
- }
- /*****************************************************************************************************
- ***********************************************鍵值處理***********************************************
- *****************************************************************************************************/
- void KeyDataProcess() //10mS處理一次
- { //按鍵操作部分
- if(key1==0)
- {
- delayms(10);
- if(key1==0)
- {
- while(!key1);
- laba_flag=1; //蜂鳴器響一聲
- shezhi_display_flag=1;
- RTC_Set_ID++;
- if(RTC_Set_ID>=8)
- RTC_Set_ID=1;
- Open_Flag=1;
- display1();
- if(RTC_Set_ID==1)
- {
- OLED_Fill(0x00);
- OLED_16x16CN(32, 0, 16);//年
- OLED_16x16CN(48, 0, 18);//份
- OLED_16x16CN(64, 0, 14);//設
- OLED_16x16CN(80, 0, 15);//置
- OLED_16x32(0, 2, display_buff[2]);//年
- OLED_16x32(16, 2, display_buff[3]);
- OLED_16x32(32, 2, 11);//-
- OLED_16x32(48, 2, display_buff[4]);//月
- OLED_16x32(64, 2, display_buff[5]);
- OLED_16x32(80, 2, 11);//-
- OLED_16x32(96, 2, display_buff[6]);//日
- OLED_16x32(112, 2, display_buff[7]);
- }
- else if(RTC_Set_ID==2)
- {
- OLED_16x16CN(32, 0, 17);//月
- OLED_16x16CN(48, 0, 18);//份
- OLED_16x16CN(64, 0, 14);//設
- OLED_16x16CN(80, 0, 15);//置
- }
- else if(RTC_Set_ID==3)
- {
- OLED_16x16CN(32, 0, 11);//日
- OLED_16x16CN(48, 0, 9);//期
- OLED_16x16CN(64, 0, 14);//設
- OLED_16x16CN(80, 0, 15);//置
- }
- else if(RTC_Set_ID==4)
- {
- OLED_16x16CN(32, 0, 13);//小
- OLED_16x16CN(48, 0, 12);//時
- OLED_16x16CN(64, 0, 14);//設
- OLED_16x16CN(80, 0, 15);//置
- OLED_16x32(0, 2, display_buff[8]);//時
- OLED_16x32(16, 2, display_buff[9]);
- OLED_16x32(32, 2, 10);//:
- OLED_16x32(48, 2, display_buff[10]);//分
- OLED_16x32(64, 2, display_buff[11]);
- OLED_16x32(80, 2, 10);//:
- OLED_16x32(96, 2, display_buff[12]);//秒
- OLED_16x32(112, 2, display_buff[13]);
- }
- else if(RTC_Set_ID==5)
- {
- OLED_16x16CN(32, 0, 19);//分
- OLED_16x16CN(48, 0, 20);//鐘
- OLED_16x16CN(64, 0, 14);//設
- OLED_16x16CN(80, 0, 15);//置
- }
- else if(RTC_Set_ID==6)
- {
- OLED_16x16CN(32, 0, 21);//秒
- OLED_16x16CN(48, 0, 22);//數
- OLED_16x16CN(64, 0, 14);//設
- OLED_16x16CN(80, 0, 15);//置
- }
- else if(RTC_Set_ID==7)
- {
- OLED_Fill(0x00);
- OLED_16x16CN(32, 0, 8);//星
- OLED_16x16CN(48, 0, 9);//期
- OLED_16x16CN(64, 0, 14);//設
- OLED_16x16CN(80, 0, 15);//置
- OLED_16x32(40, 2, 12);//星
- OLED_16x32(56, 2, 13);//期
- OLED_16x32(72, 2, display_buff[15]+13);
- }
- }
- }
- if(key2==0)
- {
- delayms(10);
- if(key2==0)
- {
- while(!key2);
- laba_flag=1; //蜂鳴器響一聲
- shezhi_display_flag=1;
- if(RTC_Set_ID==1)
- {
- if(RTC_Data[6]<99) //年+
- RTC_Data[6]++;
- display1();
- OLED_16x32(0, 2, display_buff[2]);//年
- OLED_16x32(16, 2, display_buff[3]);
- OLED_16x32(32, 2, 11);//-
- OLED_16x32(48, 2, display_buff[4]);//月
- OLED_16x32(64, 2, display_buff[5]);
- OLED_16x32(80, 2, 11);//-
- OLED_16x32(96, 2, display_buff[6]);//日
- OLED_16x32(112, 2, display_buff[7]);
- }
- else if(RTC_Set_ID==2)
- {
- if(RTC_Data[4]<12) //月+
- RTC_Data[4]++;
- display1();
- OLED_16x32(0, 2, display_buff[2]);//年
- OLED_16x32(16, 2, display_buff[3]);
- OLED_16x32(32, 2, 11);//-
- OLED_16x32(48, 2, display_buff[4]);//月
- OLED_16x32(64, 2, display_buff[5]);
- OLED_16x32(80, 2, 11);//-
- OLED_16x32(96, 2, display_buff[6]);//日
- OLED_16x32(112, 2, display_buff[7]);
- }
- else if(RTC_Set_ID==3)
- {
- if(RTC_Data[3]<31) //日+
- RTC_Data[3]++;
- display1();
- OLED_16x32(0, 2, display_buff[2]);//年
- OLED_16x32(16, 2, display_buff[3]);
- OLED_16x32(32, 2, 11);//-
- OLED_16x32(48, 2, display_buff[4]);//月
- OLED_16x32(64, 2, display_buff[5]);
- OLED_16x32(80, 2, 11);//-
- OLED_16x32(96, 2, display_buff[6]);//日
- OLED_16x32(112, 2, display_buff[7]);
- }
- else if(RTC_Set_ID==4)
- {
- if(RTC_Data[2]<23) //時+
- RTC_Data[2]++;
- display1();
- OLED_16x32(0, 2, display_buff[8]);//時
- OLED_16x32(16, 2, display_buff[9]);
- OLED_16x32(32, 2, 10);//:
- OLED_16x32(48, 2, display_buff[10]);//分
- OLED_16x32(64, 2, display_buff[11]);
- OLED_16x32(80, 2, 10);//:
- OLED_16x32(96, 2, display_buff[12]);//秒
- OLED_16x32(112, 2, display_buff[13]);
- }
- else if(RTC_Set_ID==5)
- {
- if(RTC_Data[1]<59) //分+
- RTC_Data[1]++;
- display1();
- OLED_16x32(0, 2, display_buff[8]);//時
- OLED_16x32(16, 2, display_buff[9]);
- OLED_16x32(32, 2, 10);//:
- OLED_16x32(48, 2, display_buff[10]);//分
- OLED_16x32(64, 2, display_buff[11]);
- OLED_16x32(80, 2, 10);//:
- OLED_16x32(96, 2, display_buff[12]);//秒
- OLED_16x32(112, 2, display_buff[13]);
- }
- else if(RTC_Set_ID==6)
- {
- if(RTC_Data[0]<59) //秒+
- RTC_Data[0]++;
- display1();
- OLED_16x32(0, 2, display_buff[8]);//時
- OLED_16x32(16, 2, display_buff[9]);
- OLED_16x32(32, 2, 10);//:
- OLED_16x32(48, 2, display_buff[10]);//分
- OLED_16x32(64, 2, display_buff[11]);
- OLED_16x32(80, 2, 10);//:
- OLED_16x32(96, 2, display_buff[12]);//秒
- OLED_16x32(112, 2, display_buff[13]);
- }
- else if(RTC_Set_ID==7)
- {
- if(RTC_Data[5]<7) //周+
- RTC_Data[5]++;
- display1();
- OLED_16x32(40, 2, 12);//星
- OLED_16x32(56, 2, 13);//期
- OLED_16x32(72, 2, display_buff[15]+13);
- }
- }
- }
- if(key3==0)
- {
- delayms(10);
- if(key3==0)
- {
- while(!key3);
- laba_flag=1; //蜂鳴器響一聲
- shezhi_display_flag=1;
- if(RTC_Set_ID==1)
- {
- if(RTC_Data[6]) //年-
- RTC_Data[6]--;
- display1();
- OLED_16x32(0, 2, display_buff[2]);//年
- OLED_16x32(16, 2, display_buff[3]);
- OLED_16x32(32, 2, 11);//-
- OLED_16x32(48, 2, display_buff[4]);//月
- OLED_16x32(64, 2, display_buff[5]);
- OLED_16x32(80, 2, 11);//-
- OLED_16x32(96, 2, display_buff[6]);//日
- OLED_16x32(112, 2, display_buff[7]);
- }
- else if(RTC_Set_ID==2)
- {
- if(RTC_Data[4]) //月-
- RTC_Data[4]--;
- display1();
- OLED_16x32(0, 2, display_buff[2]);//年
- OLED_16x32(16, 2, display_buff[3]);
- OLED_16x32(32, 2, 11);//-
- OLED_16x32(48, 2, display_buff[4]);//月
- OLED_16x32(64, 2, display_buff[5]);
- OLED_16x32(80, 2, 11);//-
- OLED_16x32(96, 2, display_buff[6]);//日
- OLED_16x32(112, 2, display_buff[7]);
- }
- else if(RTC_Set_ID==3)
- {
- if(RTC_Data[3]) //日-
- RTC_Data[3]--;
- display1();
- OLED_16x32(0, 2, display_buff[2]);//年
- OLED_16x32(16, 2, display_buff[3]);
- OLED_16x32(32, 2, 11);//-
- OLED_16x32(48, 2, display_buff[4]);//月
- OLED_16x32(64, 2, display_buff[5]);
- OLED_16x32(80, 2, 11);//-
- OLED_16x32(96, 2, display_buff[6]);//日
- OLED_16x32(112, 2, display_buff[7]);
- }
- else if(RTC_Set_ID==4)
- {
- if(RTC_Data[2]) //時-
- RTC_Data[2]--;
- display1();
- OLED_16x32(0, 2, display_buff[8]);//時
- OLED_16x32(16, 2, display_buff[9]);
- OLED_16x32(32, 2, 10);//:
- OLED_16x32(48, 2, display_buff[10]);//分
- OLED_16x32(64, 2, display_buff[11]);
- OLED_16x32(80, 2, 10);//:
- OLED_16x32(96, 2, display_buff[12]);//秒
- OLED_16x32(112, 2, display_buff[13]);
- }
- else if(RTC_Set_ID==5)
- {
- if(RTC_Data[1]) //分-
- RTC_Data[1]--;
- display1();
- OLED_16x32(0, 2, display_buff[8]);//時
- OLED_16x32(16, 2, display_buff[9]);
- OLED_16x32(32, 2, 10);//:
- OLED_16x32(48, 2, display_buff[10]);//分
- OLED_16x32(64, 2, display_buff[11]);
- OLED_16x32(80, 2, 10);//:
- OLED_16x32(96, 2, display_buff[12]);//秒
- OLED_16x32(112, 2, display_buff[13]);
- }
- else if(RTC_Set_ID==6)
- {
- if(RTC_Data[0]) //秒-
- RTC_Data[0]--;
- display1();
- OLED_16x32(0, 2, display_buff[8]);//時
- OLED_16x32(16, 2, display_buff[9]);
- OLED_16x32(32, 2, 10);//:
- OLED_16x32(48, 2, display_buff[10]);//分
- OLED_16x32(64, 2, display_buff[11]);
- OLED_16x32(80, 2, 10);//:
- OLED_16x32(96, 2, display_buff[12]);//秒
- OLED_16x32(112, 2, display_buff[13]);
- }
- else if(RTC_Set_ID==7)
- {
- if(RTC_Data[5]) //周-
- RTC_Data[5]--;
- display1();
- OLED_16x32(40, 2, 12);//星
- OLED_16x32(56, 2, 13);//期
- OLED_16x32(72, 2, display_buff[15]+13);
- }
- }
- }
- if(key4==0)
- {
- delayms(10);
- if(key4==0)
- {
- while(!key4);
- laba_flag=1; //蜂鳴器響一聲
- shezhi_display_flag=0;
- RTC_Save_Flag=1;
- }
- }
- //紅外遙控操作部分
- if(IrOK==1) //設置/遙控器C鍵
- {
- if(Im[2]==0x0d)
- {
- laba_flag=1; //蜂鳴器響一聲
- shezhi_display_flag=1;
- RTC_Set_ID++;
- if(RTC_Set_ID>=8)
- RTC_Set_ID=1;
- Open_Flag=1;
- display1();
- if(RTC_Set_ID==1)
- {
- OLED_Fill(0x00);
- OLED_16x16CN(32, 0, 16);//年
- OLED_16x16CN(48, 0, 18);//份
- OLED_16x16CN(64, 0, 14);//設
- OLED_16x16CN(80, 0, 15);//置
- OLED_16x32(0, 2, display_buff[2]);//年
- OLED_16x32(16, 2, display_buff[3]);
- OLED_16x32(32, 2, 11);//-
- OLED_16x32(48, 2, display_buff[4]);//月
- OLED_16x32(64, 2, display_buff[5]);
- OLED_16x32(80, 2, 11);//-
- OLED_16x32(96, 2, display_buff[6]);//日
- OLED_16x32(112, 2, display_buff[7]);
- }
- else if(RTC_Set_ID==2)
- {
- OLED_16x16CN(32, 0, 17);//月
- OLED_16x16CN(48, 0, 18);//份
- OLED_16x16CN(64, 0, 14);//設
- OLED_16x16CN(80, 0, 15);//置
- }
- else if(RTC_Set_ID==3)
- {
- OLED_16x16CN(32, 0, 11);//日
- OLED_16x16CN(48, 0, 9);//期
- OLED_16x16CN(64, 0, 14);//設
- OLED_16x16CN(80, 0, 15);//置
- }
- else if(RTC_Set_ID==4)
- {
- OLED_16x16CN(32, 0, 13);//小
- OLED_16x16CN(48, 0, 12);//時
- OLED_16x16CN(64, 0, 14);//設
- OLED_16x16CN(80, 0, 15);//置
- OLED_16x32(0, 2, display_buff[8]);//時
- OLED_16x32(16, 2, display_buff[9]);
- OLED_16x32(32, 2, 10);//:
- OLED_16x32(48, 2, display_buff[10]);//分
- OLED_16x32(64, 2, display_buff[11]);
- OLED_16x32(80, 2, 10);//:
- OLED_16x32(96, 2, display_buff[12]);//秒
- OLED_16x32(112, 2, display_buff[13]);
- }
- else if(RTC_Set_ID==5)
- {
- OLED_16x16CN(32, 0, 19);//分
- OLED_16x16CN(48, 0, 20);//鐘
- OLED_16x16CN(64, 0, 14);//設
- OLED_16x16CN(80, 0, 15);//置
- }
- else if(RTC_Set_ID==6)
- {
- OLED_16x16CN(32, 0, 21);//秒
- OLED_16x16CN(48, 0, 22);//數
- OLED_16x16CN(64, 0, 14);//設
- OLED_16x16CN(80, 0, 15);//置
- }
- else if(RTC_Set_ID==7)
- {
- OLED_Fill(0x00);
- OLED_16x16CN(32, 0, 8);//星
- OLED_16x16CN(48, 0, 9);//期
- OLED_16x16CN(64, 0, 14);//設
- OLED_16x16CN(80, 0, 15);//置
- OLED_16x32(40, 2, 12);//星
- OLED_16x32(56, 2, 13);//期
- OLED_16x32(72, 2, display_buff[15]+13);
- }
- }
- else if(Im[2]==0x40)//遙控器+鍵
- {
- laba_flag=1; //蜂鳴器響一聲
- shezhi_display_flag=1;
- if(RTC_Set_ID==1)
- {
- if(RTC_Data[6]<99) //年+
- RTC_Data[6]++;
- display1();
- OLED_16x32(0, 2, display_buff[2]);//年
- OLED_16x32(16, 2, display_buff[3]);
- OLED_16x32(32, 2, 11);//-
- OLED_16x32(48, 2, display_buff[4]);//月
- OLED_16x32(64, 2, display_buff[5]);
- OLED_16x32(80, 2, 11);//-
- OLED_16x32(96, 2, display_buff[6]);//日
- OLED_16x32(112, 2, display_buff[7]);
- }
- else if(RTC_Set_ID==2)
- {
- if(RTC_Data[4]<12) //月+
- RTC_Data[4]++;
- display1();
- OLED_16x32(0, 2, display_buff[2]);//年
- OLED_16x32(16, 2, display_buff[3]);
- OLED_16x32(32, 2, 11);//-
- OLED_16x32(48, 2, display_buff[4]);//月
- OLED_16x32(64, 2, display_buff[5]);
- OLED_16x32(80, 2, 11);//-
- OLED_16x32(96, 2, display_buff[6]);//日
- OLED_16x32(112, 2, display_buff[7]);
- }
- else if(RTC_Set_ID==3)
- {
- if(RTC_Data[3]<31) //日+
- RTC_Data[3]++;
- display1();
- OLED_16x32(0, 2, display_buff[2]);//年
- OLED_16x32(16, 2, display_buff[3]);
- OLED_16x32(32, 2, 11);//-
- OLED_16x32(48, 2, display_buff[4]);//月
- OLED_16x32(64, 2, display_buff[5]);
- OLED_16x32(80, 2, 11);//-
- OLED_16x32(96, 2, display_buff[6]);//日
- OLED_16x32(112, 2, display_buff[7]);
- }
- else if(RTC_Set_ID==4)
- {
- if(RTC_Data[2]<23) //時+
- RTC_Data[2]++;
- display1();
- OLED_16x32(0, 2, display_buff[8]);//時
- OLED_16x32(16, 2, display_buff[9]);
- OLED_16x32(32, 2, 10);//:
- OLED_16x32(48, 2, display_buff[10]);//分
- OLED_16x32(64, 2, display_buff[11]);
- OLED_16x32(80, 2, 10);//:
- OLED_16x32(96, 2, display_buff[12]);//秒
- OLED_16x32(112, 2, display_buff[13]);
- }
- else if(RTC_Set_ID==5)
- {
- if(RTC_Data[1]<59) //分+
- RTC_Data[1]++;
- display1();
- OLED_16x32(0, 2, display_buff[8]);//時
- OLED_16x32(16, 2, display_buff[9]);
- OLED_16x32(32, 2, 10);//:
- OLED_16x32(48, 2, display_buff[10]);//分
- OLED_16x32(64, 2, display_buff[11]);
- OLED_16x32(80, 2, 10);//:
- OLED_16x32(96, 2, display_buff[12]);//秒
- OLED_16x32(112, 2, display_buff[13]);
- }
- else if(RTC_Set_ID==6)
- {
- if(RTC_Data[0]<59) //秒+
- RTC_Data[0]++;
- display1();
- OLED_16x32(0, 2, display_buff[8]);//時
- OLED_16x32(16, 2, display_buff[9]);
- OLED_16x32(32, 2, 10);//:
- OLED_16x32(48, 2, display_buff[10]);//分
- OLED_16x32(64, 2, display_buff[11]);
- OLED_16x32(80, 2, 10);//:
- OLED_16x32(96, 2, display_buff[12]);//秒
- OLED_16x32(112, 2, display_buff[13]);
- }
- else if(RTC_Set_ID==7)
- {
- if(RTC_Data[5]<7) //周+
- RTC_Data[5]++;
- display1();
- OLED_16x32(40, 2, 12);//星
- OLED_16x32(56, 2, 13);//期
- OLED_16x32(72, 2, display_buff[15]+13);
- }
- }
- else if(Im[2]==0x19)//遙控器-鍵
- {
- laba_flag=1; //蜂鳴器響一聲
- shezhi_display_flag=1;
- if(RTC_Set_ID==1)
- {
- if(RTC_Data[6]) //年-
- RTC_Data[6]--;
- display1();
- OLED_16x32(0, 2, display_buff[2]);//年
- OLED_16x32(16, 2, display_buff[3]);
- OLED_16x32(32, 2, 11);//-
- OLED_16x32(48, 2, display_buff[4]);//月
- OLED_16x32(64, 2, display_buff[5]);
- OLED_16x32(80, 2, 11);//-
- OLED_16x32(96, 2, display_buff[6]);//日
- OLED_16x32(112, 2, display_buff[7]);
- }
- else if(RTC_Set_ID==2)
- {
- if(RTC_Data[4]) //月-
- RTC_Data[4]--;
- display1();
- OLED_16x32(0, 2, display_buff[2]);//年
- OLED_16x32(16, 2, display_buff[3]);
- OLED_16x32(32, 2, 11);//-
- OLED_16x32(48, 2, display_buff[4]);//月
- OLED_16x32(64, 2, display_buff[5]);
- OLED_16x32(80, 2, 11);//-
- OLED_16x32(96, 2, display_buff[6]);//日
- OLED_16x32(112, 2, display_buff[7]);
- }
- else if(RTC_Set_ID==3)
- {
- if(RTC_Data[3]) //日-
- RTC_Data[3]--;
- display1();
- OLED_16x32(0, 2, display_buff[2]);//年
- OLED_16x32(16, 2, display_buff[3]);
- OLED_16x32(32, 2, 11);//-
- OLED_16x32(48, 2, display_buff[4]);//月
- OLED_16x32(64, 2, display_buff[5]);
- OLED_16x32(80, 2, 11);//-
- OLED_16x32(96, 2, display_buff[6]);//日
- OLED_16x32(112, 2, display_buff[7]);
- }
- else if(RTC_Set_ID==4)
- {
- if(RTC_Data[2]) //時-
- RTC_Data[2]--;
- display1();
- OLED_16x32(0, 2, display_buff[8]);//時
- OLED_16x32(16, 2, display_buff[9]);
- OLED_16x32(32, 2, 10);//:
- OLED_16x32(48, 2, display_buff[10]);//分
- OLED_16x32(64, 2, display_buff[11]);
- OLED_16x32(80, 2, 10);//:
- OLED_16x32(96, 2, display_buff[12]);//秒
- OLED_16x32(112, 2, display_buff[13]);
- }
- else if(RTC_Set_ID==5)
- {
- if(RTC_Data[1]) //分-
- RTC_Data[1]--;
- display1();
- OLED_16x32(0, 2, display_buff[8]);//時
- OLED_16x32(16, 2, display_buff[9]);
- OLED_16x32(32, 2, 10);//:
- OLED_16x32(48, 2, display_buff[10]);//分
- OLED_16x32(64, 2, display_buff[11]);
- OLED_16x32(80, 2, 10);//:
- OLED_16x32(96, 2, display_buff[12]);//秒
- OLED_16x32(112, 2, display_buff[13]);
- }
- else if(RTC_Set_ID==6)
- {
- if(RTC_Data[0]) //秒-
- RTC_Data[0]--;
- display1();
- OLED_16x32(0, 2, display_buff[8]);//時
- OLED_16x32(16, 2, display_buff[9]);
- OLED_16x32(32, 2, 10);//:
- OLED_16x32(48, 2, display_buff[10]);//分
- OLED_16x32(64, 2, display_buff[11]);
- OLED_16x32(80, 2, 10);//:
- OLED_16x32(96, 2, display_buff[12]);//秒
- OLED_16x32(112, 2, display_buff[13]);
- }
- else if(RTC_Set_ID==7)
- {
- if(RTC_Data[5]) //周-
- RTC_Data[5]--;
- display1();
- OLED_16x32(40, 2, 12);//星
- OLED_16x32(56, 2, 13);//期
- OLED_16x32(72, 2, display_buff[15]+13);
- }
- }
- else if(Im[2]==0x15)//遙控器播放鍵
- {
- laba_flag=1;
- shezhi_display_flag=0;
- RTC_Save_Flag=1;
- }
- IrOK=0;
- }
- }
- void delayms(uint x)
- {
- uint i,j;
- for(i=x;i<0;i--)
- for(j=110;j<0;j--);
- }
- /*****************************************************************************************************
- ***********************************************oled12864函數******************************************
- *****************************************************************************************************/
- void oled_IIC_Start()//開始
- {
- oled_SCL = 1;
- oled_SDA = 1;
- oled_SDA = 0;
- oled_SCL = 0;
- }
- void oled_IIC_Stop()//停止
- {
- oled_SCL = 0;
- oled_SDA = 0;
- oled_SCL = 1;
- oled_SDA = 1;
- }
- void oled_Write_IIC_Byte(uchar IIC_Byte)//寫字節
- {
- uchar i;
- for(i=0;i<8;i++)
- {
- if(IIC_Byte & 0x80)
- oled_SDA=1;
- else
- oled_SDA=0;
- oled_SCL=1;
- oled_SCL=0;
- IIC_Byte<<=1;
- }
- oled_SDA=1;
- oled_SCL=1;
- oled_SCL=0;
- }
- void OLED_12864_Write_data(uint dat)//寫入數據
- {
- oled_IIC_Start();
- oled_Write_IIC_Byte(0x78);
- oled_Write_IIC_Byte(0x40);
- oled_Write_IIC_Byte(dat);
- oled_IIC_Stop();
- }
- void OLED_12864_Write_commomand(uint dat)//寫入命令
- {
- oled_IIC_Start();
- oled_Write_IIC_Byte(0x78);
- oled_Write_IIC_Byte(0x00);
- oled_Write_IIC_Byte(dat);
- oled_IIC_Stop();
- }
- void OLED_SetPos(uchar x, uchar y)//設置起始坐標
- {
- OLED_12864_Write_commomand(0xb0+y);
- OLED_12864_Write_commomand(((x&0xf0)>>4)|0x10);
- OLED_12864_Write_commomand((x&0x0f)|0x01);
- }
- void OLED_Fill(uchar bmp_dat)//屏幕填充
- {
- uchar y,x;
- for(y=0;y<8;y++)
- {
- OLED_12864_Write_commomand(0xb0+y);
- OLED_12864_Write_commomand(0x01);
- OLED_12864_Write_commomand(0x10);
- for(x=0;x<128;x++)
- OLED_12864_Write_data(bmp_dat);
- }
- }
- void OLED_12864_Init()//OLED12864初始化
- {
- delayms(500); //初始化之前的延時很重要!
- OLED_12864_Write_commomand(0xae);
- OLED_12864_Write_commomand(0x00);
- OLED_12864_Write_commomand(0x10);
- OLED_12864_Write_commomand(0x40);
- OLED_12864_Write_commomand(0x81);
- OLED_12864_Write_commomand(0xcf);
- OLED_12864_Write_commomand(0xa1);
- OLED_12864_Write_commomand(0xc8);
- OLED_12864_Write_commomand(0xa6);
- OLED_12864_Write_commomand(0xa8);
- OLED_12864_Write_commomand(0x3f);
- OLED_12864_Write_commomand(0xd3);
- OLED_12864_Write_commomand(0x00);
- OLED_12864_Write_commomand(0xd5);
- OLED_12864_Write_commomand(0x80);
- OLED_12864_Write_commomand(0xd9);
- OLED_12864_Write_commomand(0xf1);
- OLED_12864_Write_commomand(0xda);
- OLED_12864_Write_commomand(0x12);
- OLED_12864_Write_commomand(0xdb);
- OLED_12864_Write_commomand(0x40);
- OLED_12864_Write_commomand(0x20);
- OLED_12864_Write_commomand(0x02);
- OLED_12864_Write_commomand(0x8d);
- OLED_12864_Write_commomand(0x14);
- OLED_12864_Write_commomand(0xa4);
- OLED_12864_Write_commomand(0xa6);
- OLED_12864_Write_commomand(0xaf);
- OLED_Fill(0x00); //清屏
- OLED_SetPos(0,0);
- }
- void OLED_8x16Str(uchar x, uchar y, uchar ch)
- {
- uchar i=0;
- if(x>120)
- {
- x=0;y++;
- }
- OLED_SetPos(x,y);
- for(i=0;i<8;i++)
- {
- OLED_12864_Write_data(F8X16[ch*16+i]);
- }
- OLED_SetPos(x,y+1);
- for(i=0;i<8;i++)
- {
- OLED_12864_Write_data(F8X16[ch*16+i+8]);
- }
- }
- void OLED_16x16CN(uchar x,uchar y,uchar N)//顯示16*16的坐標(x,y),y為頁范圍0~7
- {
- uchar i=0;
- uint adder=32*N;
- OLED_SetPos(x,y);
- for(i=0;i<16;i++)
- {
- OLED_12864_Write_data(F16x16[adder]);
- adder+=1;
- }
- OLED_SetPos(x,y+1);
- for(i=0;i<16;i++)
- {
- OLED_12864_Write_data(F16x16[adder]);
- adder+=1;
- }
- }
- void OLED_16x32(uchar x, uchar y, uchar ch)//顯示8*16的坐標(x,y),y為頁范圍0~7
- {
- uchar i=0;
- if(x>120)
- {
- x=0;y++;
- }
- OLED_SetPos(x,y);
- for(i=0;i<16;i++)
- {
- OLED_12864_Write_data(F16X32[ch*64+i]);
- }
- OLED_SetPos(x,y+1);
- for(i=0;i<16;i++)
- {
- OLED_12864_Write_data(F16X32[ch*64+i+16]);
- }
- OLED_SetPos(x,y+2);
- for(i=0;i<16;i++)
- {
- OLED_12864_Write_data(F16X32[ch*64+i+32]);
- }
- OLED_SetPos(x,y+3);
- for(i=0;i<16;i++)
- {
- OLED_12864_Write_data(F16X32[ch*64+i+48]);
- }
- }
- /*****************************************************************************************************
- ***********************************************顯示函數***********************************************
- *****************************************************************************************************/
- void display1()
- {
- display_buff[0] =2;
- display_buff[1] =0;
- display_buff[2] =RTC_Data[6]/10;
- display_buff[3] =RTC_Data[6]%10; //年
-
- display_buff[4] =RTC_Data[4]/10;
- display_buff[5] =RTC_Data[4]%10; //月
-
- display_buff[6] =RTC_Data[3]/10;
- display_buff[7] =RTC_Data[3]%10; //日
-
- display_buff[8] =RTC_Data[2]/10;
- display_buff[9] =RTC_Data[2]%10; //時
-
- display_buff[10] =RTC_Data[1]/10;
- display_buff[11] =RTC_Data[1]%10; //分
-
- display_buff[12] =RTC_Data[0]/10;
- display_buff[13] =RTC_Data[0]%10; //秒
-
- display_buff[14] =RTC_Data[5]/10;
- display_buff[15] =RTC_Data[5]%10; //星期
- display_buff[16] =RTC_Data[7]/10;
- display_buff[17] =RTC_Data[7]%10; //溫度
- }
- void display2()
- {
- OLED_8x16Str(24, 0, display_buff[0]);// 年
- OLED_8x16Str(32, 0, display_buff[1]);
- OLED_8x16Str(40, 0, display_buff[2]);
- OLED_8x16Str(48, 0, display_buff[3]);
- OLED_8x16Str(56, 0, 10); // -
- OLED_8x16Str(64, 0, display_buff[4]);// 月
- OLED_8x16Str(72, 0, display_buff[5]);
- OLED_8x16Str(80, 0, 10); // -
- OLED_8x16Str(88, 0, display_buff[6]);// 日
- OLED_8x16Str(96, 0, display_buff[7]);
- OLED_16x32(0, 2, display_buff[8]);// 時
- OLED_16x32(16, 2, display_buff[9]);
- OLED_16x32(48, 2, display_buff[10]);// 分
- OLED_16x32(64, 2, display_buff[11]);
- OLED_16x32(96, 2, display_buff[12]);// 秒
- OLED_16x32(112, 2, display_buff[13]);
- if(RTC_Data[0]/1%2==0) OLED_16x32(32, 2, 10);// :
- else OLED_16x32(32, 2, 21);
- if(RTC_Data[0]/1%2==0) OLED_16x32(80, 2, 10);// :
- else OLED_16x32(80, 2, 21);
- OLED_16x16CN(8, 6, 8);// 星
- OLED_16x16CN(24, 6, 9);// 期
- OLED_16x16CN(40, 6, display_buff[15]);// ?
- OLED_8x16Str(96, 6, display_buff[16]);// 溫度
- OLED_8x16Str(104, 6, display_buff[17]);
- OLED_16x16CN(112, 6, 10);// ℃
- }
- /*****************************************************************************************************
- **********************************************DS3231函數**********************************************
- *****************************************************************************************************/
- void DS3231_IIC_delay()//IIC延時函數。延時4個機器周期。
- {
- _nop_();_nop_();_nop_();_nop_();
- }
- void DS3231_IIC_start()//起始信號
- {
- DS3231_IIC_SDA=1;
- DS3231_IIC_SCL=1;
- DS3231_IIC_delay();
- DS3231_IIC_SDA=0;
- DS3231_IIC_delay();
- DS3231_IIC_SCL=0;
- }
- void DS3231_IIC_stop()//停止信號
- {
- DS3231_IIC_SDA=0;
- DS3231_IIC_delay();
- DS3231_IIC_SCL=1;
- DS3231_IIC_delay();
- DS3231_IIC_SDA=1;
- DS3231_IIC_delay();
- }
- bit DS3231_IIC_Tack()//接收應答
- {
- bit ack;
- DS3231_IIC_SDA=1;
- DS3231_IIC_delay();
- DS3231_IIC_SCL=1;
- DS3231_IIC_delay();
- ack=DS3231_IIC_SDA;
- DS3231_IIC_delay();
- DS3231_IIC_SCL=0;
- DS3231_IIC_delay();
- return ack;
- }
- void DS3231_IIC_write_byte(uchar Data)//寫入數據函數
- {
- uchar i;
- for(i=0;i<8;i++)
- {
- if(Data&0x80)
- DS3231_IIC_SDA=1;
- else
- DS3231_IIC_SDA = 0;
- DS3231_IIC_delay();
- DS3231_IIC_SCL=1;
- DS3231_IIC_delay();
- DS3231_IIC_SCL=0;
- DS3231_IIC_delay();
- Data=Data<<1;
- }
- }
- uchar DS3231_IIC_read_byte()//讀取數據函數
- {
- uchar i;
- uchar Data;
- for(i=0;i<8;i++)
- {
- DS3231_IIC_SCL=1;
- DS3231_IIC_delay();
- Data=Data<<1;
- DS3231_IIC_delay();
-
- if(DS3231_IIC_SDA)
- Data=Data|0x01;
- DS3231_IIC_SCL=0;
- DS3231_IIC_delay();
- }
- return Data;
- }
- void DS3231_IIC_single_byte_write(uchar Waddr,uchar Data)//寫入數據函數
- {
- DS3231_IIC_start();
- DS3231_IIC_write_byte(0xd0);
- DS3231_IIC_Tack();
- DS3231_IIC_write_byte(Waddr);
- DS3231_IIC_Tack();
- DS3231_IIC_write_byte(Data);
- DS3231_IIC_Tack();
- DS3231_IIC_stop();
- }
- uchar DS3231_IIC_single_byte_read(uchar Waddr)//讀取數據函數
- {
- uchar Data;
- DS3231_IIC_start();
- DS3231_IIC_write_byte(0xd0);
- DS3231_IIC_Tack();
- DS3231_IIC_write_byte(Waddr);
- DS3231_IIC_Tack();
- DS3231_IIC_stop();
- DS3231_IIC_start();
- DS3231_IIC_write_byte(0xd1);
- DS3231_IIC_Tack();
- Data=DS3231_IIC_read_byte();
- DS3231_IIC_delay();
- DS3231_IIC_stop();
- return Data;
- }
- void DS3231_Read_time()//讀取時間
- {
- uchar i,tmp,tmp1,tmp2;
- for(i=0;i<8;i++)
- {
- tmp = DS3231_IIC_single_byte_read(rtc_address[i]);
- tmp1 = tmp/16;
- tmp2 = tmp%16;
- RTC_Data[i]=tmp1*10+tmp2;
- }
- }
- void DS3231_write_time()//寫入時間
- {
- uchar i,tmp;
- for(i=0;i<7;i++)
- {
- tmp=RTC_Data[i]/10;//BCD處理
- RTC_Data1[i]=RTC_Data[i]%10;
- RTC_Data1[i]=RTC_Data1[i]+tmp*16;
- }
- for(i=0;i<7;i++)
- {
- DS3231_IIC_single_byte_write(rtc_address[i],RTC_Data1[i]);
- }
- DS3231_IIC_single_byte_write(0x0e,0x0c);
- }
- void DS3231_Init()
- {
- DS3231_IIC_SDA=1;
- DS3231_IIC_SCL=1;
- DS3231_IIC_single_byte_write(0x0e,0x0c);
- }
- /*****************************************************************************************************
- *****************************************125us定時 中斷服務函數***************************************
- *****************************************************************************************************/
- void Time1() interrupt 3
- {
- static uchar timecount;
- TF1=0;
- TH1=0xff;//125us
- TL1=256-110;
-
- timecount++;
- if(timecount>=16)//1ms
- {
- timecount=0;
- T0_Flag=1;
- }
- if(laba_flag)
- {
- laba=~laba;
- }
- else
- laba=0;
- }
- /*****************************************************************************************************
- *************************************紅外解碼定時器程序***********************************************
- *****************************************************************************************************/
- void intersvr1(void) interrupt 2 using 1 //外部中斷解碼程序_外部中斷1 P3.3口
- {
- Tc=TH0*256+TL0;//提取中斷時間間隔時長
- TH0=0;
- TL0=0; //定時中斷重新置零
- if((Tc>Imin)&&(Tc<Imax))
- {
- m=0;
- f=1;
- return;
- } //找到啟始碼
- if(f==1)
- {
- if(Tc>Inum1&&Tc<Inum3)
- {
- Im[m/8]=Im[m/8]>>1|0x80; m++;
- }
- if(Tc>Inum2&&Tc<Inum1)
- {
- Im[m/8]=Im[m/8]>>1; m++; //取碼
- }
- if(m==32)
- {
- m=0;
- f=0;
- if(Im[2]==~Im[3])
- {
- IrOK=1;
- }
- else IrOK=0; //取碼完成后判斷讀碼是否正確
- }
- }
- }
復制代碼
所有資料51hei提供下載:
OLED_12864萬年歷.rar
(5.61 MB, 下載次數: 398)
2017-12-21 16:52 上傳
點擊文件名下載附件
下載積分: 黑幣 -5
|