You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

318 lines
7.0 KiB

  1. ;**** **** **** **** ****
  2. ;
  3. ; BLHeli program for controlling brushless motors in helicopters
  4. ;
  5. ; Copyright 2011, 2012 Steffen Skaug
  6. ; This program is distributed under the terms of the GNU General Public License
  7. ;
  8. ; This file is part of BLHeli.
  9. ;
  10. ; BLHeli is free software: you can redistribute it and/or modify
  11. ; it under the terms of the GNU General Public License as published by
  12. ; the Free Software Foundation, either version 3 of the License, or
  13. ; (at your option) any later version.
  14. ;
  15. ; BLHeli is distributed in the hope that it will be useful,
  16. ; but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. ; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. ; GNU General Public License for more details.
  19. ;
  20. ; You should have received a copy of the GNU General Public License
  21. ; along with BLHeli. If not, see <http://www.gnu.org/licenses/>.
  22. ;
  23. ;**** **** **** **** ****
  24. ;*********************
  25. ; Device Atmega8
  26. ;*********************
  27. .include "m8def.inc"
  28. .equ ICP = 0 ; Choose input pin. Set to 0 for INT0 (pin32) and 1 for ICP1 (pin12)
  29. ;**** **** **** **** ****
  30. ; Fuses must be set to internal calibrated oscillator = 8Mhz
  31. ;**** **** **** **** ****
  32. ;**** **** **** **** ****
  33. ; Constant definitions
  34. ;**** **** **** **** ****
  35. .equ ADC_LIMIT_L = 211 ; Power supply measurement ADC value for which main motor power is limited (low byte)
  36. .equ ADC_LIMIT_H = 0 ; Power supply measurement ADC value for which main motor power is limited (2 MSBs)
  37. ;*********************
  38. ; PORT D definitions *
  39. ;*********************
  40. ;.equ = 7 ;i
  41. .equ Comp_Com = 6 ;i Comparator common input (AIN0)
  42. .equ ApFET = 5 ;o
  43. .equ CpFET = 4 ;o
  44. .equ BpFET = 3 ;o
  45. .equ Rcp_In = 2 ;i RC pulse input
  46. ;.equ = 1 ;i
  47. ;.equ = 0 ;i
  48. .equ INIT_PD = 0
  49. .equ DIR_PD = (1<<ApFET)+(1<<BpFET)+(1<<CpFET)
  50. .MACRO Read_Rcp_Int
  51. in @0, PIND
  52. sbrc Flags2, PGM_RCP_PWM_POL ; Is pwm polarity negative?
  53. com @0 ; Yes - invert
  54. .ENDMACRO
  55. .MACRO Read_Rcp_Icp_Int
  56. in @0, PINB
  57. sbrc Flags2, PGM_RCP_PWM_POL ; Is pwm polarity negative?
  58. com @0 ; Yes - invert
  59. .ENDMACRO
  60. .MACRO Rcp_Int_Enable
  61. ldi @0, (1<<INT0) ; Enable ext0int
  62. out GICR, @0
  63. .ENDMACRO
  64. .MACRO Rcp_Int_Disable
  65. ldi @0, 0 ; Disable ext0int
  66. out GICR, @0
  67. .ENDMACRO
  68. .MACRO Rcp_Int_First
  69. sbrs Flags2, PGM_RCP_PWM_POL ; Is pwm polarity positive?
  70. ldi @0, (1<<ISC01)+(1<<ISC00) ; Yes - set next int0 to rising
  71. sbrc Flags2, PGM_RCP_PWM_POL ; Is pwm polarity negative?
  72. ldi @0, (1<<ISC01) ; Yes - set next int0 to falling
  73. out MCUCR, @0
  74. .ENDMACRO
  75. .MACRO Rcp_Int_Second
  76. sbrs Flags2, PGM_RCP_PWM_POL ; Is pwm polarity positive?
  77. ldi @0, (1<<ISC01) ; Yes - set next int0 to falling
  78. sbrc Flags2, PGM_RCP_PWM_POL ; Is pwm polarity negative?
  79. ldi @0, (1<<ISC01)+(1<<ISC00) ; Yes - set next int0 to rising
  80. out MCUCR, @0
  81. .ENDMACRO
  82. .MACRO Clear_Int_Flag
  83. clr @0
  84. sbr @0, (1<<INTF0) ; Clear ext0int flag
  85. out GIFR, @0
  86. .ENDMACRO
  87. .MACRO ApFET_on
  88. sbi PORTD,5
  89. .ENDMACRO
  90. .MACRO ApFET_off
  91. cbi PORTD,5
  92. .ENDMACRO
  93. .MACRO CpFET_on
  94. sbi PORTD,4
  95. .ENDMACRO
  96. .MACRO CpFET_off
  97. cbi PORTD,4
  98. .ENDMACRO
  99. .MACRO BpFET_on
  100. sbi PORTD,3
  101. .ENDMACRO
  102. .MACRO BpFET_off
  103. cbi PORTD,3
  104. .ENDMACRO
  105. .MACRO All_pFETs_Off
  106. in @0, PORTD
  107. cbr @0, (1<<ApFET)+(1<<BpFET)+(1<<CpFET)
  108. out PORTD, @0
  109. .ENDMACRO
  110. .MACRO All_pFETs_On
  111. in @0, PORTD
  112. sbr @0, (1<<ApFET)+(1<<BpFET)+(1<<CpFET)
  113. out PORTD, @0
  114. .ENDMACRO
  115. ;*********************
  116. ; PORT C definitions *
  117. ;*********************
  118. ;.equ = 7 ; ADC7
  119. ;.equ = 6 ; ADC6
  120. .equ Mux_A = 5 ; Phase A input
  121. .equ Mux_C = 4 ; Phase C input
  122. ;.equ = 3 ; ADC3
  123. .equ Mux_B = 2 ; Phase B input
  124. ;.equ = 1 ; ADC1
  125. ;.equ = 0 ; ADC0
  126. .equ INIT_PC = 0
  127. .equ DIR_PC = 0
  128. .MACRO Comp_Init
  129. in @0, SFIOR ; Set Analog Comparator Multiplexer Enable
  130. sbr @0, (1<<ACME)
  131. out SFIOR, @0
  132. cbi ADCSRA, ADEN ; Disable ADC
  133. .ENDMACRO
  134. .MACRO Set_Comp_Phase_A
  135. ldi @0, Mux_A ; Set comparator multiplexer to phase A
  136. out ADMUX, @0
  137. .ENDMACRO
  138. .MACRO Set_Comp_Phase_B
  139. ldi @0, Mux_B ; Set comparator multiplexer to phase B
  140. out ADMUX, @0
  141. .ENDMACRO
  142. .MACRO Set_Comp_Phase_C
  143. ldi @0, Mux_C ; Set comparator multiplexer to phase C
  144. out ADMUX, @0
  145. .ENDMACRO
  146. .MACRO Read_Comp_Out
  147. in @0, ACSR ; Read comparator output
  148. .ENDMACRO
  149. ;*********************
  150. ; PORT B definitions *
  151. ;*********************
  152. ;.equ = 7
  153. ;.equ = 6
  154. ;.equ = 5 (sck stk200 interface)
  155. .equ DebugPin = 4 ;(miso stk200 interface)
  156. ;.equ = 3 (mosi stk200 interface)
  157. .equ AnFET = 2
  158. .equ CnFET = 1
  159. .equ BnFET = 0
  160. .equ INIT_PB = 0
  161. .equ DIR_PB = (1<<DebugPin)+(1<<AnFET)+(1<<BnFET)+(1<<CnFET)
  162. .MACRO AnFET_on
  163. sbi PORTB,2
  164. .ENDMACRO
  165. .MACRO AnFET_off
  166. cbi PORTB,2
  167. .ENDMACRO
  168. .MACRO CnFET_on
  169. sbi PORTB,1
  170. .ENDMACRO
  171. .MACRO CnFET_off
  172. cbi PORTB,1
  173. .ENDMACRO
  174. .MACRO BnFET_on
  175. sbi PORTB,0
  176. .ENDMACRO
  177. .MACRO BnFET_off
  178. cbi PORTB,0
  179. .ENDMACRO
  180. .MACRO All_nFETs_Off
  181. in @0, PORTB
  182. cbr @0, (1<<AnFET)+(1<<BnFET)+(1<<CnFET)
  183. out PORTB, @0
  184. .ENDMACRO
  185. ;**********************
  186. ; MCU specific macros *
  187. ;**********************
  188. .MACRO Disable_Watchdog
  189. cli ; Disable interrupts
  190. wdr ; Reset watchdog timer
  191. in @0, WDTCR ; Write logical one to WDCE and WDE
  192. ori @0, (1<<WDCE)|(1<<WDE)
  193. out WDTCR, @0
  194. ldi @0, (0<<WDE) ; Turn off WDT
  195. out WDTCR, @0
  196. .ENDMACRO
  197. .MACRO Enable_Watchdog
  198. ldi @0, (1<<WDE) ; Turn on WDT
  199. sts WDTCR, @0
  200. .ENDMACRO
  201. .MACRO Initialize_MCU
  202. .ENDMACRO
  203. .MACRO Interrupt_Table_Definition
  204. rjmp reset
  205. rjmp ext_int0 ; ext_int0
  206. nop ; ext_int1
  207. nop ; t2oc_int
  208. rjmp t2ovfl_int; t2ovfl_int
  209. nop ; icp1_int
  210. rjmp t1oca_int ; t1oca_int
  211. nop ; t1ocb_int
  212. rjmp t1ovfl_int; t1ovfl_int
  213. rjmp t0ovfl_int; t0ovfl_int
  214. nop ; spi_int
  215. nop ; urxc
  216. nop ; udre
  217. nop ; utxc
  218. ; nop ; adc_int
  219. ; nop ; eep_int
  220. ; nop ; aci_int
  221. ; nop ; wire2_int
  222. ; nop ; spmc_int
  223. .ENDMACRO
  224. .MACRO Initialize_Interrupts
  225. ldi Temp1, (1<<TOIE0)+(1<<TOIE1)+(1<<OCIE1A)+(1<<TOIE2)
  226. out TIFR, Temp1 ; Clear interrupts
  227. out TIMSK, Temp1 ; Enable interrupts
  228. .ENDMACRO
  229. .MACRO Initialize_Adc
  230. in Temp1, ADCSRA ; Set ADCSRA register (1MHz clock)
  231. sbr Temp1, (1<<ADPS1)
  232. sbr Temp1, (1<<ADPS0)
  233. out ADCSRA, Temp1
  234. .ENDMACRO
  235. .MACRO Start_Adc
  236. ldi Temp1, (1<<REFS1)+(1<<REFS0)
  237. out ADMUX, Temp1 ; Set ADMUX register (2.56V reference, left adj result, input 0)
  238. in @0, ADCSRA
  239. sbr @0, (1<<ADEN) ; Enable ADC
  240. sbr @0, (1<<ADSC) ; Start ADC conversion
  241. out ADCSRA, @0
  242. .ENDMACRO
  243. .MACRO Get_Adc_Status
  244. in @0, ADCSRA
  245. .ENDMACRO
  246. .MACRO Read_Adc_Result
  247. in @0, ADCL
  248. in @1, ADCH
  249. .ENDMACRO
  250. .MACRO Stop_Adc
  251. in @0, ADCSRA
  252. cbr @0, (1<<ADEN) ; Disable ADC
  253. out ADCSRA, @0
  254. .ENDMACRO
  255. .MACRO Set_Timer0_CS0
  256. out TCCR0, @0
  257. .ENDMACRO
  258. .MACRO Set_Timer1_CS1
  259. out TCCR1B, @0
  260. .ENDMACRO
  261. .MACRO Set_Timer2_CS2
  262. out TCCR2, @0
  263. .ENDMACRO
  264. .MACRO Read_TCNT1L
  265. in @0, TCNT1L
  266. .ENDMACRO
  267. .MACRO Read_TCNT1H
  268. in @0, TCNT1H
  269. .ENDMACRO
  270. .MACRO Set_OCR1AL
  271. out OCR1AL, @0
  272. .ENDMACRO
  273. .MACRO Set_OCR1AH
  274. out OCR1AH, @0
  275. .ENDMACRO
  276. .MACRO Set_TCNT2
  277. out TCNT2, @0
  278. .ENDMACRO
  279. .MACRO Check_Eeprom_Ready
  280. sbic EECR, EEWE
  281. .ENDMACRO
  282. .MACRO Set_Eeprom_Address
  283. out EEARL, @0
  284. out EEARH, @1
  285. .ENDMACRO
  286. .MACRO Start_Eeprom_Write
  287. sbi EECR, EEMWE
  288. sbi EECR, EEWE
  289. .ENDMACRO