View previous topic :: View next topic |
Author |
Message |
PROMOD
Joined: 01 Feb 2018 Posts: 42
|
adc function in interrupt sub-routine |
Posted: Wed Mar 28, 2018 11:49 pm |
|
|
Code: | #include<18f4431.h>
#device adc=10
#fuses xt,nowdt,nolvp
#use delay(clock=40000000)
#include "flex.c"
float k,a,r=0,p=0,average,energy,energy_consumed; int16 i=0,q=0;
#int_timer2
void pwm_interrupt()
{
set_adc_channel(0);
k=read_adc();
printf(lcd_putc,"EC=%f",k);
}
void main()
{
setup_timer_2(t2_div_by_16,98,16);
enable_interrupts(global);
enable_interrupts(int_timer2);
lcd_init();
setup_adc_ports(all_analog);
setup_adc(adc_clock_internal);
while(1)
{
}
}
|
In the above code I have used adc function in the interrupt sub-routine. But it doesn't work. I would like to know why it doesn't work? |
|
 |
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Thu Mar 29, 2018 12:12 am |
|
|
Quote: | #include<18f4431.h>
#device adc=10
#fuses xt,nowdt,nolvp
#use delay(clock=40000000)
|
You have a 40 MHz #use delay() with the XT oscillator fuse.
There is no way that works. |
|
 |
PROMOD
Joined: 01 Feb 2018 Posts: 42
|
|
Posted: Thu Mar 29, 2018 12:36 am |
|
|
I understand; Thanks!
Another thing to look at-
According to the following code the value of a=k=1023 whenever PIN A0=A1=5V.
Code: | #include<18f4431.h>
#device adc=10
#fuses xt,nowdt,nolvp
#use delay(clock=40000000)
#include "flex.c"
float k=0,a=0,r=0,p=0,average,energy,energy_consumed; int16 i=0,q=0;
void main()
{
lcd_init();
setup_adc_ports(all_analog);
setup_adc(adc_clock_internal);
set_adc_channel(0);
k=read_adc();
set_adc_channel(1);
a=read_adc();
printf(lcd_putc,"EC=%f",a);
while(1)
{
}
}
|
According to the following code the value of a=k=65472 whenever PIN A0=A1=5V.
Code: |
#include<18f4431.h>
#device adc=10
#fuses xt,nowdt,nolvp
#use delay(clock=40000000)
#include "flex.c"
float k=0,a=0,r=0,p=0,average,energy,energy_consumed; int16 i=0,q=0;
#int_timer2
void pwm_interrupt()
{
set_adc_channel(0);
k=read_adc();
set_adc_channel(1);
a=read_adc();
}
void main()
{
setup_timer_2(t2_div_by_16,98,16);
enable_interrupts(global);
enable_interrupts(int_timer2);
lcd_init();
setup_adc_ports(all_analog);
setup_adc(adc_clock_internal);
printf(lcd_putc,"EC=%f",a);
while(1)
{
}
}
|
Why the difference? Could someone please explain? |
|
 |
Ttelmah
Joined: 11 Mar 2010 Posts: 19731
|
|
Posted: Thu Mar 29, 2018 2:46 am |
|
|
There are lots of other little comments:
Don't use ADC_CLOCK_INTERNAL.
Read the data sheet. On all the standard PIC16/18's, this clock source is not recommended if the master clock is above 1MHz, unless you actually stop the CPU executing (sleep) while acquiring.
Then you have the problem of acquisition time. There needs to be time between selecting a channel, and reading it.
Then it is generally poor practice to enable the analog multiplexer for channels that you are not going to read. 'ALL_ANALOG' should only be used if you are genuinely going to use all the inputs as analog.
Then 'think micro-controller'. Don't start using types that are larger (and slower) than you need. The value from the ADC is an integer. Writing it into a 'float' involves the code having to convert this integer into a float, and then print this as a float (so every digit of the print now involves float divisions - hundreds of times slower/bulkier than an integer). |
|
 |
PROMOD
Joined: 01 Feb 2018 Posts: 42
|
|
Posted: Fri Mar 30, 2018 11:44 am |
|
|
Ttelmah wrote: | There are lots of other little comments:
Don't use ADC_CLOCK_INTERNAL.
Read the data sheet. On all the standard PIC16/18's, this clock source is not recommended if the master clock is above 1MHz, unless you actually stop the CPU executing (sleep) while acquiring.
Then you have the problem of acquisition time. There needs to be time between selecting a channel, and reading it.
Then it is generally poor practice to enable the analog multiplexer for channels that you are not going to read. 'ALL_ANALOG' should only be used if you are genuinely going to use all the inputs as analog.
Then 'think micro-controller'. Don't start using types that are larger (and slower) than you need. The value from the ADC is an integer. Writing it into a 'float' involves the code having to convert this integer into a float, and then print this as a float (so every digit of the print now involves float divisions - hundreds of times slower/bulkier than an integer). | Thanks To you! |
|
 |
PROMOD
Joined: 01 Feb 2018 Posts: 42
|
|
Posted: Sat Oct 27, 2018 9:11 am |
|
|
Ttelmah wrote: | Don't use ADC_CLOCK_INTERNAL.
Read the data sheet. On all the standard PIC16/18's, this clock source is not recommended if the master clock is above 1MHz, unless you actually stop the CPU executing (sleep) while acquiring. | If I shouldn't use ADC_CLOCK_INTERNAL then what should I use and How can I determine that?
I request for an explanation! |
|
 |
Ttelmah
Joined: 11 Mar 2010 Posts: 19731
|
|
Posted: Sat Oct 27, 2018 11:45 am |
|
|
Data sheet.
It tells you the legal dividers for each particular clock rate.
As a comment.
LCD's are slow. They take quite a long time to write, but more importantly they take a very long time to actually display the data. Most take perhaps 1/4 second to actually update what is seen. In your original code, if your clock is 40MHz, then you are updating the display about every 400th second, which will almost certainly result in it being unreadable. |
|
 |
PROMOD
Joined: 01 Feb 2018 Posts: 42
|
|
Posted: Sat Oct 27, 2018 12:33 pm |
|
|
Ttelmah wrote: | Data sheet.
It tells you the legal dividers for each particular clock rate. | Seeing the following how can I understand which clock frequency should I use?
 |
|
 |
Ttelmah
Joined: 11 Mar 2010 Posts: 19731
|
|
Posted: Sat Oct 27, 2018 1:56 pm |
|
|
Quote: |
Table 21-2 shows the resultant Tad times derived from the device operating
frequencies and the A/D clock selected.
|
If you look at this table it has the columns usually with a white background that give a legal Tad, for the different divisors at different clock rates. |
|
 |
temtronic
Joined: 01 Jul 2010 Posts: 9426 Location: Greensville,Ontario
|
|
Posted: Sun Oct 28, 2018 5:14 am |
|
|
hmm.. I was curious, looked at the datasheet,
HMMM... there's a HUGE difference in F vs LF parts for ADC clock speeds.
I don't recall ever seeing that before. learned something new...well reinforces the 'read the datasheet'......
Course now I'm wondering why, as I assume the ADC subsection on the die is the same for both F and LF parts.
Jay |
|
 |
PROMOD
Joined: 01 Feb 2018 Posts: 42
|
|
Posted: Sun Oct 28, 2018 11:49 pm |
|
|
For PIC18F4431-
If AD clock source(TAD) is "2Tosc" then maximum device frequency is 4.8Mhz.
Could you please explain the above sentence? |
|
 |
Ttelmah
Joined: 11 Mar 2010 Posts: 19731
|
|
Posted: Mon Oct 29, 2018 12:57 am |
|
|
Exactly what it says.
If you select the ADC clock source as 2 Tosc (Fosc/2), then the maximum Fosc that can be used is 4.8MHz.
That's why there are other selections. 4 Tosc supporting up to 9.6MHz, 8 Tosc supporting up to 19.2MHz. etc.. These are the ADC_CLOCK_DIV_2, ADC_CLOCK_DIV_4, & ADC_CLOCK_DIV_8 selections from the compiler (going up to /64).
Tosc=1/Fosc. |
|
 |
Ttelmah
Joined: 11 Mar 2010 Posts: 19731
|
|
Posted: Mon Oct 29, 2018 7:57 am |
|
|
temtronic wrote: | hmm.. I was curious, looked at the datasheet,
HMMM... there's a HUGE difference in F vs LF parts for ADC clock speeds.
I don't recall ever seeing that before. learned something new...well reinforces the 'read the datasheet'......
Course now I'm wondering why, as I assume the ADC subsection on the die is the same for both F and LF parts.
Jay |
If you think about it Jay, even if the die was identical, it is going to behave differently at a lower voltage.
The FET's that form the input multiplexer and sample-hold will have a higher 'on' resistance when operated from a lower voltage. The internal leakage currents will also change. If you look at a chip like the 18F4520, in the section covering the analog input model, they have a graph giving the sampling switch resistance versus Vdd. With the other similar changes this will affect how long Tacq needs to be, and how how well the sample capacitor can hold the charge for sampling, and therefore the recommended clock rate.....  |
|
 |
PROMOD
Joined: 01 Feb 2018 Posts: 42
|
|
Posted: Mon Oct 29, 2018 9:21 am |
|
|
Code: | #fuses xt,nowdt,nolvp
#use delay(clock=40000000) |
According the code Fosc=40MHz. Am I correct? |
|
 |
Ttelmah
Joined: 11 Mar 2010 Posts: 19731
|
|
Posted: Mon Oct 29, 2018 3:26 pm |
|
|
No. The setting can't work. PCM_Programmer pointed this out at the start of the thread.
First, XT is rated for 4Mhz max.
Then in fact the chip is not rated to support a crystal at 40MHz. |
|
 |
|