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.

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 working on PIC18F4550 microcontroller to generate PWM of diffferent duty cycle. In PIC18F4550 timer2 is used for PWM. What is the use of prescaler and postscaler in timer2 for generating PWM?
 

Timer2 sets the overall PWM frequency. The prescaler bits divide the system clock before it reaches Timer2 to allow you a wider range of possible frequencies. As far as I know, the postscaler bits are not used in PWM generation.

Brian.
 

How Timer2 can be used as the shift clock for MSSP (Master Synchronous Serial Port) module?

Thankyou.
 

first you are talking PWM and now you changed the topic to MSSP?
 

Hello Sir,
I am working on a project in which I have to drive an RGB Led using PIC18f4550. For generating different colours in RGB Led I am using PWM so by changing the duty cycle I can generate diffferent colours. Also an LED driver is used which is connected to the I2C pins of the microcontroller. PWM and I2C both are used?

rgb.png
 

It depends on which serial mode you are using. In SPI and I2C mode, set the SSPM bits in the SSPCON1 register to 0011.
In serial EUSART mode, the bit rate generator is clocked from the system clock and Timer2 is not used.

Brian.

- - - Updated - - -

All the PWM work is done inside the NCP5623, the PWM generator in the PIC is not used at all. The communication is entirely by I2C, your PIC sends data to the NCP5623 to tell it what brightness each channel is to be set to. You could use almost any MCU, even one with no PWM capability.

Brian.
 
  • Like
Reactions: RRNegi

    RRNegi

    Points: 2
    Helpful Answer Positive Rating
you only need I2C, the PWM is built into the IC NCP5623
 

Can you help me with the I2C programming? what should I do further?
 

What compiler are you using? Some compilers come with libraries that might contain I2C functions.
If not then there are MANY examples of I2C code (normally written in C) that you can find on the Internet, forums, books etc. as this is a very common question. While the example you find may not exactly match your compiler/processor, once you understand the principles of I2C communication (again, there are many tutorials on the Internet) then it is simple to make any necessary changes to make the code work for you.
You also need to be clear on what address(es) you need to communicate with and what information is passed when and in which direction with the NCP5623. This is all explained in the "I2C Protocol" section of the data sheet (where I suspect you got that diagram in your post #5).
Susan
 
  • Like
Reactions: RRNegi

    RRNegi

    Points: 2
    Helpful Answer Positive Rating
Hello Sir,
I am using C compiler. I didn't find the I2C examples in the book which I referred. Examples of I2C are not available on the internet too. Can you please suggest some?
Thankyou.
 

Sir Susan :)

Just for reference RRNegi, Susan is a female name, even in Australia!

Which compiler are you using? (HiTech, CCS, MikroC, WizC....)

Brian.
 

Sorry Ma'am @Aussie Susan

Sir I am using MPLABC18 Compiler @betwixt
 

I'm not sure where the documentation is if you are using MPLAB but in MPLABX, click the path:
Help/Help Contents/
If it's the first time you have run the help system it will compile the help files, it takes a few seconds. Then in the left side of the help window (contents tab) under the heading "Language tools" click on "C18 Toolchain/C18 Standard Libraries/Software Peripheral Library/Software I2C functions" it describes the built-in library functions and gives an example of them in a program.

Personally, I write my own, I find it less confusing than the C18 ones! Do you agree Sir Susan?

Brian.
 
  • Like
Reactions: RRNegi

    RRNegi

    Points: 2
    Helpful Answer Positive Rating
In that case have a look at the "C18 C Compiler Libraries" manual (http://ww1.microchip.com/downloads/en/DeviceDoc/MPLAB_C18_Libraries_51297f.pdf) and in particular sections 2.4 and 3.4.
Also a Google search throws up matches such as http://www.pyroelectro.com/tutorials/i2c_pic_interface_tutorial - yes it does reference a slightly different PIC18F device but the principles are the same and will show you how to communicate via I2C.
Susan
(Edit: Written at the same time as Betwixt's message above)
 
  • Like
Reactions: RRNegi

    RRNegi

    Points: 2
    Helpful Answer Positive Rating
I have written this code after reading the datasheet of the device. I don't have that RGB LED Driver to test the code. So, test the Code and see if it works fine.


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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
#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 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 == LED_CURRENT_16) || (value == LED_CURRENT_8) || (value == LED_CURRENT_4)
       || (value == LED_CURRENT_2) || (value == LED_CURRENT_1)) {
          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 == BPWM16) || (value == BPWM8) || (value == BPWM4)
       || (value == BPWM2) || (value == BPWM1)) {
          I2C1_Wr(PWM_RED_LED | value);
    }
 
    I2C1_Stop();
}
 
void NCP5623_Set_Green_Led_Brightness(char value) {
    I2C1_Start();
    I2C1_Wr(NCP5623_ADDRESS);
 
    if((value == BPWM16) || (value == BPWM8) || (value == BPWM4)
       || (value == BPWM2) || (value == BPWM1)) {
          I2C1_Wr(PWM_GREEN_LED | value);
    }
 
    I2C1_Stop();
}
 
void NCP5623_Set_Blue_Led_Brightness(char value) {
    I2C1_Start();
    I2C1_Wr(NCP5623_ADDRESS);
 
    if((value == BPWM16) || (value == BPWM8) || (value == BPWM4)
       || (value == BPWM2) || (value == BPWM1)) {
          I2C1_Wr(PWM_BLUE_LED | value);
    }
 
    I2C1_Stop();
}
 
void NCP5623_Set_Gradual_Dimming_Upward(char value) {
    I2C1_Start();
    I2C1_Wr(NCP5623_ADDRESS);
 
    if((value == GDIM16) || (value == GDIM8) || (value == GDIM4)
       || (value == GDIM2) || (value == GDIM1)) {
          I2C1_Wr(TARGET_UPWARD_IEND | value);
    }
 
    I2C1_Stop();
}
 
void NCP5623_Set_Gradual_Dimming_Downward(char value) {
    I2C1_Start();
    I2C1_Wr(NCP5623_ADDRESS);
 
    if((value == GDIM16) || (value == GDIM8) || (value == GDIM4)
       || (value == GDIM2) || (value == GDIM1)) {
          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 == GDIM128ms) || (value == GDIM64ms) || (value == GDIM32ms)
       || (value == GDIM16ms) || (value == GDIM8ms)) {
          I2C1_Wr(GRADUAL_DIMMING_STEP_TIME | value);
    }
 
    I2C1_Stop();
}
 
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(PWM_GREEN_LED | g_value);
    I2C1_Wr(PWM_BLUE_LED | b_value);
 
    I2C1_Stop();
}
 
void main() {
 
    ADCON1 = 0x0F;
 
    I2C1_Init(100000);
    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);
            Delay_ms(2000);
        }
 
        Delay_ms(4000);
 
        for(i = 0x10; i >= 0x01; i>>1) {
            NCP5623_Set_Rgb_Brightness(i, i, i);
            Delay_ms(2000);
        }
 
        Delay_ms(4000);
 
        i = 0x01;
 
        NCP5623_Set_Rgb_Brightness((i << 4), (i << 2), i);
        Delay_ms(2000);
 
        i = 0x10;
 
        NCP5623_Set_Rgb_Brightness(i, (i >> 2), (i >> 4));
        Delay_ms(2000);
    }
}

 

Attachments

  • NCP5623-D Datasheet.PDF
    137.9 KB · Views: 70
  • Like
Reactions: RRNegi

    RRNegi

    Points: 2
    Helpful Answer Positive Rating
Hello,
I am gettting errors while building this program. I've gone through the mplabc18 libraries there the commands are
start-StartI2C1
read-ReadI2C1
write-WriteI2C1
stop-StopI2C1
Is this the reason for errors??

- - - Updated - - -

Hello,
I've replaced the commands for start,write,read and stop

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

#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

void  Delay_ms (unsigned int itime);

#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 ReadI2C1 (void);
void StartI2C1 (void);
void StopI2C1 (void);
unsigned char WriteI2C1 (unsigned char data_out);
 
unsigned char i = 0;
 
void NCP5623_Shutdown()
{
    StartI2C1 ();
    WriteI2C1 (NCP5623_ADDRESS);
    WriteI2C1(CHIP_SHUTDOWN);
    StopI2C1();
}
 
void NCP5623_Set_Led_Current(char value)
 {
    StartI2C1();
    WriteI2C1(NCP5623_ADDRESS);
 
    if((value == LED_CURRENT_16) || (value == LED_CURRENT_8) || (value == LED_CURRENT_4)
       || (value == LED_CURRENT_2) || (value == LED_CURRENT_1)) 
{
          WriteI2C1 (SET_MAX_LED_CURRENT | value);
    }
 
    StopI2C1();
}
 
void NCP5623_Set_Red_Led_Brightness(char value)
 {
    StartI2C1();
    WriteI2C1 (NCP5623_ADDRESS);
 
    if((value == BPWM16) || (value == BPWM8) || (value == BPWM4)
       || (value == BPWM2) || (value == BPWM1))
 {
          WriteI2C1(PWM_RED_LED | value);
    }
 
    StopI2C1 ();
}
 
void NCP5623_Set_Green_Led_Brightness(char value)
 {
    StartI2C1();
    WriteI2C1 (NCP5623_ADDRESS);
 
    if((value == BPWM16) || (value == BPWM8) || (value == BPWM4)
       || (value == BPWM2) || (value == BPWM1)) 
{
          WriteI2C1 (PWM_GREEN_LED | value);
    }
 
    StopI2C1();
}
 
void NCP5623_Set_Blue_Led_Brightness(char value)
 {
    StartI2C1();
    WriteI2C1 (NCP5623_ADDRESS);
 
    if((value == BPWM16) || (value == BPWM8) || (value == BPWM4)
       || (value == BPWM2) || (value == BPWM1)) 
{
          WriteI2C1(PWM_BLUE_LED | value);
    }
 
    StopI2C1 ();
}
 
void NCP5623_Set_Gradual_Dimming_Upward(char value)
 {
    StartI2C1();
    WriteI2C1 (NCP5623_ADDRESS);
 
    if((value == GDIM16) || (value == GDIM8) || (value == GDIM4)
       || (value == GDIM2) || (value == GDIM1))
 {
          WriteI2C1(TARGET_UPWARD_IEND | value);
    }
 
    StopI2C1 ();
}
 
void NCP5623_Set_Gradual_Dimming_Downward(char value)
{
    StartI2C1();
    WriteI2C1 (NCP5623_ADDRESS);
 
    if((value == GDIM16) || (value == GDIM8) || (value == GDIM4)
       || (value == GDIM2) || (value == GDIM1))
 {
          WriteI2C1 (TARGET_DOWNWARD_IEND | value);
    }
 
    StopI2C1 ();
}
 
void NCP5623_Set_Gradual_Dimming_Step_Time(char value) 
{
    StartI2C1 ();
    WriteI2C1(NCP5623_ADDRESS);
 
    if((value == GDIM128ms) || (value == GDIM64ms) || (value == GDIM32ms)
       || (value == GDIM16ms) || (value == GDIM8ms)) 
{
          WriteI2C1(GRADUAL_DIMMING_STEP_TIME | value);
    }
 
    StopI2C1();
}
 
void NCP5623_Set_Rgb_Brightness( char r_value, char g_value, char b_value)
{
    StartI2C1();
    WriteI2C1 (NCP5623_ADDRESS);
 
    WriteI2C1(PWM_RED_LED | r_value);
    WriteI2C1(PWM_GREEN_LED | g_value);
    WriteI2C1(PWM_BLUE_LED | b_value);
 
    StopI2C1();
}
 
void main (void)
 {
 
    ADCON1 = 0x0F;
 
    I2C1_Init(100000);
     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);
             Delay_ms (2000);
        }
 
         Delay_ms(4000);
 
        for(i = 0x10; i >= 0x01; i>>1) 
{
            NCP5623_Set_Rgb_Brightness(i, i, i);
             Delay_ms (2000);
        }
 
       Delay_ms (4000);
 
        i = 0x01;
 
        NCP5623_Set_Rgb_Brightness((i << 4), (i << 2), i);
        Delay_ms (2000);
 
        i = 0x10;
 
        NCP5623_Set_Rgb_Brightness(i, (i >> 2), (i >> 4));
       Delay_ms (2000);
    }
}

Now I am getting error in the I2C1_Init(100000); command. Help?
Thankyou.
 
Here is the fixed code.

Changes made are

Code:
OpenI2C() is added

and You were using I2C1 functions but your PIC doesn't have 2 I2C's and hence I replaced all I2C1 functions with I2C functions.


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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
#include <P18F4550.h>
#include <delays.h>
#include <i2c.h>
 
#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
 
#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(10);
        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);
             Delay_ms(2000);
        }
 
         Delay_ms(4000);
 
        for(i = 0x10; i >= 0x01; i>>1) 
        {
            NCP5623_Set_Rgb_Brightness(i, i, i);
             Delay_ms(2000);
        }
 
       Delay_ms (4000);
 
        i = 0x01;
 
        NCP5623_Set_Rgb_Brightness((i << 4), (i << 2), i);
        Delay_ms(2000);
 
        i = 0x10;
 
        NCP5623_Set_Rgb_Brightness(i, (i >> 2), (i >> 4));
       Delay_ms(2000);
    }
}

 
  • Like
Reactions: RRNegi

    RRNegi

    Points: 2
    Helpful Answer Positive Rating
If you use RB0 and RB1 pins then the code I posted is correct and you have to use Hardware I2C functions.

- - - Updated - - -

Did the code worked in the hardware ?
 
  • Like
Reactions: RRNegi

    RRNegi

    Points: 2
    Helpful Answer Positive Rating
Hello,
Yes RB0 and RB1 are used but Where have you define these pins??I don't have to define these pins?
OpenI2C(MASTER, SLEW_OFF);// Initialize I2C module
This code will consider the master and slave condition??
What is the use of ADCON1 = 0x0f
Not yet checked in the hardware.
Thankyou.
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top