[AT32L021] 【AT-START-L021测评】+DS1302读写

[复制链接]
 楼主| 比神乐 发表于 2024-11-30 18:48 | 显示全部楼层 |阅读模式
一开始读写DS1302显示全0,后来发现初始化错了,本来应该是A组管脚,我初始化成了B组,导致失败。
现在成功了。
代码:
  1. #include "at32l021_board.h"
  2. #include "at32l021_clock.h"
  3. #include "OLED.h"
  4. /** @addtogroup AT32L021_periph_examples
  5.   * @{
  6.   */
  7. #define uint unsigned int
  8. #define u8  unsigned char

  9. u8 const write_addr[]={0x80,0x82,0x84,0x86,0x88,0x8a,0x8c};   //写的地址
  10. u8 const read_addr[]={0x81,0x83,0x85,0x87,0x89,0x8b,0x8d};   //读的地址

  11. u8 const read[7]={0x81,0x83,0x85,0x87,0x89,0x8b,0x8d}; //读的地址
  12. u8 const write[7]={0x80,0x82,0x84,0x86,0x88,0x8a,0x8c};//秒分时日月周年,写的地址
  13. u8 time[7]={0x40,0x59,0x23,0x31,0x12,0x07,0x23};//存读的数据
  14. //MS延时函数
  15. void MS_delay(uint MS)
  16. {
  17.     uint x,y;
  18.     for(y=MS;y>0;y--)
  19.     {
  20.             for(x=796;x>0;x--);
  21.     }
  22. }
  23. //---存储顺序是秒分时日月周年,存储格式是用BCD码---//
  24. //uchar time[7]={0,0,0x12,0x12,0x12,0x20,0x15};  //初始化的时间为2015年12月12日12:00

  25. void Ds1302Write(u8 address,u8 date)
  26. {
  27.         u8 n;
  28.         //ce=0;
  29.         at32_ds1302_off(RST1);
  30.         //sclk=0;//
  31.         at32_ds1302_off(CLK);
  32.         //ce=1;
  33.         at32_ds1302_on(RST1);
  34.         for (n=0;n<8;n++)                        //写地址
  35.         {
  36.                 //io=address & 0x01        ;        //只保留1位
  37.                 if(address & 0x01)
  38.                         at32_ds1302_on(DAT);
  39.                 else
  40.                         at32_ds1302_off(DAT);
  41.                 address>>=1;                                //右移一位
  42.                 //sclk=1;                                                //写
  43.                 at32_ds1302_on(CLK);
  44.                 //sclk=0;
  45.                 at32_ds1302_off(CLK);
  46.         }
  47.         for (n=0; n<8; n++)//写数据
  48.         {
  49.                 //io=date & 0x01;
  50.                 if(date & 0x01)
  51.                         at32_ds1302_on(DAT);
  52.                 else
  53.                         at32_ds1302_off(DAT);
  54.                 date>>=1;
  55.                 //sclk=1;
  56.                 at32_ds1302_on(CLK);
  57.                 //sclk=0;
  58.                 at32_ds1302_off(CLK);
  59.         }         
  60.         //ce=0;
  61.         at32_ds1302_off(RST1);
  62. }
  63. u8 Ds1302Read(u8 address)
  64. {
  65.         u8 n,date,date1;
  66.         //ce=0;
  67.         //sclk=0;
  68.         //ce=1;
  69.         at32_ds1302_off(RST1);
  70.         //sclk=0;//
  71.         at32_ds1302_off(CLK);
  72.         //ce=1;
  73.         at32_ds1302_on(RST1);
  74.         for(n=0;n<8;n++)                //发地址
  75.         {
  76.                 //io=address & 0x01;
  77.                 if(address & 0x01)
  78.                         at32_ds1302_on(DAT);
  79.                 else
  80.                         at32_ds1302_off(DAT);
  81.                 address>>= 1;
  82.                 //sclk= 1;
  83.                 //sclk= 0;
  84.                 at32_ds1302_on(CLK);
  85.                 at32_ds1302_off(CLK);
  86.         }
  87.     //TRISCbits.TRISC5=1;
  88.         IIC_INPUT_MODE_SET();
  89.     for(n=0;n<10;n++);
  90.         for(n=0;n<8;n++)                //收数据
  91.         {
  92.                 //date1=PORTCbits.RC5;
  93.                 date1=at32_datin_state();
  94.                 date=(date>>1)|(date1<<7);//存入date中
  95.                 //sclk=1;
  96.                 at32_ds1302_on(CLK);
  97.                 at32_ds1302_off(CLK);
  98.                 //sclk=0;
  99.         }
  100.         //ce=0;
  101.         //sclk=1;//保证1302稳定,必须的
  102.         //io=0;
  103.         //io=1;
  104.         at32_ds1302_off(RST1);
  105.                 at32_ds1302_on(CLK);
  106.         at32_ds1302_off(DAT);
  107.                 at32_ds1302_on(DAT);
  108.     //TRISCbits.TRISC5=0;
  109.                 at32_ds1302_init(DAT);
  110.     for(n=0;n<10;n++);
  111.         return date;       
  112. }
  113. void Ds1302ReadTime()//读时间
  114. {
  115.         u8 n;
  116.         for(n=0;n<7;n++)//读取7个字节的时钟信号:分秒时日月周年
  117.         {
  118.                 time[n] =Ds1302Read(read[n]);
  119.         }
  120. }
  121. void Ds1302Init()
  122. {
  123.         u8 n;
  124.         Ds1302Write(0x8E,0x00);                 //8E为写保护寄存器地址,00为不写保护
  125.         for(n=0;n<7;n++)                                         //
  126.         {
  127.                 Ds1302Write(write[n],time[n]);        //地址加数据
  128.         }
  129.         Ds1302Write(0x8E,0x80);                 //开写保护
  130. }
  131. /**
  132.   * [url=home.php?mod=space&uid=247401]@brief[/url]  main function.
  133.   * @param  none
  134.   * @retval none
  135.   */
  136. int main(void)
  137. {
  138.         u8 i,j;
  139.   system_clock_config();

  140.   at32_board_init();
  141.         LCD_Init();
  142.   LCD_CLS();
  143.         Ds1302Init();
  144.         //LCD_P8x16Str(45,5,(u8*)"EASEAR");
  145.         //LCD_P6x8Str(0,7,(u8*)"www.holteksupport.com");
  146.         LCD_P8x16Str(0,0,(u8*)"  :  :  ");
  147.   while(1)
  148.   {
  149.     Ds1302ReadTime();
  150.                         i=(time[0]>>4)+'0';
  151.                         j=(time[0]&0x0f)+'0';
  152.             LCD_P8x16Char(68,6,i);
  153.             LCD_P8x16Char(76,6,j);
  154.             
  155.             i=(time[1]>>4)+'0';
  156.                         j=(time[1]&0x0f)+'0';
  157.             LCD_P8x16Char(44,6,i);
  158.             LCD_P8x16Char(52,6,j);
  159.             
  160.             i=(time[2]>>4)+'0';
  161.                         j=(time[2]&0x0f)+'0';
  162.             LCD_P8x16Char(20,6,i);
  163.             LCD_P8x16Char(28,6,j);
  164.             
  165.             i=(time[3]>>4)+'0';
  166.                         j=(time[3]&0x0f)+'0';
  167.             LCD_P8x16Char(84,4,i);
  168.             LCD_P8x16Char(92,4,j);
  169.             
  170.             i=(time[4]>>4)+'0';
  171.                         j=(time[4]&0x0f)+'0';
  172.             LCD_P8x16Char(60,4,i);
  173.             LCD_P8x16Char(68,4,j);
  174.             
  175.             i=(time[6]>>4)+'0';
  176.                         j=(time[6]&0x0f)+'0';
  177.             LCD_P8x16Char(36,4,i);
  178.             LCD_P8x16Char(44,4,j);
  179.             
  180.             i=time[5]+'0';
  181.                        
  182.             LCD_P8x16Char(36,2,i);
  183.            
  184.           MS_delay(500);
  185. //                 
  186. //                at32_ds1302_on(DAT);
  187. //                at32_ds1302_on(RST1);
  188. //                at32_ds1302_on(CLK);
  189. //    MS_delay(50);
  190. //    at32_ds1302_off(DAT);
  191. //                at32_ds1302_off(RST1);
  192. //                at32_ds1302_off(CLK);
  193. //    MS_delay(50);
  194.     //delay_ms(200);
  195.   }
  196. }
运行效果图:
5.jpg 6.jpg
您需要登录后才可以回帖 登录 | 注册

本版积分规则

470

主题

3535

帖子

7

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