Guest User

Untitled

a guest
Jan 23rd, 2024
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.91 KB | Source Code | 0 0
  1. #include "stm32f4xx_hal.h"
  2. #include "lcd.h"
  3.  
  4. static uint8_t switchedTo4BitMode = 0;
  5.  
  6. void delayUS_DWT(uint32_t us);
  7. void delayUS_DWT_Init(void);
  8.  
  9. void lcdWriteCommand(uint8_t command) {
  10.     HAL_GPIO_WritePin(LCD_RS_PORT, LCD_RS_PIN, GPIO_PIN_RESET);
  11.     if (switchedTo4BitMode){
  12.         lcdWrite(command);
  13.         lcdWrite(command << 4);
  14.     }else {
  15.         lcdWrite(command);
  16.     }
  17.     if ((command == LCD_COMMAND_CLEAR) || (command == LCD_COMMAND_RETURN_HOME)) {
  18.         delayUS_DWT(1640);
  19.     } else {
  20.         delayUS_DWT(40);
  21.     }
  22. }
  23.  
  24. void lcdWriteData(uint8_t data) {
  25.     HAL_GPIO_WritePin(LCD_RS_PORT, LCD_RS_PIN, GPIO_PIN_SET);
  26.     if (switchedTo4BitMode){
  27.         lcdWrite(data);
  28.         lcdWrite(data << 4);
  29.     }else {
  30.         lcdWrite(data);
  31.     }
  32. }
  33.  
  34. void lcdWrite(uint8_t data) {
  35.     //data/command
  36.     HAL_GPIO_WritePin(LCD_DB4_PORT, LCD_DB4_PIN, data & (1 << 4));
  37.     HAL_GPIO_WritePin(LCD_DB5_PORT, LCD_DB5_PIN, data & (1 << 5));
  38.     HAL_GPIO_WritePin(LCD_DB6_PORT, LCD_DB6_PIN, data & (1 << 6));
  39.     HAL_GPIO_WritePin(LCD_DB7_PORT, LCD_DB7_PIN, data & (1 << 7));
  40.     delayUS_DWT(1);
  41.     //EN high
  42.     HAL_GPIO_WritePin(LCD_EN_PORT, LCD_EN_PIN, GPIO_PIN_SET);
  43.     //wait 1 us
  44.     delayUS_DWT(1);
  45.     //EN low
  46.     HAL_GPIO_WritePin(LCD_EN_PORT, LCD_EN_PIN, GPIO_PIN_RESET);
  47.     delayUS_DWT(1);
  48. }
  49.  
  50. void lcdInit() {
  51.     delayUS_DWT_Init();
  52.     HAL_Delay(15);
  53.     lcdWriteCommand(LCD_COMMAND_FUNCTION_SET | LCD_PARAM_FUNCTION_SET_8BIT); // set interface - 8-bit
  54.     HAL_Delay(4);
  55.     lcdWriteCommand(LCD_COMMAND_FUNCTION_SET | LCD_PARAM_FUNCTION_SET_8BIT); // set interface - 8-bit
  56.     HAL_Delay(1);
  57.     lcdWriteCommand(LCD_COMMAND_FUNCTION_SET | LCD_PARAM_FUNCTION_SET_8BIT); // set interface - 8-bit
  58.     HAL_Delay(1);
  59.     lcdWriteCommand(LCD_COMMAND_FUNCTION_SET); // set interface - 4-bit
  60.     HAL_Delay(1);
  61.     switchedTo4BitMode = 1;
  62.     lcdWriteCommand(LCD_COMMAND_FUNCTION_SET | LCD_PARAM_FUNCTION_SET_2LINES); // set interface - 4-bit, 2 lines, 5x8 font
  63.     lcdWriteCommand(LCD_COMMAND_ON_OFF); // off - nothing to be on
  64.     lcdWriteCommand(LCD_COMMAND_CLEAR); // clear
  65.     lcdWriteCommand(LCD_COMMAND_ENTRY_MODE_SET | LCD_PARAM_ENTRY_MODE_SET_INCREMENT); // scroll
  66.     lcdWriteCommand(LCD_COMMAND_ON_OFF | LCD_PARAM_ON_OFF_DISPLAY); // display on
  67.  
  68. }
  69.  
  70. void lcdString(char str[]) {
  71.     uint8_t i = 0;
  72.     while (str[i] != 0) {
  73.         lcdWriteData(str[i++]);
  74.     }
  75. }
  76.  
  77. void lcdGotoXY(uint8_t x, uint8_t y){
  78.     lcdWriteCommand(LCD_COMMAND_SET_DDRAM_ADDRESS | (x + y * 0x40));
  79. }
  80.  
  81. void lcdDefineChar(uint8_t charDefinition[], uint8_t code) {
  82.     lcdWriteCommand(LCD_COMMAND_SET_CGRAM_ADDRESS + (code % 8) * 8);
  83.     for (uint8_t i = 0; i < 8; i++) {
  84.         lcdWriteData(charDefinition[i]);
  85.     }
  86. }
  87.  
  88. void delayUS_DWT_Init(void) {
  89.     CoreDebug->DEMCR |= CoreDebug_DEMCR_TRCENA_Msk;
  90.     DWT->CTRL |= DWT_CTRL_CYCCNTENA_Msk;
  91. }
  92.  
  93. #pragma GCC push_options
  94. #pragma GCC optimize ("O3")
  95. void delayUS_DWT(uint32_t us) {
  96.     volatile uint32_t cycles = (SystemCoreClock / 1000000L) * us;
  97.     volatile uint32_t start = DWT->CYCCNT;
  98.     while(DWT->CYCCNT - start < cycles);
  99. }
  100. #pragma GCC pop_options
Advertisement
Add Comment
Please, Sign In to add comment