Sao mình sử dụng đoạn code này nạp vào modun STM32F407VG-DISCOVERY để text cái LCD 16x2 mà toàn ra màn hinh sáng hết. Không biết mình sai chổ nào nữa.
Code "lcd.h"
Code "main.c"
Nhưng mà mình sử dụng code do ST cung cấp thì sử dụng được
main
Mong mọi người giúp đỡ.
Code "lcd.h"
Code:
#include "stm32f4xx.h" #define LCD_RS_GPIO_PORT GPIOB #define LCD_RS_GPIO_CLK RCC_AHB1Periph_GPIOB #define LCD_RS_GPIO_PIN GPIO_Pin_0 #define LCD_RW_GPIO_PORT GPIOB #define LCD_RW_GPIO_CLK RCC_HB1Periph_GPIOB #define LCD_RW_GPIO_PIN GPIO_Pin_1 #define LCD_EN_GPIO_PORT GPIOB #define LCD_EN_GPIO_CLK RCC_AHB1Periph_GPIOB #define LCD_EN_GPIO_PIN GPIO_Pin_2 #define LCD_D4 GPIO_Pin_4 #define LCD_D5 GPIO_Pin_5 #define LCD_D6 GPIO_Pin_6 #define LCD_D7 GPIO_Pin_7 #define LCD_DATA_GPIO_PINS (LCD_D4|LCD_D5|LCD_D6|LCD_D7) #define LCD_DATA_GPIO_PORT GPIOB #define LCD_DATA_GPIO_CLK RCC_AHB1Periph_GPIOB void lcd_init(void); void LCD_STROBE(void); void LCD_DATA(u8 cX ); void lcd_write(u8 c); void lcd_clear(void); void lcd_putchar(u8 c); void lcd_putsf(u8 *c); void lcd_gotoxy(u8 row,u8 col); void LCD_Delay_US(__IO uint32_t num) { __IO uint32_t index = 0; for(index = (72 * num); index != 0; index--) { } } void LCD_STROBE(void) { GPIO_SetBits(LCD_DATA_GPIO_PORT, LCD_EN_GPIO_PIN); LCD_Delay_US(30); GPIO_ResetBits(LCD_DATA_GPIO_PORT,LCD_EN_GPIO_PIN) ; LCD_Delay_US(30); } void LCD_DATA(u8 cX ) { GPIO_WriteBit(LCD_DATA_GPIO_PORT, LCD_D4, (BitAction)(cX & 0x01) ); GPIO_WriteBit(LCD_DATA_GPIO_PORT, LCD_D5, (BitAction)((cX & 0x02) >> 1) ); GPIO_WriteBit(LCD_DATA_GPIO_PORT, LCD_D6, (BitAction)((cX & 0x04) >> 2) ); GPIO_WriteBit(LCD_DATA_GPIO_PORT, LCD_D7, (BitAction)((cX & 0x08) >> 3) ); } void lcd_write(u8 c) { LCD_DATA( c >> 4 ); LCD_STROBE(); LCD_DATA( c ); LCD_STROBE(); } void lcd_clear(void) { GPIO_ResetBits(LCD_DATA_GPIO_PORT,LCD_RS_GPIO_PIN) ; lcd_write(0x01); LCD_Delay_US(2000); } void lcd_putchar(u8 c) { GPIO_SetBits(LCD_DATA_GPIO_PORT,LCD_RS_GPIO_PIN); lcd_write( c ); } void lcd_putsf(u8 *c) { GPIO_SetBits(LCD_DATA_GPIO_PORT,LCD_RS_GPIO_PIN); while(*c) lcd_write(*c++); } void lcd_gotoxy(u8 row,u8 col) { GPIO_ResetBits(LCD_DATA_GPIO_PORT,LCD_RS_GPIO_PIN) ; switch(row) { case 0 : lcd_write( 0x80 + col ) ; //dia chi bat dau + col (cua LCD) break ; case 1 : lcd_write( 0xC0 + col ) ; break ; case 2 : lcd_write( 0x94 + col ) ; break ; case 3 : lcd_write( 0xD4 + col ) ; break ; } } void lcd_init(void) { GPIO_InitTypeDef GPIO_InitStruct; RCC_APB2PeriphClockCmd(LCD_DATA_GPIO_CLK, ENABLE); GPIO_InitStruct.GPIO_Pin = LCD_RS_GPIO_PIN | LCD_RW_GPIO_PIN | LCD_EN_GPIO_PIN | LCD_D4 | LCD_D5 | LCD_D6 | LCD_D7 ; GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF; GPIO_Init(LCD_DATA_GPIO_PORT, &GPIO_InitStruct); GPIO_ResetBits(LCD_DATA_GPIO_PORT,LCD_RS_GPIO_PIN) ; GPIO_ResetBits(LCD_DATA_GPIO_PORT,LCD_EN_GPIO_PIN) ; GPIO_ResetBits(LCD_DATA_GPIO_PORT,LCD_RW_GPIO_PIN) ; LCD_Delay_US(15000); LCD_DATA(0x03); LCD_STROBE(); LCD_Delay_US(5000); LCD_STROBE(); LCD_Delay_US(200); LCD_STROBE(); LCD_Delay_US(200); LCD_DATA(2); LCD_STROBE(); lcd_write(0x28); // Set interface length lcd_write(0x0C); // Display On, Cursor On, Cursor Blink lcd_clear(); // Clear screen lcd_write(0x6); }
Code:
#include "stm32f4xx.h" #include "main.h" int main (void) { lcd_init(); lcd_clear(); while(1) { lcd_gotoxy(0,2); lcd_putsf(" DIEN TU "); } } #ifdef USE_FULL_ASSERT /** * @brief Reports the name of the source file and the source line number * where the assert_param error has occurred. * @param file: pointer to the source file name * @param line: assert_param error line source number * @retval None */ void assert_failed(uint8_t* file, uint32_t line) { /* User can add his own implementation to report the file name and line number, ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */ while (1) {} } #endif
Code:
#include "stm32f4xx.h" #define LCM_OUT GPIOB->ODR // connect RW leg to GND, and power supply rails to +5V and gnd // Define symbolic LCM - MCU pin mappings // We've set DATA PIN TO 4,5,6,7 for easy translation // #define LCM_PIN_RS GPIO_Pin_0 // P1.0 #define LCM_PIN_EN GPIO_Pin_2 // P1.2 #define LCM_PIN_D7 GPIO_Pin_7 // P1.7 #define LCM_PIN_D6 GPIO_Pin_6 // P1.6 #define LCM_PIN_D5 GPIO_Pin_5 // P1.5 #define LCM_PIN_D4 GPIO_Pin_4 // P1.4 GPIO_InitTypeDef GPIO_InitStructure; #define LCM_PIN_MASK ((LCM_PIN_RS | LCM_PIN_EN | LCM_PIN_D7 | LCM_PIN_D6 | LCM_PIN_D5 | LCM_PIN_D4)) #define FALSE 0 #define TRUE 1 void __delay_cycles(int a) { int i = 0; int f = 0; while(f<a) { while(i<60) {i++;} f++; } } void PulseLcm() { LCM_OUT &= ~LCM_PIN_EN; __delay_cycles(220); LCM_OUT |= LCM_PIN_EN; __delay_cycles(220); LCM_OUT &= (~LCM_PIN_EN); __delay_cycles(220); } void SendByte(char ByteToSend, int IsData) { LCM_OUT &= (~LCM_PIN_MASK); LCM_OUT |= (ByteToSend & 0xF0); if (IsData == TRUE) { LCM_OUT |= LCM_PIN_RS; } else { LCM_OUT &= ~LCM_PIN_RS; } PulseLcm(); LCM_OUT &= (~LCM_PIN_MASK); LCM_OUT |= ((ByteToSend & 0x0F) << 4); if (IsData == TRUE) { LCM_OUT |= LCM_PIN_RS; } else { LCM_OUT &= ~LCM_PIN_RS; } PulseLcm(); } void Cursor(char Row, char Col) { char address; if (Row == 0) { address = 0; } else { address = 0x40; } address |= Col; SendByte(0x80 | address, FALSE); } void ClearLcmScreen() { SendByte(0x01, FALSE); SendByte(0x02, FALSE); } void InitializeLcm(void) { RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE); GPIO_InitStructure.GPIO_Pin =GPIO_Pin_0 | GPIO_Pin_1| GPIO_Pin_4 | GPIO_Pin_5| GPIO_Pin_6| GPIO_Pin_7; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; GPIO_Init(GPIOB, &GPIO_InitStructure); LCM_OUT &= ~(LCM_PIN_MASK); __delay_cycles(32000); __delay_cycles(32000); __delay_cycles(32000); LCM_OUT &= ~LCM_PIN_RS; LCM_OUT &= ~LCM_PIN_EN; LCM_OUT = 0x20; PulseLcm(); SendByte(0x28, FALSE); SendByte(0x0E, FALSE); SendByte(0x06, FALSE); } void PrintStr(char *Text) { char *c; c = Text; while ((c != 0) && (*c != 0)) { SendByte(*c, TRUE); c++; } } //***************************************************************
Code:
#include "stm32f4xx.h" #include "lcd.h" int main(void) { int a = 0; while(a<100) { __delay_cycles(32000); a++; } InitializeLcm(); __delay_cycles(32000); ClearLcmScreen(); __delay_cycles(32000); Cursor(0,0); PrintStr("DIEN TU"); Cursor(1,0); __delay_cycles(32000); PrintStr("VIET NAM"); while(1); } //****************************************************** #ifdef USE_FULL_ASSERT /** * @brief Reports the name of the source file and the source line number * where the assert_param error has occurred. * @param file: pointer to the source file name * @param line: assert_param error line source number * @retval None */ void assert_failed(uint8_t* file, uint32_t line) { /* User can add his own implementation to report the file name and line number, ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */ /* Infinite loop */ while (1) { } } #endif /** * @} */ /** * @} */ /******************* (C) COPYRIGHT 2009 ARMVietNam *****END OF FILE****/
Comment