MOVwf TRISC ; pins 6 and 7 have to be inputs (default)
bcf STATUS, RP0 ; all other pins are inputs, too (default)
; --==*[ CCP1 - setup PWM Module ]*==--
bsf STATUS, RP0
MOVlw d’249’ ; pwm period (calculated with formula)
MOVwf PR2
bcf STATUS, RP0 ; duty cycle = 0
clrf CCPR1L ; CCPR1L is upper 8 (bit: 9...2) bits of duty cycle
MOVlw b’00000101’ ; enable timer2 (bit: 2) and set prescale of 1:4 (bit: 0-1)
MOVwf T2CON ; timer2 is important for pwm operation!
MOVlw b’00001111’ ; select pwm mode for ccp1 module (bit: 0-3)
MOVwf CCP1CON ; and reset lower two bits of duty cycle (bit: 4-5)
; --==*[ USART - setup ]*==--
bsf STATUS, RP0
MOVlw 0x40 ; configure baud generator register (calculated with formula)
MOVwf SPBRG ; with: 9600 baud, no parity, 8 data bits, no handshake
MOVlw b’00100100’ ; enable trasmit (bit: 5) and high speed baud rate (bit: 2)
MOVwf TXSTA
bcf STATUS, RP0
MOVlw b’10010000’ ; enable serial port (bit: 7) and continuous reception (bit: 4)
MOVwf RCSTA
clrw ; w = 0
MOVwf RCREG ; reset UART receiver and fifo
MOVwf RCREG ; so we CAN avoid receive/framing/overrun errors at the beginning
MOVwf RCREG
MOVwf TXREG ; just in case: the txif flag is now valid (=1; avoids infinite loops in sendByte)
; --==*[ TIMER 0 - setup ]*==--
bsf STATUS, RP0 ; this is tricky; prescaler has to be assigned to the WDT,
; in case you want to achieve 1:1 prescale
bcf OPTION_REG, PS0 ; first, set prescaler to 1:2
bcf OPTION_REG, PS1
bcf OPTION_REG, PS2
bsf OPTION_REG, PSA ; then, assign prescaler to wdt; now we have a 1:1 prescale for timer0 :-)
bcf OPTION_REG, T0SE
bcf OPTION_REG, T0CS
bcf STATUS, RP0
; --==*[ INTERRUPTS - setup ]*==--
bsf STATUS, RP0
clrf PIE1
bsf PIE1, RCIE ; enable "receive byte" interrupt
bcf STATUS, RP0
clrf INTCON ; reset all interrupt flags
bsf INTCON, RBIE ; enable "interrupt on change" interrupt
bsf INTCON, T0IE ; enable "timer0 overflow" interrupt
bsf INTCON, PEIE ; enable peripheral interrupts
bsf INTCON, GIE ; enable global interrupts
; --==*[ MAIN LOOP ]*==--
上一页 [1] [2] [3] [4] [5] [6] 下一页
本文关键字:步进电机 程序 PIC单片机,单片机-工控设备 - PIC单片机