[Atmel] 用AtmelStudio6.2跑mega16例程(64)DS1302时钟芯片

[复制链接]
1736|0
 楼主| ddllxxrr 发表于 2014-12-4 20:23 | 显示全部楼层 |阅读模式
DS1302是DALLAS公司推出的一种高性能、低功耗、带RAM的实时时钟芯片,它可以对年、月、日、周、时、分、秒进行计时,具有闰年补偿功能,最大有效年分可达2100年。
本例运行时,每隔0.5s读取DS1302实时是钏芯片时间数据,通过格式转换后显示在1602LCD上

Proteus运行时截图:


Atmel Studio6.2编译通过的截图:

程序清单:


主程序:

  1. /*
  2. * GccApplication27.c
  3. *
  4. * Created: 2014-12-4 18:47:28
  5. *  Author: Administrator
  6. */

  7. #define  F_CPU 1000000UL
  8. #include <avr/io.h>
  9. #include <util/delay.h>
  10. #include <string.h>
  11. #include <stdio.h>
  12. #include <stdint.h>

  13. extern void Initialize_LCD();
  14. extern void LCD_ShowString(uint8_t x,uint8_t y,char *str);

  15. extern void GetDateTime();
  16. extern uint8_t DateTime[];
  17. extern char *WEEK[];
  18. char LCD_DSY_BUFFER[17];

  19. int main()
  20. {
  21.         DDRA = 0xFF;
  22.         DDRC = 0xFF;
  23.         DDRD = 0xFF;
  24.         Initialize_LCD();
  25.         while(1)
  26.         {
  27.                 GetDateTime();
  28.                 //按格式:“DATA 00 - 00 - 00XX”显示年月日与星期
  29.                 sprintf(LCD_DSY_BUFFER,"DATE %02d - %02d - %02d%3s ",DateTime[6],DateTime[4],DateTime[3],WEEK[DateTime[5]-1]);
  30.                 LCD_ShowString(0,0,LCD_DSY_BUFFER);
  31.                 sprintf(LCD_DSY_BUFFER,"TIME %02d:% 02d:% 02d",DateTime[2],DateTime[1],DateTime[0]);
  32.                 LCD_ShowString(0,1,LCD_DSY_BUFFER);
  33.                 _delay_ms(100);
  34.         }
  35. }


时钟程序:

  1. #define  F_CPU 1000000UL
  2. #include <avr/io.h>
  3. #include <util/delay.h>
  4. #include <stdint.h>
  5. //DS1302引脚定义
  6. #define IO PA0
  7. #define SCLK PA1
  8. #define RST PA2

  9. //DS1302 端口定义
  10. #define DS_PORT PORTA
  11. #define DS_DDR DDRA
  12. #define DS_PIN PINA

  13. //DS1302端口数据读/写(方向)
  14. #define DDR_IO_RD() DS_DDR &= ~_BV(IO)
  15. #define DDR_IO_WR() DS_DDR |= _BV(IO)
  16. //DS1302 控制引脚操作定义
  17. #define WR_IO_0() DS_PORT &= ~_BV(IO)
  18. #define WR_IO_1() DS_PORT |= _BV(IO)
  19. #define RD_IO() (DS_PIN & _BV(IO))
  20. #define SCLK_1() DS_PORT |= _BV(SCLK)
  21. #define SCLK_0() DS_PORT &=~_BV(SCLK)
  22. #define RST_1() DS_PORT |= _BV(RST)
  23. #define RST_0() DS_PORT&=~_BV(RST)

  24. char * WEEK[] = {"SUN","MON","TUS","WEN","THU","FRI","SAT"};
  25.         
  26. uint8_t DateTime[7];

  27. void Write_Byte_TO_DS1302(uint8_t x)
  28. {
  29.         uint8_t i;
  30.         DDR_IO_WR();
  31.         for(i=0x01;i !=0x00;i<<=1)
  32.         {
  33.                 if(x & i)WR_IO_1();else WR_IO_0();SCLK_0();SCLK_1();
  34.         }
  35. }        

  36. uint8_t Get_Byte_FROM_DS1302()
  37. {
  38.         uint8_t i,dat = 0x00;
  39.         DDR_IO_RD();
  40.         for(i=0;i<8;i++)
  41.         {
  42.                 SCLK_1();SCLK_0();if (RD_IO()) dat |= _BV(i);
  43.         }
  44.         return dat/16*10 + dat % 16;
  45. }

  46. uint8_t Read_Data(uint8_t addr)
  47. {
  48.         uint8_t dat;
  49.         RST_1();
  50.         Write_Byte_TO_DS1302(addr);
  51.         dat = Get_Byte_FROM_DS1302();
  52.         RST_0();
  53.         return dat;
  54. }

  55. void GetDateTime()
  56. {
  57.         uint8_t i,addr = 0x81;
  58.         for(i = 0;i<7;i++)
  59.         {
  60.             DateTime[i] = Read_Data(addr);
  61.                 addr + = 2;        
  62.         }
  63. }

LCD显示程序:

  1. /*
  2. * GccApplication26.c
  3. *
  4. * Created: 2014-12-2 20:34:45
  5. *  Author: Administrator
  6. */

  7. #define F_CPU 1000000UL
  8. #include <avr/io.h>
  9. #include <util/delay.h>
  10. #include <stdint.h>

  11. #define RS PD0
  12. #define RW PD1
  13. #define E PD2

  14. #define LCD_CRTL_PORT PORTD
  15. #define LCD_PORT PORTC
  16. #define LCD_PIN PINC
  17. #define LCD_DDR DDRC

  18. #define RS_1() LCD_CRTL_PORT|=_BV(RS)
  19. #define RS_0() LCD_CRTL_PORT&=~_BV(RS)
  20. #define RW_1() LCD_CRTL_PORT|=_BV(RW)
  21. #define RW_0() LCD_CRTL_PORT&=~_BV(RW)
  22. #define EN_1() LCD_CRTL_PORT|=_BV(E)
  23. #define EN_0() LCD_CRTL_PORT&=~_BV(E)

  24. void LCD_BUSY_WAIT()
  25. {
  26.         RS_0(); RW_1();
  27.         LCD_DDR = 0x00;
  28.         EN_1(); _delay_us(20);
  29.         //loop_until_bit_is_clear(LCD_PIN,7);
  30.         EN_0();
  31.         LCD_DDR = 0xFF;
  32. }

  33. void Write_LCD_Command(uint8_t cmd)
  34. {
  35.         LCD_BUSY_WAIT();
  36.         RS_0(); RW_0();
  37.         LCD_PORT = cmd;
  38.         EN_1(); EN_0();
  39. }

  40. void Write_LCD_Data(uint8_t dat)
  41. {
  42.         LCD_BUSY_WAIT();
  43.         RS_1(); RW_0();
  44.         LCD_PORT = dat;
  45.         EN_1(); EN_0();
  46. }

  47. void Initialize_LCD()
  48. {
  49.         Write_LCD_Command(0x38);_delay_ms(15);
  50.         Write_LCD_Command(0x01);_delay_ms(15);
  51.         Write_LCD_Command(0x06);_delay_ms(15);
  52.         Write_LCD_Command(0x0c);_delay_ms(15);
  53.         
  54. }

  55. void LCD_ShowString(uint8_t x,uint8_t y,char *str)
  56. {
  57.         uint8_t i = 0;
  58.         if(y == 0)Write_LCD_Command(0x80 | x);
  59.         else if(y == 1)Write_LCD_Command(0xC0 | x);
  60.         for(i = 0; i<16 && str[i]!='\0';i++)
  61.         Write_LCD_Data(str[i]);
  62. }




本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?注册

×
您需要登录后才可以回帖 登录 | 注册

本版积分规则

个人签名:http://shop34182318.taobao.com/ http://shop562064536.taobao.com

2404

主题

7001

帖子

68

粉丝
快速回复 在线客服 返回列表 返回顶部