Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

oshonsoft timing issue

Status
Not open for further replies.

cybersky

Member
hi all - having a timing issue and math not really my thing ... here is my code:
Code:
Define CONF_WORD = 0x3f32  'clock on HS
'mike gave me these in another thread: reaction timer
T2CON = 5
PR2 = 249

INTCON.T0IE = 0  'enable Timer0 interrupts
INTCON.GIE = True  'enable all un-masked interrupts
OPTION_REG.T0CS = False  'set Timer0 clock source to internal instruction cycle clock
AllDigital
Config PORTB = Output
Config PORTA = Output
Config PORTD = Input
Config PORTD.4 = Output
Config PORTC.0 = Input
Config PORTC.1 = Input
Config PORTC.2 = Input
Config PORTC.3 = Input
Config PORTC.4 = Input
Config PORTC.5 = Input  'rx
Config PORTC.6 = Output
Config PORTC.7 = Input
Hseropen 19200

'Define simulation_  'waitms_VALUE = 1
'rows used for led and switches
Symbol row1 = PORTB.0
Symbol row2 = PORTB.1
Symbol row3 = PORTB.2
Symbol row4 = PORTB.3
Symbol row5 = PORTB.4
Symbol row6 = PORTB.5
'cols
Symbol col1 = PORTA.0
Symbol col2 = PORTA.1
Symbol col3 = PORTA.2
Symbol col4 = PORTA.3
Symbol col5 = PORTA.4
Symbol col6 = PORTA.5
'switches
Symbol sw1 = PORTC.0
Symbol sw2 = PORTC.1
Symbol sw3 = PORTC.2
Symbol sw4 = PORTC.3
Symbol sw5 = PORTD.0
Symbol sw6 = PORTD.1
'harware serial port
Symbol rx = PORTC.7
Symbol tx = PORTC.6
'switches to start and getinfo
Symbol btngo = PORTD.5
Symbol btnload = PORTD.6
Symbol speaker = PORTD.4

Dim ledon As Byte  'how long the led is on
Dim playmax As Byte  'how many frames to generate
Dim playmode As Byte  'mode 1 or 2
Dim playidx As Byte  'which frame we in
Dim btnpress As Byte  'which btn as pressed
Dim msec As Word  'this willhold the milli seconds
Dim i As Byte
Dim feed As Bit
Dim seed As Word
Dim temp As Word
Dim rand As Byte
Dim rcon As Byte
rcon = 1
'all off
PORTB = 0
PORTA = 0
PORTA = 255
WaitMs 500
PORTA = 0
PORTB = 255
WaitMs 500
PORTB = 0
PORTA = 0
PORTA = 255

speaker = 0

INTCON.T0IE = 1  'enable Timer0 interrupts

tme:
If msec = 1000 Then
INTCON.T0IE = 0  'disable Timer0 interrupts
Hserout 1
msec = 0
INTCON.T0IE = 1  'enable Timer0 interrupts
Goto tm2
Endif
Goto tme
'we waited for 1 sec now we wait for 10sec.
tm2:
If msec = 10000 Then
INTCON.T0IE = 0  'disable Timer0 interrupts
Hserout 1
msec = 0
INTCON.T0IE = 1  'enable Timer0 interrupts
Goto tme
Endif
Goto tm2

here is response in vb.net
^ 06:03.578 ^ 06:03.843 ^
06:06.406 ^ 06:06.656 ^
06:09.218 ^ 06:09.468 ^
06:12.031 ^ 06:12.296

vb.net code snp
Code:
Try
            buff += Sp1.ReadExisting
            If Mid(buff, 2, 1) = "C" Or Mid(buff, 2, 1) = "T" Or Mid(buff, 1, 1) = "" Then
                txtRec.Text += " ^ " & DateTime.Now.ToString("mm:ss.fff") & buff '& vbCrLf
            End if
catch
end try

this fires using the datareceived event.

Not giving me 1 sec or 10 sec , any help or suggestions,please?
I know vb has its own timing issues and even sending data 19200 has a msec or 2 per byte sent but this is too far off.
 
hi i am new to usng the timers, so i should change
T2CON = 5
PR2 = 249
to
T1CON = 5
PR1 = 249


BUT in Oshonsoft PR1 is not recognised ?
 
also in some other post i see them using
t1con =0 to disable timer? should i update my code or is it ok to use
INTCON.T0IE = 0 'disable Timer0 interrupts
 
What crystal are you using? If the timings for timer 2 have been given to you, you should use them..

Set peripheral interrupts INTCON.PEIE.
The timer 2 interrupt flag PIR1.TMR2IF.
The interrupt enable PIE1.TMR2IE.

Why are you using timers? Is other code going to be used? I would have just used " waitms 1000" and "waitms 10000"


I'm looking at your code... There is no interrupt routine so I'm confused.
 
Last edited:
hi

basically it is a reaction timer, so i am checking for the "timeout" condition or keypress which is why i cannot use waitms.

Code:
On Interrupt  'interrupt routine
Save System
msec = msec + 1
INTCON.T0IF = 0  'enable new TMR0 interrupts
Resume

and

Code:
chkkey:
If sw1 = 1 Then btnpress = 1
If sw2 = 1 Then btnpress = 2
If sw3 = 1 Then btnpress = 3
If sw4 = 1 Then btnpress = 4
If sw5 = 1 Then btnpress = 5
If sw6 = 1 Then btnpress = 6

If btnpress > 0 Then
	PORTB = 0  'disable the switched and rows
	INTCON.T0IE = 0  'stop game disable Timer0 interrupts
	Hserout "^C:", #btnpress  'send succ button to pc
	playidx = playidx + 1  'inc frame index
	speaker = 1
	WaitMs 150
	speaker = 0
	btnpress = 0
Goto playframes
Endif

'if playmode is 2 then we chk timeout else we wait for a key
If playmode = 2 Then
	If msec = ledon Then  '3 = 300ms
		INTCON.T0IE = 0  'stop game disable Timer0 interrupts
		Hserout "^T:0"  'no button
		'timeout- took too long
		playidx = playidx + 1  'inc frame index
		Goto playframes  'get next frame
	Endif
Endif
WaitUs 100

Goto chkkey

I am happy to use timer 1 if that is what i already have. I was not "given" code as such, mostly my own based on suggestions.

Although i do appreciate code snippets as it makes learning easier.
 
Right give me a mo. I'll work out for timer0 1 ms... I will assume 20mhz because you haven't told me...

option reg = 5
pre-load TMR0 with 178 that = 1ms (remember to pre-load again in the interrupt )
 
Last edited:
Hi sorry i am using 4mhz with crystal.

init section:
Code:
Define CONF_WORD = 0x3f32  'clock on HS
[COLOR="red"]OPTION_REG = 5[/COLOR]
[COLOR="red"]TMR0 = 178[/COLOR]   'is this correct ??

INTCON.T0IE = 0  'enable Timer0 interrupts
INTCON.GIE = True  'enable all un-masked interrupts
OPTION_REG.T0CS = False  'set Timer0 clock source to internal instruction cycle clock
AllDigital

interrupt section;
Code:
On Interrupt  'interrupt routine
[COLOR="red"]TMR0 = 178[/COLOR]   'is this correct ??
Save System
msec = msec + 1
INTCON.T0IF = 0  'enable new TMR0 interrupts
Resume

please recalc for 4mhz. how about a small bit of theory? just for some insight?

thanks for the help so far.
 
20mhz crystal runs at 5mhz (internally / 4)... The prescalar set to 64:1 78125 hz You need 1ms so 78125 / 1000 (1 ms) = 78.125 (rounded down 78)

So TMR0 counts to 256.. we need it to count to 78, so preload (256 - 78) = 178..

4mhz crystal runs at 1mhz If you set the prescalar at 8:1 we get 125000 If you divide by 1000 (1 ms) we get 125 so TMR0 = (256 -125) = 131

The prescalar should be chosen so the count of TMRO can be loaded with a figure between 0 - 256.. (timer0 can be set to 16 bit, but for your application, not really necessary )

Does this help..
 
Thanks that help.
question2: is the code I "changed" correct (Post #8)? I am at work and will only be able to test the code later. cant wait.
 
hi Ian,
With the simple 'tasks' the OP is doing in the ISR, the Save System IMO isnt required.
extract from OSH manual:
If arithmetic operations, arrays or any other complex statements are used in interrupt routine,
then SAVE SYSTEM statement should be placed right after ON INTERRUPT statement to save the content of registers used by system.

This action is taken on ISR use.
Code:
    W_TEMP EQU 0x7F
    STATUS_TEMP EQU 0x7E
    ORG 0x0000
    BCF PCLATH,3
    BCF PCLATH,4
    GOTO L0002

    ORG 0x0004
    MOVWF W_TEMP
    SWAPF STATUS,W
    CLRF STATUS
    MOVWF STATUS_TEMP
    CALL L0003
    SWAPF STATUS_TEMP,W
    MOVWF STATUS
    SWAPF W_TEMP,F
    SWAPF W_TEMP,W
    RETFIE

These are the Regs when Save System is used.
Code:
    W_TEMP EQU 0x7F
    STATUS_TEMP EQU 0x7E
    R0L_TEMP EQU 0x2C
    R0H_TEMP EQU 0x2D
    R1L_TEMP EQU 0x2E
    R1H_TEMP EQU 0x2F
    R2L_TEMP EQU 0x30
    R2H_TEMP EQU 0x31
    R3L_TEMP EQU 0x32
    R3H_TEMP EQU 0x33
    R4L_TEMP EQU 0x34
    R4H_TEMP EQU 0x35
    R5L_TEMP EQU 0x36
    R5H_TEMP EQU 0x37
    FSR_TEMP EQU 0x38
    ORG 0x0000
    BCF PCLATH,3
    BCF PCLATH,4
    GOTO L0002
    ORG 0x0004
    MOVWF W_TEMP
    SWAPF STATUS,W
    CLRF STATUS
    MOVWF STATUS_TEMP
    CALL L0003
    SWAPF STATUS_TEMP,W
    MOVWF STATUS
    SWAPF W_TEMP,F
    SWAPF W_TEMP,W
    RETFIE

This is fragment is also used, with Save System.
Code:
; 26: On Interrupt
L0003:
; 27: 
; 28: Save System
    MOVF R0L,W
    MOVWF R0L_TEMP
    MOVF R0H,W
    MOVWF R0H_TEMP
    MOVF R1L,W
    MOVWF R1L_TEMP
    MOVF R1H,W
    MOVWF R1H_TEMP
    MOVF R2L,W
    MOVWF R2L_TEMP
    MOVF R2H,W
    MOVWF R2H_TEMP
    MOVF R3L,W
    MOVWF R3L_TEMP
    MOVF R3H,W
    MOVWF R3H_TEMP
    MOVF R4L,W
    MOVWF R4L_TEMP
    MOVF R4H,W
    MOVWF R4H_TEMP
    MOVF R5L,W
    MOVWF R5L_TEMP
    MOVF R5H,W
    MOVWF R5H_TEMP
    MOVF FSR,W
    MOVWF FSR_TEMP
; 2
 
hi eric and ian
what about the msec = msec + 1 that runs in the interrupt routine? does that not qualify?
Thanks for the verification on the code ian.
 
hi eric and ian
what about the msec = msec + 1 that runs in the interrupt routine? does that not qualify?
Thanks for the verification on the code ian.
hi,
At the present time you are just incrementing a counter, so it dosn't apply, but as Ian suggests your ISR work may increase as the program is developed.
 
hi Ian,
For ref only, you can force the Basic to put your Dim's where you want, by this statement.
If you choose a system committed location, the compiler will warn you.

Code:
Dim image As Byte @ 0x50
Dim test As Byte @ 0x51
 
Eric.. not with you.. I know you can do this... Does this help with addition? msec in the code is a word adding a word or long requires the carry flag.

hi Ian,
You are not reading the small print.:)
hi Ian,
For ref only, you can force the Basic to put your Dim's where you want, by this statement.
If you choose a system committed location, the compiler will warn you.

Your post #11
As I don't know what registers save system messes with.
 
Last edited:
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top