; ----------------------------------------------------------------------------- ; 300 bytes brushless motor control ; ; Copyright (c) Matthias Kramm, 2008 ; ; Permission is hereby granted, free of charge, to any person obtaining a copy ; of this software and associated documentation files (the "Software"), to deal ; in the Software without restriction, including without limitation the rights ; to use, copy, modify, merge, publish, distribute, sublicense, and/or sell ; copies of the Software, and to permit persons to whom the Software is ; furnished to do so, subject to the following condition: ; ; THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR ; IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, ; FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE ; AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER ; LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, ; OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN ; THE SOFTWARE. ; ----------------------------------------------------------------------------- ; This code demonstrates sensorless commutation of a brushless dc motor, as ; well as twi bus communication and current limiting (emergency shutdown). ; It's been designed to work with the "mikrokopter" motor control hardware, ; which can be obtained from http://www.mikrokopter.de/ ; (In particular, the TWI communication is compatible with the communication ; protocol employed by the flight control used on mikrokopters, and ; the microcontroller is assumed to be an atmel atmega8) .include "atmega8.inc" ; ------------- registers ------------------- .def TMP1 = r16 .def TMP2 = r17 .def WAITFORLEVELSWITCH = r24 .def ZERO = r0 .def EIGHT = r1 .def DELAY = r26 .def CURRENT = r28 .def PWM_MAX = r29 ; right now always 255 .def PHASE = r18 .def STEPPTRL = r30 .def STEPPTRH = r31 ; irlr mosfets ;.equ CURRENT_MAX1 = 120 ;.equ CURRENT_MAX2 = 200 ; standard mosfets .equ CURRENT_MAX1 = 65 .equ CURRENT_MAX2 = 130 ; ------------- interrupts ------------------- ; atmega8 .org 0 ; program start address ldi r29, 0xff out SPL, r29 ; init stack clr ZERO out SPH, ZERO rjmp init ; lookup table for motor commutation steps: .db 0x08,0x10,2,32, 0x08,0x20,1,0, 0x04,0x20,0,32, 0x04,0x08,2,0, 0x02,0x08,1,32, 0x02,0x10,0,0 .org 0x11 ; twi twiint: in r20, SREG slaveint: ldi r19, (1< 0xd0 mean that either there's a bus error (twsr=0x00,0xf8) ; or we accidently switched into a master transmitter/receiver mode. ; Send a stop to free/reset the line. twerror:ori r19, (1<