;Brain for Robot 4 - Pradeep K. Shima
;scratch base ripped from Tutorial 1.2 by Nigel Goodwin 2002
LIST p=16F628 ;tell assembler what chip we are using
include "P16F628.inc" ;include the defaults for the chip
__config 0x3D38 ;sets the configuration settings (oscillator type etc.)
cblock 0x20 ;start of general purpose registers
count1 ;used in delay routine
counta ;used in delay routine
countb ;used in delay routine
endc
org 0x0000 ;org sets the origin, 0x0000 for the 16F628,
;this is where the program starts running
movlw 0x07
movwf CMCON ;turn comparators off (make it like a 16F84)
bsf STATUS, RP0 ;select bank 1
movlw b'00000000' ;set PortB all outputs
movwf TRISB
movwf TRISA ;set PortA all outputs
movlw d'25'
movwf PR2 ;PWM with freq~=38kHz (see [URL="https://www.micro-examples.com/public/microex-navig/doc/097-pwm-calculator.html"]https://www.micro-examples.com/public/microex-navig/doc/097-pwm-calculator.html[/URL])
bcf STATUS, RP0 ;select bank 0
movlw d'5' ;duty ratio = 20%
movwf CCPR1L
movlw d'4' ;Timer2 ON + 1:1 prescale
movwf T2CON
movlw d'12' ;set CCP module to PWM mode
movwf CCP1CON
Main
movlw 0x00
movwf PORTA ;set all bits on
movwf PORTB
call Delay ;this waits for a while!
movlw 0x00
movwf PORTA
movwf PORTB ;set all bits off
call Delay
goto Main ;go back and do it again
Delay movlw d'250' ;delay 250 ms (4 MHz clock)
movwf count1
d1 movlw 0xC7
movwf counta
movlw 0x01
movwf countb
Delay_0
decfsz counta, f
goto $+2
decfsz countb, f
goto Delay_0
decfsz count1 ,f
goto d1
retlw 0x00
end