BasicMicroUK - Forums

www.basicmicro.co.uk
It is currently Tue Jul 17, 2018 6:39 pm

All times are UTC [ DST ]




Post new topic Reply to topic  [ 4 posts ] 
Author Message
 Post subject: byte array variable glitch
PostPosted: Mon Feb 10, 2014 5:11 am 
Offline
Citizen

Joined: Wed Feb 01, 2012 5:57 am
Posts: 11
I have been working on this for a bit and ran into a problem using the array variable pulcnt(4) in byte size variables. I had them identified in sequence 1 to 4 but when I tried to use the second and 4th it wouldn't work. by allocating 8 variables and using every other one the program works fine. I don't get it.

This program is pretty crude but it gets the job done. it is for two warning lamp controllers with sound.

Code:
T1CON = 0x01
TMR1IE = 1
PEIE = 1
GIE = 1

overflow con MHZ/4000000
counterfast var sword
counterfast = 0
counterslow var byte
counterslow = 0

tenthsec var bit
tenthsec = 0
onesec var bit
onesec = 0

plow VAR bit(2)
phigh VAR bit(2)
plow(1) = 0
phigh(1) = 0
plow(2) = 0
phigh(2) = 0

pon var bit(2)

ESTOP VAR bit(2)
ESTOP(1) = 0
ESTOP(2) = 0

trig var bit(4)
trig(1) = 0
trig(2) = 0
trig(3) = 0
trig(4) = 0

pulcnt var byte(8)
pulcnt(1) = 0
pulcnt(5) = 0
pulcnt(3) = 0
pulcnt(7) = 0

green1 con p9
amber1 con p8
red1 con p13
buzz1 con p12

green2 con p7
amber2 con p6
red2 con p5
buzz2 con p4

LOW GREEN1
LOW AMBER1
LOW RED1
LOW BUZZ1

LOW GREEN2
LOW AMBER2
LOW RED2
LOW BUZZ2

PAUSE 500

main:

if counterfast>=overflow then
   counterfast=counterfast-overflow
   tenthsec=tenthsec+1
   counterslow=counterslow+1
 endif
   
if counterslow>10 then
   onesec = onesec+1
   counterslow = 0
endif

ESTOP(1) = (~IN11)
ESTOP(2) = (~IN10)

phigh(1) = (~IN3)
 plow(1) = (~IN2)
phigh(2) = (~IN1)
 plow(2) = (~IN0)

Pon(1) = plow(1) or phigh(1)
Pon(2) = plow(2) or phigh(2)

if (plow(1)=0) and (phigh(1)=0) then
 low buzz1
endif

if (plow(2)=0) and (phigh(2)=0) then
 low buzz2
endif

gosub bottom
gosub middle
gosub top
goto main

bottom:

if pon(1)=1 or (estop(1)=0 and onesec=1 and pon(1)=0)  then
low green1
else
high green1
endif

bottom2:

if pon(2)=1 or (estop(1)=0 and onesec=1 and pon(2)=0)  then
low green2
else
high green2
endif

return

middle:

if (plow(1)=1) and (tenthsec=1) and (phigh(1)=0) then
high amber1
 if pulcnt(1)<3 then
 high buzz1
 endif
Trig(1)=0
endif

if (plow(1)=1 and pulcnt(1)=3) then
low buzz1
endif

if (plow(1)=1) and (tenthsec=0) and (trig(1)=0) and (pulcnt(1)<3) then
low amber1
low buzz1
pulcnt(1)=pulcnt(1)+1
trig(1)=1
endif

if plow(1)=0 or phigh(1)=1 then
pulcnt(1)=0
low amber1
endif

middle2:

if (plow(2)=1) and (tenthsec=1) and (phigh(2)=0) then
high amber2
Trig(2)=0
 if pulcnt(5)<3 then
 high buzz2
 endif
endif

if plow(2)=1 and pulcnt(5)=3 then
low buzz2
endif

if (plow(2)=1) and (tenthsec=0) and (trig(2)=0) and (pulcnt(5)<3) then
low amber2
low buzz2
pulcnt(5)=pulcnt(5)+1
trig(2)=1
endif

if plow(2)=0 or phigh(2)=1 then
pulcnt(5)=0
low amber2
endif

return

top:

if (phigh(1)=1 and tenthsec=1 and pulcnt(3)<5) or (phigh(1)=1 and onesec=1 and pulcnt(3)=5) then
high red1
trig(3)=0
endif

if pulcnt(3)<5 and phigh(1)=1 and tenthsec=1 then
high buzz1
endif

if phigh(1)=1 and pulcnt(3)=5 then
low buzz1
endif

if phigh(1)=1 and tenthsec=0 and trig(3)=0 and pulcnt(3)<5 then
low red1
low buzz1
pulcnt(3) =  pulcnt(3)+1
trig(3)=1
endif

if phigh(1)=1 and onesec=0 and trig(3)=0 and pulcnt(3)=5 then
low red1
low buzz1
trig(3)=1
endif

if phigh(1)=0 then
pulcnt(3)=0
low red1
endif

top2:

if (phigh(2)=1 and tenthsec=1 and pulcnt(7)<5) or (phigh(2)=1 and onesec=1 and pulcnt(7)=5) then
high red2
trig(4)=0
endif

if pulcnt(7)<5 and phigh(2)=1 and tenthsec=1 then
high buzz2
endif

if phigh(2)=1 and pulcnt(7)=5 then
low buzz2
endif

if phigh(2)=1 and tenthsec=0 and trig(4)=0 and pulcnt(7)<5 then
low red2
low buzz2
pulcnt(7) =  pulcnt(7)+1
trig(4)=1
endif

if phigh(2)=1 and onesec=0 and trig(4)=0 and pulcnt(7)=5 then
low red2
low buzz2
trig(4)=1
endif

if phigh(2)=0 then
pulcnt(7)=0
low red2
endif

return

goto main

israsm{
   bcf      PIR1,TMR1IF
   banksel COUNTERfast&0x1FF
   incf   COUNTERfast&0x7F,F
   skpnz
   incf   (COUNTERfast+1)&0x7F,F
   banksel   0
}


Top
 Profile  
 
 Post subject: Re: byte array variable glitch
PostPosted: Tue Feb 11, 2014 7:08 am 
Offline
Master

Joined: Wed Oct 19, 2011 2:28 am
Posts: 123
Hi,
I wrote a program to verify your problem and I was able to write/read in each array variables with no problem. Can you run the program to see if you have the same problem. The program will add the loop + the value of 200 write it in each array location and then read it / display it.
data display:
read_data = 200
read_data = 201
read_data = 202
read_data = 203

'ran the programon in a nano28
'define variables
loop var byte
save_data var byte(4)
read_data var byte

main
'Zero array variables
for loop = 0 to 3
save_data(loop) = 0
next
' Store in each array variable the sum of the loop number plus 200
for loop = 0 to 3
save_data(loop) = save_data(loop) +loop + 200
next
' read the stored array variable and display them.
for loop = 0 to 3
read_data = save_data(loop)
serout s_out , i9600,["read_data = ",dec read_data,13]
next
serout s_out , i9600,[ 13]
pause 1000
goto main


Top
 Profile  
 
 Post subject: Re: byte array variable glitch
PostPosted: Sun Feb 16, 2014 12:18 am 
Offline
Citizen

Joined: Wed Feb 01, 2012 5:57 am
Posts: 11
Well after a little more experimentation I found that the 4 byte variables in the array numbers 1-4 instead of 0-3. Should know better than that but it compiled fine, just caused lots of problems running. It is odd though the Bit array variables had the same error but they work fine probably because I am only using 1 and 2. Maybe it sees #2 as 0 would normally be.


Top
 Profile  
 
 Post subject: Re: byte array variable glitch
PostPosted: Wed Mar 05, 2014 10:32 pm 
Offline
Site Admin
User avatar

Joined: Thu Mar 01, 2001 7:00 pm
Posts: 1316
Location: Temecula, CA
An indexing error could be more devastating in a byte(or larger sized) array. In a bit array there may be some extra bits of padding because a multiple of 8 bits was not defined leaving the extra bits unused.

_________________
Tech Support
Basic Micro - Robotic Technology Evolved


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 4 posts ] 

All times are UTC [ DST ]


Who is online

Users browsing this forum: No registered users and 0 guests


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 post attachments in this forum

Jump to:  
cron
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group

phpBB SEO