use eccp in pic16f1503
- User
use eccp in pic16f1503
- Assistant
use eccp in pic16f1503
Sure! I'd be happy to help you with using the ECCP (Enhanced Capture Compare PWM) module in the PIC16F1503 microcontroller.
Overview
The PIC16F1503 microcontroller features an Enhanced CCP (ECCP) module which can be configured to operate in various modes, including:
In this guide, we'll focus on using the ECCP module in PWM mode to generate a PWM signal.
Ensure the system clock is set to the desired frequency. The PWM frequency and resolution depend on the oscillator frequency.
// Example: Set internal oscillator to 8MHz
OSCCONbits.IRCF = 0b1110; // IRCF<3:0> = 1110 for 8MHz
OSCCONbits.SCS = 0b10; // Select internal oscillator
The ECCP module output is on the CCP1 pin (RC5). Configure this pin as a digital output.
TRISCbits.TRISC5 = 0; // Set RC5 as output
ANSELCbits.ANSELC5 = 0; // Disable analog function on RC5, if applicable
The PWM period is determined by the Timer2 period. Configure Timer2 as follows:
// Example: Configure Timer2 for a specific PWM frequency
T2CONbits.T2CKPS = 0b00; // Prescaler = 1
PR2 = [PR2_Value]; // Set PR2 to achieve the desired PWM period
T2CONbits.TMR2ON = 1; // Turn on Timer2
Set the ECCP module to PWM mode and configure the duty cycle.
// Set ECCP Module to PWM Mode
CCP1CONbits.CCP1M = 0b1100; // CCP1M<3:0> = 1100 for PWM mode
// Set Initial Duty Cycle
CCPR1L = [DutyCycle_HighByte]; // Upper 8 bits of duty cycle
CCP1CONbits.DC1B = [DutyCycle_LowBits]; // Lower 2 bits of duty cycle
PWM Period Calculation:
The PWM period is calculated using:
[ \text{PWM Period} = \left( PR2 + 1 \right) \times 4 \times T_{OSC} \times \text{(Timer2 Prescaler)} ]
Duty Cycle Calculation:
The PWM duty cycle is set using the 10-bit value formed by CCPR1L and DC1B<1:0> bits in CCP1CON.
[ \text{PWM Duty Cycle} = \frac{{(CCPR1L \times 4) + DC1B<1:0>}}{{4 \times (PR2 + 1)}} ]
Assuming an 8MHz internal oscillator.
First, calculate the PR2 value needed for a 1 kHz PWM frequency.
Calculate ( T_{OSC} ):
[ T_{OSC} = \frac{1}{8,\text{MHz}} = 125,\text{ns} ]
Assume Timer2 Prescaler = 16 (T2CKPS = 0b11):
Calculate PR2:
[ \text{PWM Period} = \left( PR2 + 1 \right) \times 4 \times T_{OSC} \times \text{Prescaler} ]
Rearranged:
[ PR2 = \frac{\text{PWM Period}}{{4 \times T_{OSC} \times \text{Prescaler}}} - 1 ]
[ PR2 = \frac{1,\text{ms}}{{4 \times 125,\text{ns} \times 16}} - 1 = 124 ]
For 50% duty cycle:
[ \text{Duty Cycle Register Value} = \frac{(PR2 + 1) \times 4 \times \text{Duty Cycle (%)} }{100} ]
[ \text{Duty Cycle Register Value} = \frac{(124 + 1) \times 4 \times 50}{100} = 250 ]
Split this 10-bit value into CCPR1L and DC1B bits:
#include <xc.h>
// Configuration bits (set according to your setup)
#pragma config FOSC = INTOSC // Internal oscillator
#pragma config WDTE = OFF // WDT disabled
#pragma config PWRTE = ON // Power-up Timer Enable
#pragma config MCLRE = OFF // MCLR Pin Function Select
#pragma config CP = OFF // Code Protection
#pragma config BOREN = ON // Brown-out Reset Enable
#pragma config CLKOUTEN = OFF // Clock Out Disable
#define _XTAL_FREQ 8000000 // 8MHz oscillator frequency
void main(void) {
// Configure oscillator
OSCCONbits.IRCF = 0b1110; // 8MHz
OSCCONbits.SCS = 0b10; // Internal oscillator block
// Configure CCP1 pin (RC5)
TRISCbits.TRISC5 = 0; // RC5 as output
ANSELCbits.ANSELC5 = 0; // Disable analog function
// Configure Timer2
T2CONbits.T2CKPS = 0b11; // Prescaler = 16
PR2 = 124; // PR2 value for 1kHz PWM
T2CONbits.TMR2ON = 1; // Enable Timer2
// Configure ECCP module (PWM mode)
CCP1CONbits.CCP1M = 0b1100; // PWM mode
CCP1CONbits.DC1B = 0b10; // Lower 2 bits of duty cycle
CCPR1L = 62; // Upper 8 bits of duty cycle (50% duty cycle)
while(1) {
// Main loop
}
}
If you have any specific questions or need further assistance with configuring other modes of the ECCP module (e.g., capture or compare modes), feel free to ask!
Not yet included in a published release.