;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; DOA_air_core_0_0_2.asm ;; ;; ;; ;; part of the PHCC (PIC HomeCockpit Controller) Project ;; ;; Copyright (c) 2004 by Manuel Bessler ;; ;; ;; This is the 4x Air core motor controller that consists ;; ;; of a PIC, 4 air core motors and 4 L293D drivers. ;; ;; ;; ;; Copyright (c) 2008 by Danny Faber ;; ;; credits go to Mike Powell. ;; ;; ;; ;; The full text of the legal notices is contained in the file called ;; ;; COPYING, included with this distribution. ;; ;; ;; ;; This program is free software; you can redistribute it and/or ;; ;; modify it under the terms of the GNU General Public License ;; ;; as published by the Free Software Foundation; either version 2 ;; ;; of the License, or (at your option) any later version. ;; ;; ;; ;; This program is distributed in the hope that it will be useful, ;; ;; but WITHOUT ANY WARRANTY; without even the implied warranty of ;; ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ;; ;; GNU General Public License for more details. ;; ;; ;; ;; You should have received a copy of the GNU General Public License ;; ;; along with this program; if not, write to the Free Software ;; ;; Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. ;; ;; ;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;uncomment the processor you use ; LIST P=18F2220, R=DEC ; what the PIC we use, decimal system as default ; LIST P=18F252, R=DEC ; what the PIC we use, decimal system as default LIST P=18F242, R=DEC ; what the PIC we use, decimal system as default IFDEF __18F2220 #include "p18f2220.inc" ; include appropriate processor definitions ; set config bits: __CONFIG _CONFIG1H, _IESO_OFF_1H & _FSCM_OFF_1H & _HSPLL_OSC_1H __CONFIG _CONFIG2L, _PWRT_ON_2L & _BOR_ON_2L & _BORV_20_2L __CONFIG _CONFIG2H, _WDT_OFF_2H & _WDTPS_32K_2H __CONFIG _CONFIG3H, _MCLRE_ON_3H & _PBAD_DIG_3H & _CCP2MX_C1_3H __CONFIG _CONFIG4L, _DEBUG_OFF_4L & _LVP_OFF_4L & _STVR_OFF_4L __CONFIG _CONFIG5L, _CP0_OFF_5L & _CP1_OFF_5L __CONFIG _CONFIG5H, _CPB_OFF_5H & _CPD_OFF_5H __CONFIG _CONFIG6L, _WRT0_OFF_6L & _WRT1_OFF_6L __CONFIG _CONFIG6H, _WRTC_OFF_6H & _WRTB_OFF_6H & _WRTD_OFF_6H __CONFIG _CONFIG7L, _EBTR0_OFF_7L & _EBTR1_OFF_7L __CONFIG _CONFIG7H, _EBTRB_OFF_7H ENDIF IFDEF __18F252 #include "p18f252.inc" ; include appropriate processor definitions ; set config bits: __CONFIG _CONFIG1H, _OSCS_OFF_1H & _HSPLL_OSC_1H __CONFIG _CONFIG2L, _PWRT_ON_2L & _BOR_ON_2L & _BORV_20_2L __CONFIG _CONFIG2H, _WDT_OFF_2H & _WDTPS_32_2H __CONFIG _CONFIG3H, _CCP2MX_OFF_3H __CONFIG _CONFIG4L, _STVR_ON_4L & _LVP_OFF_4L & _DEBUG_OFF_4L __CONFIG _CONFIG5L, _CP0_OFF_5L & _CP1_OFF_5L & _CP2_OFF_5L & _CP3_OFF_5L __CONFIG _CONFIG5H, _CPB_OFF_5H & _CPD_OFF_5H __CONFIG _CONFIG6L, _WRT0_OFF_6L & _WRT1_OFF_6L & _WRT2_OFF_6L & _WRT3_OFF_6L __CONFIG _CONFIG6H, _WRTC_OFF_6H & _WRTB_OFF_6H & _WRTD_OFF_6H __CONFIG _CONFIG7L, _EBTR0_OFF_7L & _EBTR1_OFF_7L & _EBTR2_OFF_7L & _EBTR3_OFF_7L __CONFIG _CONFIG7H, _EBTRB_OFF_7H ENDIF IFDEF __18F242 #include "p18f242.inc" ; include appropriate processor definitions ; set config bits: __CONFIG _CONFIG1H, _OSCS_OFF_1H & _HSPLL_OSC_1H __CONFIG _CONFIG2L, _PWRT_ON_2L & _BOR_ON_2L & _BORV_20_2L __CONFIG _CONFIG2H, _WDT_OFF_2H & _WDTPS_32_2H __CONFIG _CONFIG3H, _CCP2MX_OFF_3H __CONFIG _CONFIG4L, _STVR_ON_4L & _LVP_OFF_4L & _DEBUG_OFF_4L __CONFIG _CONFIG5L, _CP0_OFF_5L & _CP1_OFF_5L & _CP2_OFF_5L & _CP3_OFF_5L __CONFIG _CONFIG5H, _CPB_OFF_5H & _CPD_OFF_5H __CONFIG _CONFIG6L, _WRT0_OFF_6L & _WRT1_OFF_6L & _WRT2_OFF_6L & _WRT3_OFF_6L __CONFIG _CONFIG6H, _WRTC_OFF_6H & _WRTB_OFF_6H & _WRTD_OFF_6H __CONFIG _CONFIG7L, _EBTR0_OFF_7L & _EBTR1_OFF_7L & _EBTR2_OFF_7L & _EBTR3_OFF_7L __CONFIG _CONFIG7H, _EBTRB_OFF_7H ENDIF ; @40Mhz ; device address ;#define DEVADDR 0x80 #define DO_A_CLK PORTB, 0 ;; Digital-In Type A Clock line #define DO_A_DATA PORTB, 1 ;; Digital-In Type A Data line #define DO_A_CLK_TRIS TRISB, 0 ;; direction register setup #define DO_A_DATA_TRIS TRISB, 1 ;; direction register setup #define STATE_ADDR 0x00 #define STATE_SUBADDR 0x01 #define STATE_DATA 0x02 #define STAGE_COUNT0 0x00 #define STAGE_COUNT1 0x01 #define STAGE_COUNT2 0x02 #define MOTOR1A PORTA, 1 #define MOTOR1B PORTA, 0 #define MOTOR1C PORTA, 4 #define MOTOR1D PORTA, 3 #define MOTOR1EN PORTA, 2 #define MOTOR2A PORTC, 0 #define MOTOR2B PORTA, 5 #define MOTOR2C PORTC, 3 #define MOTOR2D PORTC, 2 #define MOTOR2EN PORTC, 1 #define MOTOR3A PORTC, 5 #define MOTOR3B PORTC, 4 #define MOTOR3C PORTB, 2 #define MOTOR3D PORTC, 7 #define MOTOR3EN PORTC, 6 #define MOTOR4A PORTB, 4 #define MOTOR4B PORTB, 3 #define MOTOR4C PORTB, 6 #define MOTOR4D PORTB, 7 #define MOTOR4EN PORTB, 5 #define FREQTIMESFOUR 10 ;for delays, speciefies the multiple of 4 MHz we're running at. eg. for 16MHz this would be 4, for 40MHz it would be 10 ; clock: 40MHz ; Tinstr: 10MHz = 10,000,000 instr./sec ; 1ms = 10,000 instr./msec ; 1 instr = 0.1usec = 100ns CBLOCK 0x0 Wsave, BSRsave, STATUSsave ap2pp_state, ap2pp_bitcounter, ap2pp_addr, ap2pp_subaddr, ap2pp_data, ap2pp_tmp jmptmp aircore aircore0, aircore1, aircore2, aircore3 aircore0_quad, aircore1_quad, aircore2_quad, aircore3_quad stage0, stage1, stage2, stage3 count00, count01, count02 count10, count11, count12 count20, count21, count22 count30, count31, count32 sin0, cos0 sin1, cos1 sin2, cos2 sin3, cos3 temp ;temp variable ENDC ;================================================================================== ;****PROGRAM START**** ;================================================================================== org 0x0000 goto INITIALIZE ;================================================================================== ;****INTERRUPT ROUTINES**** ;================================================================================== org 0x0008 ;****INTERRUPT ENTRY POINT**** movwf Wsave ;save registers movff STATUS, STATUSsave movff BSR, BSRsave ;portb pin change btfsc INTCON, INT0IF ;was it a pos edge trigger pin RB0? call DOAINT ;timer0 btfsc INTCON, TMR0IF call TMR0INT ;timer1 btfsc PIR1, TMR1IF call TMR1INT ;timer2 btfsc PIR1, TMR2IF call TMR2INT ;timer3 btfsc PIR2, TMR3IF call TMR3INT bra ENDINTERRUPT ;non-handled interrupt? then go to end ENDINTERRUPT movff BSRsave, BSR ;restore addessing movf Wsave, W movff STATUSsave, STATUS retfie ;================================================================================== ;****END OF INTERRUPT ROUTINES**** ;================================================================================== INITIALIZE clrf PORTA ;set portA pins low clrf PORTB ;set portB pins low clrf PORTC ;set PORTC pins low movlw 0x07 ;0x07 => all ANx ports digital movwf ADCON1 IFNDEF __18F242 IFNDEF __18F252 movlw 0x07 movwf CMCON ;all comparators off ENDIF ENDIF movlw b'00000000' ;all PORTA & PORTC pins output movwf TRISA movwf TRISC movlw b'00000011' ;PORTB mixed input & output movwf TRISB bsf MOTOR1EN bsf MOTOR2EN bsf MOTOR3EN bsf MOTOR4EN SETUPTMR0 movlw b'01000011' movwf T0CON bcf INTCON, TMR0IF bsf INTCON, TMR0IE SETUPTMR1 movlw b'10110100' ;16bit access, (load H first, then L which triggers load from buffer to real register) ; other clk source, ; 1:8 prescaler (11), ; t1osc disabled, ; don't care, ; src is Fosc/4, ; timer not yet started movwf T1CON ;set up timer1 bcf PIR1, TMR1IF ;clear timer1 interrupt flag bsf PIE1, TMR1IE ;enable timer1 interrupt SETUPTMR2 movlw b'00000010' ; timer movwf T2CON ;set up timer2 bcf PIR1, TMR2IF ;clear timer2 interrupt flag bsf PIE1, TMR2IE ;enable timer2 interrupt SETUPTMR3 movlw b'11110000' movwf T3CON bcf PIR2, TMR3IF bsf PIE2, TMR3IE SETUPDOA call SETUPAP2PP ;get ready to receive via DOA bsf T1CON, TMR1ON ;start timer1 SETUPINT bcf RCON, IPEN ;don't use interrupt priorities bsf INTCON, GIE ;globally enable ints bsf INTCON, PEIE ;enable peripheral ints ;================================================================================== ;****END OF INITIALIZE ROUTINE**** ;================================================================================== MAIN ;Run LOOP NOP NOP NOP bra MAIN ;================================================================================== ;****UPDATE ROUTINE**** ;================================================================================== UPDATEAIRCORE0 bcf T0CON, TMR0ON GETSINCOS0 comf aircore0, w call SINCOSTBL movwf sin0 movf aircore0, w call SINCOSTBL movwf cos0 SETCOUNTS0 comf cos0, w btfss STATUS, Z goto TEST01 movf sin0, w btfss STATUS, Z goto CASE01 CASE00 ;cos0=255 && sin0=0 movlw 1 movwf count00 movlw 1 movwf count01 movlw 255 movwf count02 goto SETSTAGE0 CASE01 ;cos0=255 && sin0!=0 movlw 1 movwf count00 movf sin0, w movwf count01 sublw 255 movwf count02 goto SETSTAGE0 TEST01 comf sin0, w btfsc STATUS, Z goto TEST02 CASE02 ;0