Question: Using PIC18f1220, design a c code of two analogue to digital input voltage,(AN0 and AN1) to light up one LED. condition to be meet is,

Using PIC18f1220, design a c code of two analogue to digital input voltage,(AN0 and AN1) to light up one LED.

condition to be meet is, when :

AN0 and AN1 is more than 500, the LED ON

AN0 more than 500, AN1 less than 500, LED ON

AN1 more than 500, AN0 less than 500, LED ON

Both less than 500, LED OFF

below is my code. i only manage to make LED ON when AN0 is more than 500.

void InitIO(void) { TRISAbits.RA0 = 1 ; //set RA0 to input TRISBbits.RB0 = 0 ; //set RB0 to output LATBbits.LATB0 = 0 ; //set RB0 to logic zero }

void init_ADC(void) { OSCCON = 0b01111110; //run mode, 8MHz, internal osc block ADCON1 = 0x7C ; //0b01111100 ADCON2 = 0xA5 ; // 0b10100101 , right justified }

//************************************************** // main function //**************************************************

void main(void)

{ InitIO(); //initialise IO init_ADC(); while(1) //infinite loop {

ADCON0 = 0x01 ; // channel AN0 ADCON0bits.GO_nDONE = 1; //start conversion while (ADCON0bits.GO_nDONE); //wait for ADC to complete adc_result = (ADRESH << 8 | ADRESL);

if (adc_result > 500) { LATBbits.LATB0 = 1; } else { LATBbits.LATB0 = 0 ; } } }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!