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

Timer0 interrupt and printf

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
foxOnTheRun



Joined: 17 Apr 2010
Posts: 43

View user's profile Send private message

Timer0 interrupt and printf
PostPosted: Mon Oct 21, 2013 1:25 pm     Reply with quote

Straight to the point:

PIC16F690@8MHz (internal osc).
setup_timer_0(RTCC_INTERNAL|RTCC_DIV_8|RTCC_8_bit);

I'm using this to generate 1khz pwm:

Code:
#int_TIMER0
void TIMER0_isr(void) // 250 * (4*4)/8000000 --> *2 = 1kHz / 1ms
{
output_toggle(DIS_ctrl);     // on/off
pwm_ctrl = 1 - pwm_ctrl;    // flip status high/low state

// will stay up for t_on time
if (pwm_ctrl == 1)
   {
    t_off = t_on;  // next
    set_timer0(get_timer0() + (255 - t_on) + tmr0_offset); // timer0 count onward
   }
// else will stay down for t_off time
else set_timer0(get_timer0() + t_off + tmr0_offset);
}


And the main is something like this:

Code:
t_on = 125; //50% duty c. pwm wave
set_adc_channel(ch_discharge);
output_low(DIS_ctrl);
set_timer0(0);
enable_interrupts(int_TIMER0);

while (key == 0) {
    delay_ms(500);
    read_i();
    read_i();
    // printf(lcd_putc,"\n%4Lu       ", value_i());
}


Where read_i() is:

Code:
void read_i(){
vett_i[1] = vett_i[0];
vett_i[0] = read_adc() + 1;
}


And value_i() is:

Code:
unsigned int16 value_i(){
return ((vett_i[0]+vett_i[1]+1)/2);
}


Well, I can see that with the printf commented, the pwm frequency is fixed at 1KHz, as expected. But if I uncomment the printf I can spot some freq. distortion on the output wave, right when printf is working.

I've searched the forum and I see no mention that the printf should interfere with interrupts sources (it's only a printf after all) - but, I guess, it's the LCD slowlyness that breaks stuff around.. is it?

About the timer0 procedure, before, I used input() function to poll the output pin state: can somebody confirm if is it's slower of faster than setting and checking a simple variable? (well, educated guess: almost the same or slower) - any other obeservation, are greatly welcomed ;)
_________________
Listen, why don't you relax? Take a pill, bake a cake or go and read the encyclopedia.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Mon Oct 21, 2013 1:37 pm     Reply with quote

Quote:

I've searched the forum and I see no mention that the printf should
interfere with interrupts sources (it's only a printf after all)

http://www.ccsinfo.com/forum/viewtopic.php?t=51316
http://www.ccsinfo.com/forum/viewtopic.php?t=51284

Always post your compiler version.
Mike Walne



Joined: 19 Feb 2004
Posts: 1785
Location: Boston Spa UK

View user's profile Send private message

PostPosted: Mon Oct 21, 2013 4:39 pm     Reply with quote

It helps if you post a SHORT but complete compilable code we can copy and paste as is to test.

How much frequency variation are you getting?
Could you not use the hardware PWM module?

Mike
Ttelmah



Joined: 11 Mar 2010
Posts: 19339

View user's profile Send private message

PostPosted: Tue Oct 22, 2013 2:24 am     Reply with quote

Yes, compiler version, and a compilable version. Problem is that there are lots of things 'not shown', which might be the core of the problem...

I don't think this is the GIE problem, since this would leave the interrupt disabled, and stop the output.

Have to 'second' the remark, why not use the hardware PWM?

However thoughts:

What port is the LCD on?. What pin is being toggled in the interrupt?. Are you using fast_io?.
If the pin being toggled, is the spare pin on the same port as the LCD, what you may be seeing, is the TRIS register being changed as the LCD is accessed. If so, consider going to fast_io.

Which LCD driver?. CCS, or flex_lcd?.

Comments like "the main is something like this:", makes us very unwilling to look further, especially given that what is posted, won't work....

Best Wishes
johanpret



Joined: 23 Oct 2006
Posts: 33
Location: South Africa

View user's profile Send private message Visit poster's website

PostPosted: Tue Oct 22, 2013 4:06 am     Reply with quote

Compiler Bug. I had the same problem only to find out the printf switch of the Global interrupt flag. Apparently its fixed in V5.0.13 but I still have problems with V5.0.13 as well. V4.0.141 its working.
_________________
Johan Pretorius
Ttelmah



Joined: 11 Mar 2010
Posts: 19339

View user's profile Send private message

PostPosted: Tue Oct 22, 2013 4:53 am     Reply with quote

The current compiler problem with the GIE bit, results in the interrupts being _disabled_. This would stop his output.
However when this was first found, it resembled an _earlier_ problem where the interrupts were being disabled, and sometimes not re-enabled.
This is why everybody has asked 'compiler version'?.

Best Wishes
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
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