pia-468x60

pia-728x90

Thursday, March 4, 2021

PIC16F887 ADC Simple DVM Example MikroC

In previous post, we demonstrate how to program ADC module of PIC16F887 using MikroC for PIC. However its result was just a binary representation of 10-bit ADC resolution. 

Typically, input voltage to ADC input channel ranges around its ADC reference voltage - about 5V. Thus input voltage of analog input is between 0 to 5V DC. In this programming example, controller read analog input voltage using its ADC module, and display the value of voltage on a multiplexing 7-Segments display.

PIC16F887 ADC Simple DVM Example MikroC
Sample of this programming example

Analog input channel 0 (AN0) reads input voltage. C program configures, and converts analog input value to digital representation. Another block of code convert digital conversion result to readable voltage value in floating point format.



MikroC source code:

  1. #define STEP_SIZE 0.0048
  2. #define ONE_SECOND 30
  3. #define RATE 10
  4. #define DIGIT1 PORTA.RA6
  5. #define DIGIT2 PORTA.RA7
  6.  
  7. unsigned ADC_Value;
  8. float Voltage;
  9. char int_count,volt_count;
  10. char LED[10]={0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F};
  11. bit ADC_RUN;
  12.  
  13. void interrupt() {
  14. if(INTCON.T0IF) {
  15. int_count++;
  16. if(int_count==ONE_SECOND)
  17. { int_count=0; ADC_RUN=1; }
  18. }
  19. INTCON.T0IF=0;
  20. }
  21.  
  22. void PORT_SETUP() {
  23. PORTA=0x00;
  24. PORTB=0x00;
  25. TRISA=0x01; // RA0 IS ANALOG IN PUT
  26. TRISB=0x00; // PORTB is OUTPUT
  27. ANSEL=0x01; // SELECT AN0
  28. ANSELH=0x00; // OTHERS ARE DIGITAL IO
  29. OSCCON|=0x07; // SELECT 8MHz INT RC
  30. }
  31.  
  32. void Timer0_Setup(){
  33. OPTION_REG.T0CS=0; // SELECT FOSC/4
  34. OPTION_REG.PSA=0; // SELECT TIMER0 PRESCALER
  35. OPTION_REG|=0x07; // SELECT 1:256 PRESCALER
  36. }
  37.  
  38. void interrupt_setup() {
  39. INTCON.GIE=1;
  40. INTCON.T0IE=1;
  41. INTCON.T0IF=0;
  42. }
  43.  
  44. void Read_Voltage () {
  45. if(ADC_RUN=1) {
  46. ADC_Value=ADC_Get_Sample(0); // READ AN0
  47. delay_ms(10);
  48. Voltage=ADC_Value*STEP_SIZE; // Convert to Decimal
  49. }
  50. ADC_RUN=0;
  51. }
  52.  
  53. void SSD() {
  54. volt_count=Voltage*10; // convert Voltage to integer
  55.  
  56. PORTB=LED[Volt_count/10]|0x80;
  57. DIGIT1=1;
  58. delay_ms(RATE);
  59. PORTA=0x00;
  60.  
  61. PORTB=LED[Volt_count%10];
  62. DIGIT2=1;
  63. delay_ms(RATE);
  64. PORTA=0x00;
  65. }
  66.  
  67. void main() {
  68. ADC_Value=0;
  69. int_count=0;
  70. ADC_RUN=0;
  71. PORT_SETUP();
  72. ADC_Init();
  73. Timer0_Setup();
  74. Interrupt_Setup();
  75. while(1) {
  76. Read_Voltage();
  77. SSD();
  78. }
  79. }

Multiplexed display is common cathode type, driven by an inverting buffer. Embedded controller operates from its 8MHz internal oscillator.

Schematic Diagram:

PIC16F887 ADC Simple DVM Example MikroC
Schematic diagram

Click here to download this example.




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.



Friday, February 5, 2021

PIC16F887 Analog-to-Digital Converter (ADC)

Introduction

Analog-to-Digital Converter (ADC) module of a microcontroller converter analog input voltage to digital value representation that microcontroller process with. There are many different types of ADC module inside microcontroller.

ADC module has some parameters:

  1. Resolution : Define the amount of digital number of analog to digital conversion. It could be 8-bit, 10-bit, 12-bit, etc. Larger resolution gives the conversion result more accurate.
  2. Voltage reference: It also effect Resolution. Lower voltage reference of ADC makes an accurate conversion result. It could be 2.5V, 3.3V, or 5V.
  3.  Clock : It could be driven from microcontroller clock source, or its own internal clock. 
  4. Number of Analog Input Channel : It  refers to the amount of input pins of ADC. It usually multiplexed with other digital I/O pins.

The figure below is ADC block diagram of PIC16F887.

PIC16F887 Analog-to-Digital Converter (ADC)
Block diagram of PIC16F887 ADC Module
PIC16F887 has up to 14 analog input channels. They are multiplex within Port A and Port B.

Programming for ADC

ADC module inside PIC16F887 has some registers for programmer to work with. Programmer need to configure,

  1. ADC clock
  2. Analog channel inputs
  3. Result arrangement
  4. Channel selection
  5. Start of conversion
  6. Testing the result and reading the result
  7. Interrupt control (optional)

 These registers are,



PIC16F887 Analog-to-Digital Converter (ADC)
Registers relate to ADC of PIC16F887

Now let start a simple example of using ADC module. This program read analog input on RA0/AN0. Conversion result will display on Port C and Port D.

PIC16F887 Analog-to-Digital Converter (ADC)
Schematic Diagram



 MikroC source code:


Click here to download zip file of this working example.





Labels