Advertisement
Roniere

Analog_to_Digital_Converter_Using_LCD_Display

Nov 30th, 2016
235
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.19 KB | None | 0 0
  1. /*
  2.  * File:   main.c
  3.  * Author: Roniere
  4.  *
  5.  * Created on 28 de Novembro de 2016, 23:40
  6.  */
  7.  
  8. #include <p18f4550.h>
  9. #include <delays.h>
  10. #include <adc.h>
  11. #include <stdio.h>
  12. #include <stdlib.h>
  13. #include <string.h>
  14. #include "biblioteca_lcd_2x16.h"
  15. #include "config_PIC18F4550.h"
  16.  
  17. #define LED_green  LATBbits.LATB0
  18. #define LED_red    LATBbits.LATB1
  19. #define LED_yellow LATBbits.LATB2
  20. #define LED_blue   LATBbits.LATB3
  21.  
  22. unsigned buffer [16];
  23. float voltage_value;
  24. float temp_value;
  25. float temp;
  26.  
  27. float voltage_convert(float converter_value)
  28. {
  29.     return(converter_value*5000)/1023;
  30. }
  31.  
  32. float temp_convert(float temp_value)
  33. {
  34.     return temp_value/2;
  35. }
  36.  
  37. unsigned int channel_filter()
  38. {
  39.     unsigned int filter_count;
  40.     unsigned long channel_value = 0;
  41.    
  42.     for(filter_count = 0;filter_count < 256; filter_count++)
  43.     {
  44.         ConvertADC();
  45.         while(BusyADC());
  46.         channel_value += ReadADC();
  47.     }
  48.     return(channel_value >> 8);
  49. }
  50.  
  51. void main(void)
  52. {
  53.     unsigned int conv_outcome;
  54.    
  55.     OpenADC(ADC_FOSC_32                 // Fosc = 10 MHz. Tad = 32/20M = 1,6 us
  56.         &ADC_RIGHT_JUST                 // Justified outcome to the right
  57.         &ADC_2_TAD,                     // Sets the Automatic Aquisition Time (2&Tad = 3,2 us)    
  58.         ADC_CH0                         // Selects the channel 0
  59.         &ADC_INT_OFF                    // Disabled interruption
  60.         &ADC_VREFPLUS_VDD               // Vref+ = Vcc
  61.         &ADC_VREFMINUS_VSS,             // Vref- = Vss
  62.         ADC_1ANA);                      // Enables only channel AN0
  63.    
  64.     Delay10TCYx(5);
  65.     ADCON1bits.PCFG = 0b1101;
  66.     TRISAbits.TRISA0 = 1;
  67.     TRISBbits.TRISB0 = 0;
  68.     TRISBbits.TRISB1 = 0;
  69.     TRISBbits.TRISB2 = 0;
  70.     TRISBbits.TRISB3 = 0;
  71.     TRISD = 0x00;
  72.     PORTD = 0x00;
  73.    
  74.     lcd_inicia(0x28,0x0f,0x06);
  75.     lcd_LD_cursor(0);
  76.    
  77.     while(1)
  78.     {
  79.         SetChanADC(ADC_CH0);
  80.         Delay10TCYx(5);
  81.         ConvertADC();
  82.         while(BusyADC());
  83.         conv_outcome = channel_filter();
  84.         voltage_value = voltage_convert(conv_outcome);
  85.        
  86.         lcd_posicao(1,1);
  87.         sprintf(buffer,"Pot: %04ld mV ",(long)voltage_value);
  88.         lcd_LD_cursor(0);
  89.         imprime_buffer_lcd(buffer,16);
  90.         Delay10TCYx(100);
  91.        
  92.         SetChanADC(ADC_CH1);
  93.         Delay10TCYx(5);
  94.         ConvertADC();
  95.         while(BusyADC());
  96.         temp_value = channel_filter();//channel_filter;
  97.         temp = temp_convert(temp_value);
  98.                
  99.         lcd_posicao(2,1);
  100.         sprintf(buffer,"Temp: %02ld C ",(long)temp);
  101.         lcd_LD_cursor(0);
  102.         imprime_buffer_lcd(buffer,16);
  103.         Delay10TCYx(100);
  104.        
  105.         if(voltage_value >= 2000)
  106.             LED_green = 1;
  107.         else
  108.             LED_green = 0;
  109.        
  110.         if (temp >= 50)
  111.             LED_red = 1;
  112.         else
  113.             LED_red = 0;
  114.        
  115.         if (temp >= 30 && temp < 50)
  116.             LED_yellow = 1;
  117.         else
  118.             LED_yellow = 0;
  119.        
  120.         if (temp < 0)
  121.             LED_blue = 1;
  122.         else
  123.             LED_blue = 0;
  124.        
  125.         Delay10TCYx(10000);
  126.         //lcd_LD_cursor(0);
  127.     }
  128.     return;
  129. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement