Advertisement
kiler129

ZZAAG v3 mod

Jun 9th, 2012
1,696
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ' zzaag UC01 firmware rev.A2
  2.  
  3. $regfile = "m32def.dat" ' Chip definition - atmega32
  4. $crystal = 16000000 ' Crystal oscilator (16Mhz)
  5. $baud = 19200 ' UART speed
  6.  
  7. Pwm_al Alias Ocr1al ' Right motor PWM output
  8. Pwm_bl Alias Ocr1bl ' Left motor PWM output
  9. Vreg Alias Portd.2 ' Enable H-driver circuit
  10. Buzz Alias Portc.0 ' BuZzEr out
  11. Cw_ccw_a Alias Portd.7 ' Direction control for right motor??
  12. Cw_ccw_b Alias Portd.6 ' Direction control for left motor??
  13.  
  14. Config Porta = Input ' 0 - Acc Y, 1 - Acc X, 2 - Gyro X, 3 - Gyro Y, 6 - divided bat voltage
  15. Config Portb = Input ' programming interface
  16. Config Portc = Output ' some undefined GPIO to driver
  17. Config Portd = Output ' PWM and other signals for driver, first to pins handle UART RxD/TxD
  18. Config Adc = Single , Prescaler = 128 , Reference = Off ' Prepare ADC, probably for stering potentiometr
  19. Start Adc ' Run ADC
  20. Config Timer0 = Timer , Prescale = 1024 ' Config timer0 to be a cpu-time meter, it`s increase by 1 every 1024 cpu cycles
  21. On Timer0 Tinter ' Run every 1024 cpu cycles
  22. Portb = 3 ' Pull-up inputs
  23.  
  24. Dim Buzzer As Byte ' Buzzer status, will be assigned to real buzzer output (Buzz) in Tinter method
  25. Dim Ad_adxl As Long ' ??
  26. Dim Ad_gyro As Long ' ??
  27. Dim Ad_batt As Integer ' ADC for battery?
  28. Dim Ad_swi As Integer ' ??
  29. Dim Total_adxl_gyro As Long ' ??
  30. Dim Average_gyro As Long ' ??
  31. Dim Average_batt As Long ' ??
  32. Dim Drivea As Integer ' PWM for right motor
  33. Dim Driveb As Integer ' PWM for left motor
  34. Dim Buf1 As Long ' Temp?
  35. Dim Buf As Long ' Temp?
  36. Dim Buf2 As Long ' Temp?
  37. Dim Buf5 As Integer ' Temp?
  38. Dim Tilt_angle As Long ' Actual tilt angle of platform
  39. Dim Drive_a As Integer ' ??
  40. Dim Drive_b As Integer ' ??
  41. Dim Drive_as As Integer ' ?? - never contain value diff from 0
  42. Dim Drive_bs As Integer ' ?? - never contain value diff from 0
  43. Dim Tcount As Integer ' Counter used to limit logs limit
  44. Dim Drivespeed As Integer ' Your speed?
  45.  
  46. Dim Steeringsignal As Long ' ??
  47. Dim Anglecorrection As Long ' Correction gyro using acc?
  48. Dim Angle_rate As Long ' ??
  49. Dim Balance_moment As Long ' ??
  50. Dim Overspeed As Long ' ?? - never contain value diff from 0
  51.  
  52. Dim Mmode As Byte ' Operation mode, can be 1 (const _run), 0 (const _standby) or 2 (const _warmup)
  53. Dim Overspeed_flag As Byte ' ?? -  never contain value diff from 0 (maybe its a part of unifinished warning system if you driving too fast?)
  54. Dim Drive_sum As Long ' ??
  55. Dim Ad_rocker As Integer ' ??
  56.  
  57. Dim Timeout As Long ' I think this is used in timer of two seconds before stoping engines when someone falloff the segway
  58. Dim Delta As Byte ' ??
  59. Dim Buf3 As Long ' Temp?
  60. Dim Rockersq As Long ' Something with steering?
  61.  
  62. Dim Rocker_zero As Integer ' Contain "zero" position of steering pipe?
  63. Dim Adxl_zero As Word ' Accelerometr zero point?
  64. Dim Gyro_zero As Word ' Gyro zero poin?
  65. Dim Loopct As Integer ' ??
  66.  
  67. Const First_adc_input = 0 ' ?? - never used in code
  68. Const Last_adc_input = 6 ' ?? - never used in code
  69. Const Adc_vref_type = $0x40 ' ?? - never used in code
  70.  
  71. Const Mdrivesumlimit = -43000 ' ??
  72.  
  73. Const Total_looptime = 500 ' ?? - Looptime For Filters
  74. Const Total_looptime10 = 50 ' ?? - Looptime For Filters
  75. Const Adxl_offset = -13 ' ?? - Stick Angle Offset
  76. Const _run = 1
  77. Const _standby = 0
  78. Const _warmup = 2
  79. Const Safespeed = 5000 ' safe speed /\ ?
  80. Const Msafespeed = -5000 ' safe speed \/ ?
  81. Const Sw_down = 50
  82. Const Critical = 80
  83.  
  84. Const Battok = 1023 ' Battery is ok if voltage is 24V (adc counts to 1023 and resistors make divider to provide safe, 5V max output to CPU)
  85. Const Batt_low = 990 ' Minimum input to ADC when battery is low
  86.  
  87. Const Max_pwm = 210 ' Maximum safe PWM value for /\
  88. Const Mmax_pwm = -210 ' Maximum safe PWM value for \/
  89.  
  90. Declare Sub Get_bat_volt ' Read battery voltage from ADC
  91. Declare Sub Get_tilt_angle ' Compute tilt angle using gyro and acc
  92. Declare Sub Set_pwm ' Set PWM value for motors. It also cares about limitations
  93. Declare Sub Algo ' ??
  94. Declare Sub Process ' ??
  95. Declare Sub Ini ' Initiation procedure
  96. Declare Sub Getspeedlimit ' Read speed limit
  97. Declare Sub Err_eeprom ' If eeprom error detected this will give a signal
  98.  
  99.  
  100.  
  101. ' motorcontroller disabled / PWM-Signals to zero
  102. Set Vreg
  103. Pwm_al = 255 ' Right motor at 0 RPM
  104. Pwm_bl = 0 ' Left motor at 0 RPM [its reversed]
  105.  
  106. Gosub Ini ' Some init stuff - it`s ASM
  107.  
  108. '********************* CALIBRATION *********************'
  109. ' First of all check state of jumper. It can be at calibration or operation position
  110. If Pinb.0 = 0 Then ' Calibration mode
  111.  Print "CLB-MDE"
  112.  For Buf = 1 To 4 ' Four long beeps
  113.   Set Buzz
  114.   Waitms 75
  115.   Reset Buzz
  116.   Waitms 75
  117.  Next Buf
  118.  
  119.  Waitms 500
  120.  Buf1 = 0
  121.  For Buf = 1 To 10 ' Get 10 samples from axis Y of gyro with 100ms resolution (so it will take 1 second)
  122.   Buf3 = Getadc(3)
  123.   Buf1 = Buf1 + Buf3
  124.   Waitms 100
  125.  Next Buf
  126.  
  127.  Buf5 = Buf1 / 10 ' Calculate middle value
  128.  Delta = High(buf5) ' ??
  129.  
  130.  'store data in eeprom'
  131.  Writeeeprom Delta , 2
  132.  Writeeeprom Delta , 4
  133.  Delta = Low(buf5) ' ??
  134.  Writeeeprom Delta , 3
  135.  Writeeeprom Delta , 5
  136.  
  137.  Buf1 = 0
  138.  For Buf = 1 To 10 ' Get 10 samples from axis Y of gyro with 100ms resolution (so it will take 1 second)
  139.   Buf3 = Getadc(1)
  140.   Buf1 = Buf1 + Buf3
  141.   Waitms 100
  142.  Next Buf
  143.  
  144.  Buf5 = Buf1 / 10 ' Calculate middle value
  145.  Delta = High(buf5) ' ??
  146.  Writeeeprom Delta , 6
  147.  Writeeeprom Delta , 8
  148.  Delta = Low(buf5)
  149.  Writeeeprom Delta , 7
  150.  Writeeeprom Delta , 9
  151. End If
  152. '********************* END OF CALIBRATION *********************'
  153.  
  154.  
  155. '********************* BOOTLOADER JUMPING *********************'
  156. If Pinb.1 = 0 Then ' Bootloader jumper set
  157.   Print "BTL-MDE"
  158.   For Buf = 1 To 5 ' Five short beeps indicates bootloader mode
  159.     Set Buzz
  160.     Waitms 45
  161.     Reset Buzz
  162.     Waitms 75
  163.   Next Buf
  164.   jmp $3c00 ' Jump to bootloader code
  165. End If
  166. '********************* END OF BOOTLOADER JUMPING *********************'
  167.  
  168.  
  169. '********************* RUNTIME CHECK&CONFIG *********************'
  170. 'read Calib.Data (pair 1)
  171. Readeeprom Delta , 2
  172. Buf = Delta * 256
  173. Readeeprom Delta , 3
  174. Buf = Buf + Delta
  175.  
  176. 'read Calib.Data (pair 2)
  177. Readeeprom Delta , 4
  178. Buf3 = Delta * 256
  179. Readeeprom Delta , 5
  180. Buf3 = Buf3 + Delta
  181.  
  182. 'set gyro_zero value'
  183. Gyro_zero = Buf
  184.  
  185. 'Datapairs not equal-> Epprom-err'
  186. If Buf3 <> Buf Then
  187.  Goto Err_eeprom
  188. End If
  189.  
  190. 'or empty eeporm (default value) ?
  191. If Buf3 = 65535 Then
  192.  Goto Err_eeprom
  193. End If
  194.  
  195. 'read Calib.Data (pair 1)
  196. Readeeprom Delta , 6
  197. Buf = Delta * 256
  198. Readeeprom Delta , 7
  199. Buf = Buf + Delta
  200.  
  201. 'read Calib.Data (pair 2)
  202. Readeeprom Delta , 8
  203. Buf3 = Delta * 256
  204. Readeeprom Delta , 9
  205. Buf3 = Buf3 + Delta
  206.  
  207. 'set adxl_zero value'
  208. Adxl_zero = Buf
  209.  
  210. 'Datapairs not equal-> Epprom-err'
  211. If Buf3 <> Buf Then
  212.  Goto Err_eeprom
  213. End If
  214.  
  215. 'or empty eeporm (default value) ?
  216. If Buf3 = 65535 Then
  217.  Goto Err_eeprom
  218. End If
  219.  
  220. 'set average_gyro
  221. Average_gyro = Total_looptime * Gyro_zero
  222. '********************* END OF RUNTIME CHECK&CONFIG *********************'
  223.  
  224.  
  225.  
  226. '********************* STEERING PIPE 0-POINT CALIBRATION *********************'
  227. 'Get Steering_zero Position'
  228. Ad_rocker = 0
  229. For Buf = 1 To 10 ' get 10 samples in 50ms - this eliminate errors
  230.  Ad_rocker = Ad_rocker + Getadc(5)
  231.  Waitms 5
  232. Next Buf
  233. Rocker_zero = Ad_rocker / 10 ' use middle calculateg from 10 samples
  234. '********************* END OF STEERING PIPE 0-POINT CALIBRATION *********************'
  235.  
  236.  
  237.  
  238. '********************* NORMAL OPERATION *********************'
  239. ' Give 2 short beeps - system doesnt detected any runtime error, in future check here some security like RFID card
  240. Set Buzz
  241. Waitms 45
  242. Reset Buzz
  243. Waitms 75
  244. Set Buzz
  245. Waitms 45
  246. Reset Buzz
  247.  
  248. Reset Vreg ' enable H-bridges circuit
  249. Enable Interrupts ' Wake-up interrupts
  250. Enable Timer0 ' ...and timer0
  251.  
  252. S1: ' Main loop - nothing to do, all work is done by the Timer0 interrupt
  253. Goto S1 ' loop
  254. '********************* END OF NORMAL OPERATION *********************'
  255.  
  256.  
  257.  
  258. '********************* GET BATTERY VOLTAGE *********************'
  259. Sub Get_bat_volt
  260.   Buf = Average_batt / Total_looptime
  261.   Average_batt = Average_batt - Buf
  262.   Average_batt = Average_batt + Ad_batt
  263.  
  264.   ' check voltage
  265.   Buf = Batt_low * Total_looptime
  266.   If Average_batt < Buf Then
  267.    Set Buzz ' if too low turn turn buzzer on
  268.   End If
  269. End Sub
  270. '********************* END OF GET BATTERY VOLTAGE *********************'
  271.  
  272.  
  273. '********************* MOTORS PWM SET *********************'
  274. Sub Set_pwm
  275.  'Limiting PWM for right motor
  276.  If Drive_a > Max_pwm Then
  277.   Drive_a = Max_pwm
  278.  End If
  279.  
  280.  If Drive_a < Mmax_pwm Then
  281.   Drive_a = Mmax_pwm
  282.  End If
  283.  
  284.  'set direction bit for right motor
  285.  If Drive_a < 0 Then
  286.   Drivea = Drive_a * -1
  287.   Cw_ccw_a = 1
  288.  End If
  289.  
  290.  If Drive_a >= 0 Then
  291.   Drivea = Drive_a
  292.   Cw_ccw_a = 0
  293.  End If
  294.  
  295.  'Inverse signal to have 180° phaseshift to PWMB bcs motor is mounted 180*?
  296.  Pwm_al = 255 - Drivea ' set right motor speed
  297.  
  298.  
  299.  'Limiting PWM for left motor
  300.   If Drive_b > Max_pwm Then
  301.    Drive_b = Max_pwm
  302.   End If
  303.   If Drive_b < Mmax_pwm Then
  304.    Drive_b = Mmax_pwm
  305.   End If
  306.  
  307.  
  308.  'set direction bit for left motor
  309.  If Drive_b < 0 Then
  310.   Driveb = Drive_b * -1
  311.   Cw_ccw_b = 1
  312.  End If
  313.  
  314.  If Drive_b >= 0 Then
  315.   Driveb = Drive_b
  316.   Cw_ccw_b = 0
  317.  End If
  318.  
  319.  Pwm_bl = Driveb ' set left motor
  320.  
  321.  ' Idea - its a better to first calculate pwm`s and next set it eliminating delay between setting left and right motor
  322. End Sub
  323. '********************* END OF MOTORS PWM SET *********************'
  324.  
  325.  
  326.  
  327. '********************* MAIN ALGORITHM? *********************'
  328. Sub Algo
  329.  Buf = Tilt_angle + Anglecorrection
  330.  Buf = Buf * 17
  331.  Buf1 = Angle_rate * 20
  332.  
  333.  'calculate balance moment
  334.  Balance_moment = Buf1 + Buf
  335.  
  336.  'check speedlimit
  337.  Gosub Getspeedlimit
  338.  
  339.  ' calculate drive_sum
  340.  ' damn ... but what is drive_sum?!
  341.  Drive_sum = Drive_sum + Balance_moment
  342.  
  343.  ' limitting
  344.  ' max speed limiter?
  345.  If Drive_sum > 55000 Then
  346.   Drive_sum = 55000
  347.  End If
  348.  If Drive_sum < -55000 Then
  349.   Drive_sum = -55000
  350.  End If
  351.  
  352.  'calculate drive speed
  353.  Buf = Drive_sum / 125
  354.  Buf1 = Balance_moment / 125
  355.  Drivespeed = Buf + Buf1
  356. End Sub
  357. '********************* END OF MAIN ALGORITHM? *********************'
  358.  
  359.  
  360. '********************* GET SPEED LIMIT *********************'
  361. Sub Getspeedlimit
  362.  If Drive_sum < 0 Then
  363.   If Drive_sum < Mdrivesumlimit Then
  364.    Anglecorrection = -13
  365.    Buzzer = 1
  366.    Set Buzz
  367.   Else
  368.    Anglecorrection = 0
  369.   End If
  370.  End If
  371. End Sub
  372. '********************* END OF GET SPEED LIMIT *********************'
  373.  
  374.  
  375.  
  376. '********************* MODE HANDLER *********************'
  377. Sub Process
  378.  ' First of all push some logs
  379.  Tcount = Tcount + 1
  380.  If Tcount > 30 Then //Send logs every 30 loops of whole program[?]
  381.   Tcount = 1
  382.   Print "ST-TA:" ; Tilt_angle ' Just angle of platform tilt
  383.   Print "ST-ADXL:" ; Ad_adxl ' ??
  384.   Print "ST-GYRO:" ; Ad_gyro ' ??
  385.   Print "ST-AVBT:" ; Average_batt ' Some battery stuff
  386.   Print "ST-ADBT" ; Ad_batt ' SOME battery stuff
  387.   Print "ST-AVGR" ; Average_gyro ' ??
  388.   Print "ST-MDE" ; Mmode ' Mode of operation (see constants at few first lines)
  389.  End If
  390.  
  391.  
  392.  'Steering calculation
  393.  Rockersq = Rocker_zero - Ad_rocker
  394.  If Rockersq > 0 Then
  395.   ' ?? - Make it progressiv
  396.   Rockersq = Rockersq * Rockersq
  397.  End If
  398.  
  399.  If Rockersq < 0 Then
  400.   Rockersq = Rockersq * Rockersq
  401.   Rockersq = Rockersq * -1
  402.  End If
  403.  
  404.  ' Hmm is this part decrase sensitivity of stearing when speed going up?
  405.  Buf1 = Drive_sum / 600
  406.  If Buf1 < 0 Then
  407.   Buf1 = Buf1 * -1
  408.  End If
  409.  Buf1 = Buf1 + 77
  410.  Rockersq = Rockersq / Buf1
  411.  
  412.  ' Some safety - limits the max. steering values
  413.  If Rockersq > 25 Then Rockersq = 25
  414.  If Rockersq < -25 Then Rockersq = -25
  415.  
  416.  
  417.  ' Make the steering smooth
  418.  If Steeringsignal < Rockersq Then
  419.   Steeringsignal = Steeringsignal + 4
  420.  End If
  421.  
  422.  If Steeringsignal > Rockersq Then
  423.   Steeringsignal = Steeringsignal - 4
  424.  End If
  425.  
  426.  ' Set speed and steering
  427.  Drive_a = Drivespeed - Steeringsignal
  428.  Drive_b = Drivespeed + Steeringsignal
  429.  
  430.  
  431.  ' Need some seconds to compensate the gyro temp-drift
  432.  If Mmode = _warmup Then
  433.   Drive_a = 0
  434.   Drive_b = 0
  435.   Drive_as = 0
  436.   Drive_bs = 0
  437.   Drivespeed = 0
  438.   Anglecorrection = 0
  439.   Overspeed = 0
  440.   Drive_sum = 0
  441.   Total_adxl_gyro = 0
  442.   Tilt_angle = 0
  443.  
  444.   If Loopct > 1100 Then
  445.    Mmode = _standby
  446.  
  447.    ' Give some beeps - damn, but what it means?
  448.    Set Buzz
  449.    Waitms 20
  450.    Reset Buzz
  451.    Waitms 50
  452.    Set Buzz
  453.    Waitms 20
  454.    Reset Buzz
  455.   End If
  456.  End If
  457.  
  458.  If Mmode = _standby Then
  459.   Drive_a = 0
  460.   Drive_b = 0
  461.   Drive_as = 0
  462.   Drive_bs = 0
  463.   Drivespeed = 0
  464.   Anglecorrection = 0
  465.   Overspeed = 0
  466.  
  467.   Drive_sum = 0
  468.   Total_adxl_gyro = 0
  469.   Tilt_angle = 0
  470.  
  471.  
  472.   Buf2 = Ad_adxl - Adxl_zero
  473.   Buf2 = Buf2 + Adxl_offset
  474.   Buzzer = 0
  475.   Timeout = 0
  476.  
  477.   ' If someone stand on platform
  478.   If Ad_swi > Sw_down Then
  479.    Mmode = _run ' …change mode to RUN
  480.   End If
  481.  End If
  482.  
  483.  If Mmode = _run Then
  484.  'check platform buttons
  485.  
  486.   If Ad_swi > Sw_down Then ' person is on platform
  487.    If Buzzer = 1 Then 'if buzzer still sounds turn it off
  488.     Buzzer = 0
  489.    End If
  490.   End If
  491.  
  492.   If Ad_swi < Sw_down Then ' no person on board
  493.    If Drive_sum < 0 Then ' for moving backwards?
  494.     If Drive_sum < Msafespeed Then ' hmm this proably turn on buzzer only if platform is moving
  495.      Buzzer = 1
  496.     End If
  497.    
  498.     If Drive_sum > Msafespeed Then ' similar to above but hmm …
  499.      Buzzer = 0
  500.     End If
  501.    End If
  502.  
  503.    If Drive_sum > 0 Then ' form moving normal?
  504.     If Drive_sum > Safespeed Then
  505.      Buzzer = 1
  506.     End If
  507.  
  508.     If Drive_sum < Safespeed Then
  509.      Buzzer = 0
  510.     End If
  511.    End If
  512.  
  513.    If Buzzer = 1 Then
  514.     If Timeout > Critical Then ' if buzzer is on and timeout (2 seconds?) is reached turn off engines
  515.      Mmode = _standby
  516.     End If
  517.    End If
  518.  
  519.   End If ' end "no person on board" if
  520.  
  521.   If Buzzer = 0 Then ' if buzzer turned off…
  522.    Timeout = 0 ' ...reset critical timeout
  523.   End If
  524.  
  525.   Timeout = Timeout + Buzzer ' some freaky trick (?)
  526.  End If ' end of "if Mmode = _run" if
  527. End Sub
  528. '********************* END OF MODE HANDLER *********************'
  529.  
  530. '********************* INIT STUFF *********************'
  531. Sub Ini
  532.   $asm
  533.   ldi r16,0
  534.   Out Porta , R16
  535.   Out Ddra , R16
  536.  
  537.   'ldi r16,$e2
  538.   'Out Portb , R16
  539.   'ldi r16,$e3
  540.   'Out Ddrb , R16
  541.  
  542.   ldi r16,0
  543.   Out Portc , R16
  544.   ldi r16,255
  545.   Out Ddrc , R16
  546.  
  547.   ldi r16,$34
  548.   Out Portd , R16
  549.   ldi r16,$ff
  550.   Out Ddrd , R16
  551.  
  552.   ldi r16,$b1                                               'b1=8bit ,b2=9bit,b3=10bit
  553.   Out Tccr1a , R16
  554.   ldi r16,$01                                               ' 01 no prescaler!
  555.   Out Tccr1b , R16
  556.   ldi r16,0
  557.   Out Tcnt1h , R16
  558.   Out Tcnt1l , R16
  559.   Out Icr1h , R16
  560.   Out Icr1l , R16
  561.   Out Ocr1ah , R16
  562.   ldi r16,$ff
  563.   'Out Ocr1ah , R16
  564.   Out Ocr1al , R16
  565.   ldi r16,0
  566.   'Out Ocr1bh , R16
  567.   ldi r16,0
  568.   Out Ocr1bl , R16
  569.  
  570.   Out Assr , R16
  571.   Out Tccr2 , R16
  572.   Out Tcnt2 , R16
  573.   Out Ocr2 , R16
  574.   $end Asm
  575.  
  576.  Average_batt = Total_looptime * Battok
  577.  Total_adxl_gyro = 0
  578.  Average_gyro = 0
  579.  Drivea = 0
  580.  Driveb = 0
  581.  Tilt_angle = 0
  582.  Drive_a = 0
  583.  Drive_b = 0
  584.  Drivespeed = 0
  585.  Steeringsignal = 0
  586.  Anglecorrection = 0
  587.  Angle_rate = 0
  588.  Balance_moment = 0
  589.  Overspeed = 0
  590.  
  591.  Mmode = _warmup
  592.  Overspeed_flag = 0
  593.  Drive_sum = 0
  594.  Ad_rocker = 0
  595.  Tcount = 0
  596.  Steeringsignal = 0
  597.  Drive_as = 0
  598.  Drive_bs = 0
  599.  
  600.  Timeout = 0
  601.  Delta = 0
  602.  Loopct = 0
  603. End Sub
  604. '********************* END OF INIT STUFF *********************'
  605.  
  606. '********************* TIMER0 INT. HANDLER *********************'
  607. ' Handle interputs from Timer0
  608. Tinter:
  609.  Disable Interrupts ' prevents from re-entering "Tinter" if this block of code exceed 1024 cpu cycles which cause infinite-loop ?
  610.  Gosub Get_bat_volt ' Masure battery voltage
  611.  Gosub Get_tilt_angle ' Calculate platform tilt angle
  612.  
  613.  Gosub Algo
  614.  Gosub Process
  615.  Gosub Set_pwm ' Set PWM`s to motors
  616.  Buzz = Buzzer ' If some code above turn on buzzer turn it real (Buzzer is int var, Buzz is link to output). I don`t know why author of this code used extra variable for that insted of setting Buzz directly
  617.  
  618.  Timer0 = 100 ' why 100?
  619.  
  620.  Enable Interrupts
  621. Return
  622.  
  623.  
  624. '********************* END OF TIMER0 INT. HANDLER *********************'
  625.  
  626. '********************* EEPROM-ERROR *********************'
  627. Sub Err_eeprom
  628.  Print "ERR-EEPROM" ' EEPROM error
  629.   S3:
  630.    For Buf = 1 To 6 ' Six long beeps repeated every 2 seconds
  631.     Set Buzz
  632.     Waitms 75
  633.     Reset Buzz
  634.     Waitms 75
  635.   Next Buf
  636.  
  637.   Wait 2
  638.   Goto S3 ' Why its not a classic DO-LOOP?
  639. End Sub
  640. '********************* END OF EEPROM-ERROR *********************'
  641.  
  642. '********************* GET TILT ANGLE *********************'
  643.  Sub Get_tilt_angle
  644.   Ad_gyro = Getadc(3)
  645.   Ad_adxl = Getadc(1)
  646.   Ad_batt = Getadc(6)
  647.   Ad_rocker = Getadc(5)
  648.   Ad_swi = 1024 - Getadc(4)
  649.  
  650.   Buf = Total_adxl_gyro / Total_looptime
  651.   Total_adxl_gyro = Total_adxl_gyro - Buf
  652.  
  653.   ' ADXL part
  654.   Buf = Ad_adxl - Adxl_zero
  655.   Buf = Buf + Adxl_offset
  656.  
  657.   Total_adxl_gyro = Total_adxl_gyro + Buf
  658.  
  659.   ' Gyro part
  660.   Buf1 = Average_gyro / Total_looptime
  661.   Average_gyro = Average_gyro - Buf1
  662.  
  663.   Average_gyro = Average_gyro + Ad_gyro
  664.   Buf1 = Average_gyro / Total_looptime10
  665.  
  666.   ' calculate the Angle Rate
  667.   Buf = Ad_gyro * 10
  668.   Buf1 = Buf1 - Buf
  669.   Buf1 = Buf1 * 35
  670.   Buf1 = Buf1 / 100
  671.   Angle_rate = Buf1
  672.  
  673.   ' calculate the Tilt Angle
  674.   Total_adxl_gyro = Total_adxl_gyro + Angle_rate
  675.   Tilt_angle = Total_adxl_gyro / Total_looptime10
  676.  
  677.  
  678.   Loopct = Loopct + 1
  679.  
  680.   Return
  681.  End Sub
  682. '********************* END OF GET TITL ANGLE *********************'
  683.  
  684. End ' empty infinite loop :)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement