ADC Library
ADC (Analog to Digital Converter) module is available with a number of PIC32 MCU modules. ADC is an electronic circuit that converts continuous signals to discrete digital numbers. ADC Library provides you a comfortable work with the module.
Library Routines
ADC1_Init
Prototype |
sub procedure ADC1_Init() |
---|---|
Description |
This routines configures ADC module to work with default settings. The internal ADC module is set to:
|
Parameters |
None. |
Returns |
Nothing. |
Requires |
|
Example |
ADC1_Init() ' Initialize ADC module with default settings |
Notes |
None. |
ADC1_Init_Advanced
Prototype |
sub procedure ADC1_Init_Advanced(dim Reference as word) |
||||||||
---|---|---|---|---|---|---|---|---|---|
Description |
This routine configures the internal ADC module to work with user defined settings. |
||||||||
Parameters |
|
||||||||
Returns |
Nothing. |
||||||||
Requires |
|
||||||||
Example |
ADC1_Init_Advanced(_ADC_INTERNAL_REF) ' set internal reference used |
||||||||
Notes |
|
ADC1_Get_Sample
Prototype |
sub function ADC1_Get_Sample(dim channel as word) as word |
---|---|
Description |
The function enables ADC module and reads the specified analog channel input. |
Parameters |
|
Returns |
10-bit unsigned value from the specified |
Requires |
|
Example |
dim adc_value as word ... adc_value = ADC1_Get_Sample(10) ' read analog value from ADC module channel 10 |
Notes |
|
ADC1_Read
Prototype |
sub function ADC1_Read(dim channel as word) as word |
---|---|
Description |
The function initializes, enables ADC module and reads the specified analog channel input. |
Parameters |
|
Returns |
10-bit unsigned value from the specified |
Requires |
|
Example |
dim adc_value as word ... adc_value = ADC1_Read(10) ' read analog value from ADCmodule channel 10 |
Notes |
|
Library Example
This code snippet reads analog value from the channel 1 and sends readings as a text over UART1.
program Temperature_Sensor
' LCD module connections
dim LCD_RS as sbit at LATB2_bit
dim LCD_EN as sbit at LATB3_bit
dim LCD_D4 as sbit at LATB4_bit
dim LCD_D5 as sbit at LATB5_bit
dim LCD_D6 as sbit at LATB6_bit
dim LCD_D7 as sbit at LATB7_bit
dim LCD_RS_Direction as sbit at TRISB2_bit
dim LCD_EN_Direction as sbit at TRISB3_bit
dim LCD_D4_Direction as sbit at TRISB4_bit
dim LCD_D5_Direction as sbit at TRISB5_bit
dim LCD_D6_Direction as sbit at TRISB6_bit
dim LCD_D7_Direction as sbit at TRISB7_bit
' End LCD module connections
dim temp as float
txt as char[16]
' Convert ADC value to Celsius degrees format
sub function ADC_to_degC() as float
result = ADC1_Get_Sample(8) ' Read ADC value from AN8 pin
result = (((3.25/1024) * result - 0.5) * 100)
end sub
main:
CHECON = 0x32
AD1PCFG = 0xFFF7 ' Configure AN8 pin as analog I/O
ADC1_Init() ' Initialize ADC
Delay_100ms()
Lcd_Init() ' Initialize LCD
Lcd_Cmd(_LCD_CLEAR) ' Clear LCD
Lcd_Cmd(_LCD_CURSOR_OFF) ' Turn cursor off
Lcd_Out(1, 1, " Temperature: ")
while(TRUE)
temp = ADC_to_degC() ' Convert ADC value to Celsius degrees format
FloatToStr(temp, txt)
Lcd_Chr(2,13,223) ' Print degree character, 'C' for Centigrades
' Different LCD displays have different char code for degree
Lcd_Chr(2,14,"C") ' If you see greek alpha letter try typing 178 instead of 223
Lcd_Out(2, 5, txt) ' Display value on the LCD
Delay_1sec() ' 1 second delay
wend
end.
What do you think about this topic ? Send us feedback!