lcd.c 3.6 KB

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