Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

PIC18F Microcontroller - prescaler and postscaler in timer2 for generating PWM

Status
Not open for further replies.
There is one hardware I2C module in your PIC18F4550 and i2c library knows that when OpenI2C() is used it if for that I2C. ADCON is configured to disable all AD Channels and configure all pins as digital IO pin...

WS2812B is another LED Driver which is simpler to use and also code is simple.
 
  • Like
Reactions: RRNegi

    RRNegi

    Points: 2
    Helpful Answer Positive Rating
Hello,
The code isn't working in hardware.
 

What is not working ? What is happening ? Does the RGB LEDs light up ?
 

RGB is getting positive supply from the anode pins as the RGB used is common anode. All pins of RGB are getting high voltage. So the RGB LED's is not Lighting up.
 

Config bits settings are no complete. You have to Configure PPLDIV, CPUDIV etc... Include a LED blink code and see if LED is blinking. If not, then Oscillator is not configured properly in #pragma derectives.
 
  • Like
Reactions: RRNegi

    RRNegi

    Points: 2
    Helpful Answer Positive Rating
Code:
#pragma config FOSC = HS    // Using 20 MHz crystal with PLL
#pragma config WDT = OFF        // Disable Watchdog timer
#pragma config MCLRE = ON       // Enable MCLR Enable
#pragma config LVP = OFF        // Disable low voltage ICSP
config bits settings
 

Yes, As you have enabled MCLR you should pullup MCLR pin to 5V using a 10k resistor. Configure PPLDIV and CPUDIV in #pragma derectives.
 
  • Like
Reactions: RRNegi

    RRNegi

    Points: 2
    Helpful Answer Positive Rating
OK I'll configure PPLDIV and CPUDIV and yes I have connected pullup to the MCLR pin.
 

Which type of delay function is used in the program??
 

Why the code is'nt working??

Code:
#include <P18F4550.h>
#include <delays.h>
#include <i2c.h>

#pragma config PLLDIV = 5
#pragma config CPUDIV = OSC1_PLL2
#pragma config USBDIV = 2
#pragma config FOSC = HS
#pragma config FCMEN = ON
#pragma config IESO = OFF
#pragma config PWRT = ON
#pragma config BOR = ON
#pragma config BORV = 2
#pragma config VREGEN = OFF
#pragma config WDT = OFF
#pragma config WDTPS = 2048
#pragma config CCP2MX = OFF
#pragma config PBADEN = OFF
#pragma config LPT1OSC = OFF
#pragma config MCLRE = ON
#pragma config STVREN = ON
#pragma config LVP = OFF
#pragma config ICPRT = OFF
#pragma config XINST = OFF
#pragma config CP0 = OFF
#pragma config CP1 = OFF
#pragma config CP2 = OFF
#pragma config CP3 = OFF
#pragma config CPB = OFF
#pragma config CPD = OFF
#pragma config WRT0 = OFF
#pragma config WRT1 = OFF
#pragma config WRT2 = OFF
#pragma config WRT3 = OFF
#pragma config WRTC = OFF
#pragma config WRTB = OFF
#pragma config WRTD = OFF
#pragma config EBTR0 = OFF
#pragma config EBTR1 = OFF
#pragma config EBTR2 = OFF
#pragma config EBTR3 = OFF
#pragma config EBTRB = OFF
 
#define NCP5623_ADDRESS 0x70

#define CHIP_SHUTDOWN             0b00000000
#define SET_MAX_LED_CURRENT       0b00100000
#define PWM_RED_LED               0b01000000
#define PWM_GREEN_LED             0b01100000
#define PWM_BLUE_LED              0b10000000

#define TARGET_UPWARD_IEND        0b10100000
#define TARGET_DOWNWARD_IEND      0b11000000
#define GRADUAL_DIMMING_STEP_TIME 0b11100000
 
 
#define LED_CURRENT_16 0b00010000
#define LED_CURRENT_8  0b00001000
#define LED_CURRENT_4  0b00000100
#define LED_CURRENT_2  0b00000010
#define LED_CURRENT_1  0b00000001
 
#define BPWM16 0b00010000
#define BPWM8  0b00001000
#define BPWM4  0b00000100
#define BPWM2  0b00000010
#define BPWM1  0b00000001
 
#define GDIM16 0b00010000
#define GDIM8  0b00001000
#define GDIM4  0b00000100
#define GDIM2  0b00000010
#define GDIM1  0b00000001
 
#define GDIM128ms 0b00010000
#define GDIM64ms  0b00001000
#define GDIM32ms  0b00000100
#define GDIM16ms  0b00000010
#define GDIM8ms   0b00000001
 
unsigned char i = 0;
 
void Delay_ms(unsigned int mSec);
void NCP5623_Shutdown();
void NCP5623_Set_Led_Current(char value);
void NCP5623_Set_Red_Led_Brightness(char value);
void NCP5623_Set_Green_Led_Brightness(char value);
void NCP5623_Set_Blue_Led_Brightness(char value);
void NCP5623_Set_Gradual_Dimming_Upward(char value);
void NCP5623_Set_Gradual_Dimming_Downward(char value);
void NCP5623_Set_Gradual_Dimming_Step_Time(char value);
void NCP5623_Set_Rgb_Brightness( char r_value, char g_value, char b_value);
 

void Delay_ms(unsigned int mSec) 
{
    while(mSec != 0) {
        Delay1KTCYx(2);
        mSec--;
    }
}
 
void NCP5623_Shutdown()
{
    StartI2C();
    WriteI2C(NCP5623_ADDRESS);
    WriteI2C(CHIP_SHUTDOWN);
    StopI2C();
}
 
void NCP5623_Set_Led_Current(char value)
{
    StartI2C();
    WriteI2C(NCP5623_ADDRESS);
 
    if((value == LED_CURRENT_16) || (value == LED_CURRENT_8) || (value == LED_CURRENT_4)
       || (value == LED_CURRENT_2) || (value == LED_CURRENT_1)) 
{
          WriteI2C(SET_MAX_LED_CURRENT | value);
    }
 
    StopI2C();
}
 
void NCP5623_Set_Red_Led_Brightness(char value)
{
    StartI2C();
    WriteI2C (NCP5623_ADDRESS);
 
    if((value == BPWM16) || (value == BPWM8) || (value == BPWM4)
       || (value == BPWM2) || (value == BPWM1))
 {
          WriteI2C(PWM_RED_LED | value);
    }
 
    StopI2C();
}
 
void NCP5623_Set_Green_Led_Brightness(char value)
{
    StartI2C();
    WriteI2C (NCP5623_ADDRESS);
 
    if((value == BPWM16) || (value == BPWM8) || (value == BPWM4)
       || (value == BPWM2) || (value == BPWM1)) 
{
          WriteI2C(PWM_GREEN_LED | value);
    }
 
    StopI2C();
}
 
void NCP5623_Set_Blue_Led_Brightness(char value)
{
    StartI2C();
    WriteI2C(NCP5623_ADDRESS);
 
    if((value == BPWM16) || (value == BPWM8) || (value == BPWM4)
       || (value == BPWM2) || (value == BPWM1)) 
{
          WriteI2C(PWM_BLUE_LED | value);
    }
 
    StopI2C();
}
 
void NCP5623_Set_Gradual_Dimming_Upward(char value)
{
    StartI2C();
    WriteI2C(NCP5623_ADDRESS);
 
    if((value == GDIM16) || (value == GDIM8) || (value == GDIM4)
       || (value == GDIM2) || (value == GDIM1))
    {
          WriteI2C(TARGET_UPWARD_IEND | value);
    }
 
    StopI2C();
}
 
void NCP5623_Set_Gradual_Dimming_Downward(char value)
{
    StartI2C();
    WriteI2C(NCP5623_ADDRESS);
 
    if((value == GDIM16) || (value == GDIM8) || (value == GDIM4)
       || (value == GDIM2) || (value == GDIM1))
    {
          WriteI2C(TARGET_DOWNWARD_IEND | value);
    }
 
    StopI2C();
}
 
void NCP5623_Set_Gradual_Dimming_Step_Time(char value) 
{
    StartI2C();
    WriteI2C(NCP5623_ADDRESS);
 
    if((value == GDIM128ms) || (value == GDIM64ms) || (value == GDIM32ms)
       || (value == GDIM16ms) || (value == GDIM8ms)) 
    {
          WriteI2C(GRADUAL_DIMMING_STEP_TIME | value);
    }
 
    StopI2C();
}
 
void NCP5623_Set_Rgb_Brightness( char r_value, char g_value, char b_value)
{
    StartI2C();
    WriteI2C (NCP5623_ADDRESS);
 
    WriteI2C(PWM_RED_LED | r_value);
    WriteI2C(PWM_GREEN_LED | g_value);
    WriteI2C(PWM_BLUE_LED | b_value);
 
    StopI2C();
}
 
void main (void)
 {
 
    ADCON1 = 0x0F;
 
    OpenI2C(MASTER, SLEW_OFF);// Initialize I2C module
    Delay_ms(200);
 
    NCP5623_Set_Led_Current(LED_CURRENT_16);
    NCP5623_Set_Red_Led_Brightness(BPWM16);
    NCP5623_Set_Green_Led_Brightness(BPWM16);
    NCP5623_Set_Blue_Led_Brightness(BPWM16);
    NCP5623_Set_Gradual_Dimming_Upward(GDIM16);
    NCP5623_Set_Gradual_Dimming_Step_Time(GDIM32ms);
 
    while(1)
    {
 
        for(i = 0x01; i < 0x20; i<<1)
        {
            NCP5623_Set_Rgb_Brightness(i, i, i);
             Delay100TCYx(100);
        }
 
         Delay100TCYx(100);
         Delay100TCYx(100);
 
        for(i = 0x10; i >= 0x01; i>>1) 
        {
            NCP5623_Set_Rgb_Brightness(i, i, i);
             Delay100TCYx(100);
        }
 
      Delay100TCYx(100);
      Delay100TCYx(100);
 
        i = 0x01;
 
        NCP5623_Set_Rgb_Brightness((i << 4), (i << 2), i);
       Delay100TCYx(100);
 
        i = 0x10;
 
        NCP5623_Set_Rgb_Brightness(i, (i >> 2), (i >> 4));
       Delay100TCYx(100);
    }
}
 

Fixed code.


Code C - [expand]
1
2
3
4
5
6
7
8
9
10
void NCP5623_Set_Rgb_Brightness(char r_value, char g_value, char b_value) {
    I2C1_Start();
    I2C1_Wr(NCP5623_ADDRESS);
    I2C1_Wr(PWM_RED_LED | r_value);
    I2C1_Wr(NCP5623_ADDRESS);
    I2C1_Wr(PWM_GREEN_LED | g_value);
    I2C1_Wr(NCP5623_ADDRESS);
    I2C1_Wr(PWM_BLUE_LED | b_value);
    I2C1_Stop();
}



The communication is a two byte communication and 1st byte is always device address which is 0x70. The second byte is the command byte. So, always you have to send address and after that command byte.


Edi:

A better way.


Code C - [expand]
1
2
3
4
5
void NCP5623_Set_Rgb_Brightness(char r_value, char g_value, char b_value) {
    NCP5623_Set_Red_Led_Brightness(r_value);
    NCP5623_Set_Green_Led_Brightness(g_value);
    NCP5623_Set_Blue_Led_Brightness(b_value);
}

 
Last edited:

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top