[PIC] RGB LED programming using PIC18F4550

Status
Not open for further replies.

RRNegi

Junior Member level 2
Joined
Jan 30, 2016
Messages
22
Helped
1
Reputation
2
Reaction score
1
Trophy points
3
Activity points
143
I am using NCP5623 LED driver for the RGB LED.. The code isn't working in hardware.

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) {
    NCP5623_Set_Red_Led_Brightness(r_value);
    NCP5623_Set_Green_Led_Brightness(g_value);
    NCP5623_Set_Blue_Led_Brightness(b_value);
}
 

 
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);
    }
}

- - - Updated - - -

Only 3 bytes are being transmitted instead of 6 bytes.

- - - Updated - - -

it is mikroC PRO PIC project and Crystal frequency is 8 MHz.

- - - Updated - - -

.why only 3 bytes are transmitting.
 

Attachments

  • Saleae Capture.png
    43.8 KB · Views: 112
  • NCP5623 RGB LED Driver Rev1.rar
    30.4 KB · Views: 83

Try this code. This is fixed code.

I have removed the #defines used for command values. I had overlooked the datasheet and had considered only 5 values for the command values. They actually have 2^5 = 32 (0 to 31) different values.

For max brightness the value is 0b00011111.

In the code i will have 0 to 31 values.


Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#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
 
unsigned char i = 0;
 
void NCP5623_Shutdown() {
    I2C1_Start();
    I2C1_Wr(NCP5623_ADDRESS);
    I2C1_Wr(CHIP_SHUTDOWN);
    I2C1_Stop();
}
 
void NCP5623_Set_Led_Current(char value) {
    I2C1_Start();
    I2C1_Wr(NCP5623_ADDRESS);
 
    if((value >= 0x00) && (value <= 0x1F)) {
          I2C1_Wr(SET_MAX_LED_CURRENT | value);
    }
 
    I2C1_Stop();
}
 
void NCP5623_Set_Red_Led_Brightness(char value) {
    I2C1_Start();
    I2C1_Wr(NCP5623_ADDRESS);
 
    if((value >= 0x00) && (value <= 0x1F)) {
          I2C1_Wr(PWM_RED_LED | value);
    }
 
    I2C1_Stop();
}
 
void NCP5623_Set_Green_Led_Brightness(char value) {
    I2C1_Start();
    I2C1_Wr(NCP5623_ADDRESS);
 
    if((value >= 0x00) && (value <= 0x1F)) {
          I2C1_Wr(PWM_GREEN_LED | value);
    }
 
    I2C1_Stop();
}
 
void NCP5623_Set_Blue_Led_Brightness(char value) {
    I2C1_Start();
    I2C1_Wr(NCP5623_ADDRESS);
 
    if((value >= 0x00) && (value <= 0x1F)) {
          I2C1_Wr(PWM_BLUE_LED | value);
    }
 
    I2C1_Stop();
}
 
void NCP5623_Set_Gradual_Dimming_Upward(char value) {
    I2C1_Start();
    I2C1_Wr(NCP5623_ADDRESS);
 
    if((value >= 0x00) && (value <= 0x1F)) {
          I2C1_Wr(TARGET_UPWARD_IEND | value);
    }
 
    I2C1_Stop();
}
 
void NCP5623_Set_Gradual_Dimming_Downward(char value) {
    I2C1_Start();
    I2C1_Wr(NCP5623_ADDRESS);
 
    if((value >= 0x00) && (value <= 0x1F)) {
          I2C1_Wr(TARGET_DOWNWARD_IEND | value);
    }
 
    I2C1_Stop();
}
 
void NCP5623_Set_Gradual_Dimming_Step_Time(char value) {
    I2C1_Start();
    I2C1_Wr(NCP5623_ADDRESS);
 
    if((value >= 0x00) && (value <= 0x1F)) {
          I2C1_Wr(GRADUAL_DIMMING_STEP_TIME | value);
    }
 
    I2C1_Stop();
}
 
void NCP5623_Set_Rgb_Brightness(char r_value, char g_value, char b_value) {
    NCP5623_Set_Red_Led_Brightness(r_value);
    Delay_ms(100);
    NCP5623_Set_Green_Led_Brightness(g_value);
    Delay_ms(100);
    NCP5623_Set_Blue_Led_Brightness(b_value);
    Delay_ms(100);
}
 
void main() {
 
    ADCON1 = 0x0F;
 
    TRISB = 0x03;
    PORTB = 0x00;
    LATB = 0x00;
    
    I2C1_Init(100000);
    Delay_ms(2000);
 
    while(1) {
    
        if(i > 0x1F)i = 0;
        NCP5623_Set_Rgb_Brightness(i, i, i);
        i++;
        Delay_ms(100);
    }
}

 
Last edited:
Reactions: RRNegi

    RRNegi

    Points: 2
    Helpful Answer Positive Rating
I am getting errors in this code while building.
 

In the previous post as you posted mikroC code I have also provided mikroC code. Are you using both MPLAB C18 and mikroC for the project ?

mikroC PRO PIC I2C library doesn't have any problems. I made a eeprom write code and checked the I2C signals and they are correct but with RGB LED Driver code only 3 bytes are getting transmitted instead of 6 bytes.

I don't know what is the problem. Code seems to be correct. I don't have the RGB LED Driver to test. I am just checking the SCL and SDA signals using Saleae Logic Analyzer.

Maybe more expert people can help you.
 
Last edited:
Reactions: RRNegi

    RRNegi

    Points: 2
    Helpful Answer Positive Rating
I am using Mplab C18.

- - - Updated - - -

I have replaced the start,stop and write functions.
Code:
#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
 
unsigned char i = 0;
 
void NCP5623_Shutdown() {
    StartI2C();
    WriteI2C(NCP5623_ADDRESS);
     WriteI2C(CHIP_SHUTDOWN);
    StopI2C();
}
 
void NCP5623_Set_Led_Current(char value) {
    StartI2C();
     WriteI2C(NCP5623_ADDRESS);
 
    if((value >= 0x00) && (value <= 0x1F)) {
           WriteI2C(SET_MAX_LED_CURRENT | value);
    }
 
    StopI2C();
}
 
void NCP5623_Set_Red_Led_Brightness(char value) {
    StartI2C();
     WriteI2C(NCP5623_ADDRESS);
 
    if((value >= 0x00) && (value <= 0x1F)) {
          WriteI2C(PWM_RED_LED | value);
    }
 
    StopI2C();
}
 
void NCP5623_Set_Green_Led_Brightness(char value) {
    StartI2C();
     WriteI2C(NCP5623_ADDRESS);
 
    if((value >= 0x00) && (value <= 0x1F)) {
          WriteI2C(PWM_GREEN_LED | value);
    }
 
  StopI2C();
}
 
void NCP5623_Set_Blue_Led_Brightness(char value) {
    StartI2C();
     WriteI2C(NCP5623_ADDRESS);
 
    if((value >= 0x00) && (value <= 0x1F)) {
           WriteI2C(PWM_BLUE_LED | value);
    }
 
    StopI2C();
}
 
void NCP5623_Set_Gradual_Dimming_Upward(char value) {
    StartI2C();
     WriteI2C(NCP5623_ADDRESS);
 
    if((value >= 0x00) && (value <= 0x1F)) {
           WriteI2C(TARGET_UPWARD_IEND | value);
    }
 
    StopI2C();
}
 
void NCP5623_Set_Gradual_Dimming_Downward(char value) {
    StartI2C();
     WriteI2C(NCP5623_ADDRESS);
 
    if((value >= 0x00) && (value <= 0x1F)) {
           WriteI2C(TARGET_DOWNWARD_IEND | value);
    }
 
    StopI2C();
}
 
void NCP5623_Set_Gradual_Dimming_Step_Time(char value) {
    StartI2C();
     WriteI2C(NCP5623_ADDRESS);
 
    if((value >= 0x00) && (value <= 0x1F)) {
          WriteI2C(GRADUAL_DIMMING_STEP_TIME | value);
    }
 
   StopI2C();
}
 
void NCP5623_Set_Rgb_Brightness(char r_value, char g_value, char b_value) {
    NCP5623_Set_Red_Led_Brightness(r_value);
    Delay_ms(100);
    NCP5623_Set_Green_Led_Brightness(g_value);
    Delay_ms(100);
    NCP5623_Set_Blue_Led_Brightness(b_value);
    Delay_ms(100);
}
 
void main() {
 
    ADCON1 = 0x0F;
 
    TRISB = 0x03;
    PORTB = 0x00;
    LATB = 0x00;
    
    I2C1_Init(100000);
    Delay_ms(2000);
 
    while(1) {
    
        if(i > 0x1F)i = 0;
        NCP5623_Set_Rgb_Brightness(i, i, i);
        i++;
        Delay_ms(100);
    }
}
 

Then just change the I2C functions. You already have both MPLAB C18 and mikroC Codes.
 
Reactions: RRNegi

    RRNegi

    Points: 2
    Helpful Answer Positive Rating
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
 
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--;
    }
}
unsigned char i = 0;
 
void NCP5623_Shutdown() {
    StartI2C();
    WriteI2C(NCP5623_ADDRESS);
     WriteI2C(CHIP_SHUTDOWN);
    StopI2C();
}
 
void NCP5623_Set_Led_Current(char value) {
    StartI2C();
     WriteI2C(NCP5623_ADDRESS);
 
    if((value >= 0x00) && (value <= 0x1F)) {
           WriteI2C(SET_MAX_LED_CURRENT | value);
    }
 
    StopI2C();
}
 
void NCP5623_Set_Red_Led_Brightness(char value) {
    StartI2C();
     WriteI2C(NCP5623_ADDRESS);
 
    if((value >= 0x00) && (value <= 0x1F)) {
          WriteI2C(PWM_RED_LED | value);
    }
 
    StopI2C();
}
 
void NCP5623_Set_Green_Led_Brightness(char value) {
    StartI2C();
     WriteI2C(NCP5623_ADDRESS);
 
    if((value >= 0x00) && (value <= 0x1F)) {
          WriteI2C(PWM_GREEN_LED | value);
    }
 
  StopI2C();
}
 
void NCP5623_Set_Blue_Led_Brightness(char value) {
    StartI2C();
     WriteI2C(NCP5623_ADDRESS);
 
    if((value >= 0x00) && (value <= 0x1F)) {
           WriteI2C(PWM_BLUE_LED | value);
    }
 
    StopI2C();
}
 
void NCP5623_Set_Gradual_Dimming_Upward(char value) {
    StartI2C();
     WriteI2C(NCP5623_ADDRESS);
 
    if((value >= 0x00) && (value <= 0x1F)) {
           WriteI2C(TARGET_UPWARD_IEND | value);
    }
 
    StopI2C();
}
 
void NCP5623_Set_Gradual_Dimming_Downward(char value) {
    StartI2C();
     WriteI2C(NCP5623_ADDRESS);
 
    if((value >= 0x00) && (value <= 0x1F)) {
           WriteI2C(TARGET_DOWNWARD_IEND | value);
    }
 
    StopI2C();
}
 
void NCP5623_Set_Gradual_Dimming_Step_Time(char value) {
    StartI2C();
     WriteI2C(NCP5623_ADDRESS);
 
    if((value >= 0x00) && (value <= 0x1F)) {
          WriteI2C(GRADUAL_DIMMING_STEP_TIME | value);
    }
 
   StopI2C();
}
 
void NCP5623_Set_Rgb_Brightness(char r_value, char g_value, char b_value) {
    NCP5623_Set_Red_Led_Brightness(r_value);
    Delay_ms(100);
    NCP5623_Set_Green_Led_Brightness(g_value);
    Delay_ms(100);
    NCP5623_Set_Blue_Led_Brightness(b_value);
    Delay_ms(100);
}
 
void main() {
 
    ADCON1 = 0x0F;
 
    TRISB = 0x03;
    PORTB = 0x00;
    LATB = 0x00;
      
    OpenI2C(MASTER, SLEW_OFF);// Initialize I2C module
    Delay_ms(2000);
 
    while(1) {
    
        if(i > 0x1F)i = 0;
        NCP5623_Set_Rgb_Brightness(i, i, i);
        i++;
        Delay_ms(100);
    }
}
I have made the changes in the code. Please can anyone help??

- - - Updated - - -

I have done all that I could do. Help
 

Code is correct. Check i2C Communication. It has to send 2 bytes (address and command) once every 100 ms and after 3 suct transmission it has to repeat.
 
Reactions: RRNegi

    RRNegi

    Points: 2
    Helpful Answer Positive Rating
Code is correct. Check i2C Communication. It has to send 2 bytes (address and command) once every 100 ms and after 3 suct transmission it has to repeat.

But it is only transmitting 1 byte instead of 2 every 100 ms.
 

Physical device address is 0x38 but if it is shifted left once will give 0x70. There is no address for command register.

So, the communication is like this

Open I2C
Start I2C
Send address
send command
Stop.
 
Reactions: RRNegi

    RRNegi

    Points: 2
    Helpful Answer Positive Rating
Two device addresses.
For NCP5623 it is 0X70 and
For NCP5623C it is 0X72.

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     //20MHz oscillator
#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 0x72
 
#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
 
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(500);  //100ms delay
        mSec--;
    }
}
unsigned char i = 0;
 
void NCP5623_Shutdown() 
{
    StartI2C();   
    WriteI2C(NCP5623_ADDRESS);
    WriteI2C(CHIP_SHUTDOWN);
    StopI2C();
}
 
void NCP5623_Set_Led_Current(char value)
 {
    StartI2C();
     WriteI2C(NCP5623_ADDRESS);
 
    if((value >= 0x00) && (value <= 0x1F))
 {
           WriteI2C(SET_MAX_LED_CURRENT | value);
    }
 
    StopI2C();
}
 
void NCP5623_Set_Red_Led_Brightness(char value)
 {
    StartI2C();
     WriteI2C(NCP5623_ADDRESS);
 
    if((value >= 0x00) && (value <= 0x1F)) 
{
          WriteI2C(PWM_RED_LED | value);
    }
 
    StopI2C();
}
 
void NCP5623_Set_Green_Led_Brightness(char value)
 {
    StartI2C();
     WriteI2C(NCP5623_ADDRESS);
 
    if((value >= 0x00) && (value <= 0x1F)) 
{
          WriteI2C(PWM_GREEN_LED | value);
    }
 
  StopI2C();
}
 
void NCP5623_Set_Blue_Led_Brightness(char value)
 {
    StartI2C();
     WriteI2C(NCP5623_ADDRESS);
 
    if((value >= 0x00) && (value <= 0x1F)) 
{
           WriteI2C(PWM_BLUE_LED | value);
    }
 
    StopI2C();
}
 
void NCP5623_Set_Gradual_Dimming_Upward(char value)
 {
    StartI2C();
     WriteI2C(NCP5623_ADDRESS);
 
    if((value >= 0x00) && (value <= 0x1F)) 
{
           WriteI2C(TARGET_UPWARD_IEND | value);
    }
 
    StopI2C();
}
 
void NCP5623_Set_Gradual_Dimming_Downward(char value)
 {
    StartI2C();
     WriteI2C(NCP5623_ADDRESS);
 
    if((value >= 0x00) && (value <= 0x1F)) 
{
           WriteI2C(TARGET_DOWNWARD_IEND | value);
    }
 
    StopI2C();
}
 
void NCP5623_Set_Gradual_Dimming_Step_Time(char value) 
{
    StartI2C();
     WriteI2C(NCP5623_ADDRESS);
 
    if((value >= 0x00) && (value <= 0x1F))
 {
          WriteI2C(GRADUAL_DIMMING_STEP_TIME | value);
    }
 
   StopI2C();
}
 
void NCP5623_Set_Rgb_Brightness(char r_value, char g_value, char b_value) 
{
    NCP5623_Set_Red_Led_Brightness(r_value);
    Delay_ms(100);
    NCP5623_Set_Green_Led_Brightness(g_value);
    Delay_ms(100);
    NCP5623_Set_Blue_Led_Brightness(b_value);
    Delay_ms(100);
}
 
void main() 
{
 
    ADCON1 = 0x0F; //To make the pins as Digital Output
 
    TRISB = 0x00;  //PORTB as output port
    PORTB = 0x00;
    LATB = 0x00;
      
    OpenI2C(MASTER, SLEW_OFF); //Initialize I2C module
    Delay_ms(2000);
 
    while(1) 
{
    
        if(i > 0x1F)i = 0;
        NCP5623_Set_Rgb_Brightness(i, i, i);
        i++;
        Delay_ms(100);
    }
}
 

Status
Not open for further replies.

Similar threads

Cookies are required to use this site. You must accept them to continue using the site. Learn more…