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.

357 lines
8.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 = 45 ; 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 BpFET = 7 ;o
  41. .equ Comp_Com = 6 ;i Comparator common input (AIN0)
  42. .equ CpFET = 5 ;o
  43. .equ ApFET = 4 ;o
  44. .equ AnFET = 3 ;o
  45. .equ Rcp_In = 2 ;i RC pulse input
  46. .equ BnFET = 1 ;o
  47. .equ CnFET = 0 ;o
  48. .equ INIT_PD = 0
  49. .equ DIR_PD = (1<<AnFET)+(1<<BnFET)+(1<<CnFET)+(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 Rcp_Icp_Int_Enable
  88. in @0, TIMSK
  89. sbr @0, (1<<TICIE1) ; Enable icp1int
  90. out TIMSK, @0
  91. .ENDMACRO
  92. .MACRO Rcp_Icp_Int_Disable
  93. in @0, TIMSK
  94. cbr @0, (1<<TICIE1) ; Disable icp1int
  95. out TIMSK, @0
  96. .ENDMACRO
  97. .MACRO Rcp_Icp_Int_First
  98. in @0, TCCR1B
  99. sbrs Flags2, PGM_RCP_PWM_POL ; Is pwm polarity positive?
  100. sbr @0, (1<<ICES1) ; Yes - set icp1int to trig on rising edge
  101. sbrc Flags2, PGM_RCP_PWM_POL ; Is pwm polarity negative?
  102. cbr @0, (1<<ICES1) ; Yes - set icp1int to trig on falling edge
  103. out TCCR1B, @0
  104. .ENDMACRO
  105. .MACRO Rcp_Icp_Int_Second
  106. in @0, TCCR1B
  107. sbrs Flags2, PGM_RCP_PWM_POL ; Is pwm polarity positive?
  108. cbr @0, (1<<ICES1) ; Yes - set icp1int to trig on falling edge
  109. sbrc Flags2, PGM_RCP_PWM_POL ; Is pwm polarity negative?
  110. sbr @0, (1<<ICES1) ; Yes - set icp1int to trig on rising edge
  111. out TCCR1B, @0
  112. .ENDMACRO
  113. .MACRO Clear_Icp_Int_Flag
  114. clr @0
  115. sbr @0, (1<<ICF1) ; Clear icp1int flag
  116. out TIFR, @0
  117. .ENDMACRO
  118. .MACRO AnFET_on
  119. sbi PORTD,3
  120. .ENDMACRO
  121. .MACRO AnFET_off
  122. cbi PORTD,3
  123. .ENDMACRO
  124. .MACRO BnFET_on
  125. sbi PORTD,1
  126. .ENDMACRO
  127. .MACRO BnFET_off
  128. cbi PORTD,1
  129. .ENDMACRO
  130. .MACRO CnFET_on
  131. sbi PORTD,0
  132. .ENDMACRO
  133. .MACRO CnFET_off
  134. cbi PORTD,0
  135. .ENDMACRO
  136. .MACRO All_nFETs_Off
  137. in @0, PORTD
  138. cbr @0, (1<<AnFET)+(1<<BnFET)+(1<<CnFET)
  139. out PORTD, @0
  140. .ENDMACRO
  141. .MACRO ApFET_on
  142. sbi PORTD,4
  143. .ENDMACRO
  144. .MACRO ApFET_off
  145. cbi PORTD,4
  146. .ENDMACRO
  147. .MACRO BpFET_on
  148. sbi PORTD,7
  149. .ENDMACRO
  150. .MACRO BpFET_off
  151. cbi PORTD,7
  152. .ENDMACRO
  153. .MACRO CpFET_on
  154. sbi PORTD,5
  155. .ENDMACRO
  156. .MACRO CpFET_off
  157. cbi PORTD,5
  158. .ENDMACRO
  159. .MACRO All_pFETs_Off
  160. in @0, PORTD
  161. cbr @0, (1<<ApFET)+(1<<BpFET)+(1<<CpFET)
  162. out PORTD, @0
  163. .ENDMACRO
  164. .MACRO All_pFETs_On
  165. in @0, PORTD
  166. sbr @0, (1<<ApFET)+(1<<BpFET)+(1<<CpFET)
  167. out PORTD, @0
  168. .ENDMACRO
  169. ;*********************
  170. ; PORT C definitions *
  171. ;*********************
  172. ;.equ = 7 ; ADC7
  173. ;.equ = 6 ; ADC6
  174. ;.equ = 5 ; ADC5
  175. .equ Mux_A = 4 ; Phase A input
  176. .equ Mux_B = 3 ; Phase B input
  177. .equ Mux_C = 2 ; Phase C input
  178. ;.equ = 1 ; ADC1
  179. ;.equ = 0 ; ADC0
  180. .equ INIT_PC = 0
  181. .equ DIR_PC = 0
  182. .MACRO Comp_Init
  183. in @0, SFIOR ; Set Analog Comparator Multiplexer Enable
  184. sbr @0, (1<<ACME)
  185. out SFIOR, @0
  186. cbi ADCSRA, ADEN ; Disable ADC
  187. .ENDMACRO
  188. .MACRO Set_Comp_Phase_A
  189. ldi @0, Mux_A ; Set comparator multiplexer to phase A
  190. out ADMUX, @0
  191. .ENDMACRO
  192. .MACRO Set_Comp_Phase_B
  193. ldi @0, Mux_B ; Set comparator multiplexer to phase B
  194. out ADMUX, @0
  195. .ENDMACRO
  196. .MACRO Set_Comp_Phase_C
  197. ldi @0, Mux_C ; Set comparator multiplexer to phase C
  198. out ADMUX, @0
  199. .ENDMACRO
  200. .MACRO Read_Comp_Out
  201. in @0, ACSR ; Read comparator output
  202. .ENDMACRO
  203. ;*********************
  204. ; PORT B definitions *
  205. ;*********************
  206. ;.equ = 7
  207. ;.equ = 6
  208. ;.equ = 5 (sck stk200 interface)
  209. .equ DebugPin = 4 ;(miso stk200 interface)
  210. ;.equ = 3 (mosi stk200 interface)
  211. ;.equ = 2
  212. ;.equ = 1
  213. .equ Rcp_Icp_In = 0
  214. .equ INIT_PB = 0
  215. .equ DIR_PB = (1<<DebugPin)
  216. ;**********************
  217. ; MCU specific macros *
  218. ;**********************
  219. .MACRO Disable_Watchdog
  220. cli ; Disable interrupts
  221. wdr ; Reset watchdog timer
  222. in @0, WDTCR ; Write logical one to WDCE and WDE
  223. ori @0, (1<<WDCE)|(1<<WDE)
  224. out WDTCR, @0
  225. ldi @0, (0<<WDE) ; Turn off WDT
  226. out WDTCR, @0
  227. .ENDMACRO
  228. .MACRO Enable_Watchdog
  229. ldi @0, (1<<WDE) ; Turn on WDT
  230. sts WDTCR, @0
  231. .ENDMACRO
  232. .MACRO Initialize_MCU
  233. .ENDMACRO
  234. .MACRO Interrupt_Table_Definition
  235. rjmp reset
  236. rjmp ext_int0 ; ext_int0
  237. nop ; ext_int1
  238. nop ; t2oc_int
  239. rjmp t2ovfl_int; t2ovfl_int
  240. rjmp icp1_int ; icp1_int
  241. rjmp t1oca_int ; t1oca_int
  242. nop ; t1ocb_int
  243. rjmp t1ovfl_int; t1ovfl_int
  244. rjmp t0ovfl_int; t0ovfl_int
  245. nop ; spi_int
  246. nop ; urxc
  247. nop ; udre
  248. nop ; utxc
  249. ; nop ; adc_int
  250. ; nop ; eep_int
  251. ; nop ; aci_int
  252. ; nop ; wire2_int
  253. ; nop ; spmc_int
  254. .ENDMACRO
  255. .MACRO Initialize_Interrupts
  256. ldi Temp1, (1<<TOIE0)+(1<<TOIE1)+(1<<OCIE1A)+(1<<TOIE2)
  257. out TIFR, Temp1 ; Clear interrupts
  258. out TIMSK, Temp1 ; Enable interrupts
  259. .ENDMACRO
  260. .MACRO Initialize_Adc
  261. in Temp1, ADCSRA ; Set ADCSRA register (1MHz clock)
  262. sbr Temp1, (1<<ADPS1)
  263. sbr Temp1, (1<<ADPS0)
  264. out ADCSRA, Temp1
  265. .ENDMACRO
  266. .MACRO Start_Adc
  267. ldi Temp1, (1<<REFS1)+(1<<REFS0)+(1<<MUX2)+(1<<MUX1)+(1<<MUX0)
  268. out ADMUX, Temp1 ; Set ADMUX register (2.56V reference, left adj result, input 7)
  269. in @0, ADCSRA
  270. sbr @0, (1<<ADEN) ; Enable ADC
  271. sbr @0, (1<<ADSC) ; Start ADC conversion
  272. out ADCSRA, @0
  273. .ENDMACRO
  274. .MACRO Get_Adc_Status
  275. in @0, ADCSRA
  276. .ENDMACRO
  277. .MACRO Read_Adc_Result
  278. in @0, ADCL
  279. in @1, ADCH
  280. .ENDMACRO
  281. .MACRO Stop_Adc
  282. in @0, ADCSRA
  283. cbr @0, (1<<ADEN) ; Disable ADC
  284. out ADCSRA, @0
  285. .ENDMACRO
  286. .MACRO Set_Timer0_CS0
  287. out TCCR0, @0
  288. .ENDMACRO
  289. .MACRO Set_Timer1_CS1
  290. out TCCR1B, @0
  291. .ENDMACRO
  292. .MACRO Set_Timer2_CS2
  293. out TCCR2, @0
  294. .ENDMACRO
  295. .MACRO Read_TCNT1L
  296. in @0, TCNT1L
  297. .ENDMACRO
  298. .MACRO Read_TCNT1H
  299. in @0, TCNT1H
  300. .ENDMACRO
  301. .MACRO Read_ICR1L
  302. in @0, ICR1L
  303. .ENDMACRO
  304. .MACRO Read_ICR1H
  305. in @0, ICR1H
  306. .ENDMACRO
  307. .MACRO Set_OCR1AL
  308. out OCR1AL, @0
  309. .ENDMACRO
  310. .MACRO Set_OCR1AH
  311. out OCR1AH, @0
  312. .ENDMACRO
  313. .MACRO Set_TCNT2
  314. out TCNT2, @0
  315. .ENDMACRO
  316. .MACRO Check_Eeprom_Ready
  317. sbic EECR, EEWE
  318. .ENDMACRO
  319. .MACRO Set_Eeprom_Address
  320. out EEARL, @0
  321. out EEARH, @1
  322. .ENDMACRO
  323. .MACRO Start_Eeprom_Write
  324. sbi EECR, EEMWE
  325. sbi EECR, EEWE
  326. .ENDMACRO