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.




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)