16f628a pwm routine

Thread Starter

Dodgydave

Joined Jun 22, 2012
11,303
Trying to set up a pwm routine on this pic16f628a, has anyone got a working asm routine. I have one for the16f690, but the files are different. Prefer files is in assembly language.
 
Last edited:

ErnieM

Joined Apr 24, 2011
8,377
Microchip's AN1074 is a complete project to slowly change an RGB LED thru all colors. It contains an assembly module to generate 3 PWM signals in assembly.

The routine mostly lives inside an ISR so all you need do is set the PWM constants and they are automatically controlled.

It's based on the PIC12HV615 chip, which is a fairly baseline device meaning you probably have whatever resources it needs, such as Timer0.
 

Thread Starter

Dodgydave

Joined Jun 22, 2012
11,303
This is what i have so far, it works but ramps up/dn very slowly, the delay dosent seem to make any difference, what ever time i set it to, is there some thing in the Timer2 thats not set?

Rich (BB code):
LIST P=16F628;f=inhx8m
#include "P16F628.INC" ; Include header file
__CONFIG _PWRTE_ON & _WDT_OFF & _INTRC_OSC_NOCLKOUT & _BODEN_ON & _LVP_OFF & _CP_OFF & _MCLRE_OFF
;------------------------------------------------------------
errorlevel -302 ; suppress banksel warning messages during assembly
errorlevel -311 ; suppress HIGH operator warning messages during assembly
 
cblock 0x20 ; Beginn General Purpose-Register
;-------------------------- counters 
d1
d2
d3
;--------------------------
endc
;--------------------------
;--- Reset --------------------------------------------------
org h'00'
goto init ; reset -> init
;--- Interrupt ----------------------------------------------
org h'04'
 
 
 
init;clrf PORTA
;clrf PORTB
movlw 0x07 ; Turn comparators off and enable pins for I/O 
movwf CMCON 
bcf STATUS,RP1
bsf STATUS,RP0 ; Bank 1
movlw 0xFF ; all input
movwf TRISA
movlw 0x00
movwf TRISB ; all output
bcf STATUS,RP0 ; Bank 0 
call pause
movlw 0xFF
movwf PORTB
call pause ; wait 
 
;---------------------
clrf T2CON
clrf TMR2
clrf INTCON
bsf STATUS,RP0
clrf PIE1
bcf STATUS,RP0
clrf PIR1
bsf STATUS,RP0
movlw 0xFF
movwf PR2 ; compare with 255
bcf STATUS,RP0
movlw b'00000011'
movwf T2CON ; prescaler 1:16 and postscaler 1:1
movlw 0x3C
movwf CCP1CON
;************************************************************
Start 
movlw .0
movwf CCPR1L
call pause
incfsz CCPR1L,f
goto $-2
call pause
movlw .255
movwf CCPR1L
call pause
decfsz CCPR1L,f
goto $-2
goto Start ; repeat
 
;************************************************************************
; 
;************************************************************************
pause
 
;199998 cycles 2msec
movlw .100
movwf d1
movlw .3
movwf d2
Delay_0
decfsz d1, f
goto $+2
decfsz d2, f
goto Delay_0
 
return
end
 
Last edited:

Markd77

Joined Sep 7, 2009
2,806
The ramping speed is entirely determined by the delay, changing d1 won't make much difference to the delay, and setting d2 to 0 makes the delay as long as possible, setting it to 1 would be around the shortest delay.
 

Thread Starter

Dodgydave

Joined Jun 22, 2012
11,303
Fixed it, it was the prescaler setting in Tmr2 not set i have highlighted it
should be movlw b'00000111' ...Doh!!!
 
Top