Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Delay 1s by NOP
; clock frequency is 40MHz, delay 1s = 200x250x200x4x25nS delay1s movlw D'200' movwf delay_cnt1,A loop1 movlw D'250' movwf delay_cnt2,A loop2 movlw D'19 movwf delay_cnt3,A loop3 nop decfsz delay_cnt3,F, A goto loop3 decfsz delay_cnt2,F, A goto loop2 decfsz delay_cnt1,F,A goto loop1 return
A0066078X
Delay 10mS by interrupt
; Creates a 10 ms delay using timer 0 with f OSC= 32 MHz. wait10ms bcf INTCON,INT0IE,A ; disable TMR0 interrupt bcf INTCON,INT0IF,A ; clear TMR0IF flag movlw 0x03 ; configure TMR0 with 1:16 prescaler movwf T0CON,A ; configure TMR0 movlw 0xEC movwf TMR0H,A movlw 0x83 movwf TMR0L,A ; load 60547 into TMR0 ; TMR0 will overflow in (5000-12) counts bsf T0CON,TMR0ON,A ;enable TMR0 dlyloop btfss INTCON,INT0IF,A ;is 10 ms over yet? goto dlyloop return
Interrupt frequency =
Instruction frequency =
= ,
Brake motor
brake bsf movlw movwf call bcf return PORTB,RB4,A ; reverse the applied voltage 0x00 . CCPRlL,A ; set PWMl duty cycle to 0 brake-time ; wait for brake time PORTB,RB4,A ; stop braking
= 4,
Frequency ratio = = = 32000 = (124 + 1) 16 16 movlw D124 ; load 124 into PR2 so that TMR2 counts movwf PR2,A ; up to 124 and reset bsf RCON,IPEN,A ; enable priority interrupt bsf IPR1,TMR2IP,A ; place TMR2 interrupt at high priority bcf PIR1,TMR2IF,A ; clear TMR2 interrupt flag movlw 0xC0 movwf INTCON, ; enable global interrupt movlw 0x7E ; enable TMR2, set prescaler to 16, movwf T2CON,A ; set postscaler to 16 bsf PIE1,TMR2IE,A ; enable TMR2 overflow interrupt
loop
div
Use Timer0 as a timer to create a one-second delay and use Timer1 as a counter to count the rising (or falling) edges of an unknown signal (at the T1CKI pin)
t1ov_cnt set 0x00 ; Timer1 rollover interrupt count freq set 0x01 ; to save the contents of Timer1 at the end org 0x00 goto start org0x08 btfss PIR1,TMR1IF,A ; skip if Timer1 roll-over interrupt retfie ; return if not Timer1 interrupt bcf PIR1, TMR1IF,A ; clear the interrupt flag incf t1ov_cnt,F,A ; increment Timer1 roll-over count retfie org0x18 retfie start clrf t1ov_cnt, A ; initialize Timer1 overflow cnt to 0 clrf freq,A clrf freq+1,A ; initialize frequency to 0 clrf TMR1H clrf TMR1L ; initialize Timer1 to 0 clrf PIR1 ; clear all interrupt flags bsf RCON,IPEN,A ; enable priority interrupt movlw 0x01 movwf IPR1,A ; set TMR1 interrupt to high priority movwf PIE1,A ; enable Timer1 roll-over interrupt movlw 0x87 ; enable Timer1, select external clock, set movwf T1CON,A ; prescaler to 1, disable crystal oscillator movlw 0xC0 movwf INTCON,A ; enable global and peripheral interrupt movlw 0x0A movwf PRODL,A ; prepare to call delay to wait for 1 second call delay ; Timer1 overflow interrupt occur in this second movff TMR1L,freq ; save frequency low byte movff TMR1H,freq+1 ; save frequency high byte bcfINTCON,GIE,A ; disable global interrupt forever nop bra forever end
Use the CCP channel 1 in capture mode to measure the period of an unknown signal
org 0x00 goto start org 0x08 retfie org0x18 retfie start bsf TRISC,CCP1,A ; configure CCP1 pin for input movlw 0x81 movwf T3CON,A ; use TMR1 as the time base of CCP1 capture bcf PIE1,CCP1IE,A ; disable CCP1 capture interrupt movlw 0x81 ; enable Timer1, prescaler set to 1, movwf T1CON,A ; 16-bit TMR, use instruction cycle clock movlw 0x05 movwf CCP1CON,A ; set CCP1 to capture on every edge bcf PIR1,CCP1IF,A ; clear the CCP1IF flag edge1 btfss PIR1,CCP1IF,A ; wait for the first edge to arrive bra edge1 movff CCPR1H,PRODH movff CCPR1L,PRODL ; save the1st edge movff CCPR1L,PRODL bcf PIR1,CCP1IF,A ; clear the CCP1IF flag edge2 btfss PIR1,CCP1IF,A ; wait for the second edge to arrive edge2 bra clrf CCP1CON ; disable CCP1 capture movf PRODL,W,A subwf CCPR1L,W,A ; subtract first edge from 2nd edge movwf PRODL,A ; and leave the period in PRODH:PRODL movf PRODH,W,A subwfb CCPR1H,W,A movwf PRODH,A forever goto forever end
Lin Shaodun
A0066078X
Convert onvert a Binary Bi number to Gray code, start from LSB, if next bit is zero, copy the value, if next bit is one, toggle the value. Convert Gray code to Binary number number, , start from LSB, count no. n of ones from next to end, if its odd number , toggle the value, if its even, copy the the. Number of hamming code needed: 1 2 , hamming code position: 2 , 2 , 2 , 2 ... P0 =even {M3, M2, M0}, P1 =even {M3, M1, M0},