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.

352 lines
7.9 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 Atmega48V
  26. ;*********************
  27. .include "m48def.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 = 254 ; 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 Mux_A = 7 ;i Phase A input
  41. .equ Comp_Com = 6 ;i Comparator common input (AIN0)
  42. ;.equ = 5
  43. ;.equ = 4
  44. .equ ApFET = 3 ;o
  45. .equ Rcp_In = 2 ;i RC pulse input
  46. .equ BpFET = 1 ;o
  47. .equ CpFET = 0 ;o
  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 EIMSK, @0
  63. .ENDMACRO
  64. .MACRO Rcp_Int_Disable
  65. ldi @0, 0 ; Disable ext0int
  66. out EIMSK, @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. sts EICRA, @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. sts EICRA, @0
  81. .ENDMACRO
  82. .MACRO Clear_Int_Flag
  83. clr @0
  84. sbr @0, (1<<INTF0) ; Clear ext0int flag
  85. out EIFR, @0
  86. .ENDMACRO
  87. .MACRO ApFET_on
  88. sbi PORTD,3
  89. .ENDMACRO
  90. .MACRO ApFET_off
  91. cbi PORTD,3
  92. .ENDMACRO
  93. .MACRO BpFET_on
  94. sbi PORTD,1
  95. .ENDMACRO
  96. .MACRO BpFET_off
  97. cbi PORTD,1
  98. .ENDMACRO
  99. .MACRO CpFET_on
  100. sbi PORTD,0
  101. .ENDMACRO
  102. .MACRO CpFET_off
  103. cbi PORTD,0
  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 = 5 ; ADC5
  121. ;.equ = 4 ; ADC4
  122. .equ Mux_B = 3 ; Phase B input
  123. .equ Mux_C = 2 ; Phase C input
  124. ;.equ = 1 ; ADC1
  125. ;.equ = 0 ; ADC0
  126. .equ INIT_PC = 0
  127. .equ DIR_PC = 0
  128. .MACRO Comp_Init
  129. lds @0, ADCSRA ; Disable ADC
  130. cbr @0, (1<<ADEN)
  131. sts ADCSRA, @0
  132. lds @0, ADCSRB ; Set Analog Comparator Multiplexer Enable
  133. sbr @0, (1<<ACME)
  134. sts ADCSRB, @0
  135. .ENDMACRO
  136. .MACRO Set_Comp_Phase_A
  137. lds @0, ADCSRB ; Set Analog Comparator Multiplexer Disable
  138. cbr @0, (1<<ACME)
  139. sts ADCSRB, @0
  140. .ENDMACRO
  141. .MACRO Set_Comp_Phase_B
  142. lds @0, ADCSRB ; Set Analog Comparator Multiplexer Enable
  143. sbr @0, (1<<ACME)
  144. sts ADCSRB, @0
  145. ldi @0, Mux_B ; Set comparator multiplexer to phase B
  146. sts ADMUX, @0
  147. .ENDMACRO
  148. .MACRO Set_Comp_Phase_C
  149. lds @0, ADCSRB ; Set Analog Comparator Multiplexer Enable
  150. sbr @0, (1<<ACME)
  151. sts ADCSRB, @0
  152. ldi @0, Mux_C ; Set comparator multiplexer to phase C
  153. sts ADMUX, @0
  154. .ENDMACRO
  155. .MACRO Read_Comp_Out
  156. in @0, ACSR ; Read comparator output
  157. .ENDMACRO
  158. ;*********************
  159. ; PORT B definitions *
  160. ;*********************
  161. ;.equ = 7
  162. ;.equ = 6
  163. ;.equ = 5 (sck stk200 interface)
  164. .equ DebugPin = 4 ;(miso stk200 interface)
  165. ;.equ = 3 (mosi stk200 interface)
  166. .equ CnFET = 2 ;o
  167. .equ BnFET = 1 ;o
  168. .equ AnFET = 0 ;o
  169. .equ INIT_PB = 0
  170. .equ DIR_PB = (1<<AnFET)+(1<<BnFET)+(1<<CnFET)+(1<<DebugPin)
  171. .MACRO AnFET_on
  172. sbi PORTB,0
  173. .ENDMACRO
  174. .MACRO AnFET_off
  175. cbi PORTB,0
  176. .ENDMACRO
  177. .MACRO BnFET_on
  178. sbi PORTB,1
  179. .ENDMACRO
  180. .MACRO BnFET_off
  181. cbi PORTB,1
  182. .ENDMACRO
  183. .MACRO CnFET_on
  184. sbi PORTB,2
  185. .ENDMACRO
  186. .MACRO CnFET_off
  187. cbi PORTB,2
  188. .ENDMACRO
  189. .MACRO All_nFETs_Off
  190. in @0, PORTB
  191. cbr @0, (1<<AnFET)+(1<<BnFET)+(1<<CnFET)
  192. out PORTB, @0
  193. .ENDMACRO
  194. ;**********************
  195. ; MCU specific macros *
  196. ;**********************
  197. .MACRO Disable_Watchdog
  198. cli ; Disable interrupts
  199. wdr ; Reset watchdog timer
  200. in @0, MCUSR ; Clear WDRF in MCUSR
  201. andi @0, (0xff & (0<<WDRF))
  202. out MCUSR, @0
  203. lds @0, WDTCSR ; Write logical one to WDCE and WDE
  204. ori @0, (1<<WDCE) | (1<<WDE)
  205. sts WDTCSR, @0
  206. ldi @0, (0<<WDE) ; Turn off WDT
  207. sts WDTCSR, @0
  208. .ENDMACRO
  209. .MACRO Enable_Watchdog
  210. ldi @0, (1<<WDE) ; Turn on WDT
  211. sts WDTCSR, @0
  212. .ENDMACRO
  213. .MACRO Initialize_MCU
  214. ldi @0, (1<<CLKPCE) ; Set clock prescaler change enable
  215. sts CLKPR, @0
  216. ldi @0, 0 ; Change clock prescaler (to divide by 1)
  217. sts CLKPR, @0
  218. .ENDMACRO
  219. .MACRO Interrupt_Table_Definition
  220. rjmp reset
  221. rjmp ext_int0 ; ext_int0
  222. nop ; ext_int1
  223. nop ; pci0_int
  224. nop ; pci1_int
  225. nop ; pci2_int
  226. nop ; wdt_int
  227. nop ; t2oca_int
  228. nop ; t2ocb_int
  229. rjmp t2ovfl_int; t2ovfl_int
  230. rjmp icp1_int ; icp1_int
  231. rjmp t1oca_int ; t1oca_int
  232. nop ; t1ocb_int
  233. rjmp t1ovfl_int; t1ovfl_int
  234. nop ; t0oca_int
  235. nop ; t0ocb_int
  236. rjmp t0ovfl_int; t0ovfl_int
  237. nop ; spi_int
  238. nop ; urxc
  239. nop ; udre
  240. nop ; utxc
  241. ; nop ; adc_int
  242. ; nop ; eep_int
  243. ; nop ; aci_int
  244. ; nop ; wire2_int
  245. ; nop ; spmc_int
  246. .ENDMACRO
  247. .MACRO Initialize_Interrupts
  248. ldi Temp1, (1<<TOIE0)
  249. out TIFR0, Temp1 ; Clear interrupts
  250. sts TIMSK0, Temp1 ; Enable interrupts
  251. ldi Temp1, (1<<TOIE1)+(1<<OCIE1A)
  252. out TIFR1, Temp1 ; Clear interrupts
  253. sts TIMSK1, Temp1 ; Enable interrupts
  254. ldi Temp1, (1<<TOIE2)
  255. out TIFR2, Temp1 ; Clear interrupts
  256. sts TIMSK2, Temp1 ; Enable interrupts
  257. .ENDMACRO
  258. .MACRO Initialize_Adc
  259. lds Temp1, ADCSRA ; Set ADCSRA register (1MHz clock)
  260. sbr Temp1, (1<<ADPS1)
  261. sbr Temp1, (1<<ADPS0)
  262. sts ADCSRA, Temp1
  263. .ENDMACRO
  264. .MACRO Start_Adc
  265. ldi Temp1, (1<<REFS1)+(1<<REFS0)+(1<<MUX2)+(1<<MUX1)+(1<<MUX0)
  266. sts ADMUX, Temp1 ; Set ADMUX register (1.1V reference, left adj result, input 7)
  267. lds @0, ADCSRA
  268. sbr @0, (1<<ADEN) ; Enable ADC
  269. sbr @0, (1<<ADSC) ; Start ADC conversion
  270. sts ADCSRA, @0
  271. .ENDMACRO
  272. .MACRO Get_Adc_Status
  273. lds @0, ADCSRA
  274. .ENDMACRO
  275. .MACRO Read_Adc_Result
  276. lds @0, ADCL
  277. lds @1, ADCH
  278. .ENDMACRO
  279. .MACRO Stop_Adc
  280. lds @0, ADCSRA
  281. cbr @0, (1<<ADEN) ; Disable ADC
  282. sts ADCSRA, @0
  283. .ENDMACRO
  284. .MACRO Set_Timer0_CS0
  285. out TCCR0B, @0
  286. .ENDMACRO
  287. .MACRO Set_Timer1_CS1
  288. sts TCCR1B, @0
  289. .ENDMACRO
  290. .MACRO Set_Timer2_CS2
  291. sts TCCR2B, @0
  292. .ENDMACRO
  293. .MACRO Read_TCNT1L
  294. lds @0, TCNT1L
  295. .ENDMACRO
  296. .MACRO Read_TCNT1H
  297. lds @0, TCNT1H
  298. .ENDMACRO
  299. .MACRO Read_ICR1L
  300. lds @0, ICR1L
  301. .ENDMACRO
  302. .MACRO Read_ICR1H
  303. lds @0, ICR1H
  304. .ENDMACRO
  305. .MACRO Set_OCR1AL
  306. sts OCR1AL, @0
  307. .ENDMACRO
  308. .MACRO Set_OCR1AH
  309. sts OCR1AH, @0
  310. .ENDMACRO
  311. .MACRO Set_TCNT2
  312. sts TCNT2, @0
  313. .ENDMACRO
  314. .MACRO Check_Eeprom_Ready
  315. sbic EECR, EEPE
  316. .ENDMACRO
  317. .MACRO Set_Eeprom_Address
  318. out EEARL, @0
  319. .ENDMACRO
  320. .MACRO Start_Eeprom_Write
  321. sbi EECR, EEMPE
  322. sbi EECR, EEPE
  323. .ENDMACRO