I pretend to count how many times a pin (ra4) goes to 0V or V+. My idea in the future will be to build an anemometer or an frequency counter or something based on that. I know that timers are excelent to do the job but until now I wasn't able to make a test with a switch.
I have a resistor from RA4 to V+ and a switch to 0V. I supose that when I press the switch the timer will be incremented and then I can display Timer0 value to my 7seg.display.
Until now none of my approaches give me a good result.
Can anyone give me an idea how to make timer0 work?
Thank you all
Eduardo Freitas
the code I'm using is this:
'=================================================
'Chip model
#chip 16F628A, 4
#config OSC = INTOSC_OSC_NOCLKOUT, WDT = OFF
'config OSC = XT, WDT = OFF
#define StartUpDelay 100 ms
'setting the 7 segment display with 2 displays
#define displaycount 4
#define DisplayPortA PORTB
#define DisplayPortB PORTB
#define DisplayPortC PORTB
#define DisplayPortD PORTB
'#define DispSelectA set PORTA.0 ON set PORTA.1 OFF
'#define DispSelectB set PORTA.0 OFF set PORTA.1 ON
dir PORTB.0 out
dir PORTB.1 out
dir PORTB.2 out
dir PORTB.3 out
dir PORTB.4 out
dir PORTB.5 out
dir PORTB.6 out
dir PORTB.7 out
dir PORTA.0 out
dir PORTA.1 out
dir PORTA.2 out
dir PORTA.3 out
dir PORTA.4 in
InitTimer0(Ext,PS0_1/2)
Startup:
Wait StartUpDelay
'MAIN PROGRAM
dataCount=0
Main:
' First time value =0, after that count pulses on RA4(remember prescaler /2)
dataCount=timer0
ClearTimer 0
'START COUNT PULSES on pin
Centenas=dataCount/100
Dezenas=(dataCount %100)/10
Unidades=dataCount % 10
for icounter = 1 to 128
Set Display2 ON:Set Display3 ON:Set Display4 ON:Set Display1 OFF
displayvalue 1, unidades
waitDisplay
Set Display1 ON:Set Display3 ON:Set Display4 ON:Set Display2 OFF
displayvalue 2, dezenas
waitDisplay
Set Display1 ON:Set Display2 ON:Set Display4 ON:Set Display3 OFF
displayvalue 3, centenas
waitDisplay
Set Display1 ON:Set Display2 ON:Set Display3 ON:Set Display4 OFF
displayvalue 4, dataCount
waitDisplay
next
iCounter=1
'Clear all
unidades=0
dezenas=0
centenas=0
'clear display
Set Display1 ON:Set Display2 ON:Set Display3 ON:Set Display4 ON
displaychar 1, "0"
Set Display1 ON:Set Display2 ON:Set Display3 ON:Set Display4 ON
displaychar 2, "0"
Set Display1 ON:Set Display2 ON:Set Display3 ON:Set Display4 ON
displaychar 3, "0"
Set Display1 ON:Set Display2 ON:Set Display3 ON:Set Display4 ON
displaychar 4, "0"
wait 1 s
Goto Main
'================================================
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Sorry Edward, but that precisely is the error in the original GCBasic code, the clock source is selected in OPTION_REG.T0CS register, not in OPTION_REG.T0SE register, that is for select rising or falling edge when in counter mode.
Then as Eduardo Freitas said, there was an error in my code but was this:
if TMR0Source = Ext THEN SET OPTION_REG.T0SC ON
Should read:
if TMR0Source = Ext THEN SET OPTION_REG.T0CS ON
_________________________________________________
You can check it in the datasheet:
bit 7 RBPU
bit 6 INTEDG
bit 5 T0CS: TMR0 Clock Source Select bit
1 = Transition on T0CKI pin
0 = Internal instruction cycle clock (CLKO)
bit 4 T0SE: TMR0 Source Edge Select bit
1 = Increment on high-to-low transition on T0CKI pin
0 = Increment on low-to-high transition on T0CKI pin
bit 3 PSA: Prescaler Assignment bit
1 = Prescaler is assigned to the WDT
0 = Prescaler is assigned to the Timer0 module
bit 2-0 PS2:PS0: Prescaler Rate Select bits
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Hi to all
I pretend to count how many times a pin (ra4) goes to 0V or V+. My idea in the future will be to build an anemometer or an frequency counter or something based on that. I know that timers are excelent to do the job but until now I wasn't able to make a test with a switch.
I have a resistor from RA4 to V+ and a switch to 0V. I supose that when I press the switch the timer will be incremented and then I can display Timer0 value to my 7seg.display.
Until now none of my approaches give me a good result.
Can anyone give me an idea how to make timer0 work?
Thank you all
Eduardo Freitas
the code I'm using is this:
'=================================================
'Chip model
#chip 16F628A, 4
#config OSC = INTOSC_OSC_NOCLKOUT, WDT = OFF
'config OSC = XT, WDT = OFF
#define StartUpDelay 100 ms
'setting the 7 segment display with 2 displays
#define displaycount 4
#define DispSelectA nop
#define DispSelectB nop
#define DispSelectC nop
#define DispSelectD nop
#define Display1 PortA.0
#define Display2 PortA.1
#define Display3 PortA.2
#define Display4 PortA.3
#define waitDisplay wait 6 ms
#define DisplayPortA PORTB
#define DisplayPortB PORTB
#define DisplayPortC PORTB
#define DisplayPortD PORTB
'#define DispSelectA set PORTA.0 ON set PORTA.1 OFF
'#define DispSelectB set PORTA.0 OFF set PORTA.1 ON
dir PORTB.0 out
dir PORTB.1 out
dir PORTB.2 out
dir PORTB.3 out
dir PORTB.4 out
dir PORTB.5 out
dir PORTB.6 out
dir PORTB.7 out
dir PORTA.0 out
dir PORTA.1 out
dir PORTA.2 out
dir PORTA.3 out
dir PORTA.4 in
InitTimer0(Ext,PS0_1/2)
Startup:
Wait StartUpDelay
'MAIN PROGRAM
dataCount=0
Main:
' First time value =0, after that count pulses on RA4(remember prescaler /2)
dataCount=timer0
ClearTimer 0
'START COUNT PULSES on pin
Centenas=dataCount/100
Dezenas=(dataCount %100)/10
Unidades=dataCount % 10
for icounter = 1 to 128
Set Display2 ON:Set Display3 ON:Set Display4 ON:Set Display1 OFF
displayvalue 1, unidades
waitDisplay
Set Display1 ON:Set Display3 ON:Set Display4 ON:Set Display2 OFF
displayvalue 2, dezenas
waitDisplay
Set Display1 ON:Set Display2 ON:Set Display4 ON:Set Display3 OFF
displayvalue 3, centenas
waitDisplay
Set Display1 ON:Set Display2 ON:Set Display3 ON:Set Display4 OFF
displayvalue 4, dataCount
waitDisplay
next
iCounter=1
'Clear all
unidades=0
dezenas=0
centenas=0
'clear display
Set Display1 ON:Set Display2 ON:Set Display3 ON:Set Display4 ON
displaychar 1, "0"
Set Display1 ON:Set Display2 ON:Set Display3 ON:Set Display4 ON
displaychar 2, "0"
Set Display1 ON:Set Display2 ON:Set Display3 ON:Set Display4 ON
displaychar 3, "0"
Set Display1 ON:Set Display2 ON:Set Display3 ON:Set Display4 ON
displaychar 4, "0"
wait 1 s
Goto Main
'================================================
Hi Eduardo.
I think there is a bug in the Timer0 function in timer.h that afect only to the counter mode
You can try this function: copy in yor code afther "Goto Main" and use "MyInitTimer0 (Ext, PS0_1/2)" instead of "InitTimer0(Ext,PS0_1/2)":
Sub MyInitTimer0(TMR0Source, TMR0Pres) #NR
OPTION_REG = OPTION_REG AND 192
if TMR0Source = Ext THEN SET OPTION_REG.T0SC ON
OPTION_REG = TMR0Pres OR OPTION_REG
clrwdt
End Sub
If this works, you can edit your ../include/lowlevel/timer.h file, look for the function "Sub SInitTimer0" (line 152) and change it for this one:
Sub Sub SInitTimer0
OPTION_REG = OPTION_REG AND 192
if TMR0Source = Ext THEN SET OPTION_REG.T0SC ON
OPTION_REG = TMR0Pres OR OPTION_REG
clrwdt
End Sub
Regards.
Thank you Santiago,
The change result! I changed the OPTION_REG.T0SC to OPTION_REG.T0CS and now is counting!
Regards
You're right... was a typo error... but you realized it.
Sorry for the mistake.
Santiago, there is an error in your code:
if TMR0Source = Ext THEN SET OPTION_REG.T0SC ON
Should read:
if TMR0Source = Ext THEN SET OPTION_REG.T0SE ON
Regards, Ed.
Sorry Edward, but that precisely is the error in the original GCBasic code, the clock source is selected in OPTION_REG.T0CS register, not in OPTION_REG.T0SE register, that is for select rising or falling edge when in counter mode.
Then as Eduardo Freitas said, there was an error in my code but was this:
if TMR0Source = Ext THEN SET OPTION_REG.T0SC ON
Should read:
if TMR0Source = Ext THEN SET OPTION_REG.T0CS ON
_________________________________________________
You can check it in the datasheet:
bit 7 RBPU
bit 6 INTEDG
bit 5 T0CS: TMR0 Clock Source Select bit
1 = Transition on T0CKI pin
0 = Internal instruction cycle clock (CLKO)
bit 4 T0SE: TMR0 Source Edge Select bit
1 = Increment on high-to-low transition on T0CKI pin
0 = Increment on low-to-high transition on T0CKI pin
bit 3 PSA: Prescaler Assignment bit
1 = Prescaler is assigned to the WDT
0 = Prescaler is assigned to the Timer0 module
bit 2-0 PS2:PS0: Prescaler Rate Select bits
Santiago, I got an error compiling it the way it was: TOSC not previously defined. Works fine after I changed it to TOSE as above. Regards, Ed.
Edwuard, in my previous post i said (read carefully):
___________________________________________________
Then as Eduardo Freitas said, there was an error in my code but was this:
if TMR0Source = Ext THEN SET OPTION_REG.T0SC ON
Should read:
if TMR0Source = Ext THEN SET OPTION_REG.T0CS ON
___________________________________________________
TOSC bit doesn't exist, the correct name is TOCS.
This error only appears when using External clock source, when using internal clock there is no problem.
Greetings.
Sorry i forgot to say, the correct is T0CS:
0 = number
O = capital letter
The correct is with number: T0CS = Timer 0 Clock Source