<meter id="pryje"><nav id="pryje"><delect id="pryje"></delect></nav></meter>
          <label id="pryje"></label>

          新聞中心

          ATmega128(DS1302)

          作者: 時間:2016-11-10 來源:網(wǎng)絡(luò) 收藏
          //ATmega128DS1302實時時鐘芯片驅(qū)動程序

          //編譯環(huán)境 AVR Studio 4.17/AVR GCC
          //系統(tǒng)時鐘7.3728MHZ,設(shè)置熔絲位為外部高頻石英晶體振蕩,啟動時間4.1ms
          //作者:張奇zhangqi123zq@126.com
          //日期:2010.02.20

          本文引用地址:http://www.ex-cimer.com/article/201611/317433.htm

          //***********************************************************************
          //包含文件
          //***********************************************************************

          #include
          #include
          #define F_CPU7372800/* 單片機(jī)主頻為7.3728MHz,用于延時子程序 */
          #include
          #include <avr/io.h>
          #include
          //***********************************************************************
          //定義變量區(qū)
          //***********************************************************************
          #define uchar unsigned char
          #define uint unsigned int

          #define delay_us(x) _delay_us(x) //AVR GCC延時函數(shù) x(us)
          #define delay_ms(x) _delay_ms(x) //AVR GCC延時函數(shù) x(ms)
          /*復(fù)位腳*/
          #define RST_CLRPORTE &= ~(1 << PE7) //電平置低
          #define RST_SETPORTE |= (1 << PE7) //電平置高
          #define RST_INDDRE &= ~(1 << PE7) //方向輸入
          #define RST_OUTDDRE |= (1 << PE7) //方向輸出

          /*雙向數(shù)據(jù)*/
          #define IO_CLRPORTE &= ~(1 << PE6) //電平置低
          #define IO_SETPORTE |= (1 << PE6) //電平置高
          #define IO_RPINE & (1 << PE6) //電平讀取
          #define IO_INDDRE &= ~(1 << PE6) //方向輸入
          #define IO_OUTDDRE |= (1 << PE6) //方向輸出

          /*時鐘信號*/
          #define SCK_CLRPORTE &= ~(1 << PE5) //時鐘信號
          #define SCK_SETPORTE |= (1 << PE5) //電平置高
          #define SCK_INDDRE &= ~(1 << PE5) //方向輸入
          #define SCK_OUTDDRE |= (1 << PE5) //方向輸出

          /*ds1302命令與相關(guān)地址*/
          #define ds1302_sec_add0x80//秒數(shù)據(jù)地址
          #define ds1302_min_add0x82//分?jǐn)?shù)據(jù)地址
          #define ds1302_hr_add0x84//時數(shù)據(jù)地址
          #define ds1302_date_add0x86//日數(shù)據(jù)地址
          #define ds1302_month_add0x88//月數(shù)據(jù)地址
          #define ds1302_day_add0x8a//星期數(shù)據(jù)地址
          #define ds1302_year_add0x8c//年數(shù)據(jù)地址
          #define ds1302_control_add0x8e//控制數(shù)據(jù)地址
          #define ds1302_charger_add0x90
          #define ds1302_clkburst_add0xbe

          #define RS_CLRPORTF &= ~(1 << PF1) //RS置低
          #define RS_SETPORTF |= (1 << PF1) //RS置高

          #define RW_CLRPORTF &= ~(1 << PF2) //RW置低
          #define RW_SETPORTF |= (1 << PF2) //RW置高

          #define EN_CLRPORTF &= ~(1 << PF3) //E置低
          #define EN_SETPORTF |= (1 << PF3) //E置高

          #define Data_IO PORTA //液晶數(shù)據(jù)口
          #define Data_DDR DDRA //數(shù)據(jù)口方向寄存器
          #define D_LE0 PORTD &= ~(1 << PD4) //數(shù)碼管段控制位為0,鎖存端口數(shù)據(jù)
          #define D_LE1 PORTD |= (1 << PD4) //數(shù)碼管段控制位為1,鎖存器輸出與端口一致
          #define W_LE0 PORTD &= ~(1 << PD5) //數(shù)碼管位控制位為0
          #define W_LE1 PORTD |= (1 << PD5) //數(shù)碼管位控制位為1

          unsigned char time_buf[8] = {0x20,0x10,0x01,0x14,0x09,0x55,0x00,0x02};

          //*************************************************************************
          //初始化子程序
          //*************************************************************************
          void system_init()
          {

          Data_IO = 0xFF; //電平設(shè)置
          Data_DDR = 0xFF; //方向輸出
          PORTF = 0xFF; //電平設(shè)置
          DDRF = 0xFF; //方向輸出
          PORTD=0xFF;
          DDRD=0xFF;
          D_LE1; //關(guān)掉數(shù)碼管,以免顯示亂碼
          W_LE1;
          Data_IO=0xFF; //關(guān)數(shù)碼管
          W_LE0;
          }

          //***********************************************************************
          //向DS1302寫入一字節(jié)數(shù)據(jù)
          //***********************************************************************
          void ds1302_write_byte(unsigned char addr, unsigned char d)
          {

          unsigned char i;
          RST_SET;//啟動DS1302總線

          //寫入目標(biāo)地址:addr
          IO_OUT;
          addr = addr & 0xFE; //最低位置零
          for (i = 0; i < 8; i ++) {
          if (addr & 0x01) {
          IO_SET;
          }
          else {
          IO_CLR;
          }
          SCK_SET;
          SCK_CLR;
          addr = addr >> 1;
          }

          //寫入數(shù)據(jù):d
          IO_OUT;
          for (i = 0; i < 8; i ++) {
          if (d & 0x01) {
          IO_SET;
          }
          else {
          IO_CLR;
          }
          SCK_SET;
          SCK_CLR;
          d = d >> 1;
          }
          RST_CLR;//停止DS1302總線
          }
          //***********************************************************************
          // 從DS1302讀出一字節(jié)數(shù)據(jù)
          //***********************************************************************
          unsigned char ds1302_read_byte(unsigned char addr)
          {

          unsigned char i;
          unsigned char temp;
          RST_SET;//啟動DS1302總線

          //寫入目標(biāo)地址:addr
          IO_OUT;
          addr = addr | 0x01; //最低位置高
          for (i = 0; i < 8; i ++) {
          if (addr & 0x01) {
          IO_SET;
          }
          else {
          IO_CLR;
          }
          SCK_SET;
          SCK_CLR;
          addr = addr >> 1;
          }

          /*輸出數(shù)據(jù):temp*/
          IO_IN;
          for (i = 0; i < 8; i ++) {
          temp = temp >> 1;
          if (IO_R) {
          temp |= 0x80;
          }
          else {
          temp &= 0x7F;
          }
          SCK_SET;
          SCK_CLR;
          }

          RST_CLR;//停止DS1302總線
          return temp;
          }

          //***********************************************************************
          // 向DS302寫入時鐘數(shù)據(jù)
          //***********************************************************************

          void ds1302_write_time(void)
          {

          ds1302_write_byte(ds1302_control_add,0x00);//關(guān)閉寫保護(hù)
          ds1302_write_byte(ds1302_sec_add,0x80);//暫停
          //ds1302_write_byte(ds1302_charger_add,0xa9);//涓流充電
          ds1302_write_byte(ds1302_year_add,time_buf[1]);//年
          ds1302_write_byte(ds1302_month_add,time_buf[2]);//月
          ds1302_write_byte(ds1302_date_add,time_buf[3]);//日
          ds1302_write_byte(ds1302_day_add,time_buf[7]);//周
          ds1302_write_byte(ds1302_hr_add,time_buf[4]);//時
          ds1302_write_byte(ds1302_min_add,time_buf[5]);//分
          ds1302_write_byte(ds1302_sec_add,time_buf[6]);//秒
          ds1302_write_byte(ds1302_day_add,time_buf[7]);//周
          ds1302_write_byte(ds1302_control_add,0x80);//打開寫保護(hù)
          }

          //***********************************************************************
          // 從DS302讀出時鐘數(shù)據(jù)
          //***********************************************************************

          void ds1302_read_time(void)
          {

          time_buf[1]=ds1302_read_byte(ds1302_year_add);//年
          time_buf[2]=ds1302_read_byte(ds1302_month_add);//月
          time_buf[3]=ds1302_read_byte(ds1302_date_add);//日
          time_buf[4]=ds1302_read_byte(ds1302_hr_add);//時
          time_buf[5]=ds1302_read_byte(ds1302_min_add);//分
          time_buf[6]=(ds1302_read_byte(ds1302_sec_add))&0x7F;//秒
          time_buf[7]=ds1302_read_byte(ds1302_day_add);//周
          }

          //***********************************************************************
          // DS302初始化函數(shù)
          //***********************************************************************

          void ds1302_init(void)
          {

          RST_CLR;//RST腳置低
          SCK_CLR;//SCK腳置低
          RST_OUT;//RST腳設(shè)置為輸出
          SCK_OUT;//SCK腳設(shè)置為輸出
          }

          //***********************************************************************
          //顯示屏命令寫入函數(shù)
          //***********************************************************************


          void LCD_write_com(unsigned char com)
          {
          RS_CLR;
          RW_CLR;
          EN_SET;
          Data_IO = com;
          delay_ms(5);
          EN_CLR;
          }
          //***********************************************************************
          //顯示屏數(shù)據(jù)寫入函數(shù)
          //***********************************************************************

          void LCD_write_data(unsigned char data)
          {
          RS_SET;
          RW_CLR;
          EN_SET;
          Data_IO = data;
          delay_ms(5);
          EN_CLR;
          }
          //***********************************************************************
          //顯示屏清空顯示
          //***********************************************************************

          void LCD_clear(void)
          {
          LCD_write_com(0x01);
          delay_ms(5);
          }
          //***********************************************************************
          //顯示屏字符串寫入函數(shù)
          //***********************************************************************

          void LCD_write_str(unsigned char x,unsigned char y,unsigned char *s)
          {

          if (y == 0)
          {
          LCD_write_com(0x80 + x);
          }
          else
          {
          LCD_write_com(0xC0 + x);
          }

          while (*s)
          {
          LCD_write_data( *s);
          s ++;
          }
          }
          //***********************************************************************
          //顯示屏單字符寫入函數(shù)
          //***********************************************************************

          void LCD_write_char(unsigned char x,unsigned char y,unsigned char data)
          {

          if (y == 0)
          {
          LCD_write_com(0x80 + x);
          }
          else
          {
          LCD_write_com(0xC0 + x);
          }

          LCD_write_data( data);
          }
          //***********************************************************************
          //顯示屏初始化函數(shù)
          //***********************************************************************

          void LCD_init(void)
          {
          system_init();
          LCD_write_com(0x38);//顯示模式設(shè)置
          delay_ms(5);
          LCD_write_com(0x08);//顯示關(guān)閉
          delay_ms(5);
          LCD_write_com(0x01);//顯示清屏
          delay_ms(5);
          LCD_write_com(0x06);//顯示光標(biāo)移動設(shè)置
          delay_ms(5);
          LCD_write_com(0x0C);//顯示開及光標(biāo)設(shè)置
          delay_ms(5);
          }

          //***********************************************************************
          //主函數(shù)
          //***********************************************************************

          void main(void)

          {

          unsigned char temp;
          delay_ms(200);
          LCD_init(); //1602液晶初始化
          ds1302_init(); //DS1302初始化
          delay_ms(10);
          ds1302_write_time(); //向DS1302寫初始數(shù)據(jù),如年月和時間等

          while (1)
          {
          delay_ms(200);
          ds1302_read_time(); //讀DS1302數(shù)據(jù)

          temp = (time_buf[0] >> 4) + 0; //在LCD上寫要顯示的數(shù)據(jù)
          LCD_write_char(0, 0, temp);/*年*/
          temp = (time_buf[0] & 0x0F) + 0;
          LCD_write_char(1, 0, temp);
          temp = (time_buf[1] >> 4) + 0;
          LCD_write_char(2, 0, temp);
          temp = (time_buf[1] & 0x0F) + 0;
          LCD_write_char(3, 0, temp);
          LCD_write_char(4, 0, -);

          temp = (time_buf[2] >> 4) + 0;
          LCD_write_char(5, 0, temp);/*月*/
          temp = (time_buf[2] & 0x0F) + 0;
          LCD_write_char(6, 0, temp);
          LCD_write_char(7, 0, -);

          temp = (time_buf[3] >> 4) + 0;
          LCD_write_char(8, 0, temp);/*日*/
          temp = (time_buf[3] & 0x0F) + 0;
          LCD_write_char(9, 0, temp);

          temp = (time_buf[4] >> 4) + 0;
          LCD_write_char(8, 1, temp);/*時*/
          temp = (time_buf[4] & 0x0F) + 0;
          LCD_write_char(9, 1, temp);
          LCD_write_char(10, 1, :);

          temp = (time_buf[5] >> 4) + 0;
          LCD_write_char(11, 1, temp);/*分*/
          temp = (time_buf[5] & 0x0F) + 0;
          LCD_write_char(12, 1, temp);
          LCD_write_char(13, 1, :);

          temp = (time_buf[6] >> 4) + 0;
          LCD_write_char(14, 1, temp);/*秒*/
          temp = (time_buf[6] & 0x0F) + 0;
          LCD_write_char(15, 1, temp);
          }
          }



          關(guān)鍵詞: ATmega128DS130

          評論


          技術(shù)專區(qū)

          關(guān)閉
          看屁屁www成人影院,亚洲人妻成人图片,亚洲精品成人午夜在线,日韩在线 欧美成人 (function(){ var bp = document.createElement('script'); var curProtocol = window.location.protocol.split(':')[0]; if (curProtocol === 'https') { bp.src = 'https://zz.bdstatic.com/linksubmit/push.js'; } else { bp.src = 'http://push.zhanzhang.baidu.com/push.js'; } var s = document.getElementsByTagName("script")[0]; s.parentNode.insertBefore(bp, s); })();