View previous topic :: View next topic |
Author |
Message |
AIVA
Joined: 15 Nov 2008 Posts: 18
|
Posted: Wed Jan 11, 2017 2:52 pm Post subject: Variable problem with interupt |
|
|
Hello
I have a problem with the management of a variable
in the example below the variable K0 is increased by interrupt timer0 and when k0> P5 active a job.
Sometimes the active even if K0 does not exceed P5
After various test seems that while running the control k0> p5 and simultaneously increases K0 by interrupt is generated the problem
thanks,
bascom avr version 2.0.7.8
--------------------------------------------------
Code: | $regfile = "M32def.dat"
$crystal = 14745600
$hwstack = 34
$swstack = 34
$framesize = 34
Dim A0 As Word
Dim P4 As Word
Dim T0 As Word
Dim V1 As Single
Config Timer0 = Timer , Prescale = 1
On Timer0 Dec_timer
enable Timer0
Config Spi = Hard , Interrupt = Off , Data Order = Msb , Master = Yes , Polarity = High , Phase = 0 , Clockrate = 16
Spiinit
Enable Interrupts
P4 = 57
P5=100
K0=0
V1=10
Main:
do
If K0 > P5 Then
' make job
end if
loop
end
Dec_timer:
If T0 > 0 Then
Decr T0
Else
T0 = P4
Spiout V1 , 1 : Incr K0
End If
Return |
(BASCOM-AVR version : 2.0.7.8 ) |
|
Back to top |
|
|
MWS
Joined: 22 Aug 2009 Posts: 2262
|
Posted: Wed Jan 11, 2017 10:02 pm Post subject: |
|
|
This code won't even compile, as some variables weren't Dim'd.
Beside the code looks obfuscated and K0 is never reset. |
|
Back to top |
|
|
AIVA
Joined: 15 Nov 2008 Posts: 18
|
Posted: Wed Jan 11, 2017 10:28 pm Post subject: |
|
|
The code is just a small part of the original program
When K0> P5 then k0 value is reset and the program performs other functions
The question is how sometime k0> P5 when the K0 value reaches about 80% of the value of P5
Compile version
-------------------------
Code: | $regfile = "M32def.dat"
$crystal = 14745600
$hwstack = 34
$swstack = 34
$framesize = 34
Dim A0 As Word
Dim P4 As Word
Dim T0 As Word
Dim V1 As Single
Config Timer0 = Timer , Prescale = 1
On Timer0 Dec_timer
enable Timer0
Config Spi = Hard , Interrupt = Off , Data Order = Msb , Master = Yes , Polarity = High , Phase = 0 , Clockrate = 16
Spiinit
Enable Interrupts
P4 = 57
P5=100
K0=0
V1=10
Main:
do
If K0 > P5 Then
k0=0
' make job
end if
loop
end
Dec_timer:
If T0 > 0 Then
Decr T0
Else
T0 = P4
Spiout V1 , 1 : Incr K0
End If
Return |
|
|
Back to top |
|
|
kimmi
Joined: 24 Feb 2006 Posts: 1922 Location: Denmark
|
Posted: Wed Jan 11, 2017 10:49 pm Post subject: |
|
|
@AIVA
- please use the code tags when posting code.
missing Dim for K0 P5 V1 ect check your code "VAR" again _________________ / Kim |
|
Back to top |
|
|
AIVA
Joined: 15 Nov 2008 Posts: 18
|
Posted: Wed Jan 11, 2017 10:55 pm Post subject: |
|
|
Code: | $regfile = "M32def.dat"
$crystal = 14745600
$hwstack = 34
$swstack = 34
$framesize = 34
Dim A0 As Word
Dim P4 As Word
Dim K0 As Word
Dim P5 As Word
Dim T0 As Word
Dim V1 As Single
Config Timer0 = Timer , Prescale = 1
On Timer0 Dec_timer
Enable Timer0
Config Spi = Hard , Interrupt = Off , Data Order = Msb , Master = Yes , Polarity = High , Phase = 0 , Clockrate = 16
Spiinit
Enable Interrupts
P4 = 10000
P5 = 100
K0 = 0
V1 = 10
Main:
Do
If K0 > P5 Then
K0 = 0
' make job
End If
Loop
End
Dec_timer:
If T0 > 0 Then
Decr T0
Else
T0 = P4
Spiout V1 , 1 : Incr K0
End If
Return |
MOD:
@AIVA
- please use the code tags when posting code. !!! |
|
Back to top |
|
|
kimmi
Joined: 24 Feb 2006 Posts: 1922 Location: Denmark
|
Posted: Wed Jan 11, 2017 11:31 pm Post subject: |
|
|
Did you config timer0 right ?
you need time to do spiout !
Quote: | Config Timer0 = Timer , Prescale = 1
$crystal = 14745600
' Prescale = 1 overflow at 0.01736ms
'~~~~
' Prescale = 1024 overflow at 17.77777ms
|
_________________ / Kim |
|
Back to top |
|
|
JC
Joined: 15 Dec 2007 Posts: 585 Location: Cleveland, OH
|
Posted: Thu Jan 12, 2017 3:19 am Post subject: |
|
|
I think everything said above is good advice.
Additionally, I would try adding the "Save All" parameter to your On Interrupt instruction.
You have declared V1 as Single.
Whenever you use a Single (float) type within an Interrupt Subroutine you need to use Save All.
Whether just passing a single type variable to the SPI requires Save All or not I have not checked on real hardware, but it should be an easy test for you to run.
See the Help section for On Interrupt for information on the On Interrupt extra parameters.
Good Luck!
JC |
|
Back to top |
|
|
|