找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 6312|回复: 3
收起左侧

LCD1602的STM32驱动工程模板

[复制链接]
ID:249540 发表于 2017-11-15 09:24 | 显示全部楼层 |阅读模式
lcd1602数字钟,运行有点小错
0B1A4D5A0B3E4592611CD5EBC31477D7.png

0.png

单片机源程序如下:
  1. /* 下载完程序后要把BOOT1拔掉在重新上电即可看到效果  */


  2. #include "public.h"
  3. #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)
  4. #define rs (GPIO_Pin_1)
  5. #define rw (GPIO_Pin_2)
  6. #define e (GPIO_Pin_0)
  7. u8 num[]="0123456789";
  8. u8 b[]=" welcome to PZ  ";
  9. u8 c[6]="Time: ";
  10. u16 m,miao,shi,fen;
  11. u8 a[6];
  12. void GPIOINIT()          //端口初始化
  13. {
  14.         GPIO_InitTypeDef GPIO_InitStructure;
  15.         GPIO_InitStructure.GPIO_Pin=DATA|rs|rw|e;
  16.         GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;
  17.         GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
  18.         GPIO_Init(GPIOB,&GPIO_InitStructure);
  19.         GPIO_PinRemapConfig(GPIO_Remap_SWJ_Disable,ENABLE);//把调试设置普通IO口
  20. }
  21. void RCCINIT() //系统初始化
  22. {
  23.         SystemInit();
  24.         RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);
  25.         RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3,ENABLE);
  26.         RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE);
  27.         //如果不加这条语句程序显示就会出错,即没有打开端口复用功能的时钟配置
  28. }
  29. u8 readbusy()        //忙信号检测
  30. {               
  31.         u8 f;       
  32.         GPIO_ResetBits(GPIOB,rs);               
  33.         GPIO_SetBits(GPIOB,rw);
  34.         GPIO_SetBits(GPIOB,e);       
  35.         f=((GPIO_ReadInputData(GPIOB)&0X8000));
  36.         delayms(10);
  37.         GPIO_ResetBits(GPIOB,e);
  38.         return f;               
  39. }
  40. void lcdwrc(u8 c)
  41. {       
  42.         while(readbusy());
  43.         GPIO_ResetBits(GPIOB,rs);               
  44.         GPIO_ResetBits(GPIOB,rw);
  45.         GPIO_ResetBits(GPIOB,e);
  46.         delayms(1);
  47.         GPIOB->BSRR = c<<8 & 0xf000;  //将数据送到P0口
  48.     GPIOB->BRR = ((~c)<<8) & 0xf000;

  49.         delayms(1);
  50.         GPIO_SetBits(GPIOB,e);
  51.         delayms(1);
  52.         GPIO_ResetBits(GPIOB,e);
  53.         delayms(1);
  54. }

  55. void lcdwrc4bit(long c)
  56. {       
  57.         while(readbusy());
  58.         GPIO_ResetBits(GPIOB,rs);               
  59.         GPIO_ResetBits(GPIOB,rw);
  60.         GPIO_ResetBits(GPIOB,e);
  61.         delayms(1);
  62.         GPIOB->BSRR = c<<8 & 0xf000;  //将数据送到P0口
  63.     GPIOB->BRR = ((~c)<<8) & 0xf000;
  64.         delayms(1);
  65.         GPIO_SetBits(GPIOB,e);
  66.         delayms(1);
  67.         GPIO_ResetBits(GPIOB,e);
  68.         delayms(1);

  69.         GPIOB->BSRR = c<<12 & 0xf000;  //将数据送到P0口
  70.     GPIOB->BRR = ((~c)<<12) & 0xf000;
  71.         delayms(1);
  72.         GPIO_SetBits(GPIOB,e);
  73.         delayms(1);
  74.         GPIO_ResetBits(GPIOB,e);
  75.         delayms(1);
  76. }

  77. void lcdwrd(long dat)
  78. {
  79.         while(readbusy());               
  80.         GPIO_SetBits(GPIOB,rs);               
  81.         GPIO_ResetBits(GPIOB,rw);
  82.         GPIO_ResetBits(GPIOB,e);
  83.         delayms(1);
  84.         GPIOB->BSRR = dat<<8 & 0xf000;  //将数据送到P0口
  85.     GPIOB->BRR = ((~dat)<<8) & 0xf000;
  86.         delayms(1);
  87.         GPIO_SetBits(GPIOB,e);
  88.         delayms(1);
  89.         GPIO_ResetBits(GPIOB,e);
  90.         delayms(1);
  91.         GPIOB->BSRR = dat<<12 & 0xf000;  //将数据送到P0口
  92.     GPIOB->BRR = ((~dat)<<12) & 0xf000;
  93.         delayms(1);
  94.         GPIO_SetBits(GPIOB,e);
  95.         delayms(1);
  96.         GPIO_ResetBits(GPIOB,e);
  97.         delayms(1);
  98.         GPIO_ResetBits(GPIOB,rs);
  99. }
  100. void lcdinit()
  101. {
  102.         delayms(15);
  103.         lcdwrc4bit(0x32);
  104.         delayms(5);       
  105.         lcdwrc4bit(0x28);
  106.         delayms(5);
  107.         lcdwrc4bit(0x08);
  108.         delayms(5);
  109.         lcdwrc4bit(0x01);
  110.         delayms(5);
  111.         lcdwrc4bit(0x06);
  112.         delayms(5);
  113.         lcdwrc4bit(0x0c);
  114.         delayms(5);
  115. }
  116. void NVICINIT()
  117. {
  118.        
  119.         NVIC_InitTypeDef NVIC_InitStructure;

  120.         NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);

  121.         NVIC_InitStructure.NVIC_IRQChannel=TIM3_IRQn;
  122.         NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=0;
  123.         NVIC_InitStructure.NVIC_IRQChannelSubPriority=1;
  124.         NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  125.         NVIC_Init(&NVIC_InitStructure);

  126.                
  127. }

  128. void TIMINIT()
  129. {
  130.         TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure;

  131.         TIM_ClearITPendingBit(TIM3,TIM_IT_Update);

  132.         TIM_TimeBaseInitStructure.TIM_Period = 2000;//如果等于2000的话是定时1s
  133.         TIM_TimeBaseInitStructure.TIM_Prescaler = 35999;
  134.         TIM_TimeBaseInitStructure.TIM_ClockDivision = 0;
  135.         TIM_TimeBaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up;
  136.         TIM_TimeBaseInit(TIM3,&TIM_TimeBaseInitStructure);
  137.        
  138.         TIM_Cmd(TIM3,ENABLE); //使能或者失能TIMx外设
  139.         TIM_ITConfig(TIM3, TIM_IT_Update, ENABLE );        //使能或者失能指定的TIM中断

  140.                        
  141. }

  142. void display()
  143. {
  144.         u8 i;
  145.         lcdwrc4bit(0x00+0x80);
  146.         for(i=0;i<16;i++)
  147.         {
  148.                 lcdwrd(b[i]);       
  149.         }
  150.         lcdwrc4bit(0x40+0x80);
  151.         for(i=0;i<6;i++)
  152.         {
  153.                 lcdwrd(c[i]);       
  154.         }
  155. ……………………

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

所有资料51hei提供下载:
163.STM32-LCD数字时钟设计.rar (483.68 KB, 下载次数: 88)





回复

使用道具 举报

ID:219160 发表于 2018-10-27 23:14 | 显示全部楼层
你好,谢谢大佬的分享,这对我这个菜鸟有巨大的帮助。不过对于大佬的工程里有些疑问,如果大佬能抽空回答一下我的问题,感激不尽。
疑问:在函数lcdinit()中,lcd的初始化用的始终是同一个函数lcdwrc4bit(),而在此函数中,每次只能4bit初始化,想问大佬,后面的08H,01H,06H,0CH的初始化如何完成?
附上我对lcdwrc4bit函数的注释:(乱码部分不影响程序)
void lcdwrc4bit(long c)
{       
        while(readbusy()); //readbusy = f=1 --> busy --> LOOP WITA
        GPIO_ResetBits(GPIOB,rs);                //rs=0
        GPIO_ResetBits(GPIOB,rw);   //rw=0
        GPIO_ResetBits(GPIOB,e);    //e=0
        delayms(1);                                        //IF C = 0011 0010           ->
        GPIOB->BSRR = c<<8 & 0xf000;  //½«êy¾YËíμ½P0¿ú  yfr//BSRR = 0011 0000 0000 0000 ->  //D7~D0 = 0011 0000
    GPIOB->BRR = ((~c)<<8) & 0xf000;                 // BRR = 1100 0000 0000 0000 ->
        delayms(1);                                       
        GPIO_SetBits(GPIOB,e);  //e=1  ->
        delayms(1);             //     ->  here a pulse -> get D7~D4
        GPIO_ResetBits(GPIOB,e);//e=0  ->
        delayms(1);
                                                     //IF C = 0011 0010           ->
        GPIOB->BSRR = c<<12 & 0xf000;  //½«êy¾YËíμ½P0¿ú yfr//BSRR = 0010 0000 0000 0000 ->  //D7~D0 = 0010 0000
    GPIOB->BRR = ((~c)<<12) & 0xf000;                // BRR = 1101 0000 0000 0000 ->
        delayms(1);
        GPIO_SetBits(GPIOB,e);
        delayms(1);             //e=1  ->
        GPIO_ResetBits(GPIOB,e);//     ->  here a pulse -> get D7~D4
        delayms(1);             //e=0  ->
}
回复

使用道具 举报

ID:636416 发表于 2019-11-7 20:36 | 显示全部楼层
时钟是用的ds1302吗
回复

使用道具 举报

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

本版积分规则

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

Powered by 单片机教程网

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