MSP430G2553实现LCD1602的字符显示

2019-07-31 15:06发布

网上转的一篇文章,MSP430G2553实现LCD1602的字符显示,而且是半字节的,用的是P2口的高四位。

  1. /*********************************************************************
  2. *
  3. * LCD1602显示之高四位相连的方法
  4. *
  5. * 描述:4线数据宽度,操作Lcd1602
  6. * 在LCD1602屏幕上第一行显示 Hello!LCD1602
  7. * 第二行显示 MSP430G2553
  8. * 硬件电路:MSP430g2553
  9. * 硬件连接:
  10. * MSP430与LCD连接信息
  11. * LCD1602,4位接口,即使用D4-D7数据口,D0-D3不接入MCU
  12. * PIN1 --> 地
  13. * PIN2 --> VCC(一定要接+5V)
  14. * PIN3 -->仿真时悬空,实际电路 2K电阻-->地 (一定要接好,否则没有任何显示)
  15. * PIN4 --> RS --> P1.6
  16. * PIN5 --> R/W --> GND
  17. * PIN6 --> EN --> P1.7
  18. * PIN7 --> D0不接
  19. * PIN8 --> D1不接
  20. * PIN9 --> D2不接
  21. * PIN10 --> D3不接
  22. * PIN11 --> D4 --> P2.4
  23. * PIN12 --> D5 --> P2.5
  24. * PIN13 --> D6 --> P2.6
  25. * PIN14 --> D7 --> P2.7
  26. * PIN15 --> VCC
  27. * PIN16 --> 地
  28. * 调试器:MSP430FET全系列JTAG仿真器
  29. * 调试软件: CCS5.1.1 编译
  30. *****************************************************************/
  31. #include <intrinsics.h>
  32. #include <msp430g2253.h>
  33. #include <msp430.h>
  34. /*****************************************************

  35.                                                      端口定义

  36. ****************************************************/
  37. #define LCD_EN_PORT P1OUT    //以下2个要设为同一个口
  38. #define LCD_EN_DDR P1DIR
  39. #define LCD_RS_PORT P1OUT    //以下2个要设为同一个口
  40. #define LCD_RS_DDR P1DIR
  41. #define LCD_DATA_PORT P2OUT  //以下3个要设为同一个口
  42. #define LCD_DATA_DDR P2DIR   //一定要用高4位
  43. #define LCD_RS BIT6
  44. #define LCD_EN BIT7
  45. #define LCD_DATA    BIT7|BIT6|BIT5|BIT4   //4位数据线连接模式
  46. /***************************************************

  47.                                                           预定义函数

  48. **************************************************/
  49. void LCD_init(void);
  50. void LCD_init_first(void);
  51. void LCD_en_write1(void);  //上升沿使能
  52. void LCD_en_write2(void);  //下降沿使能
  53. void LCD_write_command(unsigned char command);
  54. void LCD_write_data(unsigned char data);
  55. void LCD_set_xy (unsigned char x, unsigned char y);
  56. void LCD_write_string(unsigned char X,unsigned char Y, unsigned char *s);
  57. void LCD_write_char(unsigned char X,unsigned char Y, unsigned char data);
  58. void delay_1ms(void);
  59. void delay_nus(unsigned int n);
  60. void delay_nms(unsigned int n);
  61. unsigned char LCDBuf1[]={"Hello!LCD1602"};   //第一行要显示的内容
  62. unsigned char LCDBuf2[]={"MSP430G2553"};     //第二行要显示的内容

  63. /********************************************

  64.                                                主函数

  65. *******************************************/

  66. void main()
  67. {
  68. WDTCTL = WDTPW + WDTHOLD;     // 关闭看门狗
  69. LCD_init_first();
  70. LCD_init();
  71. delay_nms(100);
  72. LCD_write_string(0,0,LCDBuf1);
  73. delay_nms(10);
  74. LCD_write_string(0,1,LCDBuf2);
  75. }
  76. /********************************************

  77.             LCD液晶操作函数

  78. *******************************************/

  79. void LCD_init_first(void)         //LCD1602液晶初始化函数(热启动)
  80. {
  81.         delay_nms(500);
  82.      LCD_DATA_DDR|=LCD_DATA;   //数据口方向为输出
  83.      LCD_EN_DDR|=LCD_EN;       //设置EN方向为输出
  84.      LCD_RS_DDR|=LCD_RS;       //设置RS方向为输出

  85.         delay_nms(50);
  86.         LCD_write_command(0x30);
  87.         delay_nms(50);
  88.         LCD_write_command(0x30);
  89.         delay_nms(5);
  90.         LCD_write_command(0x30);
  91.         delay_nms(500);

  92. }

  93. /*****************************************
  94. *
  95. *             LCD1602液晶初始化函数
  96. *
  97. ****************************************/
  98. void LCD_init(void)
  99. {
  100. delay_nms(500);
  101. LCD_DATA_DDR|=LCD_DATA;   //数据口方向为输出
  102. LCD_EN_DDR|=LCD_EN;       //设置EN方向为输出
  103. LCD_RS_DDR|=LCD_RS;       //设置RS方向为输出

  104. delay_nms(500);

  105. LCD_write_command(0x28);  //4位数据接口
  106. delay_nms(50);
  107. LCD_write_command(0x28);  //4位数据接口
  108. delay_nms(50);
  109.     LCD_write_command(0x28);  //4位数据接口
  110. delay_nms(50);
  111.     LCD_en_write2();
  112.     delay_nms(50);
  113. LCD_write_command(0x28); //4位数据接口
  114. delay_nms(500);
  115. LCD_write_command(0x01); //清屏
  116. LCD_write_command(0x0c); //显示开,关光标,不闪烁
  117.     LCD_write_command(0x06); //设定输入方式,增量不移位
  118. delay_nms(50);


  119. }

  120. /*****************************************
  121. *
  122. *             液晶使能上升沿
  123. *
  124. ****************************************/

  125. void LCD_en_write1(void)
  126. {
  127.     LCD_EN_PORT&=~LCD_EN;
  128.     delay_nus(10);
  129.     LCD_EN_PORT|=LCD_EN;
  130. }

  131. /*****************************************
  132. *
  133. *             液晶使能下降沿
  134. *
  135. ****************************************/
  136. void LCD_en_write2(void)
  137. {
  138.    LCD_EN_PORT|=LCD_EN;
  139.    delay_nus(10);
  140.    LCD_EN_PORT&=~LCD_EN;
  141. }

  142. /*****************************************
  143. *
  144. *               写指令函数
  145. *
  146. ****************************************/
  147. void LCD_write_command(unsigned char command)
  148. {
  149.    delay_nus(16);
  150.    P2SEL=0x00;
  151.    LCD_RS_PORT&=~LCD_RS; //RS=0
  152.    LCD_en_write1();
  153.    LCD_DATA_PORT&=0X0f; //清高四位
  154.    LCD_DATA_PORT|=command&0xf0; //写高四位

  155.    delay_nus(16);
  156.    LCD_en_write2();
  157.    command=command<<4; //低四位移到高四位
  158.    LCD_en_write1();
  159.    LCD_DATA_PORT&=0x0f; //清高四位
  160.    LCD_DATA_PORT|=command&0xf0; //写低四位
  161.    LCD_en_write2();
  162. }

  163. /*****************************************
  164. *
  165. *               写数据函数
  166. *
  167. ****************************************/
  168. void LCD_write_data(unsigned char data)
  169. {
  170.    delay_nus(16);
  171.    P2SEL=0x00;
  172.    LCD_RS_PORT|=LCD_RS;      //RS=1
  173.    LCD_en_write1();          //E上升沿
  174.    LCD_DATA_PORT&=0X0f;      //清高四位
  175.    LCD_DATA_PORT|=data&0xf0; //写高四位
  176.    delay_nus(16);
  177.    LCD_en_write2();
  178.    data=data<<4;             //低四位移到高四位
  179.    LCD_en_write1();
  180.    LCD_DATA_PORT&=0X0f;      //清高四位
  181.    LCD_DATA_PORT|=data&0xf0; //写低四位
  182.    LCD_en_write2();
  183. }

  184. /*****************************************
  185. *
  186. *               写地址函数
  187. *
  188. ****************************************/
  189. void LCD_set_xy( unsigned char x, unsigned char y )
  190. {
  191.    unsigned char address;
  192.    if (y == 0) address = 0x80 + x;
  193.    else address = 0xc0 + x;
  194.    LCD_write_command( address);
  195. }

  196. /*****************************************
  197. *
  198. *LCD在任意位置写字符串,列x=0~15,行y=0,1
  199. *
  200. ****************************************/
  201. void LCD_write_string(unsigned char X,unsigned char Y,unsigned char *s)
  202. {
  203. LCD_set_xy( X, Y ); //写地址
  204.     while (*s)          //写显示字符
  205.     {
  206.       LCD_write_data( *s );
  207.       s++;
  208.     }
  209. }

  210. /*****************************************
  211. *
  212. *     LCD在任意位置写字符,列x=0~15,行y=0,1
  213. *
  214. ****************************************/

  215. void LCD_write_char(unsigned char X,unsigned char Y,unsigned char data)
  216. {
  217.    LCD_set_xy( X, Y ); //写地址
  218.    LCD_write_data( data);
  219. }

  220. /*****************************************
  221. *
  222. *               1us延时函数
  223. *
  224. ****************************************/

  225. void delay_1us(void)
  226. {
  227.    asm("nop");
  228. }

  229. /*****************************************
  230. *
  231. *               N us延时函数
  232. *
  233. ****************************************/
  234. void delay_nus(unsigned int n)
  235. {
  236.    unsigned int i;
  237.    for (i=0;i<n;i++)
  238.    delay_1us();
  239. }

  240. /*****************************************
  241. *
  242. *               1ms延时函数
  243. *
  244. ****************************************/
  245. void delay_1ms(void)
  246. {
  247.    unsigned int i;
  248.    for (i=0;i<1140;i++);
  249. }

  250. /*****************************************
  251. *
  252. *               N ms延时函数
  253. *
  254. ****************************************/
  255. void delay_nms(unsigned int n)
  256. {
  257.    unsigned int i=0;
  258.    for (i=0;i<n;i++)
  259.    delay_1ms();
  260. }
复制代码
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。