Otsisin välja oma vana robotiprojekti ja üle pika aja leidsin aega vähe PICutada. Oma paar kolm tundi läks, et olemasolevast vanast koodist taas aru saada (NB! Selgitustel on ka iseenda jaoks ja iseenda programmis suur mõte!!! ) Aga probleem nüüd selline, et Taimerid ei taha interrupte tekitada... Igaks juhuks panin kogu faili siia, natuke jurakas ta ju on, kuid ehk hakkab midagi seal silma, kus ise ei kahtlustagi...
Kood:
#include "p16f876a.inc" LIST P=16f876a errorlevel -302 ;Suppress bank warning __CONFIG _WDT_OFF & _XT_OSC & _CP_OFF & _LVP_OFF cblock 0x20 ; local variables, only in bank 0 R_Motor ; temp address to know the motor position L_Motor COUNTER1 ; Counters for COUNTER2 ; Stepper direction MOTOR_DIR1 ; Temp files for motor direction MOTOR_DIR2 ; W_Temp ; S_Temp ; TempL ; TempR ; Right_Speed ; Left_Speed endc ;************************************************************ ;* Makrod * ;************************************************************ BANK0 MACRO BCF STATUS,RP1 BCF STATUS,RP0 ENDM BANK1 MACRO BCF STATUS,RP1 BSF STATUS,RP0 ENDM BANK2 MACRO BSF STATUS,RP1 BCF STATUS,RP0 ENDM BANK3 MACRO BSF STATUS,RP1 BSF STATUS,RP0 ENDM DRIVE macro mdir1, mdir2, rspd, lspd movlw mdir1 movwf MOTOR_DIR1 movlw mdir2 movwf MOTOR_DIR2 movlw rspd movwf Right_Speed movlw lspd movwf Left_Speed endm ;************************************* ;**** Hakkame aga pihta ************** ;************************************* org 0x00 ; reset vector goto Start ;=================================================================== ; Interruptid ;=================================================================== Interrupt org 0x04 ; interrupt vector movwf W_Temp ; salvestame hetkeollukorra swapf STATUS,W movwf S_Temp bcf STATUS,RP0 Disable_Int bcf INTCON,GIE ; keelame globaalselt ära interruptid btfsc INTCON,T0IF ; kas TIMER0 lipp on püsti? goto Right_Step ; On küll! btfsc PIR1,TMR2IF ; kas TIMER2 lipp on püsti? goto Left_Step ; On küll! Pop swapf S_Temp,W ; taastame enneinterruptiaegse olukorra movwf STATUS swapf W_Temp,f swapf W_Temp,w retfie ;****The main program***** Start CLRF PORTB ; Initialize Port B CLRW BANK1 ; Select Register Bank 1 MOVWF TRISB ; Set B to all outputs CLRF TMR0 ; Clear Timer0 register CLRF INTCON ; Disable interrupts and clear T0IF BANK1 MOVLW 0xC3 ; PortB pull-ups are disabled, MOVWF OPTION_REG ; Interrupt on rising edge of RB0 ; Timer0 increment from internal clock ; with a prescaler of 1:16. BANK0 CLRF T2CON ; Stop Timer2, Prescaler = 1:1, ; Postscaler = 1:1 CLRF TMR2 ; Clear Timer2 register BANK1 CLRF PIE1 ; Disable peripheral interrupts BANK0 CLRF PIR1 ; Clear peripheral interrupts Flags MOVLW 0x72 ; Postscaler = 1:15, Prescaler = 1:16 MOVWF T2CON ; Timer2 is off BSF T2CON, TMR2ON ; Timer2 starts to increment BSF INTCON, T0IE ; Enable TMR0 interrupt BSF INTCON, GIE ; Enable all interrupts MAIN DRIVE 1, 0, 0xC8, 0xC8 goto MAIN ;=============================================== ; Mootorite astumised ;=============================================== Right_Step movlw Right_Speed ; loeme etteantud ajaväärtuse TIMER0 jaoks movwf TMR0 ; omistame väärtuse TIMER0-le bcf INTCON,T0IF ; nullime TIMER0 interrupt flägi swapf PORTB,w ; loeme mootori seisu, keerame tulemuse ringi andlw b'11110000' ; nullime selle mootori bitid, teisi ei puudu movwf TempL ; seivime call R_Motor_Pos ; vaatame, mis on mootori A uus seis movf R_Motor,W ; Loeme mootori A uue seisu iorwf TempL,F ; valmis juhtsõna mootorite jaoks movf TempL,W movwf PORTB ; tulemus mootorite porti return Left_Step movf Left_Speed,w; loeme etteantud ajaväärtuse TIMER2 jaoks movwf TMR2 ; omistame väärtuse TIMER2-le bcf PIR1,TMR2IF ; nullime TIMER2 interrupt flägi movf PORTB,w ; loeme mootori seisu andlw b'11110000' ; nullime selle mootori bitid, teisi ei puudu movwf TempR ; seivime call L_Motor_Pos ; vaatame, mis on mootori B uus seis movf L_Motor,W ; loeme mootori B uue seisu iorwf TempR,F ; valmis juhtsõna mootorite jaoks movf TempR,W movwf PORTB ; tulemus mootorite porti return R_Motor_Pos: btfsc MOTOR_DIR1,0; Kas liigume edasi või tagasi? incf COUNTER1,f ; Liigume edasi! btfss MOTOR_DIR1,0 decf COUNTER1,f ; Liigume tagasi! movf COUNTER1,w call GET_R_MOTOR_POSITION; pöördume tabeli poole movwf R_Motor ; salvestame tulemuse faili return GET_R_MOTOR_POSITION: andlw b'00000111' addwf PCL,F retlw 0x8 retlw 0xA retlw 0x2 retlw 0x6 retlw 0x4 retlw 0x5 retlw 0x1 retlw 0x9 L_Motor_Pos btfsc MOTOR_DIR2,0; Kas liigume edasi või tagasi? incf COUNTER2,f ;Liigume edasi! btfss MOTOR_DIR2,0 decf COUNTER2,f ;Liigume tagasi! movf COUNTER2,w call GET_L_MOTOR_POSITION; pöördume tabeli poole movwf L_Motor ; salvestame tulemuse faili return GET_L_MOTOR_POSITION: andlw b'00000111' addwf PCL,F retlw 0x8 retlw 0xA retlw 0x2 retlw 0x6 retlw 0x4 retlw 0x5 retlw 0x1 retlw 0x9 END
Comment