Wednesday, March 3, 2021

PIC16F887 dual ADC reading example MikroC

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.

PIC16F887 dual ADC reading example MikroC
Simulation sample of this program
In this example, program is written to read one of two ADC channels. An additional digital switch used for selecting one of these two channel. Its ten-bit ADC result will display on PORTD and PORTC.

MikroC program:

  1. #define SW PORTA.RA7
  2.  
  3. void PORT_Init() {
  4. PORTA=0x00; // Clear PORTA
  5. PORTC=0x00; // Clear PORTC
  6. PORTD=0x00; // Clear PORTD
  7. PORTB=0x00; // Clear PORTB
  8. PORTA=0x83; // RA0-RA1-RA7 are INPUT
  9. ANSEL=0x03; // RA0 IS ANALOG
  10. TRISC=0x00; // PORTC AS OUTPUT
  11. TRISD=0x00; // PORTD AS OUTPUT
  12. }
  13.  
  14. void Analog_Init() {
  15. ADCON0=0x80; // Select Fosc/32 AND AN0
  16. ADCON1=0x80; // Select Right Justified and VDD-VSS
  17. ADCON0.ADON=1; // Enable ADC Module
  18. }
  19.  
  20.  
  21. void Read_ADC() {
  22. delay_ms(10);
  23. if(SW==1) ADCON0|=0x04; // select AN1
  24. else ADCON0&=0x83; // select AN0
  25. ADCON0.GO=1; // start of conversion
  26. while(ADCON0.GO); // waiting for Conversion's Complete
  27. PORTC=ADRESL;
  28. PORTD=ADRESH;
  29. }
  30.  
  31. void main() {
  32. PORT_Init();
  33. OSCCON|=0x70; // Select Internal 8MHz OSC
  34. Analog_Init();
  35. while(1) Read_ADC();
  36. }
Click here to download this example.


Schematic diagram:

PIC16F887 dual ADC reading example MikroC
Schematic diagram

PIC16F887 operates at 8MHz from its internal oscillator.



No comments:

Post a Comment

Labels

ADC (10) Analog (14) Arduino (12) Atmega16 (19) Audio (2) AVR (20) Charger (1) Cortex-M0 (1) Counter (10) CPLD (25) Digital I/O (22) Display (34) EEPROM (2) Environment Sensor (1) esp8266 (2) Experiment Board (10) I2C (4) Interrupt (7) LCD (1) LDmicro (29) measurement and instrumentation (7) Microchip Studio (3) MikroC (1) One-Shot (3) OpAmp (1) PCB (31) PIC16 Microcontrollers (16) PIC16F877A (2) PIC16F887 MikroC (22) PLC (35) PWM (11) Regulator (1) RTC (2) Sensor (8) Shift Registers (5) SPI (5) Timer (34) UART (2) ultra-sonic sensor (1) USB (1) VHDL (21) xc8 (1) XC95108 (9) XC9536 (15) XC9572 (1) Xilinx (23) Xilinx ISE (22)