View previous topic :: View next topic |
Author |
Message |
Requan
Joined: 11 May 2008 Posts: 74
|
24FJ256GB206 + 1ms timer? |
Posted: Tue Jul 15, 2014 11:32 am |
|
|
Dear CCS forum Memeber,
I need to have timer interrupt each 1ms on 24FJ256GB206.
I use CCS 24F USB PCB and CCS v5.026.
I use CCSUSB.h so i have set:
Code: | #use delay(crystal=20M, clock=32M, USB_FULL) |
My code:
Code: | #include "CCSUSB.h"
#int_TIMER1
void TIMER1_isr()
{
output_toggle(RED_LED);
}
void main()
{
LED_OFF(RED_LED);
setup_timer1(TMR_INTERNAL|TMR_DIV_BY_1);
set_timer1(16000);
enable_interrupts(INT_TIMER1);
enable_interrupts(GLOBAL);
while(TRUE)
{
}
} |
When i connect to oscilloscope i get 8.20ms
I used timer with PIC18F without any problem. The strange think is when i change timer preset value in code, on oscilloscope nothing change.
Could You advise me when i did mistake? |
|
|
jeremiah
Joined: 20 Jul 2010 Posts: 1353
|
|
Posted: Tue Jul 15, 2014 12:01 pm |
|
|
Your interrupt fires every 4.1 ms, so your 8.2ms makes sense. You need to use the optional parameter in setup_timer1() to set the period of the register to 16000 instead. Doing set_timer1(16000) doesn't really do what you think it does. |
|
|
Requan
Joined: 11 May 2008 Posts: 74
|
|
Posted: Wed Jul 16, 2014 4:02 am |
|
|
jeremiah wrote: | Your interrupt fires every 4.1 ms, so your 8.2ms makes sense. You need to use the optional parameter in setup_timer1() to set the period of the register to 16000 instead. Doing set_timer1(16000) doesn't really do what you think it does. |
Thanks for Your quick help. You was right. i through in wrong way.
Now i have 1ms |
|
|
Requan
Joined: 11 May 2008 Posts: 74
|
|
Posted: Wed Jul 16, 2014 6:39 am |
|
|
jeremiah:
Could You help me in one more issue:
i set up PWM:
Code: |
#pin_select OC1 = PIN_B9
void main()
{
setup_timer2(TMR_INTERNAL | TMR_DIV_BY_1, 1000);
setup_compare(1,COMPARE_PWM_EDGE|COMPARE_TIMER2|COMPARE_CONTINUE_IDLE);
set_pwm_duty (1,25000);
while(TRUE)
{
}
|
PWM duty is ok, but when i changed period value i always get 245Hz.
I read forum and tried different settings of setup_compare but nothing help |
|
|
Requan
Joined: 11 May 2008 Posts: 74
|
|
Posted: Wed Jul 16, 2014 11:48 am |
|
|
Requan wrote: | jeremiah:
Could You help me in one more issue:
i set up PWM:
Code: |
#pin_select OC1 = PIN_B9
void main()
{
setup_timer2(TMR_INTERNAL | TMR_DIV_BY_1, 1000);
setup_compare(1,COMPARE_PWM_EDGE|COMPARE_TIMER2|COMPARE_CONTINUE_IDLE);
set_pwm_duty (1,25000);
while(TRUE)
{
}
|
PWM duty is ok, but when i changed period value i always get 245Hz.
I read forum and tried different settings of setup_compare but nothing help |
i added
Code: |
enable_interrupts(INT_TIMER1);
|
and now i can adjust frequency - strange because i read a lot of threads and nobody added it.
Now i cant't adjust duty - independently from set_pwm_duty i have 42% on 58% |
|
|
jeremiah
Joined: 20 Jul 2010 Posts: 1353
|
|
Posted: Wed Jul 16, 2014 11:59 am |
|
|
I don't have a way to check at the moment, but most of those chips have an additional PWM parameter to set...something with both SYNC and TMR2 in the name. Check the .h file for your chip. I've had a few chips where I had to specify that parameter in addition to the timer 2 parameter.
The fact that enabling timer1 interrupts had any effect makes me worried. In your latest code you don't have a timer1 isr handler and that can be dangerous with the interrupt enabled. Either way it shouldn't affect the PWM since you link it to timer 2. |
|
|
|