⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 motor_control_module.asm

📁 这个开发机器人项目源码
💻 ASM
📖 第 1 页 / 共 2 页
字号:
 goto FinishISRI2CState2SMBusState5            ; SMBus state 5 (3rd data byte in block) clrf SMBusState goto FinishISRI2CState2SMBusState6            ; SMBus state 6 (4th data byte in block) clrf SMBusState goto FinishISRI2CState2SMBusState7            ; SMBus state 7 (5th data byte in block) clrf SMBusState goto FinishISRI2CState2SMBusState8            ; SMBus state 8 (2nd data byte / write word) movf SMBusCommand,w sublw WRITE_PWM_COMMAND btfss STATUS,Z goto NotWritePWM2 movf I2CRxByte,w movwf CCPR1L movf I2CDataBuffer0,w  movwf CCPR2L clrf SMBusState goto FinishISRNotWritePWM2 clrf SMBusState goto FinishISRI2CNotState2 movlw b'00001100'              ; I2C state 3 (Read, last byte address)? subwf SspStatSaved,w btfss STATUS,Z goto I2CNotState3              ; No; Read, last byte address (I2C State 3) movlw HIGH($+4) movwf PCLATH movf SMBusState,w addwf PCL,f                    ; SMBus state jump table goto I2CState3SMBusState0 goto I2CState3SMBusState1 goto I2CState3SMBusState2 goto I2CState3SMBusState3 goto I2CState3SMBusState4 goto I2CState3SMBusState5 goto I2CState3SMBusState6 goto I2CState3SMBusState7 goto I2CState3SMBusState8 IF ((HIGH($)) != (HIGH($-10)))     ERROR("Table crosses page boundary!") ENDIFI2CState3SMBusState0 clrf SSPBUF                    ; Shouldn't be here, but the master is  bsf SSPCON,CKP                 ; expecting data, so we must oblige. goto FinishISR                 I2CState3SMBusState1            ; SMBus state 1 (transferring data byte) movf SMBusCommand,w sublw GET_OUTPUT_STATE_COMMAND btfss STATUS,Z goto NotGetOutputState; Get Output State movf OutputState,w movwf SSPBUF bsf SSPCON,CKP clrf SMBusState goto FinishISRNotGetOutputState movf SMBusCommand,w sublw GET_ENCODER_MASK_COMMAND btfss STATUS,Z goto NotGetEncoderMask; Get Encoder Mask movf EncoderMask,w movwf SSPBUF bsf SSPCON,CKP clrf SMBusState goto FinishISRNotGetEncoderMask  movf SMBusCommand,w sublw GET_TIMER_COMMAND btfss STATUS,Z goto NotGetTimer; Get Timer movf Timer,w movwf SSPBUF bsf SSPCON,CKP clrf SMBusState goto FinishISRNotGetTimer movf SMBusCommand,w sublw GET_PWM_COMMAND btfss STATUS,Z goto NotGetPWMCommand; Get PWM Command movf CCPR2L,w                  ; Left duty cycle movwf SSPBUF                   ; Send it bsf SSPCON,CKP movlw 8                        ; SMBus state 8 next movwf SMBusState goto FinishISRNotGetPWMCommand clrf SSPBUF                    ; Shouldn't be here, but the master is  bsf SSPCON,CKP                 ; expecting data, so we must oblige. goto FinishISR                 I2CState3SMBusState2            ; SMBus state 2 (num bytes) movf SMBusCommand,w sublw GET_ODOMETRY_COMMAND btfss STATUS,Z goto NotGetOdometry3; Get number of data bytes movlw 5 movwf SSPBUF bsf SSPCON,CKP movlw 3 movwf SMBusState               ; 1st data byte next goto FinishISRNotGetOdometry3 clrf SSPBUF                    ; Shouldn't be here, but the master is  bsf SSPCON,CKP                 ; expecting data, so we must oblige. goto FinishISR                 I2CState3SMBusState3            ; SMBus state 3 (1st data - block read) clrf SSPBUF                    ; Shouldn't be here, but the master is  bsf SSPCON,CKP                 ; expecting data, so we must oblige. goto FinishISR                 I2CState3SMBusState4            ; SMBus state 4 (2nd data - block read) clrf SSPBUF                    ; Shouldn't be here, but the master is  bsf SSPCON,CKP                 ; expecting data, so we must oblige. goto FinishISR                 I2CState3SMBusState5            ; SMBus state 5 (3rd data - block read) clrf SSPBUF                    ; Shouldn't be here, but the master is  bsf SSPCON,CKP                 ; expecting data, so we must oblige. goto FinishISR                 I2CState3SMBusState6            ; SMBus state 6 (4th data - block read) clrf SSPBUF                    ; Shouldn't be here, but the master is  bsf SSPCON,CKP                 ; expecting data, so we must oblige. goto FinishISR                 I2CState3SMBusState7            ; SMBus state 7 (5th data - block read) clrf SSPBUF                    ; Shouldn't be here, but the master is  bsf SSPCON,CKP                 ; expecting data, so we must oblige. goto FinishISR                 I2CState3SMBusState8            ; SMBus state 8 (2nd data - write word) clrf SSPBUF                    ; Shouldn't be here, but the master is  bsf SSPCON,CKP                 ; expecting data, so we must oblige. goto FinishISR                 I2CNotState3 movlw b'00101100'              ; I2C state 4 (Read, last byte data)? subwf SspStatSaved,w btfss STATUS,Z goto I2CNotState4              ; No; Read, last byte data (I2C State 4) movlw HIGH($+4) movwf PCLATH movf SMBusState,w addwf PCL,f                    ; SMBus state jump table goto I2CState4SMBusState0 goto I2CState4SMBusState1 goto I2CState4SMBusState2 goto I2CState4SMBusState3 goto I2CState4SMBusState4 goto I2CState4SMBusState5 goto I2CState4SMBusState6 goto I2CState4SMBusState7 goto I2CState4SMBusState8 IF ((HIGH($)) != (HIGH($-10)))     ERROR("Table crosses page boundary!") ENDIFI2CState4SMBusState0 clrf SSPBUF                    ; Shouldn't be here, but the master is  bsf SSPCON,CKP                 ; expecting data, so we must oblige. goto FinishISR                 I2CState4SMBusState1 clrf SSPBUF                    ; Shouldn't be here, but the master is  bsf SSPCON,CKP                 ; expecting data, so we must oblige. goto FinishISR                 I2CState4SMBusState2 clrf SSPBUF                    ; Shouldn't be here, but the master is  bsf SSPCON,CKP                 ; expecting data, so we must oblige. goto FinishISR                 I2CState4SMBusState3            ; SMBus state 3 (1st data - block read) movf LeftCounterLSB,w movwf SSPBUF movf LeftCounterMSB,w movwf I2CDataBuffer1 movf RightCounterLSB,w movwf I2CDataBuffer2 movf RightCounterMSB,w movwf I2CDataBuffer3 movf Timer,w movwf I2CDataBuffer4 bsf SSPCON,CKP movlw 4 movwf SMBusState goto FinishISRI2CState4SMBusState4            ; SMBus state 4 (2nd data - block read) movf I2CDataBuffer1,w movwf SSPBUF bsf SSPCON,CKP movlw 5 movwf SMBusState goto FinishISR                 I2CState4SMBusState5            ; SMBus state 5 (3rd data - block read) movf I2CDataBuffer2,w movwf SSPBUF bsf SSPCON,CKP movlw 6 movwf SMBusState goto FinishISR                 I2CState4SMBusState6            ; SMBus state 6 (4th data - block read) movf I2CDataBuffer3,w movwf SSPBUF bsf SSPCON,CKP movlw 7 movwf SMBusState goto FinishISR                 I2CState4SMBusState7            ; SMBus state 7 (5th data - block read) movf I2CDataBuffer4,w movwf SSPBUF bsf SSPCON,CKP clrf SMBusState goto FinishISR                 I2CState4SMBusState8 movf SMBusCommand,w sublw GET_PWM_COMMAND btfss STATUS,Z goto NotGetPWMCommand4; Get PWM Command movf CCPR1L,w                  ; Right duty cycle movwf SSPBUF                   ; Send it bsf SSPCON,CKP clrf SMBusState goto FinishISRNotGetPWMCommand4 clrf SSPBUF                    ; Shouldn't be here, but the master is  bsf SSPCON,CKP                 ; expecting data, so we must oblige. goto FinishISR                 I2CNotState4 movlw b'00101000'              ; Nack received from master after reading data? subwf SspStatSaved,w btfss STATUS,Z goto FinishISR                 ; No. Impossible state.; Nack received from master (I2C State 5) clrf SMBusState                ; SMBus state is expecting command nextFinishISR retfie; ----------------------------------------------------------------------; Main Program start pointMainStart call InitIO                    ; Initialize IO ports call InitTimer                 ; Initialize Timer call InitI2C                   ; Initialize the I2C interface call InitPWM                   ; Initialize CCP1 and CCP2 clrf OutputState               ; PWM output state clrf Timer                     ; Up counter clrf LeftCounterLSB            ; Left wheel odometer count least sig. byte clrf LeftCounterMSB            ; Left wheel odometer count most sig. byte clrf RightCounterLSB           ; Right wheel odometer count least sig. byte clrf RightCounterMSB           ; Right wheel odometer count most sig. byte call GetEncoderMaskEEPROM movwf EncoderMask              ; XOR encoder mask byte from EEPROM call InitInterrupts            ; Enable interrupts goto $                         ; All work done in ISR; ----------------------------------------------------------------------; Subroutines; ----------------------------------------------------------------------; Global interrupts are assumed to be disabled before this subroutine is calledWriteEncoderMaskEEPROM bsf STATUS,RP1 bcf STATUS,RP0 movwf EEDATA^0x100 movlw ENCODER_MASK_EE_ADDR movwf EEADR^0x100 bsf STATUS,RP0 bcf EECON1^0x180,EEPGD bsf EECON1^0x180,WREN movlw 0x055 movwf EECON2^0x180 movlw 0x0AA movwf EECON2^0x180 bsf EECON1^0x180,WR btfsc EECON1^0x180,WR goto $-1 bcf EECON1^0x180,WREN bcf STATUS,RP0 bcf STATUS,RP1 return; ----------------------------------------------------------------------GetEncoderMaskEEPROM bsf STATUS,RP1 bcf STATUS,RP0 movlw ENCODER_MASK_EE_ADDR movwf EEADR^0x100 bsf STATUS,RP0 bcf EECON1^0x180,EEPGD bsf EECON1^0x180,RD bcf STATUS,RP0 movf EEDATA^0x100,w bcf STATUS,RP1 return; ----------------------------------------------------------------------InitInterrupts; Enable interrupts.  bsf STATUS,RP0 bcf STATUS,RP1 bsf PIE1^0x080,SSPIE           ; Enable I2C interrupt bsf OPTION_REG^0x080,INTEDG    ; RBO/INT rising edge interrupts bsf PIE1^0x080,TMR1IE          ; Enable timer 1 overflow interrupt bcf STATUS,RP0 bsf INTCON,PEIE                ; Enable unmasked peripheral interrupts bsf INTCON,INTE                ; Enable interrupt on RB0/INT pin  bsf INTCON,RBIE                ; Enable Port B Change interrupt bsf INTCON,GIE                 ; Global Interrupt Enable return; ----------------------------------------------------------------------InitPWM movlw 128 movwf CCPR1L movwf CCPR2L                   ; Initial PWM 50% return; ----------------------------------------------------------------------InitTimer; Configure timer 1 movlw b'00000001' movwf T1CON; Configure timer 2 bsf STATUS,RP0 bcf STATUS,RP1 movlw 255 movwf PR2^0x080 bcf STATUS,RP0 movlw b'00000100' movwf T2CON                    ; No prescaler or postscaler return; ----------------------------------------------------------------------InitIO; Set port A, B and C data directions movlw b'00001000'              ; 'brake' high, 'pwm' low movwf PORTA clrf PORTB clrf PORTC bsf STATUS,RP0 bcf STATUS,RP1 movlw b'00000110' movwf ADCON1^0x80              ; Not using ADC - set to I/O clrf TRISA^0x080 movlw b'10000001' movwf TRISB^0x080  movlw b'01111000' movwf TRISC^0x080 bcf STATUS,RP0 return; ----------------------------------------------------------------------InitI2C clrf SMBusState bcf SSPCON,SSPM3               ; SSPM<3:0> = 0110 bsf SSPCON,SSPM2               ; (7-bit address slave mode) bsf SSPCON,SSPM1 bcf SSPCON,SSPM0 bsf SSPCON,CKP                 ; SCK release control: Enable Clock bcf SSPCON,SSPOV               ; Make sure overflow bit is clear movf SSPBUF,w                  ; Read SSPBUF to ensure buffer flag BF is clear movlw I2C_ADDRESS << 1 bsf STATUS,RP0 bcf STATUS,RP1 movwf SSPADD^0x80              ; I2C slave address of this device bcf STATUS,RP0 bsf SSPCON,SSPEN               ; Enable I2C return; ----------------------------------------------------------------------; End end

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -