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

          新聞中心

          EEPW首頁 > 嵌入式系統(tǒng) > 設計應用 > AVR128的程序——12864液晶與ds1302與ds18b20的綜合運用

          AVR128的程序——12864液晶與ds1302與ds18b20的綜合運用

          作者: 時間:2016-11-22 來源:網(wǎng)絡 收藏
          此程序包含子文件
          #include
          #include
          #include "12864lcd.h"
          #include "ds1302.h"
          #include "ds18B20.h"
          void mcu_init(void)
          {
          DDRA = 0XFF;
          PORTA = 0XFF;
          DDRC = 0XFF;
          PORTC = 0XFF;
          DDRF |= 0X0E;
          PORTF |= 0X0E;
          PORTF &= 0xF0;
          DDRB |= 0X10;
          PORTB |= 0X10;
          psbset;
          rsout;
          rwout;
          enout;
          }
          void main(void)
          {
          uchar i,j;
          uint k;
          mcu_init();
          shi_BCD();
          ds1302_init();
          ds1302_write_time();
          init12864lcd();
          display();
          display1();
          display2();
          display3();
          while(1)
          {
          ds1302_read_time();
          time_Process();
          write_sfm(0x8D,num);
          write_sfm(0x8B,fen);
          write_sfm(0x89,shi);
          write_sfm(0x86,ri);
          write_sfm(0x84,yue);
          write_sfm(0x92,zhou);
          ds1820_reset();
          ds1820_write_byte(0xCC);
          ds1820_write_byte(0x44);
          Delayms(2);
          ds1820_reset();
          ds1820_write_byte(0xCC);
          ds1820_write_byte(0xbe);
          i=ds1820_read_byte();
          j=ds1820_read_byte();
          k=j*256+i; //這里k的值會大于255 所以定義的時候要寫成uint型,否則會溢出。
          k=k*0.0625;
          write_wendu(0x96,k);
          }
          }
          #ifndef __12864LCD_H__
          #define __12864LCD_H__
          #define uint unsigned int
          #define uchar unsigned char
          #define psbset DDRB |= (1<<4)
          #define psbset PORTB |= (1<<4)
          #define psbclr PORTB &=~(1<<4)
          #define rsout DDRB |= (1<<5)
          #define rsset PORTB |=(1<<5)
          #define rsclr PORTB &=~(1<<5)
          #define rwout DDRB |= (1<<6)
          #define rwset PORTB |= (1<<6)
          #define rwclr PORTB &= ~(1<<6)
          #define enout DDRB |= (1<<7)
          #define enset PORTB |= (1<<7)
          #define enclr PORTB &= ~(1<<7)
          unsigned char table1[]=" 2012年02月04日";
          unsigned char table2[]="星期六 溫度 0℃";
          unsigned char table3[]=" 00: 00: 00";
          unsigned char table4[]=" 科嵌電子創(chuàng)作";
          uint num=50,fen=9,shi=8,ri=8,yue=2,zhou=3;
          void Delayus(uint US)
          {
          uint i;
          US=US*5/4;
          for( i=0;i
          }
          void Delayms(uint MS)
          {
          uint i,j;
          for( i=0;i
          for(j=0;j<1141;j++);
          }
          void write_12864com(uchar com)
          {
          rwclr;
          rsclr;
          Delayus(500);
          PORTA=com;
          enset;
          Delayus(1000);
          enclr;
          Delayus(1000);
          }
          void write_12864dat(uchar dat)
          {
          rwclr;
          rsset;
          Delayus(500);
          PORTA=dat;
          enset;
          Delayus(1000);
          enclr;
          Delayus(1000);
          }
          void init12864lcd(void)
          {
          Delayms(500);
          write_12864com(0x30);
          Delayus(500);
          write_12864com(0x30);
          Delayus(500);
          write_12864com(0x0f);
          Delayus(500);
          write_12864com(0x01);
          Delayms(25);
          write_12864com(0x06);
          Delayus(2500);
          write_12864com(0x0c);
          Delayus(500);
          }
          void display(void)
          {
          uchar i;
          write_12864com(0x80);
          for(i=0;i<16;i++)
          {
          write_12864dat(table1[i]);
          Delayms(10);
          }
          }
          void display1(void)
          {
          uchar i;
          write_12864com(0x90);
          for(i=0;i<16;i++)
          {
          write_12864dat(table2[i]);
          Delayms(10);
          }
          }
          void display2(void)
          {
          uchar i;
          write_12864com(0x88);
          for(i=0;i<12;i++)
          {
          write_12864dat(table3[i]);
          Delayms(10);
          }
          }
          void display3(void)
          {
          uchar i;
          write_12864com(0x98);
          for(i=0;i<14;i++)
          {
          write_12864dat(table4[i]);
          Delayms(10);
          }
          }
          void write_sfm(uchar add,uchar date)
          {
          uchar shi,ge;
          shi=date/10;
          ge=date;
          write_12864com(add);
          write_12864dat(0x30+shi);
          write_12864dat(0x30+ge);
          }
          void write_wendu(uchar add,uchar date)
          {
          uchar shi,ge;
          shi=date/10;
          ge=date;
          write_12864com(add);
          write_12864dat(0x30+shi);
          write_12864dat(0x30+ge);
          }
          #endif
          #ifndef __DS1302_H__
          #define __DS1302_H__
          #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< //復位端置為輸出
          #define set_ds1302_rst() PORTE|=1< //復位端置1
          #define clr_ds1302_rst() PORTE&=~(1< //復位端清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 //分數(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 timer[7]={12,2,29,23,59,50,3};
          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(); //清復位,停止所有操作
          clr_ds1302_sclk(); //清時鐘,準備操作
          set_ds1302_rst(); //置復位,開始操作
          add = add & 0xFE;
          for(i=0;i<8;i++) //此循環(huán)寫入控制碼
          {
          if(add&0x01)
          set_ds1302_io(); //當前位為1,置數(shù)據(jù)位
          else
          clr_ds1302_io(); //當前位為0,清數(shù)據(jù)位
          set_ds1302_sclk(); //產(chǎn)生時鐘脈沖,寫入數(shù)據(jù)
          clr_ds1302_sclk();
          add>>=1; //移位,準備寫入下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(); //清復位
          clr_ds1302_sclk(); //清時鐘
          set_ds1302_rst(); //置復位
          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)
          {
          uchar temp1,temp2,temp3,temp4,temp5,temp6;
          temp1 = timer[3]/16;
          shi = timer[3]+10*temp1;
          temp2 = timer[4]/16;
          fen = timer[4]+10*temp2;
          temp3 = timer[5]/16;
          num = timer[5]+10*temp3;
          temp4 = timer[2]/16;
          ri = timer[2]+10*temp4;
          temp5 = timer[1]/16;
          yue = timer[1]+10*temp5;
          temp6 = timer[6]/16;
          zhou = timer[6]+10*temp6;
          }
          #endif
          #ifndef __DS18B20_H__
          #define __DS18B20_H__
          #define uchar unsigned char
          #define uint unsigned int
          #define DQ_IN DDRE &= ~(1 << PE6)
          #define DQ_OUT DDRE |= (1 << PE6)
          #define DQ_CLR PORTE &= ~(1 << PE6)
          #define DQ_SET PORTE |= (1 << PE6)
          #define DQ_R PINE & (1 << PE6)
          unsigned char ds1820_reset(void)
          {
          unsigned char i;
          DQ_OUT;
          DQ_CLR;
          Delayus(500);
          DQ_SET;
          DQ_IN;
          Delayus(100);
          i = DQ_R;
          Delayus(500);
          return i;
          }
          unsigned char ds1820_read_byte(void)
          {
          unsigned char i;
          unsigned char value = 0;
          for (i = 0; i < 8; i++)
          {
          value >>= 1;
          DQ_OUT;
          DQ_CLR;
          Delayus(10);
          DQ_SET;
          DQ_IN;
          if (DQ_R) value|=0x80;
          Delayus(60);
          }
          return value;
          }
          void ds1820_write_byte(unsigned char value)
          {
          unsigned char i;
          for (i = 0; i < 8; i++)
          {
          DQ_OUT;
          DQ_CLR;
          Delayus(10);
          if (value & 0x01) DQ_SET;
          Delayus(100);
          DQ_SET;
          value >>= 1;
          }
          }
          #endif



          關(guān)鍵詞: AVR12812864液晶ds1302ds18b2

          評論


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

          關(guān)閉