Need Help With Pwm In PIC16F628A


#define XTAL_FREQ 20MHZ /* Crystal frequency in MHz */
#include <pic16f628a.h>
#include <xc.h>
#include "delay.h"

#pragma config FOSC = HS // Oscillator Selection bits (XT oscillator: Crystal/resonator on RA6/OSC2/CLKOUT and RA7/OSC1/CLKIN)
#pragma config WDTE = OFF // Watchdog Timer Enable bit (WDT disabled)
#pragma config PWRTE = OFF // Power-up Timer Enable bit (PWRT disabled)
#pragma config MCLRE = ON // RA5/MCLR/VPP Pin Function Select bit (RA5/MCLR/VPP pin function is MCLR)
#pragma config BOREN = OFF // Brown-out Detect Enable bit (BOD disabled)
#pragma config LVP = OFF // Low-Voltage Programming Enable bit (RB4/PGM pin has digital I/O function, HV on MCLR must be used for programming)
#pragma config CPD = OFF // Data EE Memory Code Protection bit (Data memory code protection off)
#pragma config CP = OFF // Flash Program Memory Code Protection bit (Code protection off)

#define r1 RA0
#define r2 RA1
#define ll1 RA2
#define l2 RA3
#define buzzer RB7
#define t1 RB4
#define t2 RB5
int pattern,off;
void System_init(void) {
TRISA = 0X00;
TRISB = 0b00000011;
CCP1CON = 0b00001100; 
PR2 = 249; 
T2CON = 0b00000100; 
void main(void) {
while (1) {
if (pattern == 1) {
r1 = ~r1;
r2 = ~r2;
l1 = 0;
ll2 = 0;


if (pattern == 2) {

CCPR1L= 50;
CCPR1L= 240;

this is my PWM program in PIC16F628A (see datasheet). Everything working fine
But when controller running pattern=1 also i’m getting output from PWM…

I don’t know what is the problem with my code.

Hi @yumi22820,

One thing you can be sure of, the MCU is doing exactly what it is told, byte for byte! Looking at page 61 of the datasheet, to fully disable PWM you need to set CCP1M3:CCP1M0 to 0000

There are other ways to do it (as always), but the best thing is to go step by step through your code and ensure you are doing everything needed.

1 Like