void main(void)
{
// Initialize the device
SYSTEM_Initialize();
__delay_ms (500);
// Add your application code
ADC_Initialize();
int ADC_Result = 0;
while(1)
{
ADC_StartConversion(0);
__delay_ms (500);
ADC_Result = ADC_GetConversion(0);
sprintf(str_nr , "%d", ADC_Result);
EUSART_Write("\tADC CH0 = ");
EUSART_Write(str_nr);
__delay_ms (500);
}
}
void main(void)
{
// Initialize the device
SYSTEM_Initialize();
__delay_ms (500);
int index_nr = 0;
char index_str[50] = "";
char str_nr[50] = "";
// Add your application code
ADC_Initialize();
int ADC_Result = 0;
while(1)
{
index_nr++;
strcpy(index_str, "\r\nIndex Number = ");
sprintf(str_nr , "%d", index_nr);
strcat(index_str , str_nr)...
no good error description.I don't get the correct values from ADC
Valid output for a 12 bit ADC is 0...4095 (not 4096!) ... but you use "left aligned" thus the result appears as multiplied with 16. Thus valid output is 0, 16, 32, 48.....65520).It should show 0 ADC value on the serial terminal when pin (2) connected to Ground and 4096 ADC value when pin (2) connected to VCC which is 5 V.
void main(void)
{
// Initialize the device
SYSTEM_Initialize();
__delay_ms (500);
int index_nr = 0;
char index_str[50] = "";
char str_nr[50] = "";
// Add your application code
ADC_Initialize();
int ADC_Result = 0;
while(1)
{
index_nr++;
strcpy(index_str, "\r\nIndex Number = ");
sprintf(str_nr , "%d", index_nr);
strcat(index_str , str_nr);
EUSART_Write(index_str);
ADC_StartConversion(0);
PORTCbits.RC4 = 1;
PORTCbits.RC5 = 1;
__delay_ms (500);
if (ADC_IsConversionDone == 1 )
{
ADC_Result = ADC_GetConversionResult();
EUSART_Write("\tADC CH0 = ");
sprintf(str_nr , "%d", ADC_Result);
EUSART_Write(str_nr);
}
else
{
EUSART_Write("\tADC Conversion is not done");
}
PORTCbits.RC4 = 0;
PORTCbits.RC5 = 0;
__delay_ms (500);
}
}
void SYSTEM_Initialize(void)
{
PIN_MANAGER_Initialize();
OSCILLATOR_Initialize();
ADC_Initialize();
TMR0_Initialize();
EUSART1_Initialize();
}
void OSCILLATOR_Initialize(void)
{
// SCS INTOSC; HFIOFS not stable; IDLEN disabled; IRCF 16MHz_HF;
OSCCON = 0x72;
// SOSCGO disabled; MFIOSEL disabled; SOSCDRV Low Power;
OSCCON2 = 0x00;
// INTSRC INTRC; PLLEN disabled; TUN 0;
OSCTUNE = 0x00;
// ROSEL System Clock(FOSC); ROON disabled; ROSSLP Disabled in Sleep mode; RODIV Fosc;
REFOCON = 0x00;
}
void ADC_Initialize(void)
{
// set the ADC to the options selected in the User Interface
// GO_nDONE start; ADON enabled; CHS AN0;
ADCON0 = 0x03;
// TRIGSEL CTMU; VNCFG AVSS; VCFG AVDD; CHSN AN0;
ADCON1 = 0x41;
// ADFM Left; ACQT 20_Tad; ADCS FOSC/16;
ADCON2 = 0x3D;
// ADRESH 0;
ADRESH = 0x00;
// ADRESL 0;
ADRESL = 0x00;
}
* then wait until the conversion is finished (which is not fulfilled in the code of the screenshot. "IF" does not "wait". "WHILE" does wait.)
do
{
EUSART_Write("\r\nADC Conversion is not done");
__delay_ms (500);
} while (ADC_IsConversionDone != 1);
ADC_Result = ADC_GetConversionResult();
EUSART_Write("\tADC CH0 = ");
sprintf(str_nr , "%d", ADC_Result);
EUSART_Write(str_nr);
does this wait "as long as 0" or "as long as 1"?while (ADC_IsConversionDone != 1);
ADC_StartConversion(0);
do
{
EUSART_Write("\r\nADC Conversion is not done");
} while (ADC_IsConversionDone == 1);
ADC_Result = ADC_GetConversionResult();
EUSART_Write("\tADC CH0 = ");
sprintf(str_nr , "%d", ADC_Result);
EUSART_Write(str_nr);
???This means to wait until the bit is 1 and come out of the loop when the bit is 0.
/**
TRISx registers
*/
TRISA = 0xEF;
TRISB = 0xFF;
TRISC = 0x8F;
Are you sure?The do while loop should be valid and running as long as the ADC_IsConversionDone == 1
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?