0% found this document useful (0 votes)
5 views3 pages

pwm

The document contains C code for controlling two PWM (Pulse Width Modulation) channels on a microcontroller. It includes functions to initialize the PWM channels, set their duty cycles, and start or stop the PWM signals. The code defines macros for pin control and sets up Timer2 for PWM operation.

Uploaded by

technosanky
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views3 pages

pwm

The document contains C code for controlling two PWM (Pulse Width Modulation) channels on a microcontroller. It includes functions to initialize the PWM channels, set their duty cycles, and start or stop the PWM signals. The code defines macros for pin control and sets up Timer2 for PWM operation.

Uploaded by

technosanky
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

/*

* File: pwm.c
* Author: Sanket
*/

#include "pwm.h"

#define PWM_1_DIR (TRISCbits.TRISC2)


#define PWM_1_PIN (PORTCbits.RC2)
#define PWM_1_ON() ((PWM_1_PIN) =
(BIT_SET))
#define PWM_1_OFF() ((PWM_1_PIN) =
(BIT_CLR))
#define PWM_1_CHK (PORTCbits.RC2)

#define PWM_2_DIR (TRISCbits.TRISC1)


#define PWM_2_PIN (PORTCbits.RC1)
#define PWM_2_ON() ((PWM_2_PIN) =
(BIT_SET))
#define PWM_2_OFF() ((PWM_2_PIN) =
(BIT_CLR))
#define PWM_2_CHK (PORTCbits.RC1)

/*
* Global variables
*/

/*
* Private function prototypes
*/

/*
* Function definitions
*/
void init_pwm_1(void) {
// Set PWM 1 pin direction as Output
PWM_1_DIR = DIR_OP;
PWM_1_OFF();

// Disable the Timer2.


T2CONbits.TMR2ON = BIT_CLR;

// Set Timer2 Pre-scaler 1:1


T2CONbits.T2CKPS = 0b00;

// Set Timer2 Post-scaler 1:1


T2CONbits.TOUTPS = 0b0000;

// Configure the Timer2 period


PR2 = 0xFF;

// Initialize the PWM to 0% duty cycle


CCP1CONbits.DC1B0 = BIT_CLR;
CCP1CONbits.DC1B1 = BIT_CLR;
CCPR1L = 0x00;

// Configure CCP1 module in PWM mode


CCP1CONbits.CCP1M = 0b1100;
// Enable the Timer2, hence enable the PWM.
T2CONbits.TMR2ON = BIT_SET;
}

void pwm_1_set_duty_cycle(const uint16_t duty_cycle) {


// Put MSB 8 bits in CCPR1L
CCPR1L = duty_cycle >> 2;

// Make bit 4 and 5 zero


CCP1CONbits.DC1B0 = BIT_CLR;
CCP1CONbits.DC1B1 = BIT_CLR;

// Assign Last 2 LSBs to CCP1CON


CCP1CON |= (0x30 & (duty_cycle << 4));
}

void init_pwm_2(void) {
// Set PWM 2 pin direction as Output
PWM_2_DIR = DIR_OP;
PWM_2_OFF();

// Disable the Timer2.


T2CONbits.TMR2ON = BIT_CLR;

// Set Timer2 Pre-scaler 1:1


T2CONbits.T2CKPS = 0b00;

// Set Timer2 Post-scaler 1:1


T2CONbits.TOUTPS = 0b0000;

// Configure the Timer2 period


PR2 = 0xFF;

// Initialize the PWM to 0% duty cycle


CCP2CONbits.DC2B0 = BIT_CLR;
CCP2CONbits.DC2B1 = BIT_CLR;
CCPR2L = 0x00;

// Configure CCP2 module in PWM mode


CCP2CONbits.CCP2M = 0b1100;
}

void pwm_2_start(void) {
// Enable the Timer2, hence enable the PWM.
T2CONbits.TMR2ON = BIT_SET;
}

void pwm_2_stop(void) {
// Disable the Timer2, hence disable the PWM.
T2CONbits.TMR2ON = BIT_CLR;
}

void pwm_2_set_duty_cycle(const uint16_t duty_cycle) {


// Put MSB 8 bits in CCPR2L
CCPR2L = duty_cycle >> 2;

// Make bit 4 and 5 zero


CCP2CONbits.DC2B0 = BIT_CLR;
CCP2CONbits.DC2B1 = BIT_CLR;

// Assign Last 2 LSBs to CCP2CON


CCP2CON |= (0x30 & (duty_cycle << 4));
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy