標(biāo)題: stm32單片機(jī)驅(qū)動(dòng)1602顯示的源程序 [打印本頁(yè)]

作者: a123455    時(shí)間: 2018-7-11 01:54
標(biāo)題: stm32單片機(jī)驅(qū)動(dòng)1602顯示的源程序
注意供電電壓,測(cè)試時(shí)因?yàn)橐恢庇?.3V所以導(dǎo)致無(wú)顯示,浪費(fèi)大量時(shí)間。且對(duì)比度引腳接地

STM32單片機(jī)源程序如下:
  1. #include "lcd1602.h"
  2. #include "delay.h"
  3.                              
  4. #define DELAY_2N     0

  5. //==================================================
  6. void LCD_init(void)
  7. {
  8.     /*********************液晶使用的I/O口初始化**************************/
  9.                 GPIO_InitTypeDef GPIO_InitStructure;
  10.        
  11.     RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);
  12.   
  13.     GPIO_InitStructure.GPIO_Pin =  GPIO_Pin_1 | GPIO_Pin_2| GPIO_Pin_0| GPIO_Pin_3
  14.                                                                                                                                         | GPIO_Pin_4| GPIO_Pin_5| GPIO_Pin_6| GPIO_Pin_7
  15.                                                                                                                                         | GPIO_Pin_8| GPIO_Pin_9| GPIO_Pin_10;
  16.     GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  17.     GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  18.     GPIO_Init(GPIOC, &GPIO_InitStructure);
  19.        
  20.     LCD_RW(0);                        //讀寫(xiě)位直接低電平,只寫(xiě)不讀

  21.     /*********************液晶初始化**************************/        
  22.     delay_us(340);
  23.                 LCD_RS(0);
  24.    
  25.     LCD_write_cmd(0x38);          // 8bit顯示模式,2行,5x7字體
  26.     delay_ms(4);  
  27.     LCD_write_cmd(0x08);         // 顯示關(guān)閉
  28.     delay_ms(4);
  29.     LCD_write_cmd(0x01);         // 顯示清屏
  30.     delay_ms(4);
  31.     LCD_write_cmd(0x06);         // 顯示光標(biāo)移動(dòng)設(shè)置
  32.     delay_ms(4);
  33.     LCD_write_cmd(0x0c);         // 顯示開(kāi),光標(biāo)開(kāi),光標(biāo)閃爍
  34.     delay_ms(4);
  35.                 LCD_write_cmd(0x01);         //清屏
  36.                 delay_ms(4);
  37. }
  38. /*--------------------------------------------------
  39. 函數(shù)說(shuō)明:寫(xiě)命令到液晶


  40. ---------------------------------------------------*/
  41. void LCD_write_cmd(unsigned char cmd)
  42. {
  43.     LCD_RS(0);
  44.     LCD_Write_byte(cmd);
  45.     delay_us(340);
  46. }
  47. /*--------------------------------------------------
  48. 函數(shù)說(shuō)明:寫(xiě)數(shù)據(jù)到液晶


  49. ---------------------------------------------------*/
  50. void LCD_write_data(unsigned char w_data)
  51. {
  52.     LCD_RS(1);
  53.     LCD_Write_byte(w_data);
  54.     delay_us(340);
  55. }
  56. /*--------------------------------------------------
  57. 函數(shù)說(shuō)明:寫(xiě)4bit到液晶
  58. --------------------------------------------------*/
  59. void LCD_Write_byte(unsigned char num)
  60. {  
  61.                 if (num&0x01)
  62.                                 data0(1);
  63.                 else
  64.                                 data0(0);

  65.                 if (num&0x02)
  66.                                 data1(1);
  67.                 else
  68.                                 data1(0);

  69.                 if (num&0x04)
  70.                                 data2(1);
  71.                 else
  72.                                 data2(0);

  73.                 if (num&0x08)
  74.                                 data3(1);
  75.                 else
  76.                                 data3(0);

  77.                 if (num&0x10)
  78.                                 data4(1);
  79.                 else
  80.                                 data4(0);

  81.                 if (num&0x20)
  82.                                 data5(1);
  83.                 else
  84.                                 data5(0);

  85.                 if (num&0x40)
  86.                                 data6(1);
  87.                 else
  88.                                 data6(0);
  89.                
  90.                 if (num&0x80)
  91.                                 data7(1);
  92.                 else
  93.                                 data7(0);
  94.                 delay_us(340);
  95.     LCD_EN(1);
  96.     delay_us(340);
  97.     LCD_EN(0);
  98.     delay_us(340);
  99. }

  100. /*----------------------------------------------------
  101. LCD_set_xy        : 設(shè)置LCD顯示的起始位置
  102. 輸入?yún)?shù):x、y    : 顯示字符串的位置,X:0-15,Y:0-1               
  103. -----------------------------------------------------*/
  104. void LCD_set_xy( unsigned char x, unsigned char y )
  105. {
  106.     unsigned char address = 0;
  107.     if (y==0)
  108.     {
  109.         address=0x80+x;
  110.     }
  111.     else
  112.     {
  113.         address=0xc0+x;
  114.     }
  115. //                y ? (address=0xc0+x): (address=0x80+x) ;
  116.     LCD_write_cmd(address);
  117. }
  118. /*---------------------------------------------------
  119. LCD_write_string  : 英文字符串顯示函數(shù)
  120. 輸入?yún)?shù):*s      :英文字符串指針;
  121.           X、Y    : 顯示字符串的位置               
  122. ---------------------------------------------------*/
  123. void LCD_write_string(unsigned char X,unsigned char Y, char *s)
  124. {
  125.     LCD_set_xy(X,Y);   
  126.     while (*s)
  127.     {
  128.         LCD_write_data(*s);
  129.         s++;
  130.     }
  131. }

  132. //=======================================================
  133. void LCD_wstring(unsigned char X,unsigned char *s)
  134. {
  135.     LCD_write_cmd(X);   
  136.     while (*s)
  137.     {
  138.         LCD_write_data(*s);
  139.         s++;
  140.     }
  141. }
  142. //void delay(vu32 cnt)
  143. //{
  144. //  cnt <<= DELAY_2N;

  145. //  while (cnt--);
  146. //}
復(fù)制代碼

所有資料51hei提供下載:

lcd1602.rar (301.67 KB, 下載次數(shù): 100)




作者: LXH121    時(shí)間: 2019-1-17 18:15
你好,請(qǐng)問(wèn)如何顯示一個(gè)變量的值
作者: HuM1989    時(shí)間: 2019-6-2 20:55
挺好的額
作者: lk3272    時(shí)間: 2019-11-5 15:41
下來(lái)看看




歡迎光臨 (http://www.torrancerestoration.com/bbs/) Powered by Discuz! X3.1