Date post: | 25-Nov-2015 |
Category: |
Documents |
Upload: | law-zhan-hong |
View: | 100 times |
Download: | 2 times |
Lin Shaodun A0066078X
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
Use PWM mode, output to CCP1. The value to be written to PR2:16M/4/16/2K-1=124 The value to be written to CCPR1L (and CCPR1H) =125x60%=75 start movlw 0x7C ;set period value to 124 movwf PR2, A movlw 0x4B ; set duty cycle value to 75 movwf CCPR1L, A movwf CCPR1H, A bcf TRISC,2,A ; configure CCP1 pin for output movlw 0x81 ; enable Timer 3 in 16-bit mode and use movwf T3CON, A ; Timer 2 as time base for PWM1 thru PWM5 clrf TMR2, A ; force TMR2 to count from 0 movlw 0x07 ; enable Timer 2 and set its prescaler to 16 movwf T2CON, A movlw 0x0C ;enable CCP1 PWM mode movwf CCP1CON, A
Counting with interrupt and output to PORTD #include
count_1 set 0x10 ; counter outputs to lower 4 bit of PORTD
count_2 set 0x11 ; counter outputs to higher 4 bit of PORTD
output set 0x12 ; combined output to PORTD
org 0x00 goto start
org 0x08 goto int0; interrupt vector
retfie org 0x18 retfie
start clrf count_1, A ; initialize the counter 1 to 0
clrf count_2, A ; initialize the counter 2 to 0
movlw 0x00 ; config PORTD pins for output
movwf TRISD ;
call update ; call update subroutine
bsf RCON, IPEN, A ; enable priority interrupt
bsf INTCON, GIE ; enable global interrupt
bsf INTCON, INT0IE ; enable INT0 interrupt
bsf INTCON3, INT1IE ; enable INT1 interrupt
bsf INTCON2, INTEDG0 ; set edge triggering for INT0
bcf INTCON2, INTEDG1 ; set edge triggering for INT1
bcf INTCON,INT0IF ; clear INT0 flag
bcf INTCON,INT1IF ; clear INT1 flag
forever goto forever
; INT0 interrupt service routine
int0 btfsc INTCON3,INT1IF ; check if INT1 interrupt trigged
goto int1 ; goto int1 routine if INT1 trigged
bcf INTCON,INT0IF ; clear the INT0 interrupt flag bit
movlw 0x0F
cpfseq count_1, A ; was count_1 equal to 15 already?
goto count_1_add_1 ; goto count_1_add_1 if count_1 < 15
clrf count_1, A ; reset count_1 to 0 after reaching 15
goto count_1_update
count_1_add_1 incf count_1, F, A;[count_1]=[count_1]+1
count_1_update call update; call update subroutine retfie fast
;INT1 interrupt service routine
int1 bcf INTCON3,INT1IF ; clear the INT1 interrupt flag bit
movlw 0x0F
cpfseq count_2, A ; was count_2 equal to 15 already?
goto count_2_add_1
clrf count_2,A ; reset count_2 to 0 after reaching 15
goto count_2_update
count_2_add_1 incf count_2, F, A ; [count_2]=[count_2]+1
count_2_update call update ; call update subroutine retfie fast
update movff count_2,output ; [output]=[count_2]
swapf output,F,A; swap nibbles of [output] register
movf count_1, W, A ; WREG = [count_1]
xorwf output, F, A ; [output]=[count_1]XOR[output]
movff output, PORTD ; sent combined output to port D
return end
Interrupt frequency =
= ,
Instruction frequency =
= 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
Add 32 8-bit numbers, squared and averaged no_of_loop set 0x30 ;loop index
sum_temp1 set 0x31 ;temporary data register for sum calculation
sum_temp2 set 0x32 ;temporary data register for sum calculation
sum_temp3 se t0x33 ;temporary data register for sum calculation
org 0x00 goto start
org 0x08 retfie org 0x18 retfie
start movlw D32 ; WREG = array size (32)
movwf no_of_loop ;set number of loops equal to array size
movlw upper array movwf TBLPTRU,A
movlw high array movwf TBLPTRH,A
movlw low array movwf TBLPTRL,A
loop TBLRD*+ ; read an array element into TABLAT
movf TABLAT,W,A ;WREG = array element
mulwf TABLAT,A ;square the array element
movf PRODL,W,A ;WREG = lower byte of squared number
addwf sum_temp1,F,A ; sum_temp1 = sum_temp1 + lower byte
movf PRODH,W,A; WREG = upper byte of squared number
addwfc sum_temp2,F,A; sum_temp2 = sum_temp2 + upper byte
BNC next ; branch if no carry
incf sum_temp3,F,A ; sum_temp3 = sum_temp3 + 1 (from carry)
next decfsz no_of_loop,F,A
bra loop
movlw 0x05 ; set loop count to 5 (25=32)
div bcf STATUS, C, A ; clear the C flag
rrcf sum_temp3, F, A ; shift the bits to right 1 place
rrcf sum_temp2, F, A rrcf sum_temp1, F, A
decfsz WREG, W, A ; have we shifted right five places yet?
bra div ; not yet, continue
movff sum_temp2,0x22 movff sum_temp1,0x21 end
Brake motor brake bsf PORTB,RB4,A ; reverse the applied voltage movlw 0x00 . movwf CCPRlL,A ; set PWMl duty cycle to 0 call brake-time ; wait for brake time bcf PORTB,RB4,A ; stop braking return
Display in 7-segment LED with 1s delay In 100 ms, =1x106 instructions. Using prescaler 16, 1x106=16x62500. 65535-62500=3035. org 0x00 goto start org 0x08 retfie org 0x18 retfie start clrf TRISD, A ; configure port D for output movlw 0x04 ; 4 digits to be displayed movwf loop_cnt movlw upper display_tab movwf TBLPTRU, A movlw high display_tab movwf TBLPTRH, A movlw low display_tab movwf TBLPTRL, A loop tblrd*+ movff TABLAT, PORTD call wait_1s ; wait for 1 second decfsz loop_cnt, F, A ; decrement the loop count goto loop forever goto forever wait_1s movlw 0x83 ;enable TMR0, select internal clock movwf T0CON, A ;set prescaler to 16 movlw 0x0A ;load 10 into PRODL movwf PRODL, A loopd movlw 0x0B ;load 3035 into TMR0, it will roll over in 62500 clock cycles movwf TMR0H, A movlw 0xDB movwf TMR0L, A bcf INTCON, TMR01F, A ;clear TMR0IF flag wait btfss INTCON, TMR01F, A ;wait until 100 ms is over bra wait decfsz PRODL, F, A bra loopd ;repeat 10 times to get 1s delay return display_tab db 0x79, 0x6D, 0x30, 0x7E end
Scan Keypad , return value in WREG keypad_dir equ TRISB ; keypad direction control register
keypad equ PORTB ; keypad port
get_key movlw 0x0F ; configure the upper four pins of keypad
movwf keypad_dir,A ; port for output, others for input
movlw 0xF0 iorwf keypad, F ; set all keypad rows to high
scan_r0 movlw 0xEF andwf keypad,F,A ; prepare to scan row 0 (driven by RB4)
scan_k0 btfss keypad,0,A ; check key 0 goto db_key0
scan_k1 btfss keypad,1,A ; check key 1 goto db_key1
scan_k2 btfss keypad,2,A ; check key 2 goto db_key2
scan_k3 btfss keypad,3,A ; check key 3 goto db_key3
scan_r1 movlw 0xFF iolwf keypad,F,A bcf keypad,5 ; prep to scan row 1 (by RB5)
scan_k4 btfss keypad,0,A ; check key 4 goto db_key4
scan_k5 btfss keypad,1,A ; check key 5 goto db_key5
scan_k6 btfss keypad,2,A ; check key 6 goto db_key6
scan_k7 btfss keypad,3,A ; check key 7 goto db_key7
db_key0 call wait10ms btfsc keypad,0,A ; is key 0 still closed?
goto scan_k1; key 0 not pressed, check key 1 movlw 0x30 return
db_key1 call wait10ms btfsc keypad, 1, A ; is key 1 still closed?
goto scan_k2 movlw0x31 return
db_key2 call wait10ms btfsc keypad,2,A ; is key 2 still closed?
goto scan_k3 movlw0x32 return
db_key3 call wait10ms btfsc keypad,3,A
goto scan_r1 movlw0x33
db_key4 call wait10ms btfsc keypad,0,A goto scan_k1; movlw 0x34 return
db_key5 call wait10ms btfsc keypad,1,A goto scan_k2 movlw0x35 return
db_key6 call wait10ms btfsc keypad,2,A goto scan_k3 movlw0x36 return
db_key7 call wait10ms btfsc keypad,3,A goto scan_r2 movlw0x37 return
return
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
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
Enable A/D interrupt Bsf RCON,IPEN ; enable priority interrupt Bsf IPR1,ADIP ; enable A/D to high priority Bcf PIR1,ADIF ; clear ADIF flag Bsf PIE1,ADIE ; enable A/D interrupt movlw 0xC0 movwf INTCON ; enable global and peripheral interrupts
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 bra edge2 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
Instruction cycle = 16MHz/4 = 4MHz = 2.5 x 10-7 s = 250 ns Waveform = 1/ 2kHz = 5 x 10-4 s 60% of duty cycle = (5 x 10-4) x 0.6 = 3 x 10-4 s No. of cycle = 3 x 10-4 /2.5 x 10-7 = 1200 40% of waveform = 2 x 10-4 s No. of cycle = 2 x 10-4 /2.5 x 10-7 = 800 hi_lo equ 0xB0 ; number (1200) of clock cycles hi_hi equ 0x04 ; that signal is high lo_lo equ 0x20 ; number (800) of clock cycles lo_hi equ 0x03 ; that signal is low bcf TRISC, CCP1, A ; configure CCP1 for output movlw 0xC9 movwf T3CON ; configure TMR3 to 16 bit ; prescaler 1, TMR3 as time base, enable bcf PIR1, CCP1IF, A ; clear CCP1 flag bit movlw 0x09 ; load B0000 1001 to CCP1CON movwf CCP1CON ; CCP1 set high initially and pull low on match ; CCPR1=TMR3 + 1200 movlw hi_lo addwf TMR3L, W movwf CCPR1L movlw hi_hi addwfc TMR3H,W movwf CCPR1H bcf PIR1, CCP1IF, A ; clear CCP1 flag bit hi_time btfss PIR1, CCP1IF, A bra hi_time bcf PIR1, CCP1IF, A movlw 0x08 ; load B0000 1000 to CCP1CON movwf CCP1CON ; CCP1 set low initially and pull high on match ; CCPR1 =CCPR1 + 800 movlw lo_lo addwf CCPR1L, F movlw lo_hi addwfc CCPR1H, F lo_time btfss PIR1, CCP1IF, A bra lo_time bcf PIR1, CCP1IF, A movlw 0x09 movwf CCP1CON ; CCP1 set high initially and pull low on match movlw hi_lo addwf CCPR1L, F movlw hi_hi addwfc CCPR1H, F bra hi_time end
Lin Shaodun
Convert a Bi
Convert Gray code
Number of
Lin Shaodun
onvert a Binary number to Gray code, start from LSB, if next bit is zero, copy the value, if next bit is one, toggle the value.
Gray code to Binary number
Number of hamming code needed:
to Gray code, start from LSB, if next bit is zero, copy the value, if next bit is one, toggle the value.
Binary number, start from LSB,
hamming code needed:
to Gray code, start from LSB, if next bit is zero, copy the value, if next bit is one, toggle the value.
, start from LSB, count n
1 2 , hamming code position:
Binary to Gray code
to Gray code, start from LSB, if next bit is zero, copy the value, if next bit is one, toggle the value.
count no. of ones from next to end, if its odd number , toggle the value, if its even, copy the
, hamming code position:
Binary to Gray code Gray code
to Gray code, start from LSB, if next bit is zero, copy the value, if next bit is one, toggle the value.
from next to end, if its odd number , toggle the value, if its even, copy the
, hamming code position: 2, 2, 2,
Gray code to binary code
to Gray code, start from LSB, if next bit is zero, copy the value, if next bit is one, toggle the value.
from next to end, if its odd number , toggle the value, if its even, copy the
2... P0 =even {M
to binary code
to Gray code, start from LSB, if next bit is zero, copy the value, if next bit is one, toggle the value.
from next to end, if its odd number , toggle the value, if its even, copy the
{M3, M2, M0}, P1 =even
A0066078X
from next to end, if its odd number , toggle the value, if its even, copy the
=even {M3, M1, M0
A0066078X
from next to end, if its odd number , toggle the value, if its even, copy the.
0},