shomikc
Member level 4
Hello all,
I have written a test program that will take a voltage(square pulse of 4V max for 1 second and 2V min for 1 second and then repeat) and check if it is more than 3 volts or less than 3 volts.
My understanding of ADCON registers is nowhere near complete.
Can anyone please spare the time and please check the program listed below and tell me if it is wrong.
Thanks and Regards,
Shomik.
I have written a test program that will take a voltage(square pulse of 4V max for 1 second and 2V min for 1 second and then repeat) and check if it is more than 3 volts or less than 3 volts.
My understanding of ADCON registers is nowhere near complete.
Can anyone please spare the time and please check the program listed below and tell me if it is wrong.
Code C - [expand] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 #pragma config OSC = INTIO67 #pragma config FCMEN = OFF #pragma config IESO = OFF // CONFIG2L #pragma config PWRT = OFF // Power-up Timer Enable bit (PWRT enabled) #pragma config BOREN = OFF #pragma config BORV = 3 // Brown-out Reset Voltage bits (Minimum setting) // CONFIG2H #pragma config WDT = OFF #pragma config WDTPS = 32768 // Watchdog Timer Postscale Select bits (1:32768) // CONFIG3H #pragma config CCP2MX = PORTC #pragma config PBADEN = OFF #pragma config LPT1OSC = OFF #pragma config MCLRE = ON // CONFIG4L #pragma config STVREN = ON #pragma config LVP = OFF // Single-Supply ICSP Enable bit (Single-Supply ICSP enabled) #pragma config XINST = OFF #include <p18f4520.h> #include <delays.h> #define vref 5.00 void adc_INIT(); int adc_READ(int); void main() { int digital; float voltage; OSCCON = 0x72; // Set internal oscillator frequency to 8 MHz adc_INIT(); while(1) { digital = adc_READ(1); voltage = digital*(((float)vref)/((float)1023)); TRISB = 0 ; // Set Port B as Output if(voltage > 3.0) { LATBbits.LATB0 = 1; // RB_0 is high } else if(voltage < 3.0) { LATBbits.LATB1 = 1; // RB_1 is high } } } void adc_INIT() { TRISA = 0xFF; // Set Port A as the input port ADCON1 = 0x0D; // Set AN0 and AN1 as Analog Input ADCON0 = 0x05; // Select Channel 1 (AN1) and set AD Module ON (ADON = 1) ADCON2 = 0x92; // Right justified, 4*Tad, Fosc/32 } int adc_READ(int channel) { int digital; ADCON0 |= 0x02; // while(ADCON0bits.GO == 1) { digital = (ADRESH*256)|(ADRESL); } return(digital); }
Thanks and Regards,
Shomik.