Forum - MCS Electronics

 

FAQFAQ SearchSearch RegisterRegister Log inLog in

Variable problem with interupt

 
Post new topic   Reply to topic    www.mcselec.com Forum Index -> BASCOM-AVR
View previous topic :: View next topic  
Author Message
AIVA

Bascom Member



Joined: 15 Nov 2008
Posts: 18

italy.gif
PostPosted: Wed Jan 11, 2017 2:52 pm    Post subject: Variable problem with interupt Reply with quote

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
View user's profile
MWS

Bascom Member



Joined: 22 Aug 2009
Posts: 2262

blank.gif
PostPosted: Wed Jan 11, 2017 10:02 pm    Post subject: Reply with quote

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
View user's profile
AIVA

Bascom Member



Joined: 15 Nov 2008
Posts: 18

italy.gif
PostPosted: Wed Jan 11, 2017 10:28 pm    Post subject: Reply with quote

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
View user's profile
kimmi

Moderator



Joined: 24 Feb 2006
Posts: 1922
Location: Denmark

denmark.gif
PostPosted: Wed Jan 11, 2017 10:49 pm    Post subject: Reply with quote

@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
View user's profile Visit poster's website MSN Messenger
AIVA

Bascom Member



Joined: 15 Nov 2008
Posts: 18

italy.gif
PostPosted: Wed Jan 11, 2017 10:55 pm    Post subject: Reply with quote

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
View user's profile
kimmi

Moderator



Joined: 24 Feb 2006
Posts: 1922
Location: Denmark

denmark.gif
PostPosted: Wed Jan 11, 2017 11:31 pm    Post subject: Reply with quote

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
View user's profile Visit poster's website MSN Messenger
JC

Bascom Member



Joined: 15 Dec 2007
Posts: 586
Location: Cleveland, OH

usa.gif
PostPosted: Thu Jan 12, 2017 3:19 am    Post subject: Reply with quote

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
View user's profile Visit poster's website
Display posts from previous:   
Post new topic   Reply to topic    www.mcselec.com Forum Index -> BASCOM-AVR All times are GMT + 1 Hour
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You cannot download files in this forum