Advertisement
mutchuloko

Display 16x2

Dec 27th, 2015
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.04 KB | None | 0 0
  1. /*
  2.  */
  3.  
  4. #include <avr/io.h>
  5. #include <util/delay.h>
  6.  
  7. #define F_CPU 16000000
  8.  
  9. /*
  10.  *  Este codigo eh para ser usado com display LCD 16x2
  11.  *  no modo 8-bit
  12.  *
  13.  */
  14.  
  15.  
  16. //**************Definicoes para pinos de Controle***********
  17. #define RS      (1 << PB0)
  18. #define E       (1 << PB1)
  19.  
  20. //********************Definicoes do Display*****************
  21. #define Linha   2
  22. #define Coluna  16
  23.  
  24.  
  25. int main(void)
  26. {
  27.     DDRD  = 0xFF;
  28.     DDRB |= RS | E;
  29.  
  30.     iniciaDisplay();
  31.  
  32.     while(1){
  33.     }
  34.  
  35.  
  36.     return 0;
  37. }
  38.  
  39.  
  40. /*
  41.  * Esta inicializacao e recomEdada pelo datasheet do display
  42.  * para casos onde a fonte de Eergia nao e satisfatoria
  43.  */
  44.  
  45. void iniciaDisplay(void){
  46.  
  47.     PORTB &= ~RS; // Desabilitar o sinal de Register Select
  48.     PORTB &= ~E;  // Desabilitar o sinal de Enable
  49.     _delay_ms(20);
  50.     PORTD |= 0x30;
  51.     _delay_ms(5);
  52.     PORTD |= 0x30;
  53.     _delay_ms(1);
  54.     PORTD |= 0x30;
  55.     _delay_ms(10);
  56.  
  57.     functionSet();
  58.     clearDisplay();
  59.     entryMode();
  60.     displayOn();
  61. }
  62.  
  63. //Function Set para 8-Bits, duas linhas e 5x10 dots
  64. void functionSet(void){
  65.     /*
  66.      *  RS R/W DB7 DB6 DB5 DB4 DB3 DB2 DB1 DB0
  67.      *  0   0   0   0   1   1   1   1   *   *
  68.      */
  69.     PORTB &= ~RS; // Desabilitar o sinal de Register Select
  70.     PORTD |= 0x38;
  71.     PORTB |=  E;  // Desabilitar o sinal de Enable
  72.     _delay_ms(1);
  73.     PORTB &= ~E;
  74.     _delay_ms(1);
  75. }
  76.  
  77. // Limpa o Display
  78. void clearDisplay(void){
  79.     /*
  80.      *  RS R/W DB7 DB6 DB5 DB4 DB3 DB2 DB1 DB0
  81.      *  0   0   0   0   0   0   0   0   0   1
  82.      */
  83.     PORTB &= ~RS; // Desabilitar o sinal de Register Select
  84.     PORTD |= 0x01;
  85.     PORTB |=  E;
  86.     _delay_ms(1);
  87.     PORTB &= ~E;
  88.     _delay_ms(1);
  89. }
  90.  
  91. // Voltar o cursor a posicao 0 (Edereco 0)
  92. void cursorAtHome(void){
  93.     /*
  94.      *  RS R/W DB7 DB6 DB5 DB4 DB3 DB2 DB1 DB0
  95.      *  0   0   0   0   0   0   0   0   1   *
  96.      *  Testar DB0 com 0 ou 1
  97.      */
  98.  
  99.     PORTB &= ~RS; // Desabilitar o sinal de Register Select
  100.     PORTD |= 0x02;
  101.     PORTB |=  E;
  102.     _delay_ms(1);
  103.     PORTB &= ~E;
  104.     _delay_ms(1);
  105.  
  106. }
  107.  
  108. void displayOn(void){
  109.     /*
  110.      *  RS R/W DB7 DB6 DB5 DB4 DB3 DB2 DB1 DB0
  111.      *  0   0   0   0   0   0   1   1   1   1
  112.      *  Display On com cursor piscando
  113.      */
  114.     PORTB &= ~RS; // Desabilitar o sinal de Register Select
  115.     PORTD |= 0x0F;
  116.     PORTB |=  E;
  117.     _delay_ms(1);
  118.     PORTB &= ~E;
  119.     _delay_ms(1);
  120. }
  121.  
  122. void displayOff(void){
  123.     /*
  124.      *  RS R/W DB7 DB6 DB5 DB4 DB3 DB2 DB1 DB0
  125.      *  0   0   0   0   0   0   1   0   0   0
  126.      *  Display On com cursor piscando
  127.      */
  128.     PORTB &= ~RS; // Desabilitar o sinal de Register Select
  129.     PORTD |= 0x08;
  130.     PORTB |=  E;
  131.     _delay_ms(1);
  132.     PORTB &= ~E;
  133.     _delay_ms(1);
  134.  
  135. }
  136.  
  137. void entryMode(void){
  138.     /*
  139.      *  RS R/W DB7 DB6 DB5 DB4 DB3 DB2 DB1 DB0
  140.      *  0   0   0   0   0   0   0   1   1   0
  141.      *  Display On com cursor piscando
  142.      */
  143.     PORTB &= ~RS; // Desabilitar o sinal de Register Select
  144.     PORTD |= 0x06;
  145.     PORTB |=  E;
  146.     _delay_ms(1);
  147.     PORTB &= ~E;
  148.     _delay_ms(1);
  149. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement