The 12bit ADC divides the range of voltage. A 12 bit A/D will take a 0 to 5 volt input and convert it to 2^12 bits or offer up 0 to 4096 bits or quantization levels.What is it that you do not understand?
What is it that you want to know?
// random code UART code found on forum
// read ADC input and print result on UART
// updated by amateur hobby programmer - use at own risk - update author accepts no responsibility for anything
// 9600 baud - connect RC6 to Rx, RC7 to Tx
// connect on-board pot to RA0
// Status 23 October 2020
// - Commenting not complete yet
// - TAD is probably far longer than needed - need to reduce
// - code is very amateur and messy - needs improvement
#define _XTAL_FREQ 20000000 // 2-pin crystal 20MHz
// PIC18F45K80 Configuration Bit Settings
// CONFIG1L
#pragma config RETEN = ON // VREG Sleep Enable bit (Ultra low-power regulator is Enabled (Controlled by SRETEN bit))
#pragma config INTOSCSEL = LOW // LF-INTOSC Low-power Enable bit (LF-INTOSC in Low-power mode during Sleep)
// SOSCSEL = No Setting
#pragma config XINST = OFF // Extended Instruction Set (Disabled)
// CONFIG1H
#pragma config FOSC = HS2 // HS oscillator (high power, 16 MHz-25 MHz
#pragma config PLLCFG = OFF // PLL x4 Enable bit (Disabled)
#pragma config FCMEN = OFF // Fail-Safe Clock Monitor (Disabled)
#pragma config IESO = OFF // Internal External Oscillator Switch Over Mode (Disabled)
// CONFIG2L
#pragma config PWRTEN = ON // Power Up Timer (Enabled)
#pragma config BOREN = OFF // Brown Out Detect (Disabled in hardware, SBOREN disabled)
#pragma config BORV = 0 // Brown-out Reset Voltage bits (3.0V)
#pragma config BORPWR = LOW // BORMV Power level (BORMV set to low power level)
// CONFIG2H
#pragma config WDTEN = OFF // Watchdog Timer (WDT disabled in hardware; SWDTEN bit disabled)
#pragma config WDTPS = 1 // Watchdog Postscaler (1:1)
// CONFIG3H
#pragma config CANMX = PORTC // ECAN Mux bit (ECAN TX and RX pins are located on RC6 and RC7, respectively)
#pragma config MSSPMSK = MSK5 // MSSP address masking (5 bit address masking mode)
#pragma config MCLRE = ON // Master Clear Enable (MCLR Enabled, RE3 Disabled)
// CONFIG4L
#pragma config STVREN = OFF // Stack Overflow Reset (Disabled)
#pragma config BBSIZ = BB1K // Boot Block Size (1K word Boot Block size)
// CONFIG5L
#pragma config CP0 = ON // Code Protect 00800-01FFF (Enabled)
#pragma config CP1 = ON // Code Protect 02000-03FFF (Enabled)
#pragma config CP2 = ON // Code Protect 04000-05FFF (Enabled)
#pragma config CP3 = ON // Code Protect 06000-07FFF (Enabled)
// CONFIG5H
#pragma config CPB = ON // Code Protect Boot (Enabled)
#pragma config CPD = ON // Data EE Read Protect (Enabled)
// CONFIG6L
#pragma config WRT0 = ON // Table Write Protect 00800-01FFF (Enabled)
#pragma config WRT1 = ON // Table Write Protect 02000-03FFF (Enabled)
#pragma config WRT2 = ON // Table Write Protect 04000-05FFF (Enabled)
#pragma config WRT3 = ON // Table Write Protect 06000-07FFF (Enabled)
// CONFIG6H
#pragma config WRTC = ON // Config. Write Protect (Enabled)
#pragma config WRTB = ON // Table Write Protect Boot (Enabled)
#pragma config WRTD = ON // Data EE Write Protect (Enabled)
// CONFIG7L
#pragma config EBTR0 = ON // Table Read Protect 00800-01FFF (Enabled)
#pragma config EBTR1 = ON // Table Read Protect 02000-03FFF (Enabled)
#pragma config EBTR2 = ON // Table Read Protect 04000-05FFF (Enabled)
#pragma config EBTR3 = ON // Table Read Protect 06000-07FFF (Enabled)
// CONFIG7H
#pragma config EBTRB = ON // Table Read Protect Boot (Enabled)
// #pragma config statements should precede project file includes.
// Use project enums instead of #define for ON and OFF.
#include <xc.h>
// global variables
unsigned char txt_buf[10]; // general purpose text buffer
void Port_Initialized (void)
{
ANCON0 = 0x01; // AN0 analogue, rest digital
ANCON1 = 0; // Set to digital port
CM1CON = 0; // Comparator off
CM2CON = 0; // Comparator off
ADCON0 = 0; // A/D conversion Disabled
ADCON1 = 0; // A/D conversion Disabled
ADCON2 = 0; // A/D conversion Disabled
LATA = 0;
LATB = 0;
LATC = 0;
LATD = 0;
LATE = 0;
TRISA = 0b0000001; // A0 input, rest output
TRISB = 0b0000000; // all are output, Unused
TRISC = 0b0000000; // C0-C5 output - C6=Tx, C7=Rx
TRISD = 0b0000000; // all are output, Unused
TRISE = 0b0000000; // All are output, Unused
}
void send(char message)
{
while(TXIF == 0 ); // Wait till the transmitter register becomes empty
TXREG1 = message;
}
void string(char *p)
{
while(*p != '\0') {
__delay_ms(1);
send(*p);
p++;
}
}
void Uart_Initialized(void)
{
//TXSTAx TRANSMIT STATUS AND CONTROL REGISTER
TXSTA1bits.CSRC = 0; //: Don?t care.
TXSTA1bits.TX9 = 0; // Selects 8-bit transmission bit
TXSTA1bits.TXEN = 1; // Transmit Enable
TXSTA1bits.SYNC = 0; // Asynchronous mode
TXSTA1bits.SENDB = 0;
TXSTA1bits.BRGH = 1; // High speed mode
TXSTA1bits.TX9D = 0;
//RCSTAx: RECEIVE STATUS AND CONTROL REGISTER
RCSTA1bits.SPEN = 1 ; // Serial port enabled
RCSTA1bits.RX9 = 0; // Selects 8-bit reception
RCSTA1bits.SREN = 1 ; // Don?t care.
RCSTA1bits.CREN = 1 ; // Enables receiver
RCSTA1bits.ADDEN = 0; //
RCSTA1bits.FERR = 0 ; // No framing error
RCSTA1bits.OERR = 1 ; // Overrun error
RCSTA1bits.RX9D = 0 ; // Selects 8-bit reception
SPBRGH1 = 0;
SPBRG1 = 129,
INTCON = 0x00;
PIR1 = 0x00;
PIR2= 0x00;
PIR3 = 0x00;
PIR4 = 0x00;
PIR5 = 0x00;
PIE1 = 0x00;
PIE2 = 0x00;
PIE3 = 0x00;
PIE4 = 0x00;
PIE5 = 0x00;
IPR1 = 0x00;
IPR2 = 0x00;
IPR3 = 0x00;
IPR4 = 0x00;
}
// convert raw ADC reading to ASCII decimal
// quick, dirty code - needs replacing with an elegant itoa routine
void ADC_to_ASCII(unsigned int adc_raw){
unsigned char thousands;
unsigned char hundreds;
unsigned char tens;
unsigned char units;
unsigned int temp_raw;
temp_raw = adc_raw & 0x0fff; // limit to 4095 max
thousands = temp_raw/1000;
temp_raw = temp_raw - (thousands * 1000);
hundreds = temp_raw/100;
temp_raw = temp_raw - (hundreds * 100);
tens = temp_raw/10;
temp_raw = temp_raw - (tens * 10);
units = temp_raw;
txt_buf[0] = 0x20; // space
txt_buf[1] = 0x20; // space
txt_buf[2] = thousands + '0';
txt_buf[3] = hundreds + '0';
txt_buf[4] = tens + '0';
txt_buf[5] = units + '0';
txt_buf[6] = 0x0d; // CR
txt_buf[7] = 0; // null terminate the string
}
// initialise ADC for channel 0 - 0 to 5 Volts input to RA0
void ADC_Init(){
ADCON0 = 0x00; // channel 0 ADC off
ADCON1 = 0x00; // voltage limits Vss Vdd
ADCON2 = 0xbe; // right justify, 20 TAD, Fosc/64
ADCON0 = 1; // enable ADC
txt_buf[0] = 0x0d; // CR
txt_buf[1] = 0x0a; // LF
txt_buf[2] = 0x0a; // LF
txt_buf[3] = 0; // null terminate the string
string(txt_buf); // send to UART
}
// read ADC value on RA0
unsigned int Read_ADC(void){
unsigned int retval;
while(ADCON0 & 0x02); // wait for conversion to finish
LATD = ADRESL; // debug only low result to PORT D LEDs
LATC = ADRESH *4; // debug only high result to PORT C LEDs C2 to C5
retval = (unsigned int)(( ADRESH << 8) + ADRESL); // combine high and low values
ADCON0 = 3; // start next conversion
return retval;
}
void main(void)
{
unsigned int ADC_result;
Port_Initialized ();
Uart_Initialized ();
ADC_Init();
while (1)
{
ADC_result = Read_ADC(); // get ADC result from RA0
ADC_to_ASCII(ADC_result); // convert to decimal ASCII string
string(txt_buf); // send to UART
__delay_ms(300); // limit sample rate
}
}
Hi hexreaderSpoiler alert: rubbishy, but working code is below (I got bored)
Since you are trying to learn about the module, what have you done to help yourself? I believe this thread meets the definition of homework.Hi hexreader
I was trying to understand detail about ADC module that's why posted block diagram. <snip> Please do not post code directly help me to understand basic so that I will try to write code myself
Understood...Please do not post code directly help me to understand basic so that I will try to write code myself
There are four options for TRIGSEL bits, 00, 01, 10, 11.Hi MrChips
Can you tell me when should be select special select trigger bits
My understanding is that while a special event trigger must be selected, it does not need to be used. Section 23.8 describes settings for those trigger sources, if used. One can control acquisition and processing independent of the special triggers with the ADCON0 register bit<1> ("GO/!DONE").You cannot opt out.
The default setting is 00.
@hexreader Could you please post the picture of ADC result ?Spoiler alert: rubbishy, but working code is below (I got bored)
I have connected A0 / RA0 into AN11A0 POT at centre ( 2.22 V )
1842
1844
1840
1844
1838
1841
1842
1837
1839
1839
1840
1845
1845
1839
1844
1844
1843
1842
1844
1842
1838
1844
1845
1845
1844
1836
1844
1840
1838
1837
1840
1847
1846
With, or without 10uF capacitor on Vcap - makes no difference
