lcd-twi.c 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. #include "lcd-twi.h"
  2. const char symbols[8][8] = {{ 0x00, 0x0E, 0x1F, 0x1F, 0x1F, 0x0E, 0x00, 0x00 }, /* 0 Circle */
  3. { 0x08, 0x14, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00 }, /* 1 DEGREE */
  4. { 0x00, 0x04, 0x0A, 0x11, 0x11, 0x11, 0x11, 0x11 }, /* 2 THERMOCOUPLE */
  5. { 0x04, 0x04, 0x1F, 0x04, 0x0A, 0x11, 0x1F, 0x04 }, /* 3 DIODE */
  6. { 0x04, 0x0E, 0x0A, 0x0A, 0x0A, 0x0A, 0x0E, 0x04 }, /* 4 RESISTOR */
  7. { 0x03, 0x0A, 0x08, 0x0B, 0x0A, 0x08, 0x1B, 0x02 }, /* 5 MOSFET */
  8. { 0x08, 0x08, 0x04, 0x02, 0x01, 0x08, 0x08, 0x08 }, /* 6 OPEN */
  9. { 0x08, 0x08, 0x04, 0x04, 0x02, 0x0F, 0x0A, 0x08 }};/* 7 SHORT */
  10. void I2C_SendByte(uint8_t byte)
  11. {
  12. cli();
  13. TWCR |= (1 << TWINT) | (1 << TWSTA);
  14. while(!(TWCR & (1 << TWINT)));
  15. TWDR = LCDI2CADDR;
  16. TWCR &= ~(1 << TWSTA);
  17. TWCR |= (1 << TWINT);
  18. while(!(TWCR & (1 << TWINT)));
  19. TWDR = byte;
  20. TWCR |= (1 << TWINT);
  21. while(!(TWCR & (1 << TWINT)));
  22. TWCR |= (1 << TWINT) | (1 << TWSTO);
  23. sei();
  24. }
  25. void LCD_SendData(uint8_t data)
  26. {
  27. I2C_SendByte((data & 0xF0) | LCD_RS | LCD_E);
  28. I2C_SendByte((data & 0xF0) | LCD_RS);
  29. I2C_SendByte((data << 4) | LCD_RS | LCD_E);
  30. I2C_SendByte((data << 4) | LCD_RS);
  31. _delay_us(4);
  32. }
  33. void LCD_SendCmd(uint8_t cmd)
  34. {
  35. I2C_SendByte(((cmd & 0xF0) & ~LCD_RS) | LCD_E);
  36. I2C_SendByte((cmd & 0xF0) & ~LCD_RS);
  37. I2C_SendByte(((cmd << 4) & ~LCD_RS) | LCD_E);
  38. I2C_SendByte((cmd << 4) & ~LCD_RS);
  39. if(cmd == LCD_CLR)
  40. {
  41. _delay_ms(6);
  42. }
  43. else
  44. {
  45. _delay_us(10);
  46. }
  47. }
  48. void LCD_SetPos(uint8_t x, uint8_t y)
  49. {
  50. y = ((y & 0x01) * 0x40);
  51. x &= 0x0F;
  52. LCD_SendCmd(LCD_DDRAM | (x + y));
  53. }
  54. void LoadSymbols()
  55. {
  56. for(uint8_t i = 0; i < 8; i++)
  57. {
  58. LCD_SendCmd(LCD_CGRAM | (0x08 * i));
  59. for(uint8_t j = 0; j < 8; j++)
  60. {
  61. LCD_SendData(symbols[i][j]);
  62. }
  63. }
  64. }
  65. void LCD_Init(void)
  66. {
  67. I2C_SendByte(0x00);
  68. _delay_ms(50);
  69. for(uint8_t i = 0; i < 5; i++)
  70. {
  71. I2C_SendByte(0x00 | LCD_E);
  72. I2C_SendByte(0x00 & ~LCD_E);
  73. }
  74. _delay_us(50);
  75. I2C_SendByte(LCD_WIDTH | LCD_E);
  76. I2C_SendByte(LCD_WIDTH & ~LCD_E);
  77. LCD_SendCmd(LCD_WIDTH | LCD_WIDTH_N | LCD_WIDTH_FT1);
  78. LCD_SendCmd(LCD_ONOFF);
  79. LCD_SendCmd(LCD_CLR);
  80. LCD_SendCmd(LCD_DIR | LCD_DIR_ID);
  81. LCD_SendCmd(LCD_ONOFF | LCD_ONOFF_D);
  82. LoadSymbols();
  83. }