首页 > 代码库 > STM32F302 驱动 LCD1602

STM32F302 驱动 LCD1602

平台:stm32f302,    屏幕:  LCD1602 ,

1 #ifndef __LCD1602_H__2 #define __LCD1602_H__3 #include "stm32f30x.h"4 void lcd1602_init(void);5 void lcd1602_display(u8*string);6 void lcd1602_display_pos(int row,int column, u8*string);7 #endif
  1 #include "LCD1602.h"  2   3 #define L_SHIFT     0X0  4 #define R_SHIFT     0X1  5   6 #define DISPLAY_ON  0X1  7 #define DISPLAY_OFF 0X0  8   9 #define DATA_WIDTH_8BITS    0X1 10 #define DATA_WIDTH_4BITS    0X0 11  12 #define ROW_NUM_2   0X1 13 #define ROW_NUM_1   0X0 14  15 #define DOT_MATRIC_5_7 0X0 16 #define DOT_MATRIC_5_10 0X1 17  18 #define     CMD_CLR             0X1 19 #define     CMD_CARET_REWIND    0X2 20 #define     CMD_CARET_SHIFT(X)  (0X4|(X<<1))    21 #define     CMD_DISP_ONOFF(X)   (0X8|(X<<2)) 22 #define     CMD_SC_LR(S,L)      (0X10|(S<<3)|(L<<2)) 23 #define     CMD_FUN_SET(DL,N,F) (0X20|(DL<<4)|(N<<3)|(F<<2)) 24 #define     CMD_CGRAM(ADDR)     (0X40|ADDR)    25 #define     CMD_DDRAM(ADDR)     (0X80|ADDR)  26  27 #define LCD1602_EN_HIGH()   GPIOB->BSRR = GPIO_Pin_6 28 #define LCD1602_EN_LOW()    GPIOB->BRR = GPIO_Pin_6 29  30 #define LCD1602_RS_HIGH() GPIOB->BSRR = GPIO_Pin_8 31 #define LCD1602_RS_LOW() GPIOB->BRR = GPIO_Pin_8 32  33 #define STCP_595_HIGH()   GPIOB->BSRR = GPIO_Pin_9 34 #define STCP_595_LOW()   GPIOB->BRR = GPIO_Pin_9 35  36 #define SHCP_595_HIGH()   GPIOB->BSRR = GPIO_Pin_5 37 #define SHCP_595_LOW()   GPIOB->BRR = GPIO_Pin_5 38  39 #define DS_595_HIGH()   GPIOB->BSRR = GPIO_Pin_4 40 #define DS_595_LOW()   GPIOB->BRR = GPIO_Pin_4 41  42 //#define LCD1602_RW PBout(8) 43 //#define LCD1602_DATA  44  45 static void delayus(int us) 46 { 47     int i = 0; 48     while(us--) 49     { 50         for(i=10;i;--i); 51     } 52 } 53 static void writeByte(u8 ch) 54 { 55    int i = 0; 56    STCP_595_LOW(); 57    SHCP_595_LOW(); 58    for(i=8;i;--i) 59    { 60     if(ch&0x80) 61     { 62         DS_595_HIGH(); 63     } 64     else 65     { 66         DS_595_LOW(); 67     } 68     SHCP_595_HIGH(); 69     ch <<= 1; 70     SHCP_595_LOW(); 71    } 72    STCP_595_HIGH(); 73 } 74  75 void LCD1602_writeCmd(u8 cmd) 76 { 77     LCD1602_EN_HIGH(); 78     LCD1602_RS_LOW(); 79     writeByte(cmd); 80     LCD1602_EN_LOW(); 81     delayus(1000); 82 } 83  84 void LCD1602_writeData(u8 data) 85 { 86     LCD1602_EN_HIGH(); 87     LCD1602_RS_HIGH(); 88     writeByte(data); 89     LCD1602_EN_LOW(); 90     delayus(1000); 91 } 92  93 static void hw_init(void) 94 { 95   GPIO_InitTypeDef  GPIO_InitStructure; 96    97   /* Enable the GPIO Clock */ 98   RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOB, ENABLE); 99   100   /* Configure the GPIO pin */101   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6|GPIO_Pin_8|GPIO_Pin_9|GPIO_Pin_5|GPIO_Pin_4;102   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;103   GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;104   GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;105   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;106   GPIO_Init(GPIOB, &GPIO_InitStructure); 107 }108 109 void lcd1602_init(void)110 {111    hw_init();112    113    LCD1602_writeCmd(CMD_CARET_SHIFT(R_SHIFT));114    LCD1602_writeCmd(CMD_DISP_ONOFF(DISPLAY_ON));115    LCD1602_writeCmd(CMD_FUN_SET(DATA_WIDTH_8BITS,ROW_NUM_2,DOT_MATRIC_5_10));116    LCD1602_writeCmd(CMD_CLR);117    LCD1602_writeCmd(CMD_CARET_REWIND);118 }119 120 /* u8 lcd1602_ReadStatus(void)121 {122     LCD1602_RS = 0;123     LCD1602_RW = 1;124     return (LCD1602_DATA&0X80)?1:0;125 } */126 127 void lcd1602_display(u8*string)128 {129     int i = 0;130     LCD1602_writeCmd(CMD_CLR);131     LCD1602_writeCmd(CMD_CARET_REWIND);132     while(*string)133     {134        LCD1602_writeData(*string++); 135        ++i;136        if(i == 16) LCD1602_writeCmd(CMD_DDRAM(0x40));137     }138 }139 140 void lcd1602_display_pos(int row,int column, u8*string)141 {142     int pos = (row-1)*0x40+column;143     LCD1602_writeCmd(CMD_DDRAM(pos));144     while(*string)145     {146        LCD1602_writeData(*string++); 147     }148 }
LCD1602.C

 

STM32F302 驱动 LCD1602