[STM32F1] 献给还在用LCD1602的童鞋一个STM32F103的驱动代码

[复制链接]
7427|4
 楼主| sjw1716094642 发表于 2015-2-6 18:19 | 显示全部楼层 |阅读模式
寄存器写的,应该用起来不错的:D



  1. #include "stm32f10x_lib.h"
  2. #include <stdio.h>
  3. #include <sys.h>
  4. #include <math.h>
  5. #define BTin GPIOC->CRL=0xc3333333
  6. #define BTout GPIOC->CRL=0x33333333
  7. #define BT (GPIOC->IDR&0x80)
  8. #define RS_1 GPIOC->BSRR=0x0100
  9. #define RS_0 GPIOC->BRR=0x0100
  10. #define RW_1 GPIOC->BSRR=0x0200
  11. #define RW_0 GPIOC->BRR=0x0200
  12. #define E_1 GPIOC->BSRR=0x0400
  13. #define E_0 GPIOC->BRR=0x0400
  14. #define Datain(x) GPIOC->ODR=(GPIOC->IDR&0xff00)+x
  15. u8 busytest(void)
  16. {
  17.         u8 a;
  18.         RS_0;
  19.         RW_1;
  20.         E_1;
  21.         BTin;
  22.         ysu(50);
  23.         a=BT;
  24.         E_0;
  25.         BTout;
  26.         return (a);
  27. }

  28. void Wcom(u8 com)
  29. {
  30.         while(busytest());
  31.         ysm(1);
  32.         RS_0;
  33.         RW_0;
  34.         E_0;
  35.         ysu(5);
  36.         Datain(com);
  37.         ysu(5);
  38.         E_1;
  39.         ysu(5);
  40.         E_0;
  41. }

  42. void Wdata(u8 data)
  43. {
  44.         while(busytest());
  45.         RS_1;
  46.         RW_0;
  47.         E_0;
  48.         Datain(data);
  49.         ysu(5);
  50.         E_1;
  51.         ysu(5);
  52.         E_0;
  53. }

  54. void adxy(u8 x,u8 y)
  55. {
  56.         if(x==0) Wcom(0x80|y);
  57.         else Wcom(0xc0|y);
  58. }

  59. void putstr(u8 *str)
  60. {
  61.         while(*str!='\0')
  62.         {
  63.                 Wdata(*str);
  64.                 str++;
  65.         }
  66. }

  67. void lcdinit(void)
  68. {
  69.         GPIOC->CRL=0x33333333;
  70.         GPIOC->CRH&=0xfffff000;
  71.         GPIOC->CRH|=0x333;
  72.         Wcom(0x38);
  73.         ysm(5);
  74.         Wcom(0x38);
  75.         ysm(5);
  76.         Wcom(0x38);
  77.         ysm(5);
  78.         Wcom(0x0c);
  79.         ysm(5);
  80.         Wcom(0x06);
  81.         ysm(5);
  82.         Wcom(0x01);
  83.         ysm(5);
  84. }

  85. void putnum(s32 num)   //输出整数
  86. {
  87.         u32 lt=1;
  88.         if(num<0)
  89.         {
  90.                 num=fabs(num);
  91.                 Wdata('-');
  92.         }
  93.         do
  94.         {
  95.                 lt*=10;
  96.         } while (num/lt);
  97.         lt/=10;
  98.         do
  99.         {
  100.                 Wdata(num/lt+48);
  101.                 num%=lt;
  102.                 lt/=10;
  103.         }
  104.         while(lt);
  105. }

  106. void clear(u8 n)
  107. {
  108.         while(n--) Wdata(' ');
  109. }
 楼主| sjw1716094642 发表于 2015-2-6 18:23 | 显示全部楼层
没有写注释的习惯。。
mmuuss586 发表于 2015-2-6 22:28 | 显示全部楼层

谢谢分享;
cos12a 发表于 2015-2-7 12:44 | 显示全部楼层
不注释简洁,但不知道频率跑高了,能不能正常显示.ysu();这个函数没有给出来.
eertt 发表于 2016-8-23 23:49 来自手机 | 显示全部楼层
Datain(com);这个函数是怎么实现的?楼主最好写成Data_In();比较好
您需要登录后才可以回帖 登录 | 注册

本版积分规则

24

主题

302

帖子

8

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