Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

A/D how to add two channels (CHS2 and CHS1 or CHS0)?

Status
Not open for further replies.

desire608

New Member
Anyone knows how to add two channels (CHS2 and CHS1 or CHS0)?
Using PIC18f1220
I have connected one input to AN4/RB0 pin(8) using CHS2 (bit 010) which is working good, but I want to add another input e.g. on AN5/RB1 pin(9).I have study the datasheet and have tried but unfortunately couldn’t able to do.
Any idea or can do for me please.

what I have done A/D code

ADCON1 =0x6F; //all IO are digital or 0b01101111 in binary on PORT A
TRISA = 0b00100000; //sets PORTA
PORTA = 0b00000000; //turns off PORTA outputs
TRISB = 0b01000001; //sets PORTB as all outputs
PORTB = 0b00000000; //turns off PORTB outputs, good start position
ADCON0bits.CHS2=1; //read port RB0
ADCON0bits.ADON = 1; //turn on A/D
ADCON2 = 0b10000000; //right justified, acquisition times are ok at 0 with 31kHz on PORT B
while (1)
{
ADCON0bits.GO = 1;

king regrads
 
Code:
ADCON1 = 01101111 ; // selects AN4.
//To use AN5 & AN4  (example)
ADCON1 = 01001111 ; // selects both.

ACCON0 = 0;
ADCON0bits.CHS2 =1  ); // AN4
ADCON0 += 1; // switch on
ACDON0bits. GO = 1;

// Then
ACCON0 = 0;
ADCON0bits.CHS2 =1  ); 
ADCON0bits.CHS0 =1  ); // AN5
ADCON0 += 1; // switch on
ACDON0bits. GO = 1;

If you are using C18.. its far easier to use thier ADC library.
*NOTE* there needs to be a little time ,(a couple of uS), between ADCONbits.ON and ADCONbits.GO.
 
Last edited:
You need to set your TRISB to input on AN4 and AN5 and you can't read them both at the same time Read AN4 delayUS read AN5

ADCON1 = 0x4F; Like Ian said and it should work if you delay a few us between reading and save them as there done reading the pins.

TRISB = 0b01000011;
 
Last edited:
Dear Ian Rogers and be80be

Thank you very much indeed. I have put the delay between both channels and its working
Once again thank you

Kind regards
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top