luanpcs

Relatório 13

Jul 24th, 2021 (edited)
481
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.12 KB | None | 0 0
  1. // PINAGEM ARDUINO ATMega328p:
  2. // RS = 12 PB4
  3. // EN = 11 PB3
  4. // D4 = 5 PD5
  5. // D5 = 4 PD4
  6. // D6 = 3 PD3
  7. // D7 = 2 PD7
  8. #define P5 0b00100000
  9. #define P4 0b00010000
  10. #define P3 0b00001000
  11. #define P2 0b00000100
  12. // Data Pins
  13. #define LCD_D0 P5
  14. #define LCD_D1 P4
  15. #define LCD_D2 P3
  16. #define LCD_D3 P2
  17. // Control Pins
  18. #define LCD_RS P4
  19. #define LCD_EN P3
  20.  
  21. #define LCD_EN_ON PORTB |= LCD_EN
  22. #define LCD_EN_OFF PORTB &= ~LCD_EN
  23. #define LCD_RS_ON PORTB |= LCD_RS
  24. #define LCD_RS_OFF PORTB &= ~LCD_RS
  25. #define LCD_D0_ON PORTD |= LCD_D0
  26. #define LCD_D0_OFF PORTD &= ~LCD_D0
  27. #define LCD_D1_ON PORTD |= LCD_D1
  28. #define LCD_D1_OFF PORTD &= ~LCD_D1
  29. #define LCD_D2_ON PORTD |= LCD_D2
  30. #define LCD_D2_OFF PORTD &= ~LCD_D2
  31. #define LCD_D3_ON PORTD |= LCD_D3
  32. #define LCD_D3_OFF PORTD &= ~LCD_D3
  33. #define LCD_DATA_OFF PORTD &= (~(LCD_D0 + LCD_D1 + LCD_D2 + LCD_D3))
  34. #define LCD_DATA_DIR DDRD |= (LCD_D0 + LCD_D1 + LCD_D2 + LCD_D3)
  35. // Functions
  36. // Send a command to the display
  37. void LCDCmd(uint8_t cmd)
  38. {
  39.   LCD_RS_OFF;
  40.   sendnibble(cmd >> 4);
  41.   sendnibble(cmd & 0x0F);
  42. }
  43. // Routine to work with four bits
  44. void sendnibble(uint8_t data)
  45. {
  46.   LCD_DATA_OFF;
  47.   if ((data & 0x01) == 0x01) LCD_D0_ON;
  48.   if ((data & 0x02) == 0x02) LCD_D1_ON;
  49.   if ((data & 0x04) == 0x04) LCD_D2_ON;
  50.   if ((data & 0x08) == 0x08) LCD_D3_ON;
  51.   LCD_EN_ON;
  52.   delayLCD();
  53.   LCD_EN_OFF;
  54. }
  55. // Initializes display
  56. void InitLCD(void)
  57. {
  58.   LCD_DATA_DIR;
  59.   LCD_EN_OFF;
  60.   LCD_RS_OFF;
  61.   sendnibble(0x30 >> 4);
  62.   delayLCD();
  63.   sendnibble(0x30 >> 4);
  64.   delayLCD();
  65.   sendnibble(0x30 >> 4);
  66.   delayLCD();
  67.   sendnibble(0x20 >> 4);
  68.   delayLCD();
  69.   LCDCmd(0x28);
  70.   delayLCD();
  71.   LCDCmd(0x08);
  72.   delayLCD();
  73.   LCDCmd(0x0C);
  74.   delayLCD();
  75.   LCDCmd(0x01);
  76.   delayLCD();
  77. }
  78. // Delay to set up the display
  79. void delayLCD(void)
  80. {
  81.   _delay_ms(0.2);
  82. }
  83. // Send a message to the display
  84. void LCDPrintStr (const char *data)
  85. {
  86.   while (*data != 0)
  87.     LCDChar(*data++);
  88. }
  89. // Send a message to the display in the position X,Y
  90. void LCDPrintXYStr (uint8_t x, uint8_t y, const char *data)
  91. {
  92.   uint8_t pos;
  93.   pos = x - 1;
  94.   if (y == 1)
  95.   {
  96.     pos = pos + 0x80;
  97.     LCDCmd(pos);
  98.   }
  99.   else if (y == 2)
  100.   {
  101.     pos = pos + 0xc0;
  102.     LCDCmd(pos);
  103.   }
  104.   LCDPrintStr(data);
  105. }
  106. // Send a data to the display
  107. void LCDChar (uint8_t data)
  108. {
  109.   LCD_RS_ON;
  110.   sendnibble(data >> 4);
  111.   sendnibble(data & 0x0F);
  112. }
  113. void LCDPrintVal (unsigned int dado)
  114. {
  115.   if (dado >= 10000) LCDChar((dado / 10000) + 0x30);
  116.   if (dado >= 1000) LCDChar(((dado % 10000) / 1000) + 0x30);
  117.   if (dado >= 100) LCDChar((((dado % 10000) % 1000) / 100) + 0x30);
  118.   if (dado >= 10) LCDChar(((((dado % 10000) % 1000) % 100) / 10) + 0x30);
  119.   LCDChar(((((dado % 10000) % 1000) % 100) % 10) + 0x30);
  120. }
  121. void LCDPrintXYVal (unsigned char x, unsigned char y, unsigned int dado)
  122. {
  123.   unsigned char pos;
  124.   if (y == 1)
  125.   {
  126.     pos = pos + 0x80;
  127.     LCDCmd(pos);
  128.   }
  129.   else
  130.   {
  131.     pos = pos + 0x80;
  132.     LCDCmd(pos);
  133.     pos = pos + 0xc0;
  134.     LCDCmd(pos);
  135.   }
  136.   LCDPrintVal(dado);
  137. }
  138. int main()
  139. {
  140.   InitLCD();
  141.   while (1)
  142.   {
  143.     PORTB ^= P5;
  144.     _delay_ms(1000);
  145.     LCDPrintXYStr(4, 1, "Inatel");
  146.   }
  147. }
Add Comment
Please, Sign In to add comment