STM32 practical application (I) -- 1602 Bluetooth Clock 1 LCD Display Test, 20171602

Source: Internet
Author: User

STM32 practical application (I) -- 1602 Bluetooth Clock 1 LCD Display Test, 20171602

After studying for so long from 51 to ikef4, I finally got a clue. Now I have learned the basic usage of GPIO, interrupt, timer, and watchdog, so I want to see if I can do something, I just want to review my recent knowledge. Last semester, I designed a single-chip microcomputer course with a clock that can be used for Bluetooth, button calibration, and temperature display. So I want to see if I can port the program to STM32? After three days, several program modifications and debugging finally succeeded!

For details about the clock implemented by 51 single-chip microcomputer, refer to my previous blog post, which contains a demo video link.

Single-Chip Microcomputer Course Design-electronic clock based on AT89S52 single-chip microcomputer and DS1302 clock chip (which can be calibrated by bluetooth)

Because the Internal timer of STM32 is still very accurate (one hour slow 1 s), so DS1302 clock chip is not used (the key is not in hand), the display is somewhat different from the course design, if no date or week is added, the time, alarm clock, and temperature are displayed.

Today, let's take a look at the 1602 LCD display. How can we use STM32 to control the characters, strings, or numbers displayed on LCD1602? 1602 LCD Introduction

Industrial liquid crystal, capable of displaying 16x02 characters at the same time. A 1602-inch LCD is also called a 1602-inch liquid crystal. It is a lattice LCD module used to display letters, numbers, and symbols. It consists of several 5x7 or 5x11 characters, each of which can display a single character. Each character has a dot-distance interval, and each line also has an interval, it plays a role in Character spacing and row spacing. Because of this, it cannot display graphs very well (with custom CGRAM, the display effect is not good ). Most character liquid crystal on the market is based on the HD44780 LCD chip, and the control principle is the same. Therefore, the control program based on HD44780 can be easily applied to most of the character liquid crystal on the market. A 1602LCD is a display of 16X2 characters, that is, two lines can be displayed, each line of 16 characters LCD module (display characters and numbers ).

Because the 1602 LCD is powered by 5 v, the pin height is 5 v. Unlike 51 Single-chip I/O Output High Level 5 v, STM32 high level only 3.3 v, low level 0 v, found the 1602 data manual, it did not show that the High Level voltage range, In the end can drive? I also found the Data Manual for the 1602 driver chip HD44780, which has a voltage feature, such

It can be seen that the 3.3v level drive display with STM32 is completely OK. I am relieved to see it. After several modifications to the Code, it is finally displayed successfully, so I still need to look at the data manual.

Hardware circuit connection


Program Design 1. macro definition of control line, through bit band operation, we can operate like RS = 1 like 51 in the future, isn't it very familiar? The eight ports of the data line are also defined as an LCD _DB
#define LCD_RS PAout(1)#define LCD_RW PAout(4)#define LCD_EN PAout(6)#define LCD_DB GPIO_Pin_8 | GPIO_Pin_9 | GPIO_Pin_10 | GPIO_Pin_11 |GPIO_Pin_12 | GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_15
2. The GPIO configuration connected to the LCD. The data cable port is configured as the OD (open-drain) output mode, which can be used for Bidirectional IO. When detecting whether the LCD is busy, you need to read the status of D7 bits.
Void LCD _GPIO_Config (void) {GPIO_InitTypeDef IO_Init; initialize (optional | enabled, ENABLE);/* control line initialization: */initialize = GPIO_Mode_OUT; // output struct = GPIO_OType_PP; // push-pull mode export = GPIO_Pin_1 | GPIO_Pin_4 | GPIO_Pin_6; bandwidth = GPIO_PuPd_UP; // bandwidth = GPIO_Speed_2MHz; // GPIO_Speed_2MHz GPIO_Init (GPIOA, & IO_Init ); /* initialize the data line */export = GPIO_Mode_OUT; export = GPIO_OType_OD; // bidirectional export = LCD _DB; export = GPIO_PuPd_UP; // IO_Init.GPIO_Speed = GPIO_Speed_2MHz; GPIO_Init (GPIOE, & IO_Init);/* test: High-Level 3.3 v LCD _RS = 1; LCD _RW = 1; LCD _EN = 1; GPIO_SetBits (GPIOE, LCD _DB );*/}


3. Implement the P1 = 0xff function with Parameters
Void GPIO_OutData (u8 Dat) {2010tmp; tmp = 0; tmp = Dat; tmp <= 8; // move the data left to the high 8-bit GPIO_Write (GPIOE, tmp ); // write the data to the GPIOE 8-bit high}
4. Check whether the LCD is busy
void LCD_CheckBusy(void){    u8 sta;    GPIO_OutData(0xff);    LCD_RS=0;    LCD_RW=1;    do{        LCD_EN=1;        delay_ms(5);        sta = GPIO_ReadInputDataBit(GPIOE,GPIO_Pin_15);        LCD_EN =0;    }while(sta & 0x80);}


5. Write one byte of data to the LCD
/* LCD _RS = 1, LCD _RW = 0 */void LCD _WriteData (u8 Dat) {LCD _CheckBusy (); // When busy, wait until LCD _RS = 1; LCD _RW = 0; GPIO_OutData (Dat ); LCD _EN = 1; delay_ms (1); LCD _EN = 0 ;}
6. Write a one-byte command to the LCD.
/* LCD _RS = 0, LCD _RW = 0 */void LCD _WriteCmd (u8 Cmd) {LCD _CheckBusy (); // wait until LCD _RS = 0; LCD _RW = 0; GPIO_OutData (Cmd ); LCD _EN = 1; delay_ms (1); LCD _EN = 0 ;}


7. LCD Initialization
Void LCD _Init (void) {LCD _WriteCmd (0x38); LCD _WriteCmd (0x0C); LCD _WriteCmd (0x06);/* display cursor movement settings */delay_ms (1 ); LCD _WriteCmd (0x01);/* display clear screen */}
8. LCD clear screen
void LCD_ClearScrren(void){    LCD_WriteCmd(0x01);}


9. Write the corresponding data location according to the xy Coordinate
void LCD_SetCursor(u8 x, u8 y){    u8 addr;    if (y == 0)        addr = 0x00 + x;    else        addr = 0x40 + x;    LCD_WriteCmd(addr | 0x80);}
10. display a character based on xy coordinates
Void LCD _DisChar (u8 x, u8 y, u8 ch) {LCD _SetCursor (x, y); // set the character display position to LCD _WriteData (ch );}
11. display two numbers
void LCD_DisNumber(u8 x,u8 y,u8 Num){    LCD_SetCursor(x,y);    LCD_WriteData(0x30+Num/10);    LCD_SetCursor(x+1,y);    LCD_WriteData(0x30+Num%10);}


12. Display strings
void LCD_DisString(u8 x,u8 y,u8 *str){    LCD_SetCursor(x, y);    while(*str != '\0')    {        LCD_WriteData(*str++);    }}
Main Function
int main(void){    delay_init(168);    LED_Init();        LCD_GPIO_Config();    LCD_Init();    LCD_ClearScrren();    while(1)    {        delay_ms(500);        LED1_ON;        LCD_DisString(0,0,"abcdefghijklmnop");        delay_ms(500);        LED1_OFF;        LCD_DisNumber(0,1,56);        LCD_DisChar(2,1,'a');        LCD_DisString(3,1," Hello World!");    }    }


Actual display effect:

The display is perfect. It is no different from the 51 driver.

Conclusion: in actual operation, it is okay not to use the busy detection function. 1602 is displayed as normal, and the next step is to increase the timer to display the clock. When setting the I/O port mode, you can set the mode to the open/Miss mode to achieve bidirectional IO, and output and read external input. With bitband operation and macro definition, you can directly operate the IO port output level like 51. Reference: 1602 characters LCD _ Baidu encyclopedia HD44780 device manual program source code download link: password: fk3b welcome to view my previous MCU Study Notes: STM32 Study Notes (five) -- general timer count delay STM32 learning notes (4) -- serial control LED (interrupt mode) STM32 learning notes (3) -- External Interrupt using STM32 learning notes (2) -- serial control LED STM32 Study Notes (1) -- lighting up an LED Single-Chip Modular programming PCF8591 Study Notes based on 51 Single-Chip IIC communication AT24C02 Study Notes based on 51 Single-Chip IIC communication the above is my personal understanding of the learning process, if something is wrong or inaccurate, you are welcome to correct it. April 23, 2017 14:24:45


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.