;*************************************
; Author : Mike Baird
; Program : Simple Led PWM
; Date : July 25th,2009
;*************************************
List P=16F628A ; F=inhx8m
#include "P16F628A.INC" ; Include header file
__config _PWRTE_ON & _WDT_OFF & _INTRC_OSC_NOCLKOUT & _BODEN_ON & _LVP_OFF & _CP_OFF & _MCLRE_OFF
;**************************************
Cblock 0x20 ; General purpose registers
Count1 ; For Delay
Count2 ;
Count3 ;
Endc
;**** Start and Reset Vector ****
Org H'00'
GOTO Main ; Reset -> init
;**** Interrupt Vector ****
Org H'04'
Main
CLRF PORTA ; PortA all low
CLRF PORTB ; PortB all low
MOVLW 0x07 ; Turn comparators off and enable pins for I/O
MOVWF CMCON
BSF STATUS,RP0 ; Bank 1
CLRF TRISA ; PortA all output
CLRF TRISB ; PortB all output
BCF STATUS,RP0 ; Bank 0
MOVLW 0xFF
MOVWF PR2 ; Compare with 255 (Step 1)
MOVLW 0xFF ; Not neccesary
MOVWF CCPR1L ; bits 2-10 of 10 bit pwm
MOVLW 0x03 ; b'00000011'
MOVWF T2CON ; Prescaler 1:16 (bits 0-1) and postscaler 1:1 (bits 3-6)
MOVLW 0x3C ; b'00001100'
MOVWF CCP1CON ; Set Pwm Mode (bits 0-3) Bit 4 = CCP1Y (bit 0 of PWM) Least significant bits
; Bit 5 = CCP1X (bit 1 of PWM)
BSF T2CON,TMR2ON ; Enable PWM
; PWM period = [(PR2) + 1] * 4 * Tosc * TMR2 prescale value
; PWM period = [255+1] *4 * 4 x 10^-6 * 16
; PWM period = 4.098x10^-3s or 244Hz
;The following steps should be taken when configuring the CCP module for PWM operation.
;1. Set the PWM period by writing to the PR2 register.
;2. Set the PWM duty cycle by writing to the CCPR1L register and CCP1X and CCP1Y bits of CCP1CON register.
;3. Set the TMR2 prescale value and enable Timer2 by writing to T2CON register.
;4. Configure the CCP1 module for PWM operation.
; **** Main Program ****
FADE
DECFSZ CCPR1L
CALL Delay
GOTO FADE
; **** Delay ***
Delay
MOVLW 0x02
MOVWF Count3
d3
MOVLW 0x3F
MOVWF Count1
d1
MOVLW 0xFA
MOVWF Count2
d2
DECFSZ Count2,F
GOTO d2
; DECFSZ Count1,F
; GOTO d1
; DECFSZ Count3,F
; GOTO d3
RETLW 0x00
;********************
END
; That's all folks!