Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

MCC project creations PIC

kittu20

New Member
Hello Everyone

I'm trying to control the brightness of an LED connected to pin RB1 using PWM PIC18F45K80. I have already read through the datasheet, but I'm facing some difficulties in configuring the PWM module correctly. I followed this link https://circuitdigest.com/microcontroller-projects/pic-microcontroller-pic16f877a-pwm-tutorial for guidance, but I'm still encountering issues.

I am looking for help in configuring the PWM module and writing code to gradually change the LED's brightness. Specifically, I need guidance on setting up the PWM period, duty cycle, and initializing the necessary registers.

Here's what I've done so far:

C:
#define _XTAL_FREQ 8000000
 
 
// PIC18F45K80 Configuration Bit Settings
// 'C' source line config statements
// CONFIG1L
#pragma config RETEN = OFF      // VREG Sleep Enable bit (Ultra low-power regulator is Disabled (Controlled by REGSLP bit))
#pragma config INTOSCSEL = HIGH // LF-INTOSC Low-power Enable bit (LF-INTOSC in High-power mode during Sleep)
#pragma config SOSCSEL = HIGH   // SOSC Power Selection and mode Configuration bits (High Power SOSC circuit selected)
#pragma config XINST = OFF       // Extended Instruction Set (Enabled)
// CONFIG1H
#pragma config FOSC = INTIO2    // Oscillator (Internal RC oscillator)
#pragma config PLLCFG = OFF     // PLL x4 Enable bit (Disabled)
#pragma config FCMEN = OFF      // Fail-Safe Clock Monitor (Disabled)
#pragma config IESO = OFF       // Internal External Oscillator Switch Over Mode (Disabled)
// CONFIG2L
#pragma config PWRTEN = OFF     // Power Up Timer (Disabled)
#pragma config BOREN = SBORDIS  // Brown Out Detect (Enabled in hardware, SBOREN disabled)
#pragma config BORV = 3         // Brown-out Reset Voltage bits (1.8V)
#pragma config BORPWR = ZPBORMV // BORMV Power level (ZPBORMV instead of BORMV is selected)
// CONFIG2H
#pragma config WDTEN = OFF      // Watchdog Timer (WDT disabled in hardware; SWDTEN bit disabled)
#pragma config WDTPS = 1048576  // Watchdog Postscaler (1:1048576)
// CONFIG3H
#pragma config CANMX = PORTB    // ECAN Mux bit (ECAN TX and RX pins are located on RB2 and RB3, respectively)
#pragma config MSSPMSK = MSK7   // MSSP address masking (7 Bit address masking mode)
#pragma config MCLRE = ON       // Master Clear Enable (MCLR Enabled, RE3 Disabled)
// CONFIG4L
#pragma config STVREN = ON      // Stack Overflow Reset (Enabled)
#pragma config BBSIZ = BB2K     // Boot Block Size (2K word Boot Block size)
// CONFIG5L
#pragma config CP0 = OFF        // Code Protect 00800-01FFF (Disabled)
#pragma config CP1 = OFF        // Code Protect 02000-03FFF (Disabled)
#pragma config CP2 = OFF        // Code Protect 04000-05FFF (Disabled)
#pragma config CP3 = OFF        // Code Protect 06000-07FFF (Disabled)
// CONFIG5H
#pragma config CPB = OFF        // Code Protect Boot (Disabled)
#pragma config CPD = OFF        // Data EE Read Protect (Disabled)
// CONFIG6L
#pragma config WRT0 = OFF       // Table Write Protect 00800-01FFF (Disabled)
#pragma config WRT1 = OFF       // Table Write Protect 02000-03FFF (Disabled)
#pragma config WRT2 = OFF       // Table Write Protect 04000-05FFF (Disabled)
#pragma config WRT3 = OFF       // Table Write Protect 06000-07FFF (Disabled)
// CONFIG6H
#pragma config WRTC = OFF       // Config. Write Protect (Disabled)
#pragma config WRTB = OFF       // Table Write Protect Boot (Disabled)
#pragma config WRTD = OFF       // Data EE Write Protect (Disabled)
// CONFIG7L
#pragma config EBTR0 = OFF      // Table Read Protect 00800-01FFF (Disabled)
#pragma config EBTR1 = OFF      // Table Read Protect 02000-03FFF (Disabled)
#pragma config EBTR2 = OFF      // Table Read Protect 04000-05FFF (Disabled)
#pragma config EBTR3 = OFF      // Table Read Protect 06000-07FFF (Disabled)
// CONFIG7H
#pragma config EBTRB = OFF      // Table Read Protect Boot (Disabled)
 
 
#include <xc.h>

// Function to initialize PWM
void PWM_Initialize() {
    // Set CCP4 pin (RB0) as an output
    TRISB0 = 0;

    CCP4CONbits.CCP4M = 0b1100;  // Set CCP4 for PWM mode
 
 
    // Set the PWM frequency for approximately 1 kHz with a prescaler of 16
    PR2 = 124;  // For an 8 MHz oscillator, prescaler 16, and 1 kHz PWM frequency


    // Set the initial duty cycle (50%)
    CCPR4L =
  
    DC4B1 = 0;
    DC4B0 = 0;

    // Enable Timer2 and its prescaler (1:4)
    TMR2ON = 1;
    T2CKPS1 = 1;
    T2CKPS0 = 0;
}

void main(void) {
    // Initialize the PWM module
    PWM_Initialize();
    TRISB = 0b00000010; // LED connected to RB1

    while (1) {
    
    }
}

1695188214139.png



1695188238613.png


If anyone could provide step-by-step instructions, it would be greatly appreciated.

Thank you in advance for your help!
 
Can you check the oscillator is running?, use a scope with a x10 probe, as x1 is likely to stop the oscillator anyway.

Is there any reason you asked about 20MHz, and then set it to 8MHz?.

You might also try setting the PIC to use a fully internal oscillator, thus avoiding any external issues.
 
Can you check the oscillator is running?, use a scope with a x10 probe, as x1 is likely to stop the oscillator anyway.
I don't have scope to check signals
Is there any reason you asked about 20MHz, and then set it to 8MHz?.
I used #define _XTAL_FREQ 20000000 in device_config.h file

You might also try setting the PIC to use a fully internal oscillator, thus avoiding any external issues.
So far, I've used the internal oscillator, and it seems to be working fine for my LED blinking code.

I was working on just for fun project : creating a software-based real-time clock. While I initially used the internal oscillator for this project, I've come across recommendations from experts suggesting that an external oscillator might be a better choice.
 
In the zip file you posted - device_config.h:

#define _XTAL_FREQ 8000000

But it should still flash, just at the wrong speed.

How are you building it?, hopefuilly not on a breadboard, and hopefully with nice short connections from crystal and capacitors to pins. The datasheet also advices a series resistor, to avoid over driving HF crystals.

For an RTC you certainly want a crystal, although a 32KHz one on TMR1 is probably a preferred solution, and can be set to give a very handy one second interrupt for running an RTC, which also runs while the PIC is in sleep. I use these a lot.
 

Latest threads

New Articles From Microcontroller Tips

Back
Top