找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 4087|回复: 2
收起左侧

基于stm32开发的DS1302万年历程序设计

[复制链接]
ID:493714 发表于 2019-3-20 09:52 | 显示全部楼层 |阅读模式
这是一个上次写的基于STM32开发的万年历设计
32.png
单片机源程序如下:
  1. /* 下载完程序后要把BOOT1拔掉在重新上电即可看到效果
  2.         此程序最关键的是要做好io输入输出端口的配置,不能
  3.         使用GPIO_Mode_Out_OD模式,此不是双向IO口 */


  4. #include "public.h"
  5. #define DATA (GPIO_Pin_8|GPIO_Pin_9|GPIO_Pin_10|GPIO_Pin_11|GPIO_Pin_12|GPIO_Pin_13|GPIO_Pin_14|GPIO_Pin_15)
  6. #define rs (GPIO_Pin_1)
  7. #define rw (GPIO_Pin_2)
  8. #define e (GPIO_Pin_0)

  9. #define io (GPIO_Pin_12)
  10. #define ce (GPIO_Pin_13)
  11. #define sck (GPIO_Pin_14)

  12. u8 num[]="0123456789";
  13. u8 time[7]={0x18, 0x19, 0x13, 0x01, 0x10, 0x06, 0x16};//秒分时日月周年
  14. u8 w[7]={0x80, 0x82, 0x84, 0x86, 0x88, 0x8a, 0x8c};  //写地址
  15. u8 r[7]={0x81, 0x83, 0x85, 0x87, 0x89, 0x8b, 0x8d};  //读地址
  16. u8 miao,fen,shi,zhou,ri,yue,nian;
  17. void GPIOINIT()          //端口初始化
  18. {
  19.         GPIO_InitTypeDef GPIO_InitStructure;
  20.         GPIO_InitStructure.GPIO_Pin=DATA|rs|rw|e;
  21.         GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;
  22.         GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
  23.         GPIO_Init(GPIOB,&GPIO_InitStructure);
  24.        
  25.         GPIO_PinRemapConfig(GPIO_Remap_SWJ_Disable,ENABLE);//把调试设置普通IO口
  26.        
  27.         GPIO_InitStructure.GPIO_Pin=ce|sck;
  28.         GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;
  29.         GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
  30.         GPIO_Init(GPIOA,&GPIO_InitStructure);
  31. /*
  32.         GPIO_InitStructure.GPIO_Pin=io;
  33.         GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_OD;        //经过测试并不是双向IO口
  34.         GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
  35.         GPIO_Init(GPIOA,&GPIO_InitStructure);
  36. */
  37.        
  38. }
  39. void IOOUTINIT()   //io输出配置
  40. {
  41.         GPIO_InitTypeDef GPIO_InitStructure;
  42.         GPIO_InitStructure.GPIO_Pin=io;
  43.         GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;       
  44.         GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
  45.         GPIO_Init(GPIOA,&GPIO_InitStructure);       
  46. }
  47. void IOININT()         //io输入配置
  48. {
  49.         GPIO_InitTypeDef GPIO_InitStructure;
  50.         GPIO_InitStructure.GPIO_Pin=io;
  51.         GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IPU;       
  52.         GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
  53.         GPIO_Init(GPIOA,&GPIO_InitStructure);       
  54. }
  55. void RCCINIT() //系统初始化
  56. {
  57.         SystemInit();
  58.         RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);
  59.         RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
  60.         RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE);
  61.         //如果不加这条语句程序显示就会出错,即没有打开端口复用功能的时钟配置
  62. }
  63. u8 readbusy()        //忙信号检测
  64. {               
  65.         u8 f;       
  66.         GPIO_ResetBits(GPIOB,rs);               
  67.         GPIO_SetBits(GPIOB,rw);
  68.         GPIO_SetBits(GPIOB,e);       
  69.         f=((GPIO_ReadInputData(GPIOB)&0X8000));
  70.         delayms(10);
  71.         GPIO_ResetBits(GPIOB,e);
  72.         return f;               
  73. }
  74. void lcdwrc(u8 c)
  75. {       
  76.         while(readbusy());
  77.         GPIO_ResetBits(GPIOB,rs);               
  78.         GPIO_ResetBits(GPIOB,rw);
  79.         GPIO_ResetBits(GPIOB,e);
  80.         delayms(1);
  81.         GPIOB->BSRR = c<<8 & 0xf000;  //将数据送到P0口
  82.     GPIOB->BRR = ((~c)<<8) & 0xf000;

  83.         delayms(1);
  84.         GPIO_SetBits(GPIOB,e);
  85.         delayms(1);
  86.         GPIO_ResetBits(GPIOB,e);
  87.         delayms(1);
  88. }

  89. void lcdwrc4bit(long c)
  90. {       
  91.         while(readbusy());
  92.         GPIO_ResetBits(GPIOB,rs);               
  93.         GPIO_ResetBits(GPIOB,rw);
  94.         GPIO_ResetBits(GPIOB,e);
  95.         delayms(1);
  96.         GPIOB->BSRR = c<<8 & 0xf000;  //将数据送到P0口
  97.     GPIOB->BRR = ((~c)<<8) & 0xf000;
  98.         delayms(1);
  99.         GPIO_SetBits(GPIOB,e);
  100.         delayms(1);
  101.         GPIO_ResetBits(GPIOB,e);
  102.         delayms(1);

  103.         GPIOB->BSRR = c<<12 & 0xf000;  //将数据送到P0口
  104.     GPIOB->BRR = ((~c)<<12) & 0xf000;
  105.         delayms(1);
  106.         GPIO_SetBits(GPIOB,e);
  107.         delayms(1);
  108.         GPIO_ResetBits(GPIOB,e);
  109.         delayms(1);
  110. }

  111. void lcdwrd(long dat)
  112. {
  113.         while(readbusy());               
  114.         GPIO_SetBits(GPIOB,rs);               
  115.         GPIO_ResetBits(GPIOB,rw);
  116.         GPIO_ResetBits(GPIOB,e);
  117.         delayms(1);
  118.         GPIOB->BSRR = dat<<8 & 0xf000;  //将数据送到P0口
  119.     GPIOB->BRR = ((~dat)<<8) & 0xf000;
  120.         delayms(1);
  121.         GPIO_SetBits(GPIOB,e);
  122.         delayms(1);
  123.         GPIO_ResetBits(GPIOB,e);
  124.         delayms(1);
  125.         GPIOB->BSRR = dat<<12 & 0xf000;  //将数据送到P0口
  126.     GPIOB->BRR = ((~dat)<<12) & 0xf000;
  127.         delayms(1);
  128.         GPIO_SetBits(GPIOB,e);
  129.         delayms(1);
  130.         GPIO_ResetBits(GPIOB,e);
  131.         delayms(1);
  132.         GPIO_ResetBits(GPIOB,rs);
  133. }
  134. void lcdinit()
  135. {
  136.         delayms(15);
  137.         lcdwrc4bit(0x32);
  138.         delayms(5);       
  139.         lcdwrc4bit(0x28);
  140.         delayms(5);
  141.         lcdwrc4bit(0x08);
  142.         delayms(5);
  143.         lcdwrc4bit(0x01);
  144.         delayms(5);
  145.         lcdwrc4bit(0x06);
  146.         delayms(5);
  147.         lcdwrc4bit(0x0c);
  148.         delayms(5);
  149. }

  150. void ds1302writebyte(u8 dat)   //单字节写
  151. {
  152.         u8 i,value;
  153.         GPIO_ResetBits(GPIOA,sck);
  154.         delayus(20);
  155.         IOOUTINIT();               
  156.         for(i=0;i<8;i++)
  157.         {
  158.                 value=dat&0x01;       
  159.                 if(value)
  160.                         GPIO_WriteBit(GPIOA,io,Bit_SET);
  161.                 else
  162.                         GPIO_WriteBit(GPIOA,io,Bit_RESET);
  163.                 dat>>=1;
  164.                 GPIO_SetBits(GPIOA,sck);
  165.                 delayus(20);
  166.                 GPIO_ResetBits(GPIOA,sck);
  167.                 delayus(20);       
  168.         }
  169. }
  170. void ds1302writebytes(u8 add,u8 dat) //多字节写
  171. {
  172.         GPIO_ResetBits(GPIOA,ce);
  173.         delayus(20);
  174.         GPIO_SetBits(GPIOA,ce);
  175.         delayus(20);
  176.         ds1302writebyte(add);
  177.         ds1302writebyte(dat);
  178.         GPIO_ResetBits(GPIOA,ce);
  179.         delayus(20);                       
  180. }
  181. u8 ds1302readbyte()         //单字节读
  182. {
  183.         u8 i,value;
  184.         GPIO_ResetBits(GPIOA,sck);
  185.         delayus(20);
  186.         IOININT();               
  187.         for(i=0;i<8;i++)
  188.         {
  189.                 value>>=1;
  190.                 if(GPIO_ReadInputDataBit(GPIOA,io)==1)
  191.                 {
  192.                         value|=0x80;
  193.                 }       
  194.                 GPIO_SetBits(GPIOA,sck);
  195.                 delayus(20);
  196.                 GPIO_ResetBits(GPIOA,sck);
  197.                 delayus(20);       
  198.         }
  199.         return value;                                       
  200. }
  201. u8 ds1302readbytes(u8 add)         //多字节读
  202. {
  203.         u8 temp;
  204.         GPIO_ResetBits(GPIOA,ce);
  205.         delayus(20);
  206.         GPIO_SetBits(GPIOA,ce);
  207.         delayus(20);
  208.         ds1302writebyte(add);
  209.         temp=ds1302readbyte();
  210.         GPIO_ResetBits(GPIOA,ce);
  211.         delayus(20);
  212.         GPIO_ResetBits(GPIOA,io);
  213.         delayus(20);       
  214.         GPIO_SetBits(GPIOA,io);         //释放IO
  215.         delayus(20);       
  216.         return temp;               
  217. }
  218. void settime()
  219. {
  220.         u8 i;
  221.         ds1302writebytes(0x8e,0x00);
  222.         for(i=0;i<7;i++)
  223.         {
  224.                 ds1302writebytes(w[i],time[i]);       
  225.         }
  226.         ds1302writebytes(0x8e,0x80);
  227. }
  228. void readtime()
  229. {
  230.         miao=ds1302readbytes(r[0]);
  231.         fen=ds1302readbytes(r[1]);
  232.         shi=ds1302readbytes(r[2]);
  233.         zhou=ds1302readbytes(r[5]);
  234.         ri=ds1302readbytes(r[3]);
  235.         yue=ds1302readbytes(r[4]);       
  236.         nian=ds1302readbytes(r[6]);       
  237. }
  238. void display()
  239. {
  240.         lcdwrc4bit(0x00+0x80);       
  241.         lcdwrd(num[shi/16]);
  242. ……………………

  243. …………限于本文篇幅 余下代码请从51黑下载附件…………
复制代码

所有资料51hei提供下载:
DS1302万年历设计.7z (178.13 KB, 下载次数: 224)
回复

使用道具 举报

ID:1 发表于 2019-3-20 18:47 | 显示全部楼层
本帖需要重新编辑补全电路原理图,源码,详细说明与图片即可获得100+黑币(帖子下方有编辑按钮)
回复

使用道具 举报

ID:777712 发表于 2020-6-27 22:54 | 显示全部楼层
请问有仿真图吗
回复

使用道具 举报

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

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表