View previous topic :: View next topic |
Author |
Message |
foxOnTheRun
Joined: 17 Apr 2010 Posts: 43
|
Timer0 interrupt and printf |
Posted: Mon Oct 21, 2013 1:25 pm |
|
|
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
|
|
|
Mike Walne
Joined: 19 Feb 2004 Posts: 1785 Location: Boston Spa UK
|
|
Posted: Mon Oct 21, 2013 4:39 pm |
|
|
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: 19537
|
|
Posted: Tue Oct 22, 2013 2:24 am |
|
|
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
|
|
Posted: Tue Oct 22, 2013 4:06 am |
|
|
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: 19537
|
|
Posted: Tue Oct 22, 2013 4:53 am |
|
|
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 |
|
|
|