Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * File: main.c
- * Author: Roniere
- *
- * Created on 28 de Novembro de 2016, 23:40
- */
- #include <p18f4550.h>
- #include <delays.h>
- #include <adc.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include "biblioteca_lcd_2x16.h"
- #include "config_PIC18F4550.h"
- #define LED_green LATBbits.LATB0
- #define LED_red LATBbits.LATB1
- #define LED_yellow LATBbits.LATB2
- #define LED_blue LATBbits.LATB3
- unsigned buffer [16];
- float voltage_value;
- float temp_value;
- float temp;
- float voltage_convert(float converter_value)
- {
- return(converter_value*5000)/1023;
- }
- float temp_convert(float temp_value)
- {
- return temp_value/2;
- }
- unsigned int channel_filter()
- {
- unsigned int filter_count;
- unsigned long channel_value = 0;
- for(filter_count = 0;filter_count < 256; filter_count++)
- {
- ConvertADC();
- while(BusyADC());
- channel_value += ReadADC();
- }
- return(channel_value >> 8);
- }
- void main(void)
- {
- unsigned int conv_outcome;
- OpenADC(ADC_FOSC_32 // Fosc = 10 MHz. Tad = 32/20M = 1,6 us
- &ADC_RIGHT_JUST // Justified outcome to the right
- &ADC_2_TAD, // Sets the Automatic Aquisition Time (2&Tad = 3,2 us)
- ADC_CH0 // Selects the channel 0
- &ADC_INT_OFF // Disabled interruption
- &ADC_VREFPLUS_VDD // Vref+ = Vcc
- &ADC_VREFMINUS_VSS, // Vref- = Vss
- ADC_1ANA); // Enables only channel AN0
- Delay10TCYx(5);
- ADCON1bits.PCFG = 0b1101;
- TRISAbits.TRISA0 = 1;
- TRISBbits.TRISB0 = 0;
- TRISBbits.TRISB1 = 0;
- TRISBbits.TRISB2 = 0;
- TRISBbits.TRISB3 = 0;
- TRISD = 0x00;
- PORTD = 0x00;
- lcd_inicia(0x28,0x0f,0x06);
- lcd_LD_cursor(0);
- while(1)
- {
- SetChanADC(ADC_CH0);
- Delay10TCYx(5);
- ConvertADC();
- while(BusyADC());
- conv_outcome = channel_filter();
- voltage_value = voltage_convert(conv_outcome);
- lcd_posicao(1,1);
- sprintf(buffer,"Pot: %04ld mV ",(long)voltage_value);
- lcd_LD_cursor(0);
- imprime_buffer_lcd(buffer,16);
- Delay10TCYx(100);
- SetChanADC(ADC_CH1);
- Delay10TCYx(5);
- ConvertADC();
- while(BusyADC());
- temp_value = channel_filter();//channel_filter;
- temp = temp_convert(temp_value);
- lcd_posicao(2,1);
- sprintf(buffer,"Temp: %02ld C ",(long)temp);
- lcd_LD_cursor(0);
- imprime_buffer_lcd(buffer,16);
- Delay10TCYx(100);
- if(voltage_value >= 2000)
- LED_green = 1;
- else
- LED_green = 0;
- if (temp >= 50)
- LED_red = 1;
- else
- LED_red = 0;
- if (temp >= 30 && temp < 50)
- LED_yellow = 1;
- else
- LED_yellow = 0;
- if (temp < 0)
- LED_blue = 1;
- else
- LED_blue = 0;
- Delay10TCYx(10000);
- //lcd_LD_cursor(0);
- }
- return;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement