Timer1

For Flowcode users to discuss projects, flowcharts, and any other issues related to Flowcode 2 and 3.

Moderators: Benj, Mods

Post Reply
KeesD
Posts: 6
Joined: Thu Aug 16, 2007 9:40 pm
Location: Leiden (NL)
Has thanked: 2 times
Been thanked: 1 time
Contact:

Timer1

Post by KeesD »

How can I use TMR1 of a PIC16F627A in flowcode?

User avatar
Steve
Matrix Staff
Posts: 3418
Joined: Tue Jan 03, 2006 3:59 pm
Has thanked: 114 times
Been thanked: 422 times
Contact:

Post by Steve »

You have 2 choices:

1) Use a "custom" interrupt, and configure it by hand.

2) Modify the 627A FCD file.

The second option is perhaps the easiest to explain because we already support the TMR1 module in some FCD files - e.g. the 16F877a.

Open the 16F627A FCD file and alter the following:

Code: Select all

[Interrupts]
;first 3 should always be 1=TMR0, 2=RB0INT, 3=PORTB
;any other interrupts need to also have this set: "intcon.PEIE=1;\n"
GeneralInit="intcon.GIE=1;\n"
Count=3
1=TMR0
2=RB0INT
3=PORTB
This should be changed to:

Code: Select all

[Interrupts]
;first 3 should always be 1=TMR0, 2=RB0INT, 3=PORTB
;any other interrupts need to also have this set: "intcon.PEIE=1;\n"
GeneralInit="intcon.GIE=1;\n"
Count=4
1=TMR0
2=RB0INT
3=PORTB
4=TMR1
And then add the following section at the end of the FCD file:

Code: Select all

[TMR1]
;NOTE: this osc is enabled and turned on as part of the Opt1 code
Name="TMR1 Overflow"
FlagReg=pir1
FlagBit=TMR1IF
EnReg=pie1
EnBit=TMR1IE
OptCnt=2
TmrOpt=2
Opt1="Clock Source Select"
Opt1Cnt=2
Opt1_1="Transition on T1CKI pin"
Opt1_1Val="intcon.PEIE=1;\nt1con.T1OSCEN=1;\nt1con.TMR1ON=1;\nt1con.TMR1CS=1;\n"
Opt1_2="Internal clock (Fosc/4)"
Opt1_2Val="intcon.PEIE=1;\nt1con.T1OSCEN=1;\nt1con.TMR1ON=1;\nt1con.TMR1CS=0;\n"
Opt2="Prescaler Rate"
Opt2Cnt=4
Opt2_1="1:1"
Opt2_1Val="t1con = t1con & 0x0F;\n"
Opt2_2="1:2"
Opt2_2Val="t1con = (t1con & 0x0F) | 0x10;\n"
Opt2_3="1:4"
Opt2_3Val="t1con = (t1con & 0x0F) | 0x20;\n"
Opt2_4="1:8"
Opt2_4Val="t1con = (t1con & 0x0F) | 0x30;\n"

Ondra
Posts: 325
Joined: Wed Aug 29, 2007 7:33 pm
Been thanked: 2 times
Contact:

Post by Ondra »

Hi I am using the PIC16F690 and want to use timer1. Can the above work for me? Also do you have a list of the PIC's where Flowcode already has support for timer1 built in.

Ondra

User avatar
Steve
Matrix Staff
Posts: 3418
Joined: Tue Jan 03, 2006 3:59 pm
Has thanked: 114 times
Been thanked: 422 times
Contact:

Post by Steve »

A similar approach should work for that chip. You will need to check that the same registers and bits are used in this chip.

I think the only chip currently to have the TMR1 code is the 16F877a.

Ondra
Posts: 325
Joined: Wed Aug 29, 2007 7:33 pm
Been thanked: 2 times
Contact:

Post by Ondra »

Thanks Steve.
I don't want to make a mess of this. Could you give me just a little more detail. In particular which line of code would I be looking to change changing?

Ondra

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Post by Benj »

Hello Ondra

Browse down to the [Interrupts] section of the FCD, change the line Count=3 to Count=4 and then add the following line to the end of the section.

Code: Select all

4=TMR1
Then add the following section of code to the end of the FCD file

Code: Select all

[TMR1]
;NOTE: this osc is enabled and turned on as part of the Opt1 code
Name="TMR1 Overflow"
FlagReg=pir1
FlagBit=TMR1IF
EnReg=pie1
EnBit=TMR1IE
OptCnt=2
TmrOpt=2
Opt1="Clock Source Select"
Opt1Cnt=2
Opt1_1="Transition on T1CKI pin"
Opt1_1Val="intcon.PEIE=1;\nt1con.T1OSCEN=1;\nt1con.TMR1ON=1;\nt1con.TMR1CS=1;\n"
Opt1_2="Internal clock (Fosc/4)"
Opt1_2Val="intcon.PEIE=1;\nt1con.T1OSCEN=1;\nt1con.TMR1ON=1;\nt1con.TMR1CS=0;\n"
Opt2="Prescaler Rate"
Opt2Cnt=4
Opt2_1="1:1"
Opt2_1Val="t1con = t1con & 0x0F;\n"
Opt2_2="1:2"
Opt2_2Val="t1con = (t1con & 0x0F) | 0x10;\n"
Opt2_3="1:4"
Opt2_3Val="t1con = (t1con & 0x0F) | 0x20;\n"
Opt2_4="1:8"
Opt2_4Val="t1con = (t1con & 0x0F) | 0x30;\n"


If you are worried about making mistakes then make a backup of the FCD file first so that you can always replace it if your edited file goes wrong.

Let me know how you get on.

Ondra
Posts: 325
Joined: Wed Aug 29, 2007 7:33 pm
Been thanked: 2 times
Contact:

Post by Ondra »

Thanks Steve. I'll give it try.

Ondra
Posts: 325
Joined: Wed Aug 29, 2007 7:33 pm
Been thanked: 2 times
Contact:

Re: Timer1 not working

Post by Ondra »

Ok. I tried this and can't get it working. I changed the fcd file and created a small program
that blinks an Led. I chose the a prescaler of 1:8 which gave me a frequence of 2400Hz.
I set a variable to increment and a light to blink ever second through an "If" block. when I
use the TMR0 at 1:256 (75Hz) it works. when I change to TMR1 nothing happens. I chose
both clock options with no luck. What am I missing? Thanks.

Ondra

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: Timer1

Post by Benj »

Hello Ondra

Try replacing the [TMR1] section with the following code.

I think that the problem was that the low power oscillator was being enabled for timer 1.

Code: Select all

[TMR1]
;NOTE: this osc is enabled and turned on as part of the Opt1 code
Name="TMR1 Overflow"
FlagReg=pir1
FlagBit=TMR1IF
EnReg=pie1
EnBit=TMR1IE
OptCnt=2
TmrOpt=2
Opt1="Clock Source Select"
Opt1Cnt=2
Opt1_1="Transition on T1CKI pin"
Opt1_1Val="intcon.PEIE=1;\nt1con.T1OSCEN=0;\nt1con.TMR1ON=1;\nt1con.TMR1CS=1;\n"
Opt1_2="Internal clock (Fosc/4)"
Opt1_2Val="intcon.PEIE=1;\nt1con.T1OSCEN=0;\nt1con.TMR1ON=1;\nt1con.TMR1CS=0;\n"
Opt2="Prescaler Rate"
Opt2Cnt=4
Opt2_1="1:1"
Opt2_1Val="t1con = t1con & 0x0F;\n"
Opt2_2="1:2"
Opt2_2Val="t1con = (t1con & 0x0F) | 0x10;\n"
Opt2_3="1:4"
Opt2_3Val="t1con = (t1con & 0x0F) | 0x20;\n"
Opt2_4="1:8"
Opt2_4Val="t1con = (t1con & 0x0F) | 0x30;\n"
Also make sure that you are assigning the internal clock to the Timer1 interrupt. Also worth noting that Timer 1 is a 16-bit timer and as such will take precisely 256 times longer to timeout then timer 0. This could be the problem. The Flowcode timeout value may be set up for an 8 bit timer. In this case simply devide the output frequency stated in Flowcode by 256 to get the actual frequency. If you wish to use the timer1 interrupt as an 8 bit timer then you can always run the following C command at the start of the interrupt.

Code: Select all

tmr1h = 255;
Hope this helps. :P

Ondra
Posts: 325
Joined: Wed Aug 29, 2007 7:33 pm
Been thanked: 2 times
Contact:

Re: Timer1

Post by Ondra »

IT WORKSSSSSS!
Thanks Ben

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: Timer1

Post by Benj »

Hey thats great, Thanks for letting me know.

Did you have to use the C code to clear the tmr1h register?

Ondra
Posts: 325
Joined: Wed Aug 29, 2007 7:33 pm
Been thanked: 2 times
Contact:

Re: Timer1

Post by Ondra »

No I didn't. Thanks

Ondra

Ondra
Posts: 325
Joined: Wed Aug 29, 2007 7:33 pm
Been thanked: 2 times
Contact:

Re: Timer1

Post by Ondra »

Compiler error. Benj or Steve I think this might tie in to what was done in this situation. When I try to use the RB Port change I get this error.
The timer 1 is working fine.
Ondra


C:\E-Tank\flowcode\ElinK\BBRS232.c(397:9): error: unknown identifier 'RBIE'
C:\E-Tank\flowcode\ElinK\BBRS232.c(397:9): error: unexpected '.' operator
C:\E-Tank\flowcode\ElinK\BBRS232.c(397:2): error: failed to generate expression
C:\E-Tank\flowcode\ElinK\BBRS232.c(397:2): error: invalid operand 'intcon.RBIE'
C:\E-Tank\flowcode\ElinK\BBRS232.c(397:13): error: failed to generate expression
C:\E-Tank\flowcode\ElinK\BBRS232.c(405:21): error: unknown identifier 'RBIF'
C:\E-Tank\flowcode\ElinK\BBRS232.c(405:21): error: invalid operand 'RBIF'
C:\E-Tank\flowcode\ElinK\BBRS232.c(405:18): error: failed to generate expression
C:\E-Tank\flowcode\ElinK\BBRS232.c(405:18): error: invalid operand '<<'
C:\E-Tank\flowcode\ElinK\BBRS232.c(405:13): error: failed to generate expression
C:\E-Tank\flowcode\ElinK\BBRS232.c(405:13): internal error: failed to generate 'if' expression
BBRS232.c success

failure

.........
Return code = 1

Flowcode was unable to compile the flowchart's C code due to the following errors:


If your flowchart contains C code, please review this carefully. If your flowchart contains no C-code or you have thoroughly reviewed the code, contact Technical Support.

FINISHED

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: Timer1

Post by Benj »

Hello Ondra

Which chip are you using. Also is it a custom interrupt you are using or one of the pre-set interrupts.

Ondra
Posts: 325
Joined: Wed Aug 29, 2007 7:33 pm
Been thanked: 2 times
Contact:

Re: Timer1

Post by Ondra »

I'm using the PIC 16F690. Its a pre-set interrupt.

Ondra

Ondra
Posts: 325
Joined: Wed Aug 29, 2007 7:33 pm
Been thanked: 2 times
Contact:

Re: Timer1

Post by Ondra »

Any opinions on this one?

Ondra

Ondra
Posts: 325
Joined: Wed Aug 29, 2007 7:33 pm
Been thanked: 2 times
Contact:

Re: Timer1

Post by Ondra »

I really need some help on this guys.

Ondra
Posts: 325
Joined: Wed Aug 29, 2007 7:33 pm
Been thanked: 2 times
Contact:

Re: Timer1

Post by Ondra »

I also notice that in the Interrupt list there is an RB0 But the data sheet for the 16F690 does not have an RB0
pin. Please help with this.

Ondra

User avatar
Steve
Matrix Staff
Posts: 3418
Joined: Tue Jan 03, 2006 3:59 pm
Has thanked: 114 times
Been thanked: 422 times
Contact:

Re: Timer1

Post by Steve »

Hi Ondra,

Sorry for the delay in answering this for you.

Yes, there is a problem with the FCD file for the 16F690 (in fact, the whole family: 631/677/685/687/689/690).

To rectify it, you should change the entries "RBIE" and "RBIF" in the [PORTB] section of the FCD file to "RABIE" and "RABIF".

To answer your other question, the "INT" pin on the 16F690 is on RA2.

Ondra
Posts: 325
Joined: Wed Aug 29, 2007 7:33 pm
Been thanked: 2 times
Contact:

Re: Timer1

Post by Ondra »

Thanks I'll give it a try.

Ondra

Ondra
Posts: 325
Joined: Wed Aug 29, 2007 7:33 pm
Been thanked: 2 times
Contact:

Re: Timer1

Post by Ondra »

Ok I think there are other issues with the FCD. The error's gone but the interrupt's still not working, are there other issues with the 16F690 file.
From the beginning the timer1 and timer0 were working. After I had the problem caused by the RB Port change interrupt the timer1 and timer0
stopped working. My question is, when I compiled the program could it be possible that it is corrupted, caused by earlier issue of the RB Port change issue?
And could that be causing the timers not to operate?

Ondra

User avatar
Steve
Matrix Staff
Posts: 3418
Joined: Tue Jan 03, 2006 3:59 pm
Has thanked: 114 times
Been thanked: 422 times
Contact:

Re: Timer1

Post by Steve »

I can't see how your program could become corrupted. Flowcode generates fresh C code each time it compiles, so there should be no corruption.

I suggest you create a simple program to test the timer interrupts.

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: Timer1

Post by Benj »

Here are the latest versions of the FCD files with the PortB interrupt moved to PortA.
Attachments
FCD_Generate_File.zip
(13.4 KiB) Downloaded 441 times

Post Reply