View previous topic :: View next topic |
Author |
Message |
Momboz
Joined: 03 Jun 2009 Posts: 29
|
Timer 2 on PIC16F15325 |
Posted: Fri Feb 28, 2020 4:43 am |
|
|
Hi
I would like to use timer 2 on my PIC16F15325 and start it immediately.
With following code I can't succeed in this. I can't get any signal on my scope on PIN_A1.
I would like a free running mode, i.e. started immediately and not hardware controlled.
Code: | #include <CCS_15325_Pulses_3.h>
#define LED PIN_A0
#define TST PIN_A1
#define DELAY 500
#INT_TIMER2
void TIMER2_isr(void) {
output_toggle(TST);
}
void main() {
setup_timer_2(T2_DIV_BY_1,0,1|T2_START_IMMEDIATELY); //0.1 us overflow, 0.1 us interrupt
enable_interrupts(INT_TIMER2);
enable_interrupts(GLOBAL);
while(TRUE) {
//Example blinking LED program
output_low(LED);
delay_ms(DELAY);
output_high(LED);
delay_ms(DELAY);
//TODO: User Code
}
}
|
Any advice? any help?
Many thanks. |
|
|
emaxxenon
Joined: 21 Jan 2020 Posts: 42
|
|
Posted: Fri Feb 28, 2020 5:47 am |
|
|
Where did you make your oscillator settings? |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
Re: Timer 2 on PIC16F15325 |
Posted: Fri Feb 28, 2020 5:56 am |
|
|
Momboz wrote: |
setup_timer_2(T2_DIV_BY_1, 0, 1|T2_START_IMMEDIATELY);
|
You're missing the clock source constant. Look in 16F15325.h in the
Timer 2 section. You most likely want the internal clock. |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9241 Location: Greensville,Ontario
|
|
Posted: Fri Feb 28, 2020 6:30 am |
|
|
this file.....CCS_15325_Pulses_3.h
we have NO idea what's in it, could easily have incorrect settings
you should always have the CCS supplied processor header as 1st line of code. at least that we KNOW what is in it.Well, should be... |
|
|
Momboz
Joined: 03 Jun 2009 Posts: 29
|
|
Posted: Fri Feb 28, 2020 6:46 am |
|
|
emaxxenon wrote: | Where did you make your oscillator settings? |
I did the clock settings when defining my device. And it is like this: Internal clock at 32MHz.
This is the first windows when using the CCS PIC Wizard. |
|
|
Momboz
Joined: 03 Jun 2009 Posts: 29
|
Re: Timer 2 on PIC16F15325 |
Posted: Fri Feb 28, 2020 6:49 am |
|
|
PCM programmer wrote: | Momboz wrote: |
setup_timer_2(T2_DIV_BY_1, 0, 1|T2_START_IMMEDIATELY);
|
You're missing the clock source constant. Look in 16F15325.h in the
Timer 2 section. You most likely want the internal clock. |
When I add the T2_CLK_INTERNAL, like this:
Code: | void main() {
setup_timer_2(T2_DIV_BY_1,0,1|T2_CLK_INTERNAL); //0.1 us overflow, 0.1 us interrupt
enable_interrupts(INT_TIMER2);
|
I get compiler error:
*** Error 103 "CCS_15325_Pulses_3.c" Line 15(33,49): Constant out of the valid range :: 257 is not 1..16 |
|
|
Momboz
Joined: 03 Jun 2009 Posts: 29
|
|
Posted: Fri Feb 28, 2020 6:51 am |
|
|
temtronic wrote: | this file.....CCS_15325_Pulses_3.h
we have NO idea what's in it, could easily have incorrect settings
you should always have the CCS supplied processor header as 1st line of code. at least that we KNOW what is in it.Well, should be... |
Here is the content of the that file:
Code: | #include <16F15325.h>
#device ADC=10
#use delay(internal=32MHz)
#use rs232(baud=57600,parity=N,xmit=PIN_C4,rcv=PIN_C5,bits=8,stream=PORT1) |
|
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
Re: Timer 2 on PIC16F15325 |
Posted: Fri Feb 28, 2020 7:03 am |
|
|
Momboz wrote: |
When I add the T2_CLK_INTERNAL, like this:
setup_timer_2(T2_DIV_BY_1,0,1|T2_CLK_INTERNAL);
I get compiler error:
*** Error 103 "CCS_15325_Pulses_3.c" Line 15(33,49): Constant out of the valid range :: 257 is not 1..16 |
Read the instructions in the .h file !
It says:
Quote: |
void setup_timer_2(int32 mode, int8 period, int8 postscaler);
#define T2_DISABLED 0
#define T2_DIV_BY_1 0x80
#define T2_DIV_BY_2 0x90
#define T2_DIV_BY_4 0xA0
#define T2_DIV_BY_8 0xB0
#define T2_DIV_BY_16 0xC0
#define T2_DIV_BY_32 0xD0
#define T2_DIV_BY_64 0xE0
#define T2_DIV_BY_128 0xF0
//One of the following may be OR'ed with the above
#define T2_CLK_T2IN 0
#define T2_CLK_INTERNAL 0x0100
#define T2_CLK_FOSC 0x0200
#define T2_CLK_HFINTRC 0x0300
#d
|
It does not say to OR it with the last parameter. It says the first one. |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9241 Location: Greensville,Ontario
|
|
Posted: Fri Feb 28, 2020 7:35 am |
|
|
thanks for posting the header contents.
Honestly it'd be better to just have those 4 lines IN your program. Sooner AND later , you'll cut a program and it doesn't work.. spend 3-4 hours on it , ONLY to discover you had changed something in that header.....and of course forgot you'd done that !
IE. Right now I KNOW that when you use THAT header in a program that gets data from a PC terminal program or other RS232 device, the program WILL lockup, freeze,stall, stop dead. |
|
|
Momboz
Joined: 03 Jun 2009 Posts: 29
|
Re: Timer 2 on PIC16F15325 |
Posted: Fri Feb 28, 2020 7:51 am |
|
|
PCM programmer wrote: |
void setup_timer_2(int32 mode, int8 period, int8 postscaler);
#define T2_DISABLED 0
#define T2_DIV_BY_1 0x80
#define T2_DIV_BY_2 0x90
#define T2_DIV_BY_4 0xA0
#define T2_DIV_BY_8 0xB0
#define T2_DIV_BY_16 0xC0
#define T2_DIV_BY_32 0xD0
#define T2_DIV_BY_64 0xE0
#define T2_DIV_BY_128 0xF0
//One of the following may be OR'ed with the above
#define T2_CLK_T2IN 0
#define T2_CLK_INTERNAL 0x0100
#define T2_CLK_FOSC 0x0200
#define T2_CLK_HFINTRC 0x0300
It does not say to OR it with the last parameter. It says the first one. |
Many thanks for your help. Yes, I did miss this point although clearly described in controller documentation. |
|
|
|