lcd.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /*
  2. * lcd.c
  3. *
  4. * Created on: 10/06/2018
  5. * Author: Olivier Van den Eede
  6. */
  7. #include "lcd.h"
  8. const uint8_t ROW_16[] = {0x00, 0x40, 0x10, 0x50};
  9. const uint8_t ROW_20[] = {0x00, 0x40, 0x14, 0x54};
  10. /************************************** Static declarations **************************************/
  11. static void lcd_write_data(Lcd_HandleTypeDef * lcd, uint8_t data);
  12. static void lcd_write_command(Lcd_HandleTypeDef * lcd, uint8_t command);
  13. static void lcd_write(Lcd_HandleTypeDef * lcd, uint8_t data, uint8_t len);
  14. /************************************** Function definitions **************************************/
  15. /**
  16. * Create new Lcd_HandleTypeDef and initialize the Lcd
  17. */
  18. Lcd_HandleTypeDef Lcd_create(
  19. Lcd_PortType port[], Lcd_PinType pin[],
  20. Lcd_PortType rs_port, Lcd_PinType rs_pin,
  21. Lcd_PortType en_port, Lcd_PinType en_pin, Lcd_ModeTypeDef mode)
  22. {
  23. Lcd_HandleTypeDef lcd;
  24. lcd.mode = mode;
  25. lcd.en_pin = en_pin;
  26. lcd.en_port = en_port;
  27. lcd.rs_pin = rs_pin;
  28. lcd.rs_port = rs_port;
  29. lcd.data_pin = pin;
  30. lcd.data_port = port;
  31. Lcd_init(&lcd);
  32. return lcd;
  33. }
  34. /**
  35. * Initialize 16x2-lcd without cursor
  36. */
  37. void Lcd_init(Lcd_HandleTypeDef * lcd)
  38. {
  39. if(lcd->mode == LCD_4_BIT_MODE)
  40. {
  41. lcd_write_command(lcd, 0x33);
  42. lcd_write_command(lcd, 0x32);
  43. lcd_write_command(lcd, FUNCTION_SET | OPT_N); // 4-bit mode
  44. }
  45. else
  46. lcd_write_command(lcd, FUNCTION_SET | OPT_DL | OPT_N);
  47. lcd_write_command(lcd, CLEAR_DISPLAY); // Clear screen
  48. lcd_write_command(lcd, DISPLAY_ON_OFF_CONTROL | OPT_D); // Lcd-on, cursor-off, no-blink
  49. lcd_write_command(lcd, ENTRY_MODE_SET | OPT_INC); // Increment cursor
  50. }
  51. /**
  52. * Write a number on the current position
  53. */
  54. void Lcd_int(Lcd_HandleTypeDef * lcd, int number)
  55. {
  56. char buffer[11];
  57. sprintf(buffer, "%d", number);
  58. Lcd_string(lcd, buffer);
  59. }
  60. /**
  61. * Write a string on the current position
  62. */
  63. void Lcd_string(Lcd_HandleTypeDef * lcd, char * string)
  64. {
  65. for(uint8_t i = 0; i < strlen(string); i++)
  66. {
  67. lcd_write_data(lcd, string[i]);
  68. }
  69. }
  70. /**
  71. * Set the cursor position
  72. */
  73. void Lcd_cursor(Lcd_HandleTypeDef * lcd, uint8_t row, uint8_t col)
  74. {
  75. #ifdef LCD20xN
  76. lcd_write_command(lcd, SET_DDRAM_ADDR + ROW_20[row] + col);
  77. #endif
  78. #ifdef LCD16xN
  79. lcd_write_command(lcd, SET_DDRAM_ADDR + ROW_16[row] + col);
  80. #endif
  81. }
  82. /**
  83. * Clear the screen
  84. */
  85. void Lcd_clear(Lcd_HandleTypeDef * lcd) {
  86. lcd_write_command(lcd, CLEAR_DISPLAY);
  87. }
  88. /************************************** Static function definition **************************************/
  89. /**
  90. * Write a byte to the command register
  91. */
  92. void lcd_write_command(Lcd_HandleTypeDef * lcd, uint8_t command)
  93. {
  94. HAL_GPIO_WritePin(lcd->rs_port, lcd->rs_pin, LCD_COMMAND_REG); // Write to command register
  95. if(lcd->mode == LCD_4_BIT_MODE)
  96. {
  97. lcd_write(lcd, (command >> 4), LCD_NIB);
  98. lcd_write(lcd, command & 0x0F, LCD_NIB);
  99. }
  100. else
  101. {
  102. lcd_write(lcd, command, LCD_BYTE);
  103. }
  104. }
  105. /**
  106. * Write a byte to the data register
  107. */
  108. void lcd_write_data(Lcd_HandleTypeDef * lcd, uint8_t data)
  109. {
  110. HAL_GPIO_WritePin(lcd->rs_port, lcd->rs_pin, LCD_DATA_REG); // Write to data register
  111. if(lcd->mode == LCD_4_BIT_MODE)
  112. {
  113. lcd_write(lcd, data >> 4, LCD_NIB);
  114. lcd_write(lcd, data & 0x0F, LCD_NIB);
  115. }
  116. else
  117. {
  118. lcd_write(lcd, data, LCD_BYTE);
  119. }
  120. }
  121. /**
  122. * Set len bits on the bus and toggle the enable line
  123. */
  124. void lcd_write(Lcd_HandleTypeDef * lcd, uint8_t data, uint8_t len)
  125. {
  126. for(uint8_t i = 0; i < len; i++)
  127. {
  128. HAL_GPIO_WritePin(lcd->data_port[i], lcd->data_pin[i], (data >> i) & 0x01);
  129. }
  130. HAL_GPIO_WritePin(lcd->en_port, lcd->en_pin, 1);
  131. DELAY(1);
  132. HAL_GPIO_WritePin(lcd->en_port, lcd->en_pin, 0); // Data receive on falling edge
  133. }