Electgpl

PIC - Liberia para LCD 2x16 HD44780 + ITOA

Aug 24th, 2017
215
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.86 KB | None | 0 0
  1. #include <16F676.h>
  2. #device adc=8                   // Configura ADC a 8bit
  3. #FUSES NOWDT                    // No Watch Dog Timer
  4. #FUSES INTRC_IO                 // Internal RC Osc, no CLKOUT
  5. #FUSES NOPROTECT                // Code not protected from reading
  6. #FUSES NOBROWNOUT               // No brownout reset
  7. #FUSES NOMCLR                   // Master Clear pin used for I/O
  8. #FUSES NOCPD                    // No EE protection
  9. #FUSES NOPUT                    // No Power Up Timer
  10. #use delay(int=4000000)         // Oscilador Interno
  11. #define LCD_RS  PIN_C0          // Register select
  12. #define LCD_EN  PIN_C1          // Enable
  13. #define LCD_D4  PIN_C2          // Data bits
  14. #define LCD_D5  PIN_C3          // Data bits
  15. #define LCD_D6  PIN_C4          // Data bits
  16. #define LCD_D7  PIN_C5          // Data bits
  17. #define LCD_STROBE (output_high(LCD_EN),output_low(LCD_EN))
  18. // Este código se conectará a un controlador LCD estándar
  19. // Como el Hitachi HD44780. Utiliza la pantalla LCD en modo de 4 bits, con
  20. // El hardware conectado de la siguiente manera (el estándar 14 pin
  21. // Se utiliza el conector LCD):
  22. // Los bits PORTB 0-3 están conectados a los bits de datos LCD 4-7 (nibble alto)
  23. // El bit 2 de PORTA está conectado a la entrada LCD RS (selección de registro)
  24. // El bit PORTA 3 está conectado al bit LCD EN (habilitacion)
  25. // Para utilizar estas rutinas, configure la E / S de puerto (TRISA, TRISB) y luego
  26. // llamar a lcd_init (), luego otras rutinas según sea necesario.
  27. // escribe un byte en la pantalla LCD en modo de 4 bits
  28. void lcd_write(unsigned char c){
  29.    if(c & 0x80)
  30.       output_high(LCD_D7);
  31.    else
  32.       output_low(LCD_D7);
  33.    if(c & 0x40)
  34.       output_high(LCD_D6);
  35.    else
  36.       output_low(LCD_D6);
  37.    if(c & 0x20)
  38.       output_high(LCD_D5);
  39.    else
  40.       output_low(LCD_D5);
  41.    if(c & 0x10)
  42.       output_high(LCD_D4);
  43.    else
  44.       output_low(LCD_D4);
  45.    LCD_STROBE;
  46.    if(c & 0x08)
  47.       output_high(LCD_D7);
  48.    else
  49.       output_low(LCD_D7);
  50.    if(c & 0x04)
  51.       output_high(LCD_D6);
  52.    else
  53.       output_low(LCD_D6);
  54.    if(c & 0x02)
  55.       output_high(LCD_D5);
  56.    else
  57.       output_low(LCD_D5);
  58.    if(c & 0x01)
  59.       output_high(LCD_D4);
  60.    else
  61.       output_low(LCD_D4);
  62.    LCD_STROBE;    
  63.    delay_us(40);
  64. }
  65. // Limpie y encienda el LCD
  66. void lcd_clear(void){
  67.    output_low(LCD_RS);
  68.    lcd_write(0x01);
  69.    delay_ms(2);
  70. }
  71. // Escribe un carácter en la pantalla LCD
  72. void lcd_putch(unsigned char c){
  73.    output_high(LCD_RS);                // Escribir caracteres
  74.    lcd_write(c);
  75. }
  76. // Escribir una cadena de caracteres en la pantalla LCD
  77. void lcd_puts(char *s){
  78.    output_high(LCD_RS);                // Escribir caracteres
  79.    while(*s)
  80.       lcd_write(*s++);
  81. }
  82. // Ir a la posición especificada
  83. void lcd_goto(unsigned char pos,unsigned char line){
  84.    output_low(LCD_RS);
  85.    if(line==0)
  86.       lcd_write(0x80 + pos);
  87.    else
  88.       lcd_write(0x80 + pos+ 0x40);
  89. }
  90. // Inicializar la pantalla LCD en modo de 4 bits
  91. void lcd_init(void){
  92.    output_low(LCD_RS);                 // Escribir bytes de control
  93.    delay_ms(15);                       // Retraso de encendido
  94.    output_high(LCD_D4);                // Inicializacion segun datasheet    
  95.    output_high(LCD_D5);                // Inicializacion segun datasheet  
  96.    LCD_STROBE;                         // Habilitacion / Deshabilitacion del LCD a demanda
  97.    delay_ms(5);                        // Delay de configuracion de datasheet
  98.    LCD_STROBE;                         // Habilitacion / Deshabilitacion del LCD a demanda  
  99.    delay_us(100);                      // Delay de configuracion de datasheet
  100.    LCD_STROBE;                         // Habilitacion / Deshabilitacion del LCD a demanda  
  101.    delay_ms(5);                        // Delay de configuracion de datasheet
  102.    output_low(LCD_D4);                 // Establecer el modo de 4 bits
  103.    LCD_STROBE;                         // Habilitacion / Deshabilitacion del LCD a demanda
  104.    delay_us(40);                       // Delay de configuracion de datasheet
  105.    lcd_write(0x28);                    // Modo de 4 bits, 1/16 de servicio, fuente 5x8, 2 líneas
  106.    lcd_write(0x0C);                    // Enciende Display
  107.    lcd_write(0x06);                    // Cursor de avance del modo de entrada
  108.    lcd_write(0x01);                    // Borrar la visualización y restablecer el cursor
  109. }
  110. char *itoa(long int num, char *s){     // Funcion ITOA (Entero to ASCII)
  111.      unsigned long int temp=1;         // Declaracion de valor temporal
  112.      unsigned int8 i, cnt=0;           // Declaracion de indices y contadores
  113.      char c;                           // Declaracion de variable de caracter de salida
  114.      while(temp>0){                    // Rutina de Conversion (Queda invertida)
  115.         temp=(num/10);                 // Conversion de caracter a caracter
  116.         s[cnt]=(num%10)+'0';           // utilizando divisiones y resto
  117.         if(s[cnt]>0x39)                // sumando el offset de la tabla ASCII
  118.            s[cnt]+=0x7;
  119.         cnt++;
  120.         num=temp;
  121.      }
  122.      for(i=0;i<(int8)(cnt/2);i++){     // Rutina para invertir el numero convertido
  123.         c=s[i];                        // Intercambio de variables
  124.         s[i]=s[cnt-i-1];
  125.         s[cnt-i-1]=c;
  126.      }
  127.      s[cnt]='\0';                      // Caracter nulo, fin de la conversion ITOA
  128.      return s;                         // Retorno del valor ASCII
  129. }
  130. void main(){
  131.    setup_adc_ports(sAN0|VSS_VDD);      // Configuracion ADC canal AN0. Vref = VDD
  132.    setup_adc(ADC_CLOCK_DIV_2);         // Velocidad de conversion
  133.    lcd_init();  
  134.    lcd_clear();
  135.    char string[4];
  136.    while(true){
  137.       lcd_goto(1,0);  
  138.       lcd_putch("TEST LIB. LCD");  
  139.       lcd_goto(2,1);
  140.       lcd_putch("VAL: ");
  141.       set_adc_channel(0);
  142.       delay_us(10);
  143.       itoa(read_adc(),string);
  144.       lcd_puts(string);
  145.       delay_ms(50);
  146.    }
  147. }
Advertisement
Add Comment
Please, Sign In to add comment