View previous topic :: View next topic |
Author |
Message |
Cogitum
Joined: 22 Mar 2012 Posts: 70 Location: France (Paris)
|
TIMER 1 problem with 18F2480 |
Posted: Mon May 28, 2012 2:30 am |
|
|
Hi,
This small code run perfectly with PROTEUS but do not RUN on the target board ! The LED start to blink after a long time (some mn).
Looking for some idea on this problem ... Thanks in advance.
Code is :
Code: |
#int_timer1
void timer1_isr(void)
{
set_timer1(0);//reset
// output_high(PIN_B4);// debug > Board test LED ON ok
if (msec++ ==76)// overflow
{
//T++;
//printf("T = %Lu\n\r",T);
output_toggle(PIN_B4);
msec=0;
}
}
//==================================
void main()
{
setup_timer_1(T1_INTERNAL|T1_DIV_BY_1); //
enable_interrupts(INT_TIMER1);
enable_interrupts(GLOBAL);
//output_high(PIN_B4);// debug > Board test LED ON ok
while(1);
} |
|
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19539
|
|
Posted: Mon May 28, 2012 2:57 am |
|
|
Probably you are not running the main CPU oscillator you actually think you are. Fuses.
Proteus doesn't 'care' if the fuses give operation at 1Hz, while being told the chip is running at 20MHz. The real hardware does.....
Best Wishes |
|
|
Cogitum
Joined: 22 Mar 2012 Posts: 70 Location: France (Paris)
|
Next .... Tmer 1 Problem 18F2480 |
Posted: Mon May 28, 2012 4:12 am |
|
|
HI Ttelmah,
Thank a lot for your quick answer.
With some change for tests like :
Code: |
while(TRUE)
{
//for(;;)
output_high(PIN_B4);// debug > Board test LED ON ok
delay_ms(10);
output_low(PIN_B4);
delay_ms(50);}
//while(1);
} |
LED is blinking but the TIMER 1 do not RUN if I put output_high (and low)
in remak ......?
Do you thing to a particular fuse ?
Best regards |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19539
|
|
Posted: Mon May 28, 2012 4:48 am |
|
|
What _speed_ is the LED flashing?. You should just about be able to see it is flickering (16Hz). If it is slower than this, then the CPU is not being clocked at the speed you are telling the code it is. So, what fuse have you got controlling the oscillator?. What delay statement have you got?.
Best Wishes |
|
|
Cogitum
Joined: 22 Mar 2012 Posts: 70 Location: France (Paris)
|
Problem with TIMER 1 and 18F2480 |
Posted: Mon May 28, 2012 7:39 am |
|
|
Hi,
sorry my submit was not published.
Test with the scope with differents values :
delay_ms( 10) = 10 ms positive pulse
delay_ms(50) = 50 ms low pulse
identical with values 1 and 5 ms
Xtal = 20 MHZ > scope tested
my full code for that :
Code: |
// TIMER 1 >> 1sec
#include <18F2480.h>
#fuses HS,NOWDT,NOPROTECT,BROWNOUT,PUT,NOLVP
#use delay (clock=20000000)
#use rs232(baud=9600,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8)
int16 T,msec;
#int_timer1
void timer1_isr(void)
{
set_timer1(0);//reset
// output_high(PIN_B4);// debug > Board test LED ON ok
if (msec++ ==76)// overflow
{
//T++;
//printf("T = %Lu\n\r",T);
output_toggle(PIN_B4);
msec=0;
}
}
//==================================
void main()
{
setup_timer_1(T1_INTERNAL|T1_DIV_BY_1); //
enable_interrupts(INT_TIMER1);
enable_interrupts(GLOBAL);
while(TRUE)
{
//for(;;)
output_high(PIN_B4);// debug > Board test LED ON ok
delay_ms(1);
output_low(PIN_B4);
delay_ms(5);}
//while(1);
} |
>>> Timer 1 do not run
Regards |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19539
|
|
Posted: Mon May 28, 2012 8:16 am |
|
|
Step through things:
What do you mean 'identical with pulses of 1 & 5mSec'. They should be very different...
'Xtal=20MHz scope tested'. No. You should _not_ try to test the oscillator with a scope. The capacitance is such that it invalidates any test. You test the oscillator by timing the width of a pulse, or using a programmed PWM. You could well have an oscillator that runs completely differently when the scope is present.....
Now, the code is about 3* more complex than it wants to be:
Code: |
#include <18F2480.h>
#fuses HS,NOWDT,NOPROTECT,BROWNOUT,PUT,NOLVP,NOPBADEN
#use delay (clock=20000000)
#use rs232(baud=9600,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8)
int8 msec; //Why waste time using an int16?
#int_timer1
void timer1_isr(void) {
//set_timer1(0);//reset - No. The interrupt is called _because_ the timer
//has wrapped to zero....
if (msec++ >=76) {// overflow
output_toggle(PIN_B4);
msec=0;
}
}
//==================================
void main(void) {
setup_timer_1(T1_INTERNAL|T1_DIV_BY_1); //
enable_interrupts(INT_TIMER1);
enable_interrupts(GLOBAL);
while(TRUE) {
delay_us(10);
}
}
|
Now:
1) You have a chance of seeing the toggle in the interrupt. Before it would be easy to miss with the pin being set/cleared in the main.
2) Timing - ((5E-8*4)*65536)*78 = 1.022 seconds - remember your count is incremented _after_ you test it, and counts 0 to 77.
3) Testing for ==, what happens if somehow the value gets incremented/decremented elsewhere?. >= avoids this being a problem.
4) Key one. What is port B4, configured as on boot?. Data sheet. What does NOPBADEN do?.
Best Wishes |
|
|
Cogitum
Joined: 22 Mar 2012 Posts: 70 Location: France (Paris)
|
TIMER 1 and 18F2480 solved |
Posted: Mon May 28, 2012 9:19 am |
|
|
Hi Ttelmah,
First : THANK a LOT for yours HELP !!!
When I said "identical", i mean pulses on the scope was :
1) first test 10 and 50 ms
2) second test 1 and 5 ms
Quick test on the xtal was only to have just one idea on the period (20MHZ) I'm rigth with you ..... with add capacitor from the stobe and so on.
(i'm familiar with RF)
Best regard |
|
|
|