Menu

Adskeeper - header widge

728x90

Search This Blog

468x60

Navbar

AdsKeeper - IAB Display Standard Ad Unit

Saturday, October 10, 2026

dsPIC30F2010 ADC Programming in MikroC

Introduction

An Analog to Digital Converter (ADC) translates continuous voltage signal into a digital data that a digital or computer system could understands. For short, in most of modern micro-controllers have at least one dedicated ADC module inside. It range from 8-bit, 10-bit, 12-bit or even 16-bit in a mixed signal processor such as the MSP430 micro-controller series.

 

dsPIC30F2010 ADC Programming in MikroC


An ADC is needed in an audio sampling system, analog temperature reading, strain gauge, etc. Using a micro-controller's build-in ADC module is very cost effective reducing of complexities in hardware wiring. 

 

dsPIC30F2010 ADC Programming in MikroC
10-BIT HIGH SPEED A/D FUNCTIONAL BLOCK DIAGRAM

 

The dsPIC30F2010 has six-channel 10-bit ADC module that able to convert any analog input channel up to 500KSPS. It has a programmable reference voltage pins - AVREF+ and AVREF-, and select able ADC supply voltage pins - AVDD and AVSS . 

In some compilers there is no ADC library. So the programmer need configures its registers before allowing the ADC module to operate.

The A/D module has six 16-bit registers:


• A/D Control Register1 (ADCON1)
• A/D Control Register2 (ADCON2)
• A/D Control Register3 (ADCON3)
• A/D Input Select Register (ADCHS)
• A/D Port Configuration Register (ADPCFG)
• A/D Input Scan Selection Register (ADCSSL) 

 

The programmer needs to configure its, clock source/frequency, result preparation, number of analog inputs, reference voltage pins, etc.

 

dsPIC30F2010 ADC Programming in MikroC

Using MikroC, reading any ADC inputs is very easy. Because it has a ready-to-use function library. 

Library Routines

ADC AN4 and LED 

Here I make a simple ADC reading example from AN4 pin that connects to a POT (on-board). I use its 10-bit resolution that ranges from 0 to 1023. If the result is over 512 an LED connects to RD0 will turn on, otherwise it will turns off.

 

Schematic Diagram: 

 

dsPIC30F2010 ADC Programming in MikroC
Schematic Diagram

 

It's the same to my dsPIC30F2010 DIY Prototype Board.

 

MikroC Source Code:

 


  1. unsigned
    int temp_res;

  2. void main() {

  3. TRISB = 0xFFFF; // PORTB is input
  4. TRISD = 0; // PORTD is output
  5. TRISF = 0; // PORTF is output
  6. ADC1_Init(); // Enable ADC module

  7. do {
  8. temp_res = ADC1_Read(4); // Get 10-bit results of AD conversion
  9. if(temp_res>=512) LATD=0;
  10. else LATD=1;
  11. } while(1);
  12. }

 

ADC AN4 and Character LCD

Following the Mikroelektronika Development Board example, I use a character LCD to read the analog input from AN4, showing its analog voltage result on a 16x4 character LCD. 

 

Schematic Diagram

 

dsPIC30F2010 ADC Programming in MikroC
Schematic Diagram

 

 The voltage value displaying is in floating point format.

 

MikroC Source Code:

 

  1. // LCD module connections
  2. sbit LCD_RS at LATE0_bit;
  3. sbit LCD_EN at LATE1_bit;
  4. sbit LCD_D4 at LATE2_bit;
  5. sbit LCD_D5 at LATE3_bit;
  6. sbit LCD_D6 at LATE4_bit;
  7. sbit LCD_D7 at LATE5_bit;

  8. sbit LCD_RS_Direction at TRISE0_bit;
  9. sbit LCD_EN_Direction at TRISE1_bit;
  10. sbit LCD_D4_Direction at TRISE2_bit;
  11. sbit LCD_D5_Direction at TRISE3_bit;
  12. sbit LCD_D6_Direction at TRISE4_bit;
  13. sbit LCD_D7_Direction at TRISE5_bit;
  14. // End LCD module connections

  15. unsigned char ch;
  16. unsigned int adc_rd;
  17. char *text;
  18. long tlong;

  19. void main() {
  20. ADPCFG = 0xFFFF; // Configure AN pins as digital

  21. Lcd_Init();
  22. LCD_Cmd(_LCD_CURSOR_OFF); // send command to LCD (cursor off)
  23. LCD_Cmd(_LCD_CLEAR); // send command to LCD (clear LCD)

  24. text = "mikroElektronika"; // assign text to string
  25. LCD_Out(1,1,text); // print string a on LCD, 1st row, 1st column
  26. text = "LCD example"; // assign text to string
  27. LCD_Out(2,3,text); // print string a on LCD, 2nd row, 1st column

  28. Delay_ms(7000);
  29. text = "dsPIC30F2010 ADC";
  30. LCD_Out(1,1,text);
  31. text = "Voltage: "; // assign text to string
  32. while (1) {
  33. adc_rd = ADC1_read(4); // get ADC value from 2nd channel
  34. LCD_Out(2,1,text); // print string a on LCD, 2nd row, 1st column

  35. tlong = (long)adc_rd * 5000; // covert adc reading to milivolts
  36. tlong = tlong / 1023; // 0..1023 -> 0-5000mV

  37. ch = tlong / 1000; // extract volts digit
  38. LCD_Chr(2,10,48+ch); // write ASCII digit at 2nd row, 9th column
  39. LCD_Chr_CP('.');

  40. ch = (tlong / 100) % 10; // extract 0.1 volts digit
  41. LCD_Chr_CP(48+ch); // write ASCII digit at cursor point

  42. ch = (tlong / 10) % 10; // extract 0.01 volts digit
  43. LCD_Chr_CP(48+ch); // write ASCII digit at cursor point

  44. LCD_Chr_CP('V');

  45. Delay_ms(100);
  46. }
  47. }//~!

 

I tested this example on my DIY dsPIC30F2010 Prototype Board.

 

dsPIC30F2010 ADC Programming in MikroC
Reading +5.00V

  

dsPIC30F2010 ADC Programming in MikroC
Reading +2.53V

  

dsPIC30F2010 ADC Programming in MikroC
Reading 0.00V





Sending the ADC Data Over The Serial Port

We can send the ADC conversion data over the serial port using additional C sprintf and UART function.

 

MikroC Source Code:



  1. unsigned
    int adc_rd;
  2. char *text;
  3. float voltage;

  4. void main() {
  5. ADPCFG = 0xFFFF;
  6. // Initialize UART module at 9600 bps
  7. UART1_Init(9600);
  8. Delay_ms(10);
  9. text = "dsPIC30F2010 ADC UART Example\n\r";
  10. UART1_Write_Text(text); // Write message on UART

  11. while(1){
  12. adc_rd = ADC1_read(4);
  13. voltage = 5.0 * adc_rd / 1023;
  14. sprintf(text,"AN4 Channel ADC Value: %d Decimal\n\r",adc_rd);
  15. UART1_Write_Text(text); // Write message on UART
  16. sprintf(text,"%0.2f Volts\n\r",voltage);
  17. UART1_Write_Text(text); // Write message on UART
  18. Delay_ms(2000);
  19. }
  20. }


 In the Library Manager, don't forget to tick on ADC, UART and Sprintf functions.

 

dsPIC30F2010 ADC Programming in MikroC
USART Terminal

 

 

 


No comments:

Post a Comment

Adskeep- Standard Ads

Electronics and Programming Tutorials

336x280

DIY Electronic Projects And Instruments

AdsKeeper - 2091654

Labels

23K256 (1) 24C16B (2) 25AA010A (2) 8051 (7) 93AA46B (1) ADC (40) Analog Comparator (1) Arduino (16) ARM (32) AT89C52 (7) ATMega32 (59) ATMega644P (27) AVR (88) AVR PCB (2) Bootloader (1) CCS PICC (31) Change Notify Interrupt (CNI) (2) DAC (2) DHT11 (5) Display (157) Distance Sensor (3) dot matrix (1) ds1307 (10) DS18B20 (6) ds3231 (2) ds3232 (2) dsPIC (16) dsPIC30F1010 (3) dsPIC30F2010 (15) EEPROM (5) Environment Sensor (6) ESP32 (1) esp8266 (1) Graphical LCD (3) HD44780 (12) I2C (40) ILI9341 (1) Input/Output (84) Interrupt (22) IoT PCB (1) Keil (5) Keypad (16) KS0108 (3) LCD (87) LM35 (4) Master/Slave (2) MAX7221 (1) MCP23017 (8) MCP23S17 (7) MCP4921 (1) MCP4922 (2) Meter (3) MikroC (2) MikroC ARM (1) MikroC dsPIC (11) MikroC PIC (1) Module PCB (2) Motor (15) MPLABX (73) Nokia 5110 LCD (5) OLED (2) One-Wire (7) Oscillator (8) PCB (13) PCD8544 (3) PCF8574 (13) PIC (121) PIC12F (3) PIC16 (105) PIC16F628A (3) PIC16F630 (2) PIC16F716 (4) PIC16F818 (11) PIC16F818/819 (3) PIC16F84A (16) PIC16F876A (2) PIC16F877A (9) PIC16F88 (2) PIC16F887 (60) PIC18 (19) PIC18F1220 (5) PIC18F2550 (5) PIC18F4550 (13) PICKit2 (1) PICMicro PCB (4) Pin Change Interrupt (1) PLL (1) PWM (12) RTC (13) SBN0064G (1) Sensor (15) SH1106 (3) Shift Register (15) Shift Registers (19) SN74HC595N (9) Software SPI (1) Software TWI (3) SPI (44) sprinf (3) ST7735 (1) STM32 (29) STM32 Black Pill (1) STM32 Blue Pill (12) STM32 HAL (23) STM32CubeIDE (32) STM32F103C8T6 (9) STM32F103R6 (18) STM32F401CCU6 (1) SysTick (7) temperature sensor (15) TFT (1) TG12864A (1) Thermometer (22) Timer/Counter (32) TM1637 (2) twi (10) UART (11) Ultrasonic (4) Voltmeter (7) WDT (1) XC16 (2) XC8 (96)