# Any PICBASIC codes that demonstrate the use of multiple timers for 16F628A ?

Status
Not open for further replies.

#### GDI32

##### New Member
16F628A Datasheet:
Timers 2 x 8-bit, 1 x 16-bit

How do you use those multiple timers in PICBasic ?

I would like to run two LED chaser sequence [5 LEDs each] but they are to be played out in different speeds.

I think using the 2 eight bit timers would be nice [I am assuming here that they are completely independent of each other.]

Actually I don't even mind using the 16 bit timer too if it is necessary, I just need it to work.*

The longer LED chaser sequence of the two will have a pause of say 100 [PICBASIC: Pause 100] before the next LED lights up.

Is there any PICBASIC codes that demonstrate the use of multiple timers ?

Hopefully not too overly complicated as I am just trying to flash some LEDs here.

#### Pommie

##### Well-Known Member
Setup timer 2 so it times out every mS.
In your main loop, wait for TMR2IF to be set.
Use as many counters as you like to time when LEDs should go on/off.

If you tell me what speed your pic is running at, I'll tell you how to setup timer 2.

Mike.

#### GDI32

##### New Member
I do not know what speed it is running on.
I am using the internal Resistor-Capacittor [R/C] Timer.

This is my PICBASIC PRO Codes:
@ DEVICE pic16F628A, INTRC_OSC_NOCLKOUT, MCLR_OFF, BOD_OFF
CMCON = 7 'Enable the PortA Output Controls.

So this PIC will run at whatever it is that comes from the factory ;p

So Say I want the first role of LEDs to run with this code with the first timer:
Main:
High PortB.0
pause 100
Low Port B.0
pause 100
goto Main

and I want the second Timer in the 16F628A to execute this code:
Main1:
High PortB.1
pause 300
Low Port B.1
pause 300
goto Main1

This is an over simplification of what I want to do [there will be many LEDs].
I understand that this could be accomplished using a counter variable that keep track of the tick and this will require only a SINGLE timer.

However my point is not to see how much I could squeeze out of one timer.

I would really love to utilize the second timer in the 16F628A or else, it will just be sitting there doing nothing.
I want to use the second timer just laying around doing nothing to help me flash the second row of LEDs

#### rmain1972

##### Member
According the datasheet, the internal oscillator is fixed at 4 Mhz.

#### Pommie

##### Well-Known Member
If you setup timer 2 for a 1mS period by doing,
Code:
    T2CON=5
PR2=249
Then in your main loop do,
Code:
Main:
if TMR2IF = 1 then
TMR2IF=0
Count1=Count1+1
Count2=Count2+1
endif
if Count1=100 then
High PortB.0
endif
if Count1=200 then
Low PortB.0
Count1=0
endif
if Count2=300 then
High PortB.1
endif
if Count2=600 then
Low PortB.1
Count2=0
endif
goto Main
I don't know the syntax that PBP uses so the above is just a guess. You may have to change TMR2IF to PIR1.TMR2IF.

Mike.

#### GDI32

##### New Member
What is PR2 ?
And what does setting the mysterious 249 to it does ?

Thank you Pommie and rmain1972.

#### Pommie

##### Well-Known Member
The two lines,
Code:
    T2CON=5
PR2=249
Set Timer2 prescaler to 4 and the period (PR2) to 250 (0-249=250) so the timer will time out every 4*250 = 1000 cycles. With a 4MHz crystal (1MHz instructions) 1000 cycles = 1mS.

For a more detailed explanation see the timer2 section of the data sheet.

Mike.

#### GDI32

##### New Member

As you know I am using 16F628A, the programmer software allow me some options for the oscillator and I want to use the Internal Oscillator.

I know that I have to select one of the two:
INTOSC: CLKOUT on RA6/OSC2/CLKOUT, I/O on RA7/OSC1/CLKIN
INTOSC: I/O on RA6/OSC2/CLKOUT, I/O on RA7/OSC1/CLKIN

What is the different between the two ?
As long as they "turn on" the internal Oscillator, I really don't care what other tricks they are doing.

=====================================================================

Sorry, upon closer look, I think I understand what's going on.
I will be choosing:
INTOSC: I/O on RA6/OSC2/CLKOUT, I/O on RA7/OSC1/CLKIN
as I want all the pins for I/O [more LEDs!!! ]

But I heard that PortA.5 of the 16F628A chip is "input only" is that true ?
Meaning there is no way in hell PortA.5 could output a current ?

By the way, what the difference between Data Memory Code Protect [CPD_OFF] and Program Code Protection [PROTECT_OFF] ?
What are they protecting and what are their differences ?

Last edited:

#### Pommie

##### Well-Known Member
It is true, RA5 is input only, however it is best to keep RA5 as MCLR if you are using the internal oscillator. If you select RA5 as I/O and the internal oscillator then some programmers will not reprogram the chip. You can still have 15 LEDs blinking away but be aware that the ports are limited to 100mA each, that is all 8 pins of portA must not exceed 100mA in total. This means that each LED can only have 12mA and so will not be full brightness.

The two protected areas are the EEPROM (256 bytes) and the Program Area (4096 words).

Mike.

#### GDI32

##### New Member
Thank you Mike
So Program Code Protection = Program Area
&
Data Memory Code Protect = EEPROM

Thanks

I am reading that PortA.4 cannot function as an output per say and require a pull up resistor to behave somewhat as like the other output pins that do not require as such is this true ?

I will hint your advise to make PortA.5 a MCLR, the question would then be, what is the PICBASIC PRO command to set PortA.5 as MCLR ?
Since I've placed:
@ DEVICE pic16F628A, MCLR_OFF
At the beginning, any further assigning of MCLR would be rendered null right ?

#### Pommie

##### Well-Known Member
I am reading that PortA.4 cannot function as an output per say and require a pull up resistor to behave somewhat as like the other output pins that do not require as such is this true ?
By connecting the LEDs to the 5V line and making the pin low to turn them on will solve this problem.

I will hint your advise to make PortA.5 a MCLR, the question would then be, what is the PICBASIC PRO command to set PortA.5 as MCLR ?
Since I've placed:
@ DEVICE pic16F628A, MCLR_OFF
At the beginning, any further assigning of MCLR would be rendered null right ?
Change it to,
@ DEVICE pic16F628A, MCLR_ON

Mike.

#### GDI32

##### New Member
Thank you Pommie

Let me know what you think here Pommie:
Now that I know the Pin 4 [PortA.5] is completely useless as an Output, making it an Input then would be a good opinion no ?

I mean, 16F628A could handle MCLR internally unlike the old 16F84 right ? Not to mention another having another unnecessary 4.7K resistor in the picture.

Why relegate something that the chip was designed to be able to do internally ?