|
IMG_20220324_101047.jpg (3.02 MB, 下載次數(shù): 68)
下載附件
2022-3-24 10:12 上傳
單片機(jī)源程序如下:- #include<reg52.h>
- #include<intrins.h>
- #define uchar unsigned char
- #define uint unsigned int
- #define u8 uint8_t
- #define u16 uint16_t
- #define u32 uint32_t
- typedef unsigned char uint8_t;
- typedef unsigned short uint16_t;
- typedef unsigned long uint32_t;
- #define DS3231_WriteAddress 0xD0
- #define DS3231_ReadAddress 0xD1
- #define Delay(us) {_nop_();_nop_();_nop_();_nop_();}
- typedef struct
- {
- u8 hour;
- u8 min;
- u8 sec;
- u32 w_year;
- u8 w_month;
- u8 w_date;
- u8 week;
- u8 temper_H;
- u8 temper_L;
- }Calendar_OBJ;
- extern Calendar_OBJ calendar; //日歷結(jié)構(gòu)體
- extern u8 const mon_table[12]; //月份日期數(shù)據(jù)表
- //*******GPIO 定義********************************************************
- sbit TM1640_DIN=P1^3; //TM1640數(shù)據(jù)線
- sbit TM1640_SCLK=P1^4; //TM1640時鐘線
- sbit SDA=P3^3; //IIC數(shù)據(jù)線
- sbit SCL=P3^4; //IIC時鐘線
- sbit SDA_read=P3^3;
- sbit SCL_read=P3^4;
- //************************************************************************************************
- #define TM1640MEDO_ADD 0x44 //地址模式的設(shè)置0x40自動加一模式 0x44固定地址模式
- #define TM1640MEDO_DISPLAY 0x8C //設(shè)置亮度小:0x88 0x89 0x8a 0x8b 0x8c 0x8d 0x8f最大0x80關(guān)閉
- #define TM1640MEDO_DISPLAY_OFF 0x80 //宏定義 關(guān)亮度設(shè)置
- //**************************************************************************************************
- //功能:延時1毫秒 ,入口參數(shù):x ,說明:當(dāng)晶振為12M時,j<112;當(dāng)晶振為11.0592M時,j<122*/
- //***************************************************************************************************
- void Delay_xms(uint x)
- {
- uint i,j;
- for(i=0;i<x;i++)
- for(j=0;j<112;j++);
- }
- //功能:12us延時,STC89C52為1T單片機(jī),即1個時鐘/機(jī)器周期,速度為AT89C52的12倍
- void Delay_us(uint t)
- {
- for(;t>0;t--)
- {
- _nop_();
- }
- }
- //*********TM1640驅(qū)動函數(shù) START***************
- //功能:12us延時
- //***************************************************************************************
- //通信時序 啟始
- void TM1640_start()
- {
- TM1640_DIN=1;
- TM1640_SCLK=1;
- Delay_us(1);
- TM1640_DIN=0;
- Delay_us(1);
- TM1640_SCLK=0;
- Delay_us(1);
- }
- //通信時序 結(jié)束
- void TM1640_stop()
- {
- TM1640_DIN=0;
- TM1640_SCLK=1;
- Delay_us(1);
- TM1640_DIN=1;
- Delay_us(1);
- }
- //寫數(shù)據(jù)
- void TM1640_write(u8 date)
- {
- u8 i;
- u8 aa;
- aa=date;
- TM1640_DIN=0;
- TM1640_SCLK=0;
- for(i=0;i<8;i++){
- TM1640_SCLK=0;
- Delay_us(1);
- if(aa&0x01){
- TM1640_DIN=1;
- Delay_us(1);
- }else{
- TM1640_DIN=0;
- Delay_us(1);
- }
- TM1640_SCLK=1;
- Delay_us(1);
- aa=aa>>1;
- }
- TM1640_DIN=0;
- TM1640_SCLK=0;
- }
- //TM1640接口初始化函數(shù)//TM1640設(shè)置
- void TM1640_Init(void)
- {
- TM1640_start();
- TM1640_write(TM1640MEDO_ADD); //設(shè)置工作模式
- TM1640_stop();
- TM1640_start();
- TM1640_write(TM1640MEDO_DISPLAY);//設(shè)置顯示亮度
- TM1640_stop();
- }
- //固定地址模式的顯示輸出函數(shù)
- void TM1640_display(u8 address,u8 date)
- {
- const u8 buff[12]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x40,0x00};//數(shù)字0~9及”-“”無”顯示段碼表
- //-- 0 1 2 3 4 5 6 7 8 9 - 無
- TM1640_start();
- TM1640_write(0xc0+address); //傳顯示數(shù)據(jù)對應(yīng)的地址,0-15位數(shù)碼管
- TM1640_write(buff[date]); //傳1BYTE顯示數(shù)據(jù),對應(yīng)顯示buff[12]數(shù)組0-11
- TM1640_stop();
- }
- //**************************************tm1640函數(shù)*end***********************************
-
- /**************************************IIC函數(shù)************************************
- * 函數(shù)名: void I2C_delay(void)
- * 描述 : 短暫延時
- * 說明 : 內(nèi)部定義的i可以優(yōu)化速度,經(jīng)測試最低到5還能寫入
- ***************************************************************************/
- /*
- static void I2C_delay(void)
- {
- u8 i=5;
- while(i)
- {
- i--;
- }
- }
- */
- /**************************************************************************
- * 函數(shù)名: void I2C_Start(void)
- * 描述 : 起始信號
- ***************************************************************************/
- void I2C_Start(void)
- {
- SCL=1; Delay_us(5);
- SDA=1; Delay_us(5);
- SDA=0; Delay_us(5);
- SCL=0; Delay_us(5);
- }
- /**************************************************************************
- * 函數(shù)名: I2C_Stop(void)
- * 描述 : 終止信號
- ***************************************************************************/
- void I2C_Stop(void)
- {
- SDA=0; Delay_us(5);
- SCL=1; Delay_us(5);
- SDA=1; Delay_us(5);
- }
- /**************************************************************************
- * 函數(shù)名: void I2C_Ack(void)
- * 描述 : 應(yīng)答信號
- ***************************************************************************/
- void I2C_Ack(void)
- {
- SCL=0; Delay_us(5);
- SDA=0; Delay_us(5);
- SCL=1; Delay_us(5);
- SCL=0; Delay_us(5);
- }
- /**************************************************************************
- * 函數(shù)名: void I2C_NoAck(void)
- * 描述 : 無應(yīng)答信號
- ***************************************************************************/
- void I2C_NoAck(void)
- {
- SCL=0; Delay_us(5);
- SDA=1; Delay_us(5);
- SCL=1; Delay_us(5);
- SCL=0; Delay_us(5);
- }
- /**************************************************************************
- * 函數(shù)名: u8 I2C_WaitAck(void)
- * 描述 : 等待應(yīng)答信號
- * 輸出 : TRUE : 有應(yīng)答 FALSE : 無應(yīng)答
- ***************************************************************************/
- u8 I2C_WaitAck(void)
- {
- u8 ucErrTime=0;
- SCL=0; Delay_us(5);
- SDA=1; Delay_us(5);
- SCL=1; Delay_us(5);
- while(SDA_read)
- {
- ucErrTime++;
- if(ucErrTime>250)
- {
- I2C_Stop();
- return 0;
- }
- }
- SCL=0;
- return 1;
- }
- /**************************************************************************
- * 函數(shù)名: void I2C_SendByte(u8 SendByte)
- * 描述 : 發(fā)送一個字節(jié)
- * 輸入 : SendByte : 字節(jié)數(shù)據(jù) ,說明 : 數(shù)據(jù)從高位到低位
- ***************************************************************************/
- void I2C_SendByte(u8 SendByte)
- {
- u8 i=8;
- while(i--)
- {
- SCL=0;
- Delay_us(5);
-
- if(SendByte & 0x80)
- SDA=1;
- else
- SDA=0;
- SendByte<<=1;
- Delay_us(5);
-
- SCL=1;
- Delay_us(3);
- }
- SCL=0;
- }
- /**************************************************************************
- * 函數(shù)名: u8 I2C_ReceiveByte(void)
- * 描述 : 讀取一個字節(jié)
- * 輸出 : 字節(jié)數(shù)據(jù)
- * 說明 : ReceiveByte : 數(shù)據(jù)從高位到低位
- ***************************************************************************/
- u8 I2C_ReceiveByte(void)
- {
- u8 i=8;
- u8 ReceiveByte=0;
-
- SDA=1;
- while(i--)
- {
- ReceiveByte<<=1;
- SCL=0;
- Delay_us(5);
- SCL=1;
- Delay_us(5);
- if(SDA_read)
- {
- ReceiveByte|=0x01;
- }
- }
- SCL=0;
- return ReceiveByte;
- }
- //**************************IIC END*****************************************************************
- //****************ds3231函數(shù)*******************************************************************
- Calendar_OBJ calendar;
- //bcd轉(zhuǎn)hex
- u8 BCD2HEX(u8 val)
- {
- u8 i;
- i= val&0x0f;
- val >>= 4;
- val &= 0x0f;
- val *= 10;
- i += val;
-
- return i;
- }
- u16 B_BCD(u8 val)
- {
- u8 i,j,k;
- i=val/10;
- j=val%10;
- k=j+(i<<4);
- return k;
- }
- void DS3231_WR_Byte(u8 addr,u8 bytedata)
- {
- I2C_Start();
- I2C_SendByte(DS3231_WriteAddress);
- I2C_WaitAck();
- I2C_SendByte(addr);
- I2C_WaitAck();
- I2C_SendByte(bytedata);
- I2C_WaitAck();
- I2C_Stop();
- }
- u8 DS3231_RD_Byte(u8 addr)
- {
- u8 Dat=0;
- I2C_Start();
- I2C_SendByte(DS3231_WriteAddress);
- I2C_WaitAck();
- I2C_SendByte(addr);
- I2C_WaitAck();
- I2C_Start();
- I2C_SendByte(DS3231_ReadAddress);
- I2C_WaitAck();
- Dat=I2C_ReceiveByte();
- I2C_Stop();
- return Dat;
- }
- //ds3231初始化函數(shù)
- void DS3231_Init(void)
- {
- //I2C_GPIO_Config();
- DS3231_WR_Byte(0x0e,0);
- DS3231_WR_Byte(0x0f,0x0);
- }
- //DS3231初始化設(shè)置函數(shù)( 年 月 日 時 分 秒 周)
- void Set_DS3231_Time(u8 yea,u8 mon,u8 da,u8 hou,u8 min,u8 sec,u8 week)
- {
- u8 temp=0;
- temp=B_BCD(yea);
- DS3231_WR_Byte(0x06,temp);
- temp=B_BCD(mon);
- DS3231_WR_Byte(0x05,temp);
- temp=B_BCD(da);
- DS3231_WR_Byte(0x04,temp);
- temp=B_BCD(hou);
- DS3231_WR_Byte(0x02,temp);
- temp=B_BCD(min);
- DS3231_WR_Byte(0x01,temp);
- temp=B_BCD(sec);
- DS3231_WR_Byte(0x00,temp);
- temp=B_BCD(week);
- DS3231_WR_Byte(0x03,temp);
- }
- void Get_DS3231_Time(void)
- {
- calendar.w_year=DS3231_RD_Byte(0x06);
- calendar.w_year=BCD2HEX(calendar.w_year);
- calendar.w_month=DS3231_RD_Byte(0x05);
- calendar.w_month=BCD2HEX(calendar.w_month);
- calendar.w_date=DS3231_RD_Byte(0x04);
- calendar.w_date=BCD2HEX(calendar.w_date);
-
- calendar.hour=DS3231_RD_Byte(0x02);
- calendar.hour&=0x3f;
- calendar.hour=BCD2HEX(calendar.hour);
- calendar.min=DS3231_RD_Byte(0x01);
- calendar.min=BCD2HEX(calendar.min);
- calendar.sec=DS3231_RD_Byte(0x00);
- calendar.sec=BCD2HEX(calendar.sec);
- calendar.week=DS3231_RD_Byte(0x03);
- calendar.week=BCD2HEX(calendar.week);
- DS3231_WR_Byte(0x0e,0x20);
- calendar.temper_H=DS3231_RD_Byte(0x11);
- calendar.temper_L=(DS3231_RD_Byte(0x12)>>6)*25;
- }
- //****************ds3231函數(shù)*********end*******************************************************************
- int main(void)
- {
-
- //Set_DS3231_Time(22,03,24,09,53,00,4);//22年03月23號17點(diǎn)55分00秒周3 .第1次初始化DS3231使用后注銷
- Delay_us(100);
- DS3231_Init();
- TM1640_Init(); //TM1640初始化
- while(1)
- {
- Get_DS3231_Time();
- {
- TM1640_display(0,calendar.hour/10); //第1個數(shù)碼管顯示時十位
- TM1640_display(1,calendar.hour%10); //第2個數(shù)碼管顯示時個位
- TM1640_display(2,10); //此位數(shù)碼管顯示"-"
- TM1640_display(3,calendar.min/10); //第4個數(shù)碼管顯示分十位
- TM1640_display(4,calendar.min%10); //第5個數(shù)碼管顯示分個位
- TM1640_display(5,10); //此位數(shù)碼管顯示"-"
- TM1640_display(6,calendar.sec/10); //第7個數(shù)碼管顯示秒十位
- TM1640_display(7,calendar.sec%10); //第8個數(shù)碼管顯示秒個位
- }
- }
- }
復(fù)制代碼
51hei.png (6.02 KB, 下載次數(shù): 74)
下載附件
2022-3-24 17:55 上傳
Keil代碼下載:
Keil代碼.7z
(21.58 KB, 下載次數(shù): 102)
2022-3-24 17:55 上傳
點(diǎn)擊文件名下載附件
下載積分: 黑幣 -5
|
評分
-
查看全部評分
|