MB 5.0.0.3 beta 4 : Variable value is reseted

Discuss about beta versions of mikroBasic
compiler.
Post Reply
Author
Message
Kalain
Posts: 1093
Joined: 11 Mar 2005 18:26
Location: Aubenas, France

MB 5.0.0.3 beta 4 : Variable value is reseted

#1 Post by Kalain » 07 Sep 2007 21:09

Hi,

SEE ALSO POST BELOW FOR A SHORTER EXAMPLE

18F452, Xtal 8.00MHz, EasyPic3, GLCd 128*64

Problem : structure variable value (pg_mos.decal) is changed after instruction strcat(focaletxt,"mm ")

From code below follow carefully this :
Compile it
Start debugger
Add variables :
- i
- PortA
- pg_acc
- pg_mos

F8 key is pressed while debugging.
Here is below variables to change while debugging

While debugging note that line 62 set pg_mos.decal variable to 2
Line 72 change i value to 5 (to swap loop)
Line 82 change PortA value to 16 (to go to select case 16)
Line 98 change pg_acc.current to 3 (to go to select case 3)
Line 118 change i value to 3 (to swap loop)

Stop on line 127 : strcat(focaletxt,"mm ")
Have a look at pg_mos.decal in debugger window. (value = 2)

Now, if you simulate line 127, pg_mos.decal value is set to 0. :shock:

I have checked a dozen time, but found nothing.
Is that a bug ?

Code: Select all

program Tete_pano_Orion

symbol BP_Dr = porta.0
Symbol BP_Ga = porta.1
symbol BP_Ha = porta.2
symbol BP_Ba = porta.3

const pg_acc_nb as byte = 6
Const pg_acc_txt as string[pg_acc_nb][10] = ("Appareil ","Spherique","Pano 360 ","Mosaique ","Interval ","Goto     ")
const pg_mos_nb as byte = 4
Const pg_mos_txt as string[pg_mos_nb][10] = ("Orientat :","Focale   :","Vitesse  :","Rotation :")

structure Tmenu
  dim decal as byte
  dim  Last  as  byte
  dim  Current  as  byte
end structure

dim
  someText as string[21]
  text1 as string[10]
  focaletxt as string[7]
  pg_acc, pg_mos as TMenu
  i, portrait as  byte
  focale as word

sub procedure test_portrait
  if portrait = 0 then text1 = "Paysage " else text1 = "Portrait" end if
end sub

sub procedure affich_focaletxt(dim normal as byte)
  Glcd_Write_Text(focaletxt,64,pg_mos.current + pg_mos.decal,normal)
end sub
  
sub Procedure affich_page(dim byref nom_page as string[21] )
  Glcd_Fill(0x00)
  Glcd_Set_Font(@FontSystem5x8, 5, 8, 32)
  Glcd_Write_Text(nom_page, 0, 0, 1)
  Glcd_H_Line(0, 127, 8, 1)
end sub

sub procedure delay_200ms
  delay_ms(200)
end sub

main:
  TRISA = $FF
  TRISB = 0
  TRISC = 0
  TRISD = 0
  ADCON1 = 7
  ADCON0 = 0
  CCP1CON = 0
  PORTA = 0

  Glcd_Init(PORTB, 2, 3, 4, 5, 7, 6, PORTD)

menu:

pg_acc.current = 0
pg_acc.decal = 2
pg_mos.decal = 2   'LINE 62
portrait = 0
focale = 300

pg_accueil:
'Affichage page accueil
affich_page("Tete motorisee   1.00")

i= 0
do
  Text1 = pg_acc_txt[i]    'LINE 72
  Glcd_Write_Text(Text1,0,pg_acc.decal+i,1)
  inc(i)
loop until i = pg_acc_nb

'asm nop end asm
Text1 = pg_acc_txt[pg_acc.current]
Glcd_Write_Text(text1,0,pg_acc.current+pg_acc.decal,0)

pg_acc:
  do nop loop until PortA <> 0      'LINE 82

  pg_acc.last = pg_acc.current
  select case portA
  case 4 'haut
    dec(pg_acc.current)
    if pg_acc.current = 255 then pg_acc.current = pg_acc_nb - 1 end if
    
  case 8 'bas
    inc(pg_acc.current)
    if pg_acc.current = pg_acc_nb then pg_acc.current = 0 end if

  case 16 'entree (bp haut, bas, droit, gauche enfoncés)
    select case pg_acc.current
    case 1 'page appareil
    case 2 'page pano 360
    case 3 'mosaique        'LINE 98
      goto pg_mosaique
    end select

  end select

  Text1 = pg_acc_txt[pg_acc.last]
  Glcd_Write_Text(Text1,0,pg_acc.last+pg_acc.decal,1)

  Text1 = pg_acc_txt[pg_acc.current]
  Glcd_Write_Text(Text1,0,pg_acc.current+pg_acc.decal,0)
  delay_200ms  'delay_ms(200)
  do loop until porta = 0
  
goto pg_acc

pg_mosaique:
  affich_page("Mosaique")
  i = 0
  do
    Text1 = pg_mos_txt[i]    'LINE 118
    Glcd_Write_Text(text1,0,pg_mos.decal+i,1)
    inc(i)
  loop until i = pg_mos_nb

  test_portrait
  Glcd_Write_Text(text1,64,pg_mos.decal,0)
  pg_mos.current = 0
  wordtostr(focale,focaletxt)
  strcat(focaletxt,"mm ")      'LINE 127
  Glcd_Write_Text(focaletxt,64,pg_mos.decal+1,1)
  'Glcd_Write_Text(focaletxt,64,3,1)

pg_mos:
  select case pg_mos.current
  case 0 'orientation
    do loop until PortA <> 0

    select case portA
    case 1, 2 'portrait, paysage
      portrait = not(portrait)
      test_portrait
      Glcd_Write_Text(text1,64,pg_mos.decal,0)

    case 4 'haut
      pg_mos.current = pg_mos_nb+1
      Glcd_Write_Text(text1,64,pg_mos.decal,1)
      Glcd_Write_Text("  ",64,pg_mos.current,0)

    case 8 'bas
      test_portrait
      Glcd_Write_Text(text1,64,pg_mos.decal,1)
      inc(pg_mos.current)
      affich_focaletxt(0)

    case 16 'validation, retour page precedente
      pg_acc.current = 3
      goto pg_accueil
    end select

    delay_200ms  'delay_ms(200)
    do loop until porta = 0

  case 1 'focale
    do loop until PortA <> 0

    select case portA
    case 1, 2'plus focale
      if porta = 1 then focale = focale + 2 else focale = focale - 2 end if

      WordToStr(focale,focaletxt)
      affich_focaletxt(0)
      strcat(focaletxt,"mm ")
      delay_ms(75)

    case 4 'haut
      affich_focaletxt(1)
      dec(pg_mos.current)
      test_portrait
      Glcd_Write_Text(text1,64,2,0)
      delay_200ms  'delay_ms(200)
      do loop until porta = 0

    case 8 'bas
      affich_focaletxt(1)
      inc(pg_mos.current)
      Glcd_Write_Text("  ",64,pg_mos.current + 2,0)
      delay_200ms  'delay_ms(200)
      do loop until porta = 0

    case 16 'validation, retour page precedente
      pg_acc.current = 3
      goto pg_accueil
    end select


  end select
goto pg_mos

end.
Last edited by Kalain on 08 Sep 2007 16:06, edited 3 times in total.
Alain

Kalain
Posts: 1093
Joined: 11 Mar 2005 18:26
Location: Aubenas, France

#2 Post by Kalain » 07 Sep 2007 23:40

Hi,

I think, I have another and similar example where variable are changing without any direct manipulation on it. :cry:
Alain

Kalain
Posts: 1093
Joined: 11 Mar 2005 18:26
Location: Aubenas, France

#3 Post by Kalain » 08 Sep 2007 12:16

Hi,

I investigated further (and simplified program) in order to track the problem.
Try to simulate (18F452 @8Mhz) code below :
'Rotation' is set at 255 in the program but just after "strcat(tempotxt,"ms ")" instruction in procedure, 'rotation' is set at "0" in debugger window :shock:

Code: Select all

Program test
const pg_mos_decal as byte = 2

dim
  tempotxt as string[7]
  rotation as  byte
  tempo as word

sub procedure affich_tempo(dim normal as byte)
  wordtostr(tempo,tempotxt)
  strcat(tempotxt,"ms ")
  Glcd_Write_Text(tempotxt,64,pg_mos_decal+2,normal)
end sub

main:
TRISA = $FF
TRISB = 0
TRISC = 0
TRISD = 0
ADCON1 = 7
ADCON0 = 0
CCP1CON = 0
PORTA = 0

Glcd_Init(PORTB, 2, 3, 4, 5, 7, 6, PORTD)

menu:
rotation = 255
tempo = 10
affich_tempo(1)

goto menu

end.
Alain

jpc
Posts: 1986
Joined: 22 Apr 2005 17:40
Location: France 87

#4 Post by jpc » 10 Sep 2007 10:21

the problem is that tempotxt cannot hold the resulting string of strcat . Wordtostr returns string[5] to which you add "ms " which is a string[8]

User avatar
milan
mikroElektronika team
Posts: 1013
Joined: 04 May 2006 16:36
Contact:

#5 Post by milan » 10 Sep 2007 11:26

Hi,

I was examining your last code.

Help -> Strcat
Function appends the value of string s2 to string s1 and terminates s1 with a null character.
Null character is used for string termination in mikroBasic.

After wordtostr(tempo,tempotxt) line tempotxt variable has the following content:
space, space, space, "1", "0", zero, zero, zero

after strcat(tempotxt,"ms ")
"m", "s", space and zero are appended to tempotxt end (first zero).

So now you got:

space, space, space, "1", "0", "m", "s", space and ZERO !

The last zero is put in the space that is not reserved for tempotxt as string[7] so it overwrites some other variable.

Make sure that your string is long enough to accept appended data.

User avatar
milan
mikroElektronika team
Posts: 1013
Joined: 04 May 2006 16:36
Contact:

#6 Post by milan » 10 Sep 2007 12:03

jpc wrote:the problem is that tempotxt cannot hold the resulting string of strcat . Wordtostr returns string[5] to which you add "ms " which is a string[8]
That's right (I haven't seen your answer), thanks jpc.

Post Reply

Return to “mikroBasic Beta testing”