In previous post, we show how to program ADC in MikroC. It was a single channel reading. We can read any channel as need by setting a number of ADC input, and selecting a specific channel before reading.
Simulation sample of this program |
MikroC program:
#define SW PORTA.RA7 void PORT_Init() { PORTA=0x00; // Clear PORTA PORTC=0x00; // Clear PORTC PORTD=0x00; // Clear PORTD PORTB=0x00; // Clear PORTB PORTA=0x83; // RA0-RA1-RA7 are INPUT ANSEL=0x03; // RA0 IS ANALOG TRISC=0x00; // PORTC AS OUTPUT TRISD=0x00; // PORTD AS OUTPUT } void Analog_Init() { ADCON0=0x80; // Select Fosc/32 AND AN0 ADCON1=0x80; // Select Right Justified and VDD-VSS ADCON0.ADON=1; // Enable ADC Module } void Read_ADC() { delay_ms(10); if(SW==1) ADCON0|=0x04; // select AN1 else ADCON0&=0x83; // select AN0 ADCON0.GO=1; // start of conversion while(ADCON0.GO); // waiting for Conversion's Complete PORTC=ADRESL; PORTD=ADRESH; } void main() { PORT_Init(); OSCCON|=0x70; // Select Internal 8MHz OSC Analog_Init(); while(1) Read_ADC(); }
Click here to download this example.
Schematic diagram:
No comments:
Post a Comment