Need help with PWM on K8048 and PIC16F627 Please

Hey guys, I have been playing with this board and have decided on a project I would like to make, this project however entails the use of PWM so that I can DIM the L.E.D outputs, can anybody please help me with some simple explanation or code that I can use to dim an LED using this method.

Anything would be appreciated because I really cna’t understand the information given about it in the datasheet or how i get the PWM output from the L.E.D I/O connections.

Thanks

Ok, I managed to get a PWM output with a duty cycle of 25% and so it lights the LED dimly as i would like.

The code is as follows:

Warning, it is extremely sloppy code, it’s a mash of code i found in the datasheets and half of it I don’t understand.

What I ask is that if someone could explain to me how I can vary the duty cycle while the program is running, basically to fade the LED in and out, I tried a basic test by changing the duty cycle variable on input of the switches but it has no effect.

Anything would be appreciated, thanks.

[code];*************************************************
;* ;*
;* OSC…: XT 4MHz Max. POWER…: 12V DC
;******************************************************

list      p=16f627
#include <p16f627.inc>

__CONFIG        _BODEN_ON & _CP_OFF & _DATA_CP_OFF & _PWRTE_ON & _WDT_OFF & _LVP_OFF & _MCLRE_ON & _INTRC_OSC_NOCLKOUT

;_XT_OSC

;==========================================================================
; Variable Definition
;==========================================================================
;INPUTS
SW1 EQU H’00’ ;SW1 is triggering RA0
SW2 EQU H’01’ ;SW2 is triggering RA1
SW3 EQU H’02’ ;SW3 is triggering RA2
;SW4 EQU H’03’ ;SW4 is triggering RA3
TIMER1 EQU H’20’ ;Used in delay routine
TIMER2 EQU H’21’ ; " " "
PATERN EQU H’22’ ;Pattern data for effect’s
PWM1 EQU H’03’ ;RA3 is the PWM1 output.

	ORG	0		;Reset vector address
	GOTO	RESET		;goto RESET routine when boot.

; *********************************************
; * Example of a delay routine *
; *********************************************

;DELAY_ROUTINE MOVLW D’100’ ;54 Generate approx 10mS delay at 4Mhz CLK
; MOVWF TIMER2
;DEL_LOOP1 MOVLW D’100’ ;60
; MOVWF TIMER1
;DEL_LOOP2 BTFSC PORTA,SW1
; GOTO MENU
; BTFSC PORTA,SW2
; GOTO MENU
; BTFSC PORTA,SW3
; GOTO MENU
; ;BTFSC PORTA,SW4
; ;GOTO MENU
; DECFSZ TIMER1,F
; GOTO DEL_LOOP2
; DECFSZ TIMER2,F
; GOTO DEL_LOOP1
; RETLW 0

; **********************************
; ** RESET : main boot routine **
; **********************************

RESET ;MOVLW B’00000111’ ;Disable Comparator module’s
;MOVWF CMCON
;
BSF STATUS,RP0 ;Switch to register bank 1
;Disable pull-ups
;INT on rising edge
;TMR0 to CLKOUT
;TMR0 Incr low2high trans.
;Prescaler assign to Timer0
;Prescaler rate is 1:256
MOVLW B’11010111’ ;Set PIC options (See datasheet).
MOVWF OPTION_REG ;Write the OPTION register.
;
CLRF INTCON ;Disable interrupts
MOVLW B’11000000’
MOVWF TRISB ;RB7 & RB6 are inputs.
;RB5…RB0 are outputs.
MOVLW B’10111111’ ;all RA ports are inputs
MOVWF TRISA
BCF STATUS,RP0 ;Switch Back to reg. Bank 0
CLRF PORTB
; GOTO EFFECT_4

;===================================================================
; PWM Stuff
;===================================================================
TESTY
CLRF CCP1CON ; CCP Module is off
CLRF TMR2 ; Clear Timer2
MOVLW 0x7F ;
MOVWF PR2 ;

CONTINUE
; MOVLW B’00000011’ ; WAS 0x1F
; MOVWF CCPR1L ; Duty Cycle is 25% of PWM Period
CLRF INTCON ; Disable interrupts and clear T0IF
BSF STATUS, RP0 ; Bank1
BCF TRISA, PWM1 ; Make pin output
CLRF PIE1 ; Disable peripheral interrupts
BCF STATUS, RP0 ; Bank0
CLRF PIR1 ; Clear peripheral interrupts Flags
MOVLW 0x2C ; PWM mode, 2 LSbs of Duty cycle = 10
MOVWF CCP1CON ;
BSF T2CON, TMR2ON ; Timer2 starts to increment
;
; The CCP1 interrupt is disabled,
; do polling on the TMR2 Interrupt flag bit
;

PWM_Period_Match
BTFSS PIR1, TMR2IF
GOTO PWM_Period_Match
;
; Update this PWM period and the following PWM Duty cycle
;
BCF PIR1, TMR2IF

; GOTO TESTY

;====================================================================
; END PWM Stuff
;====================================================================

;------------------------------------
MENU CLRF PORTB

	BTFSC	PORTA,SW1
	MOVLW B'00000011' ; Literal
	MOVWF CCPR1L ; Duty Cycle
	GOTO	CONTINUE
	BTFSC	PORTA,SW2
	MOVLW B'00001111' ; Literal
	MOVWF CCPR1L ; Duty Cycle
	GOTO	CONTINUE
	BTFSC	PORTA,SW3
	MOVLW B'11111111' ; Literal
	MOVWF CCPR1L ; Duty Cycle
	GOTO	CONTINUE
	;BTFSC	PORTA,SW4
	;GOTO	EFFECT_4
	GOTO	TESTY

;------------------------------------
END
[/code]

Hello Neo,

Thanks for sharing the code, i was also looking at this PWM stuff.
But i tryed to assemble you code but could not get it work.

can you send me the HEX file, than i can test it to and maybe we can come up with something.

regards

Ronald

Hi,
Perhaps you can also get some help on the Microchip forum http://forum.microchip.com/
A lot of similar questions/discussions are going on over there. :smiley:

I have been using mikrobasic for some time now. It has some good library routines one of which is PWM
Here is an example which will output to PORTB 3 the output for the PWM module on the PIC16F627 or 28
it will run on the K8048 board

program PWMtest2

dim j as byte

main:
j = 0
PORTB = $FF ’ Initialize PORTB
Pwm_Init(5000) ’ Initialize PWM module, freq = 5kHz.
Pwm_Start ’ Start PWM

while true
for j = 0 to 255
Delay_ms(50)

Pwm_Change_Duty(j)  ' Change duty ratio
next j

wend
end.

It makes using PICS so much easier.