Code error

Pytania, kody i porady dotyczące nie tylko Bascom.
ODPOWIEDZ
weako
Posty: 11
Rejestracja: 22 mar 2020, 14:37

Code error

Post autor: weako » 25 mar 2020, 5:58

Hi Everyone,
I try to modified below code, but i am getting an error, that below code not finish compile.

Could someone here help me to solved what the problem, i am new in basic compiler.
  1. $Regfile="m8def.dat"
  2. $Crystal=8000000
  3. $hwstack=40
  4. $swstack=40
  5. $framesize=40
  6.  
  7. Rem Configuration of LCD display
  8. Config Lcd = 20 * 2                                         '2 rows x 20 chars
  9. Config Lcdpin = Pin , Db4 = Portb.0 , Db5 = Portd.7 , Db6 = Portd.6 , Db7 = Portd.5 , Rs = Portb.2 , E = Portb.1
  10. Cursor Off
  11. Cls
  12.  
  13. Rem Configuration of ADC
  14. Config Adc = Single , Prescaler = Auto
  15.  
  16. Rem Declarations of variables
  17. Dim Uref As Word
  18. Dim Uf As Long
  19. Dim Ur As Long
  20. Dim Adjust As Word
  21. Dim Swr1 As Long
  22. Dim Swr2 As Long
  23. Dim Swr As Single
  24. Dim Swr$ As String * 3
  25. Dim X As Single
  26. Dim Pwr As Single
  27. Dim Scale(3) As Byte
  28. Dim X_scale As Byte
  29. Dim Scale_$(3) As String * 4
  30. Dim E_scale As Eram Byte
  31. Dim Multiply As Byte
  32.  
  33. Const Row = "                    "
  34.  
  35. Rem Config and setting ports
  36. Config Pinc.6 = Input
  37. Config Pinc.5 = Input
  38. Config Pind.0 = Output
  39. Config Pinc.0 = Input
  40. Config Pinc.1 = Input
  41. Config Pinc.2 = Input
  42.  
  43.  
  44. Set Portc.6
  45. Set Portc.5
  46. Set Portc.4
  47. Set Portd.0
  48.  
  49. Rem show scale
  50. X_scale = E_scale
  51. GoSub Show_scale
  52.  
  53. Rem MAIN LOOP
  54.  
  55. Do
  56. GoSub Read_adc
  57. GoSub Calculate_pwr
  58. GoSub Show_pwr
  59. GoSub Calculate_swr
  60. GoSub Show_swr
  61. Waitms 100
  62. Debounce Pinc.5 , 0 , Scale_sub , Sub
  63. Loop
  64.  
  65.  
  66. Rem ===============================
  67. Rem ====   SUBS   =================
  68. Rem ===============================
  69.  
  70. Rem Read ADCs
  71.  
  72. Read_adc:
  73.  
  74. Start Adc
  75. Uref = Getadc(1)
  76. Uf = Getadc(0)
  77. Ur = Getadc(2)
  78. Adjust = Getadc(3)                                          'Calculate variable "Adjust"
  79. Adjust = Adjust * 10
  80. Adjust = Adjust + 54000
  81. Stop Adc
  82. Return
  83.  
  84. Rem ===Calculate SWR====
  85.  
  86. Calculate_swr:
  87.  
  88. Swr1 = Ufi + Ur
  89. Swr2 = Ufi - Ur
  90. Swr = Swr1 / Swr2
  91. Swr$ = Str(Swr)
  92. Swr$ = Left(Swr$, 3)
  93.  
  94. Swr = Log10(Swr)                                            'calculate SWR bargraph
  95. Swr = 20 * Swr
  96. Swr = Round(Swr)
  97. If Swr > 15 Then
  98. Swr = 15
  99. End If
  100. Return
  101.  
  102. Rem ===Show SWR=========
  103.  
  104. Show_swr:
  105.  
  106. Locate 2, 1
  107. Lcd Row
  108. Locate 2, 1
  109.  
  110. For X = 1 To Swr                                            'show SWR bargraph
  111. Lcd "="
  112. Next
  113. Lcd ">" ; Swr$
  114. Return
  115.  
  116. Rem ===Calculate PWR===============
  117.  
  118. Calculate_pwr:
  119.  
  120. Uf = Uf - Uref
  121. Ur = Ur - Uref
  122. Pwr = Uf ^ 2
  123. Pwr = Pwr / Adjust
  124. If Pwr < 0.05 Then
  125. End If
  126. Pwr$ = Str(Pwr)
  127. Pwr$ = Left(Pwr$, 3)
  128.  
  129. Pwr = Multiply * Pwr                                        'calculate PWR bargraph
  130. Pwr = Round(Pwr)
  131. Return
  132.  
  133.  
  134. Rem ===Show PWR============
  135.  
  136. Show_pwr:
  137.  
  138. Locate 1, 1
  139. Lcd Row
  140. Locate 1, 1
  141.  
  142.  
  143. Lcd Pwr$
  144. Return
  145.  
  146. Rem ===Switch scale of PWR bargraph ========
  147.  
  148. Scale_sub:
  149. X_scale = E_scale
  150.  
  151. Incr X_scale
  152.   If X_scale > 3 Then
  153.   X_scale = 1
  154.   End If
  155.  
  156. GoSub Show_scale
  157.  
  158. Return
  159.  
  160. Rem ===Show PWR bargraph scale ============
  161.  
  162. Show_scale:
  163.  
  164. Scale_$(1) = "500 W"
  165. Scale_$(2) = "250 W"
  166. Scale_$(3) = "50 W"
  167. Scale(1) = 1
  168. Scale(2) = 4
  169. Scale(3) = 10
  170. Multiply = Scale(x_scale)
  171. E_scale = X_scale                                           'save PWR bargraph scale into eeprom
  172. Cls
  173. Locate 1, 4
  174. Lcd "BARTEK: " ; Scale_$(x_scale)                           'range of bargraf
  175. Wait 3
  176. Return
  177. End

Thank you.
Wea.
Awatar użytkownika
niveasoft
Posty: 1216
Rejestracja: 17 sie 2015, 12:13
Kontakt:

Re: Code error

Post autor: niveasoft » 25 mar 2020, 6:16

Now NO ERRORS and compile but for the next time please attach errors from IDE.

In your code variable Ufi was not declared, string Scale_$ was too short (3 chars) Now I change it into 10.
Please do not declare variables like that "Var$" because $ sign is a special character so Var_$ is okay where Var$ is not.
I change some variable names. For example from Pwr$ into Pwr_s.
  1.     $Regfile="m8def.dat"
  2.     $Crystal=8000000
  3.     $hwstack=40
  4.     $swstack=40
  5.     $framesize=40
  6.  
  7.     Rem Configuration of LCD display
  8.     Config Lcd = 20x2                                       '2 rows x 20 chars
  9.     Config Lcdpin = Pin , Db4 = Portb.0 , Db5 = Portd.7 , Db6 = Portd.6 , Db7 = Portd.5 , Rs = Portb.2 , E = Portb.1
  10.     Cursor Off
  11.     Cls
  12.  
  13.     Rem Configuration of ADC
  14.     Config Adc = Single , Prescaler = Auto
  15.  
  16.     Rem Declarations of variables
  17.     Dim Uref As Word
  18.     Dim Uf As Long
  19.     Dim Ufi As Long
  20.     Dim Ur As Long
  21.     Dim Adjust As Word
  22.     Dim Swr1 As Long
  23.     Dim Swr2 As Long
  24.     Dim Swr As Single
  25.     Dim Swr_s As String * 3
  26.     Dim X As Single
  27.     Dim Pwr As Single
  28.     Dim Pwr_s As String * 10
  29.     Dim Scale(3) As Byte
  30.     Dim X_scale As Byte
  31.     Dim Scale_$(3) As String * 10
  32.     Dim E_scale As Eram Byte
  33.     Dim Multiply As Byte
  34.  
  35.     Const Row = "                    "
  36.  
  37.  
  38. Rem Config and setting ports
  39.     Config Pinc.6 = Input
  40.     Config Pinc.5 = Input
  41.     Config Pind.0 = Output
  42.     Config Pinc.0 = Input
  43.     Config Pinc.1 = Input
  44.     Config Pinc.2 = Input
  45.  
  46.  
  47.     Set Portc.6
  48.     Set Portc.5
  49.     Set Portc.4
  50.     Set Portd.0
  51.  
  52.     Rem show scale
  53.     X_scale = E_scale
  54.     GoSub Show_scale
  55.  
  56.     Rem MAIN LOOP
  57.  
  58.     Do
  59.     GoSub Read_adc
  60.     GoSub Calculate_pwr
  61.     GoSub Show_pwr
  62.     GoSub Calculate_swr
  63.     GoSub Show_swr
  64.     Waitms 100
  65.     Debounce Pinc.5 , 0 , Scale_sub , Sub
  66.     Loop
  67.  
  68.  
  69.     Rem ===============================
  70.     Rem ====   SUBS   =================
  71.     Rem ===============================
  72.  
  73.     Rem Read ADCs
  74.  
  75.     Read_adc:
  76.  
  77.     Start Adc
  78.     Uref = Getadc(1)
  79.     Uf = Getadc(0)
  80.     Ur = Getadc(2)
  81.     Adjust = Getadc(3)                                          'Calculate variable "Adjust"
  82.     Adjust = Adjust * 10
  83.     Adjust = Adjust + 54000
  84.     Stop Adc
  85.     Return
  86.  
  87. Rem ===Calculate SWRUndeclared identifier Ufi. In File: G:\KOPIE\KOPIA_2019_05_01\Desktop\test_new\waeko.bas; at Line: 89, Position 12====
  88.  
  89.     Calculate_swr:
  90.  
  91.     Swr1 = Ufi + Ur
  92.     Swr2 = Ufi - Ur
  93.     Swr = Swr1 / Swr2
  94.     Swr_s = Str(swr)
  95.     Swr_s = Left(Swr_s, 3)
  96.  
  97.     Swr = Log10(Swr)                                            'calculate SWR bargraph
  98.     Swr = 20 * Swr
  99.     Swr = Round(Swr)
  100.     If Swr > 15 Then
  101.     Swr = 15
  102.     End If
  103.     Return
  104.  
  105.     Rem ===Show SWR=========
  106.  
  107.     Show_swr:
  108.  
  109.     Locate 2, 1
  110.     Lcd Row
  111.     Locate 2, 1
  112.  
  113.     For X = 1 To Swr                                            'show SWR bargraph
  114.     Lcd "="
  115.     Next
  116.     Lcd ">" ; Swr_s
  117.     Return
  118.  
  119.     Rem ===Calculate PWR===============
  120.  
  121.     Calculate_pwr:
  122.  
  123.     Uf = Uf - Uref
  124.     Ur = Ur - Uref
  125.     Pwr = Uf ^ 2
  126.     Pwr = Pwr / Adjust
  127.     If Pwr < 0.05 Then
  128.     End If
  129.     Pwr_s = Str(Pwr)
  130.     Pwr_s = Left(Pwr_s, 3)
  131.  
  132.     Pwr = Multiply * Pwr                                        'calculate PWR bargraph
  133.     Pwr = Round(Pwr)
  134.     Return
  135.  
  136.  
  137.     Rem ===Show PWR============
  138.  
  139.     Show_pwr:
  140.  
  141.     Locate 1, 1
  142.     Lcd Row
  143.     Locate 1, 1
  144.     Lcd Pwr_s
  145.     Return
  146.  
  147.     Rem ===Switch scale of PWR bargraph ========
  148.  
  149.     Scale_sub:
  150.     X_scale = E_scale
  151.  
  152.     Incr X_scale
  153.       If X_scale > 3 Then
  154.       X_scale = 1
  155.       End If
  156.  
  157.     Gosub Show_scale
  158.  
  159.     Return
  160.  
  161.     Rem ===Show PWR bargraph scale ============
  162.  
  163.     Show_scale:
  164.  
  165.     Scale_$(1) = "500 W"
  166.     Scale_$(2) = "250 W"
  167.     Scale_$(3) = "50 W"
  168.     Scale(1) = 1
  169.     Scale(2) = 4
  170.     Scale(3) = 10
  171.     Multiply = Scale(x_scale)
  172.     E_scale = X_scale                                           'save PWR bargraph scale into eeprom
  173.     Cls
  174.     Locate 1, 4
  175.     Lcd "BARTEK: " ; Scale_$(x_scale)                           'range of bargraf
  176.     Wait 3
  177.     Return
  178.     End
weako
Posty: 11
Rejestracja: 22 mar 2020, 14:37

Re: Code error

Post autor: weako » 25 mar 2020, 8:37

Hi Niveasoft,
Thank you for changes the variable and fix the problem.

After i tested that code, look like not working correctly, i trying this code hex on proteus software, that appears only LCD text BARTEK.

Hope someone here can help more to see what is a problem, please find attached proteus file.

Thank you.
Wea.
Nie masz wymaganych uprawnień, aby zobaczyć pliki załączone do tego posta.
Awatar użytkownika
Pikczu
Posty: 390
Rejestracja: 17 sie 2015, 13:46
Lokalizacja: Dublin, Ireland
Kontakt:

Re: Code error

Post autor: Pikczu » 25 mar 2020, 15:01

Hi weako
What is the version of the Proteus you are using?
weako
Posty: 11
Rejestracja: 22 mar 2020, 14:37

Re: Code error

Post autor: weako » 25 mar 2020, 15:20

Hi pkc,
My proteus i use is version 8.9 for SP2.

Thanks.
Wea.
ODPOWIEDZ