CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

adc function in interrupt sub-routine
Goto page 1, 2  Next
 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
PROMOD



Joined: 01 Feb 2018
Posts: 42

View user's profile Send private message

adc function in interrupt sub-routine
PostPosted: Wed Mar 28, 2018 11:49 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Thu Mar 29, 2018 12:12 am     Reply with quote

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

View user's profile Send private message

PostPosted: Thu Mar 29, 2018 12:36 am     Reply with quote

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: 19195

View user's profile Send private message

PostPosted: Thu Mar 29, 2018 2:46 am     Reply with quote

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

View user's profile Send private message

PostPosted: Fri Mar 30, 2018 11:44 am     Reply with quote

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

View user's profile Send private message

PostPosted: Sat Oct 27, 2018 9:11 am     Reply with quote

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: 19195

View user's profile Send private message

PostPosted: Sat Oct 27, 2018 11:45 am     Reply with quote

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

View user's profile Send private message

PostPosted: Sat Oct 27, 2018 12:33 pm     Reply with quote

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: 19195

View user's profile Send private message

PostPosted: Sat Oct 27, 2018 1:56 pm     Reply with quote

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: 9081
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Sun Oct 28, 2018 5:14 am     Reply with quote

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

View user's profile Send private message

PostPosted: Sun Oct 28, 2018 11:49 pm     Reply with quote

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: 19195

View user's profile Send private message

PostPosted: Mon Oct 29, 2018 12:57 am     Reply with quote

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: 19195

View user's profile Send private message

PostPosted: Mon Oct 29, 2018 7:57 am     Reply with quote

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..... Very Happy
PROMOD



Joined: 01 Feb 2018
Posts: 42

View user's profile Send private message

PostPosted: Mon Oct 29, 2018 9:21 am     Reply with quote

Code:
#fuses xt,nowdt,nolvp
#use delay(clock=40000000)


According the code Fosc=40MHz. Am I correct?
Ttelmah



Joined: 11 Mar 2010
Posts: 19195

View user's profile Send private message

PostPosted: Mon Oct 29, 2018 3:26 pm     Reply with quote

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.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Goto page 1, 2  Next
Page 1 of 2

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group