oshonsoft users - internal timers

Status
Not open for further replies.

goofieshot

New Member
i would like to write a time measuring cyclce in basic using a oshonsoft sim for the 16f628. I would like to
start the count on a input of one port and and end it with a input of another port. That part is no problem but
it seems that oshonsoft has a very limited internal timer utilization. They specify timer0 option only and that
it can only count external clk i/p. Is it possible for me to utilize a basic routine with a internal timer as a
counter of just clk cycles??. Thanx for your help and knowlege in advance.
 

hi,
The Oshonsoft has access to all the timers in a 16F628A.
If you post your program I would be pleased to help out.
 
Define COUNT_MODE = 1
Dim num_of_pulses As Word
Count PORTB.0, 1000, num_of_pulses
this is the only example that i found in their reference manual - where timer0 is defined as a couter to count
pulses on portb 0. How can i define timer1 as a timer that i can start it via a start/count command and stop
it with a stop command, then retreive the count in us or as a word val. count that i can convert to us so i can
calculate speed. Timer 1 is made specifically for the count/time purpose thus why i would like to utilize it and
my purpose for it is just a stopwatch. Can you please show me an example?? - thank you
Hopefully i can count of the internal clk drive, have it start with a cmd and stop with a cmd.
 

hi,
That can be done using Basic and the Special Function register commands, mixed in with the Basic statements or the Oshonsoft 'ASM' directive.
I think the best way to explain it to you would be an example.
Tell me which PORT pins you want to use as control start/stop , I assume you want to use Timer1, and will write and post a simple program for you.
How do you want to display the count value.?
Give me answers now and I will post a program for you tomorrow.
 
I would like to use ra0 for start and ra1 for stop using timer1 to measure time in us. I am using this to calc
speed in fps (feet/sec) over a distance of 3". I would like the resolution to be to be up to 5 milliseconds -
should equal 50fps. Thanks again for your help.
 
and i am using the oshonsoft's ability of compiling basic and thus my programming as sloppy as it is, is in basic.
 
OK,
So you really want to measure the 'period' from PORTA.0 high to PORTA.1 high in usec and the count in Timer1 will be number of usecs, to a maximum of 5000usec.
Are you using a 4mHz crystal.? also how do plan to display the period.?
 
i do not wish to put you out to writhe the soft for me - call it a learning experience, but thanx again for your help
 
i am actually using a 16mhz as we speak - and am using a 4x20 lcd to display it

OK,
For information only, I guess you know at 16mHz, the clock cycle time is 250Sec.
The reason for asking about the LCD, is so that when I write the program I can use the same LCD port connections as you.
I need to see the 'count' on the Sim, so it makes sense for my program to be compatible with what you have.???
Let me know.
 
Define LCD_LINES = 4
Define LCD_CHARS = 20
Define LCD_BITS = 4
Define LCD_DREG = PORTB
Define LCD_DBIT = 0
Define LCD_RSREG = PORTB
Define LCD_RSBIT = 1
Define LCD_EREG = PORTB
Define LCD_EBIT = 3
Define LCD_RWREG = PORTB
Define LCD_RWBIT = 2
my lcd init string - my reason for using 4 line is so i can display up to 4 speeds once i have the program running
 

OK, thats fine.
I see that PORTB.0 is free.??
Do you know that B.0 has external Interrupt options.? [for start/stop]
 
actually no - i am pretty new to pic and progamming for it - so my tutorial is as per the user manual of the oshonsoft - and their manual is a little weak - but they do have a nice inteface for tutorial purposes especially when it comes to the simulation portion. At least i get to see where i am messing up.
 
OK,
BTW, this line should be
Define LCD_DBIT = 4' for the upper 4 lcd data bits on the PORTB
 
hi,
Look at this quickie, it works fine in simulation, it will give an idea what to do.
I will clean it it up and post it tomorrow.
Code:
AllDigital

Define SIMULATION_WAITMS_VALUE = 1

Define LCD_LINES = 4
Define LCD_CHARS = 20
Define LCD_BITS = 4
Define LCD_DREG = PORTB
Define LCD_DBIT = 4
Define LCD_RSREG = PORTB
Define LCD_RSBIT = 1
Define LCD_EREG = PORTB
Define LCD_EBIT = 3
Define LCD_RWREG = PORTB
Define LCD_RWBIT = 2


Dim tmr1low As Word
Dim tmr1high As Byte
Dim tmr1count As Word

TRISA = 0xff


Lcdinit
Lcdout "Ready"

'set tmr1 prescaler to 4, to divide the clk to 1mHz
T1CON.T1CKPS1 = 1
T1CON.T1CKPS0 = 0

T1CON.T1OSCEN = 0

'internal clock
T1CON.TMR1CS = 0

'disable tmr1
T1CON.TMR1ON = 0

start_loop:
If PORTA.0 = 1 Then
T1CON.TMR1ON = 1
Goto counting
Else
Goto start_loop
Endif


counting:
If PORTA.1 = 0 Then
Goto counting
Else
T1CON.TMR1ON = 0
'read tmr1 count
tmr1low = TMR1L
tmr1high = TMR1H * 256
tmr1count = tmr1high + tmr1low
Lcdcmdout LcdLine2Clear
Lcdcmdout LcdLine2Home
Lcdout "Count=", #tmr1count

'clear timer1
TMR1L = 0
TMR1H = 0
Endif

Goto start_loop

EDIT:
a sample run
 

Attachments

  • AAesp01.gif
    6.8 KB · Views: 173
Last edited:
hi,
This program is a little tidier, with added comments.

When running in the OS simulator, reduce the 16mHz clock to 4mHz, you should find the the sim runs faster.
It also gives you more time to set PORTA.0 and .1 high, so the count is 'higher'.
Remember if you are too slow in pressing PORTA.1 after A.0, it is counting very fast and timer1 will overflow.

On my Forum Blog is a '16F ExtMod Push Button' external module for OS, download it, follow the install instructions on the blog.

Lets know how it goes.
Code:
'**** for SIM only
Define SIMULATION_WAITMS_VALUE = 1  '=0 or deleted for a PIC

Define LCD_LINES = 4
Define LCD_CHARS = 20
Define LCD_BITS = 4
Define LCD_DREG = PORTB
Define LCD_DBIT = 4
Define LCD_RSREG = PORTB
Define LCD_RSBIT = 1
Define LCD_EREG = PORTB
Define LCD_EBIT = 3
Define LCD_RWREG = PORTB
Define LCD_RWBIT = 2

AllDigital  'PORTA digital i/o

Dim tmr1count As Word  'the timer1 is 16bit

TRISA = 0xff  'set PORTA as all inputs


Lcdinit
Lcdout "Ready"  'just show that the lcd is OK

'set tmr1 prescaler to 4, to divide the clk to 1mHz
T1CON.T1CKPS1 = 1
T1CON.T1CKPS0 = 0

'disable osc section
T1CON.T1OSCEN = 0

'select internal clock
T1CON.TMR1CS = 0

'disable tmr1
T1CON.TMR1ON = 0


start_loop:
If PORTA.0 = 1 Then
T1CON.TMR1ON = 1  'enable timer1
Goto counting
Else
Goto start_loop  'await PORTA.0 high
Endif

'timer1 counting
counting:
If PORTA.1 = 0 Then
Goto counting  'await PORTA.1 high
Else
T1CON.TMR1ON = 0  'PORTA.1 is high
'read timer1 count
tmr1count.LB = TMR1L
tmr1count.HB = TMR1H

Lcdcmdout LcdLine2Clear  'clear old lcd line2 display
Lcdcmdout LcdLine2Home  'set lcd cursor to start of line2
Lcdout "Tmr1= ", #tmr1count, " uSec"  'using the '#' convert  bin to ascii and display

'clear timer1
TMR1L = 0
TMR1H = 0
Endif

'await for both PORTA.0 and .1 to be low
While PORTA.0 = 1
Wend
While PORTA.1 = 1
Wend

Goto start_loop
End

Link:
https://www.electro-tech-online.com...oft-simulator-16fkeypad4-external-module.html
 

Attachments

  • AAesp02.gif
    27.1 KB · Views: 170
Last edited:
thank you again for your time and help. This should clear up my timer utilization problem, now its time to
apply it.
 
thank you again for your time and help. This should clear up my timer utilization problem, now its time to
apply it.
hi,
If have a copy of the 16F628 datasheet you will find all the SFR [special function registers] commands, you can use them as I have shown, mixed in with the OS Basic.
 
yes - i do have a datasheet, or should i say a databook. I am trying to write a small routine that calculates ballistic velocity of a airsoft/paintball and displays it. I have chosen 4 line for the purpose of multi speed display so it displays 4 or 8 (staggered) speeds and just roll over the previous displayed data, displaying new values as long as the sensors (i/p's) are tripped. Therefore i would again like to thank you for your help and appreciate your time.
 
Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…