View previous topic :: View next topic |
Author |
Message |
pic16freak
Joined: 10 Oct 2011 Posts: 7
|
timer 1 interrupt in high speed |
Posted: Wed Oct 12, 2011 3:25 pm |
|
|
Hi All,
I am trying to configure timer1 overflow interrupt on PIC16F616 but i m not able to get it working.
Timer 0 is internally divided by 4 so cannot use it.
I need high speed interrupt at around 32KHz or more.
Also, how do I configure registers such as T1CON and INTCON?
trying to get PWM output at pin c2.
Code: |
#include <main.h>
#define DebounceTime 15
#define ZERO 0
#define LED1 PIN_A1
#define OnOff PIN_A0
#define KEY1 PIN_C3
#define PUMP PIN_C2
#define IGN PIN_C1
//unsigned int l = 0;
unsigned int16 timerintcnt = 0; //long timerintcnt = 0;
//unsigned
volatile int16 motintcnt = 0; //long motintcnt = 0;
int1 ONOFFDebounce = 0; //bit ONOFFDebounce = 0;
//bit PumpCtl = 0;
int1 cooldown = 0; //bit cooldown = 0;
int1 ledFlag = 0; //bit ledFlag = 0;
INT1 keyflag = 0;
//bit keyflag = 0;
#int_EXT
void EXT_isr(void)
{
motintcnt = 0;
}
#int_TIMER1
void TIMER1_isr(void)
{
keyflag = !keyflag;
if(keyflag)
{
OUTPUT_HIGH(LED1);
}
else
{
OUTPUT_LOW(LED1);
}
}
void main()
{
setup_adc_ports(sAN3);
setup_adc(ADC_CLOCK_DIV_2);
setup_timer_1(T1_INTERNAL|T1_DIV_BY_1); //32.7 ms overflow
setup_comparator(NC_NC_NC_NC);// This device COMP currently not supported by the PICWizard
enable_interrupts(INT_EXT);
enable_interrupts(INT_TIMER1);
enable_interrupts(GLOBAL);
}
|
|
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Wed Oct 12, 2011 3:49 pm |
|
|
Quote: | Timer 0 is internally divided by 4 so cannot use it.
|
All timers use Fosc/4 as the base timer clock.
Quote: | I need high speed interrupt at around 32KHz or more.
|
You didn't specify your PIC oscillator frequency.
Quote: | Also, how do I configure registers such as T1CON and INTCON? |
The setup_timer_1() function sets up T1CON, and enable_interrupts(GLOBAL)
talks to INTCON. Look at the .LST file (in Symbolic format - selectable
in MPLAB) to see what the compiler is loading them with. |
|
|
pic16freak
Joined: 10 Oct 2011 Posts: 7
|
|
Posted: Wed Oct 12, 2011 4:34 pm |
|
|
Quote: |
You didn't specify your PIC oscillator frequency.
|
I am using 8 MHz internal oscillator.
Quote: |
All timers use Fosc/4 as the base timer clock.
|
Datasheet says for Timer 1
Clock Source TMR1CS T1ACS
FOSC/4 0 0
FOSC 0 1
T1CKI pin 1 x
Please tell me which Timer should I use and how?
I appreciate the help. |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Wed Oct 12, 2011 4:39 pm |
|
|
The 16F616 data sheet says Timer1 runs off Fosc/4, unless
you driver the T1OSC pin with an external clock source:
Quote: |
6.2 Clock Source Selection
The TMR1CS bit of the T1CON register is used to select
the clock source. When TMR1CS = 0, the clock source
is FOSC/4. When TMR1CS = 1, the clock source is
supplied externally. |
Quote: | I am using 8 MHz internal oscillator.
|
Then if you use T1_DIV_BY_1, Timer1 will be clocked at 2 MHz.
Quote: | Please tell me which Timer should I use and how?
|
Tell us your overall purpose. Why do you think you need a 32 KHz interrupt ? |
|
|
pic16freak
Joined: 10 Oct 2011 Posts: 7
|
I am trying to do a motor control |
Posted: Wed Oct 12, 2011 4:46 pm |
|
|
Sorry, I might have misunderstood the Timer1 config.
You are right about it.
I am trying to get PWM output using external int and timer int to control motor speed. |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Wed Oct 12, 2011 5:08 pm |
|
|
32 KHz is nearly too fast for an 8 MHz oscillator with the CCS default
interrupt handler. It takes about 50 instructions to get in and out of
your #int_timer1 routine. That's 25 usec. Because 32 KHz is a 31.25
usec period, then you only have 7 usec for the user code inside the isr.
That's only 14 instructions. If you could even do your PWM in that
amount of code, this would leave the main code in your program with
practically no machine cycles left at all, to do anything.
You could use #int_global. There are code examples in the forum
archives for this.
I just looked at the 16F616 data sheet and it has a hardware PWM module.
Why not use it ? |
|
|
pic16freak
Joined: 10 Oct 2011 Posts: 7
|
I will have to analyze it |
Posted: Wed Oct 12, 2011 5:18 pm |
|
|
It will take some time for me to do that and I might even need some help with that. Can we control and change the speed/PWM if I use it?
I appreciate the help.
Thanks, |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Wed Oct 12, 2011 5:44 pm |
|
|
If you use hardware PWM, you can set the PWM frequency and the PWM
duty cycle. Normally you don't change the frequency once it's set - only
the duty cycle is changed. This post has links to PWM articles on the forum:
http://www.ccsinfo.com/forum/viewtopic.php?t=45968&start=1 |
|
|
asmboy
Joined: 20 Nov 2007 Posts: 2128 Location: albany ny
|
|
Posted: Wed Oct 12, 2011 6:02 pm |
|
|
My guess is that you could program a sweet 32khz hardware PWM
with the full 10 bit resolution by using a pic master frequency around 32mhz - using the 18F pic family - commit timer2 and use HSPLL clocking an 8 mhz source - no ??
BTW: have you noticed what happens after the last instruction in main()? |
|
|
freesat
Joined: 08 Feb 2011 Posts: 32
|
|
Posted: Thu Oct 13, 2011 8:29 am |
|
|
I have success on ac dimmer with lamps at 60hz at 20mhz clock using timer2 div_by_1 with prescaler 65, allowing 255 steps.
8bit or 255 steps ( almost too fast for 20mhz )
10bit or or 1024 steps ( you need 40mhz minimal clock )
best regards. |
|
|
|