Paritoshgiri
Member level 5

#include <p18f4550.h>
#include <delays.h>
void Read_ADC(void);
void Send_Value (void);
unsigned int adc;
unsigned float result;
unsigned int voltage;
void main(void)
{
TRISA = 0b00000001;
ADCON0 = 0b00000001;
// A/D is ON
ADCON1 = 0b00001110;
// Vref- set to Vss
// Vref+ set to Vdd
// AN0 is analog - others are digital
ADCON2 = 0b10111111;
// right justify
// Acq time 20TAD
// clk Frc
TXSTA = 0b10100010;
// 8-bit
// async
// low speed (BRGH=0)
RCSTA = 0b10010000;
SPBRG = 77;
while (1)
{
Read_ADC();
Send_Value();
Delay10KTCYx(240); //Delay 200 ms
Delay10KTCYx(240); //Delay 200 ms
}
}
void Read_ADC (void)
{
voltage = 0; //globally defined as unsigned long
Delay10TCYx(24); // Delay for 240TCY allow sampling cap to charge
ADCON0bits.GO = 1; //start converting
while (ADCON0bits.DONE == 1); //wait for EOC
voltage = ((unsigned int)ADRESH << 8) | ADRESL; //combines two bytes into a long int
result = (float)(voltage * 5)/1023;
adc = result * 10000;
//voltage = voltage * 5000; //Vref is in milliVolts
//voltage = voltage/1023; //10 bit ADC
}
void Send_Value (void)
{
char ch;
ch = (adc/10000) % 10; //extract thousands digit
ch = ch + 48; // now it is in ASCII
while (PIR1bits.TXIF == 0);
{
TXREG = ch;
}
while (PIR1bits.TXIF == 0);
TXREG = '.'; //We need a decimal point
ch = (adc/1000) % 10; //extract thousands digit
ch = ch + 48; // now it is in ASCII
while (PIR1bits.TXIF == 0);
{
TXREG = ch;
}
ch = (adc/100) % 10; //extract hundredth digit
ch = ch + 48;
while (PIR1bits.TXIF == 0);
TXREG = ch;
ch = (adc/10) % 10; //extract tenth digit
ch = ch + 48;
while (PIR1bits.TXIF == 0);
TXREG = ch;
ch = adc % 10;
ch = ch + 48;
while (PIR1bits.TXIF == 0);
TXREG = ch;
while (PIR1bits.TXIF == 0);
TXREG = '\n';
while (PIR1bits.TXIF == 0);
TXREG = '\r';
while (PIR1bits.TXIF == 0);
}
hai... looks like you have configured the conversion clock to RC. But are you sure the RC circuit capacitance is right? For 20MHz, the data sheet recommends 15pF. But you have used 22pF. May be you can change the value and recheck
I didn't have 15pF capacitor so I used 22 pF. So can the sampling rate be increased by using 15pF capacitor? Don't i need to change anything in the code?? Please help...
I am using a hardware and its working fine. The only problem is that sampling rate is too low and I want a sampling rate of above 1 KHz. Please help.Hai i have not at all worked with PIC controller. Just to help you i have gone through some topics of the data sheet. I think the wrong value of capacitance may give unexpected frequency errors.
you have configured the conversion clock to RC. So it suppose to use the max clock, i think
Are you using hardware/simulator?
1KHz is not too high a frequency. Microchip claims that pic18f4520's ADC can go as high as 100K samples per second.The PIC24HJ12GP202 or similar will allow you to sample 10bit AD @ 1.1 msps, In PIC16 it takes less than 40 uS to have a complete 10 bit ADC sample.
https://www.edaboard.com/threads/175050/
Using Analog to Digital Converter
Calculate adc sampling rate for PIC18F4550
I removed the delay and checked but still I am not getting the desired sample rate. What do you mean by taking average of few samples? How can it be done? Please help...Input to ADC should be low impedance (max 10K). No need to put delays for capacitor chaging which is causing slow sample rate. You can add and take average of few samples if you need.
The delay you provided for the charging of the capacitor slows down the sample rate.
But what really slows down the sample rate is the calculation afterwards. The calculation involving float mathematics and the calculations that follow such as the division really slow you down. You're calling the ADC routine periodically. To really increase the sample rate, you should use an interrupt. After interrupt occurs, move the ADC results to required registers and start another conversion. This will probably give you the fastest ADC sampling as you don't wait before starting/sampling again.
Using the FRC(A/D RC oscillator) also slows you down. Calculate such that you have just enough time for sampling and acquisition. The A/D RC oscillator slows you down.
Hope this helps.
Tahmid.
Also, I have not worked with interrupt before. Does it include performing the ADC in ISR?
Dont perform calculations in the interrupt routine. When interrupt comes, read the value and store in a global variable (also enable user defined flag) and terminate the routine. Once you come out of routine check the flag, if it is enabled then perform the calculation and do further steps.
Isn't it true that even if we acquire the digital data from ADC very fast using ISR, the time taken for calculation and converting the values to ASCII outside ISR will take much time and limit the use of ISR?
Well, depends.
For example:
You can take ADC readings every 1ms and then carry out calculations in the main routine, but if the calculation takes more than 1ms itself, then there's no point in taking ADC readings so quickly, since you'll either just be loading a value for the next calculation or just acquiring a value that will not even be used.
Hope this helps.
Tahmid.
The delay you provided for the charging of the capacitor slows down the sample rate.
But what really slows down the sample rate is the calculation afterwards. The calculation involving float mathematics and the calculations that follow such as the division really slow you down. You're calling the ADC routine periodically. To really increase the sample rate, you should use an interrupt. After interrupt occurs, move the ADC results to required registers and start another conversion. This will probably give you the fastest ADC sampling as you don't wait before starting/sampling again.
Using the FRC(A/D RC oscillator) also slows you down. Calculate such that you have just enough time for sampling and acquisition. The A/D RC oscillator slows you down.
Hope this helps.
Tahmid.
while (1)
{
Read_ADC();
Send_Value();
[B] Delay10KTCYx(240); //Delay 200 ms
Delay10KTCYx(240); //Delay 200 ms[/B]
}
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?