色婷婷AⅤ一区二区三区|亚洲精品第一国产综合亚AV|久久精品官方网视频|日本28视频香蕉

          新聞中心

          EEPW首頁 > 嵌入式系統(tǒng) > 設(shè)計應(yīng)用 > AVR128的程序——DS1302時鐘芯片

          AVR128的程序——DS1302時鐘芯片

          作者: 時間:2016-11-22 來源:網(wǎng)絡(luò) 收藏
          #include
          #include
          #define uchar unsigned char
          #define uint unsigned int
          #define ds1302_rst PE4 //定義1302的RST接在PE4
          #define ds1302_io PE3 //定義1302的IO接在PE3
          #define ds1302_sclk PE2 //定義1302的時鐘接在PE2
          #define set_ds1302_rst_ddr() DDRE|=1< //復(fù)位端置為輸出
          #define set_ds1302_rst() PORTE|=1< //復(fù)位端置1
          #define clr_ds1302_rst() PORTE&=~(1< //復(fù)位端清0
          #define set_ds1302_io_ddr() DDRE|=1< //數(shù)據(jù)端置為輸出
          #define set_ds1302_io() PORTE|=1< //數(shù)據(jù)端置1
          #define clr_ds1302_io() PORTE&=~(1< //數(shù)據(jù)端清0
          #define clr_ds1302_io_ddr() DDRE&=~(1< //數(shù)據(jù)端置為輸入
          #define in_ds1302_io() PINE&(1< //數(shù)據(jù)端輸入數(shù)據(jù)
          #define set_ds1302_sclk_ddr() DDRE|=1< //時鐘端置為輸出
          #define set_ds1302_sclk() PORTE|=1< //時鐘端置1
          #define clr_ds1302_sclk() PORTE &=~(1<//時鐘端清0
          #define ds1302_sec_add 0x80 //秒數(shù)據(jù)地址
          #define ds1302_min_add 0x82 //分?jǐn)?shù)據(jù)地址
          #define ds1302_hr_add 0x84 //時數(shù)據(jù)地址
          #define ds1302_date_add 0x86 //日數(shù)據(jù)地址
          #define ds1302_month_add 0x88 //月數(shù)據(jù)地址
          #define ds1302_day_add 0x8a //星期數(shù)據(jù)地址
          #define ds1302_year_add 0x8c //年數(shù)據(jù)地址
          #define ds1302_control_add 0x8e //控制數(shù)據(jù)地址
          #define ds1302_charger_add 0x90
          #define ds1302_clkburst_add 0xbe
          uchar TempData[8];
          uchar timer[7]={12,2,8,20,31,50,3};
          uchar WeiMa[8] = {0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80};
          uchar led_discode[11]={0xc0,0xf9,0xa4,0xb0,0x99,
          0x92,0x82,0xf8,0x80,0x90,0xff};
          void smg_io_init(void)
          {
          DDRA=0XFF;
          PORTA=0XFF;
          DDRE=0xff;
          PORTE=0X1C;
          DDRC=0XFF;
          PORTC=0XFF;
          DDRF|=0X0E;
          PORTF|=0X0E;
          PORTF&=0xF7;
          DDRB |=0X10;
          PORTB|=0X10;
          }
          void display(uchar FirstBit,uchar Num)
          {
          unsigned char i;
          for(i=0;i
          {
          PORTC=led_discode[10];
          PORTA=WeiMa[i+FirstBit];
          PORTC=TempData[i];
          Delayms(2);
          }
          }
          void Delayms(uint MS)
          {
          uint i,j;
          for( i=0;i
          for(j=0;j<1141;j++);
          }
          void shi_BCD(void)
          {
          uchar i,k;
          for(i=0;i<7;i++)
          {
          k=timer[i]/10;
          timer[i]=timer[i];
          timer[i]=timer[i]+k*16;
          }
          }
          void ds1302_init(void)
          {
          set_ds1302_rst_ddr();
          set_ds1302_sclk_ddr();
          clr_ds1302_rst();
          clr_ds1302_sclk();
          }
          void ds1302_write(uchar add,uchar data)
          {
          uchar i=0;
          set_ds1302_io_ddr(); //配置IO為輸出
          clr_ds1302_rst(); //清復(fù)位,停止所有操作
          clr_ds1302_sclk(); //清時鐘,準(zhǔn)備操作
          set_ds1302_rst(); //置復(fù)位,開始操作
          add = add & 0xFE;
          for(i=0;i<8;i++) //此循環(huán)寫入控制碼
          {
          if(add&0x01)
          set_ds1302_io(); //當(dāng)前位為1,置數(shù)據(jù)位
          else
          clr_ds1302_io(); //當(dāng)前位為0,清數(shù)據(jù)位
          set_ds1302_sclk(); //產(chǎn)生時鐘脈沖,寫入數(shù)據(jù)
          clr_ds1302_sclk();
          add>>=1; //移位,準(zhǔn)備寫入下1位
          }
          for(i=0;i<8;i++) //此循環(huán)寫入數(shù)據(jù)碼
          {
          if(data&0x01)
          set_ds1302_io();
          else
          clr_ds1302_io();
          set_ds1302_sclk();
          clr_ds1302_sclk();
          data>>=1;
          }
          clr_ds1302_rst();
          clr_ds1302_io_ddr();
          }
          uchar ds1302_read(uchar add)
          {
          uchar data=0;
          uchar i=0;
          add+=1;
          set_ds1302_io_ddr(); //端口輸出
          clr_ds1302_rst(); //清復(fù)位
          clr_ds1302_sclk(); //清時鐘
          set_ds1302_rst(); //置復(fù)位
          for(i=0;i<8;i++) //此循環(huán)寫入地址碼
          {
          if(add&0x01)
          {set_ds1302_io();}
          else
          {clr_ds1302_io();}
          set_ds1302_sclk();
          clr_ds1302_sclk();
          add>>=1;
          }
          clr_ds1302_io_ddr(); //端口輸入
          for(i=0;i<8;i++) //此循環(huán)讀出1302的數(shù)據(jù)
          {
          data>>=1;
          if(in_ds1302_io())
          {data|=0x80;}
          set_ds1302_sclk();
          clr_ds1302_sclk();
          }
          clr_ds1302_rst();
          return(data);
          }
          void ds1302_write_time(void)
          {
          ds1302_write(ds1302_control_add,0x00); //關(guān)閉寫保護
          ds1302_write(ds1302_sec_add,0x80); //暫停
          ds1302_write(ds1302_charger_add,0xa9); //涓流充電
          ds1302_write(ds1302_year_add,timer[0]); //年
          ds1302_write(ds1302_month_add,timer[1]); //月
          ds1302_write(ds1302_date_add,timer[2]); //日
          ds1302_write(ds1302_day_add,timer[6]); //周
          ds1302_write(ds1302_hr_add,timer[3]); //時
          ds1302_write(ds1302_min_add,timer[4]); //分
          ds1302_write(ds1302_sec_add,timer[5]); //秒
          ds1302_write(ds1302_control_add,0x80); //打開寫保護
          }
          void ds1302_read_time(void)
          {
          timer[0]=ds1302_read(ds1302_year_add); //年
          timer[1]=ds1302_read(ds1302_month_add); //月
          timer[2]=ds1302_read(ds1302_date_add); //日
          timer[6]=ds1302_read(ds1302_day_add); //周
          timer[3]=ds1302_read(ds1302_hr_add); //時
          timer[4]=ds1302_read(ds1302_min_add); //分
          timer[5]=ds1302_read(ds1302_sec_add); //秒
          }
          void time_Process(void)
          {
          TempData[0] = led_discode[timer[3]/16];
          TempData[1] = led_discode[timer[3]];
          TempData[2] = 0XBF;
          TempData[3] = led_discode[timer[4]/16];
          TempData[4] = led_discode[timer[4]];
          TempData[5] = 0XBF;
          TempData[6] = led_discode[timer[5]/16];
          TempData[7] = led_discode[timer[5]];
          }
          void main(void)
          {
          uchar i;
          smg_io_init();
          shi_BCD();
          ds1302_init();
          ds1302_write_time();
          while(1)
          {
          ds1302_read_time();
          time_Process();
          display(0,8);
          }
          }


          關(guān)鍵詞: AVR128DS1302時鐘芯

          評論


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

          關(guān)閉