Generating servo pulses with TMR0

I successfully wrote and tested code to generate 50 hz pulses to send position information to servos.   The next step will be to use the interrupt to toggle a pin high for 1-2 ms then back to low for 18-19 ms.

The test software is as follows:

;
;16F876 Micro Serial Communications, 2 servo, pwm airsoft turret
;
;
list p=16F876
include p16f876.inc
cblock 0x20
RX_Word
TW_Word
w_temp
status_temp
dataL
endc
org 00
goto start

org 04
goto interrupt
;initialize
start
bsf status,rp0
bcf status,rp1
;configure Porta pins as digital I/O
movlw 0x06
movwf adcon1
;configure porta as outputs
movlw B’00000000′
movwf trisa
;configure USART in async
bcf status,rp0
bcf status,rp1
movlw B’10010000′  ;async channel:  port is on, 8 bit transfer
movwf rcsta   ;continuous recieving, no address detect
bsf status,rp0
bcf status,rp1
movlw B’00100100′  ;setup async channel:  transmit enable, 8 bit
movwf txsta   ;high speed baud rate
movlw 0x0c   ;setup baudrate of 19200 with 4 meg crystal
movwf spbrg
bcf status,rp0
bcf status,rp1

;configure option_reg for tmr0
bsf status,rp0
bcf option_reg,t0cs
bcf option_reg,t0se
bcf option_reg,psa
bsf option_reg,ps2
bsf option_reg,ps1
bcf option_reg,ps0
bcf status,rp0
;configure single interrupt
bsf intcon,t0ie
bcf intcon,t0if
bsf intcon,gie
movlw D’100′
movwf tmr0

;program start
main
clrf dataL
settle
decfsz dataL,F
goto settle

movf rcreg,w
movf rcreg,w
movf rcreg,w
loop call recieve
movwf txreg
goto loop

recieve btfss pir1,rcif
goto recieve
movf rcreg,w
return

transwt bsf status,rp0
wthere btfss txsta,trmt
goto wthere

clrf status
return

;interrupt service
interrupt
movwf w_temp   ;content saving
swapf status,0
movwf status_temp
movlw d’100′
movwf tmr0
;ADD INTERRUPT CODE HERE
;END INTERRUPT CODE
;content loading
swapf status_temp,0
movwf status
swapf w_temp,1
swapf w_temp,0
;clear interrupt flag
bcf intcon,t0if
retfie
end

 

Leave a Reply

Your email address will not be published. Required fields are marked *