+ Post New Thread
Page 1 of 2 1 2 LastLast
Results 1 to 20 of 31
  1. #1
    Junior Member level 2
    Points: 229, Level: 2

    Join Date
    Jan 2016
    Posts
    22
    Helped
    1 / 1
    Points
    229
    Level
    2

    PIC18F Microcontroller - prescaler and postscaler in timer2 for generating PWM

    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?

  2. #2
    Super Moderator
    Points: 72,216, Level: 65
    Achievements:
    7 years registered
    Awards:
    2nd Helpful Member
    betwixt's Avatar
    Join Date
    Jul 2009
    Location
    Aberdyfi, West Wales, UK
    Posts
    11,838
    Helped
    3905 / 3905
    Points
    72,216
    Level
    65

    Re: PIC18F Microcontroller - 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.
    PLEASE - no friends requests or private emails, I simply don't have time to reply to them all.
    It's better to share your questions and answers on Edaboard so we can all benefit from each others experiences.



    •   AltAdvertisment

        
       

  3. #3
    Junior Member level 2
    Points: 229, Level: 2

    Join Date
    Jan 2016
    Posts
    22
    Helped
    1 / 1
    Points
    229
    Level
    2

    Re: PIC18F Microcontroller - prescaler and postscaler in timer2 for generating PWM

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

    Thankyou.



  4. #4
    Full Member level 1
    Points: 2,800, Level: 12
    gamegurus's Avatar
    Join Date
    Jan 2005
    Posts
    97
    Helped
    19 / 19
    Points
    2,800
    Level
    12

    Re: PIC18F Microcontroller - prescaler and postscaler in timer2 for generating PWM

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



  5. #5
    Junior Member level 2
    Points: 229, Level: 2

    Join Date
    Jan 2016
    Posts
    22
    Helped
    1 / 1
    Points
    229
    Level
    2

    Re: PIC18F Microcontroller - prescaler and postscaler in timer2 for generating PWM

    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?

    Click image for larger version. 

Name:	rgb.png 
Views:	26 
Size:	24.5 KB 
ID:	125615



  6. #6
    Super Moderator
    Points: 72,216, Level: 65
    Achievements:
    7 years registered
    Awards:
    2nd Helpful Member
    betwixt's Avatar
    Join Date
    Jul 2009
    Location
    Aberdyfi, West Wales, UK
    Posts
    11,838
    Helped
    3905 / 3905
    Points
    72,216
    Level
    65

    Re: PIC18F Microcontroller - prescaler and postscaler in timer2 for generating PWM

    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.
    PLEASE - no friends requests or private emails, I simply don't have time to reply to them all.
    It's better to share your questions and answers on Edaboard so we can all benefit from each others experiences.


    1 members found this post helpful.

  7. #7
    Full Member level 1
    Points: 2,800, Level: 12
    gamegurus's Avatar
    Join Date
    Jan 2005
    Posts
    97
    Helped
    19 / 19
    Points
    2,800
    Level
    12

    Re: PIC18F Microcontroller - prescaler and postscaler in timer2 for generating PWM

    you only need I2C, the PWM is built into the IC NCP5623
    -------------------------------------------------------------
    I was born intelligent but education made me dumb!
    -------------------------------------------------------------



  8. #8
    Junior Member level 2
    Points: 229, Level: 2

    Join Date
    Jan 2016
    Posts
    22
    Helped
    1 / 1
    Points
    229
    Level
    2

    Re: PIC18F Microcontroller - prescaler and postscaler in timer2 for generating PWM

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



    •   AltAdvertisment

        
       

  9. #9
    Advanced Member level 3
    Points: 6,504, Level: 19

    Join Date
    Jan 2015
    Posts
    946
    Helped
    297 / 297
    Points
    6,504
    Level
    19

    Re: PIC18F Microcontroller - prescaler and postscaler in timer2 for generating PWM

    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


    1 members found this post helpful.

    •   AltAdvertisment

        
       

  10. #10
    Junior Member level 2
    Points: 229, Level: 2

    Join Date
    Jan 2016
    Posts
    22
    Helped
    1 / 1
    Points
    229
    Level
    2

    Re: PIC18F Microcontroller - prescaler and postscaler in timer2 for generating PWM

    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.



  11. #11
    Super Moderator
    Points: 72,216, Level: 65
    Achievements:
    7 years registered
    Awards:
    2nd Helpful Member
    betwixt's Avatar
    Join Date
    Jul 2009
    Location
    Aberdyfi, West Wales, UK
    Posts
    11,838
    Helped
    3905 / 3905
    Points
    72,216
    Level
    65

    Re: PIC18F Microcontroller - prescaler and postscaler in timer2 for generating PWM

    Sir Susan

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

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

    Brian.
    PLEASE - no friends requests or private emails, I simply don't have time to reply to them all.
    It's better to share your questions and answers on Edaboard so we can all benefit from each others experiences.



  12. #12
    Junior Member level 2
    Points: 229, Level: 2

    Join Date
    Jan 2016
    Posts
    22
    Helped
    1 / 1
    Points
    229
    Level
    2

    Re: PIC18F Microcontroller - prescaler and postscaler in timer2 for generating PWM

    Sorry Ma'am @Aussie Susan

    Sir I am using MPLABC18 Compiler @betwixt



  13. #13
    Super Moderator
    Points: 72,216, Level: 65
    Achievements:
    7 years registered
    Awards:
    2nd Helpful Member
    betwixt's Avatar
    Join Date
    Jul 2009
    Location
    Aberdyfi, West Wales, UK
    Posts
    11,838
    Helped
    3905 / 3905
    Points
    72,216
    Level
    65

    Re: PIC18F Microcontroller - prescaler and postscaler in timer2 for generating PWM

    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.
    PLEASE - no friends requests or private emails, I simply don't have time to reply to them all.
    It's better to share your questions and answers on Edaboard so we can all benefit from each others experiences.


    1 members found this post helpful.

  14. #14
    Advanced Member level 3
    Points: 6,504, Level: 19

    Join Date
    Jan 2015
    Posts
    946
    Helped
    297 / 297
    Points
    6,504
    Level
    19

    Re: PIC18F Microcontroller - prescaler and postscaler in timer2 for generating PWM

    In that case have a look at the "C18 C Compiler Libraries" manual (http://ww1.microchip.com/downloads/e...ies_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...rface_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)


    1 members found this post helpful.

  15. #15
    Advanced Member level 3
    Points: 3,996, Level: 14

    Join Date
    Aug 2015
    Posts
    773
    Helped
    142 / 142
    Points
    3,996
    Level
    14

    Re: PIC18F Microcontroller - prescaler and postscaler in timer2 for generating PWM

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


    1 members found this post helpful.

  16. #16
    Junior Member level 2
    Points: 229, Level: 2

    Join Date
    Jan 2016
    Posts
    22
    Helped
    1 / 1
    Points
    229
    Level
    2

    Re: PIC18F Microcontroller - prescaler and postscaler in timer2 for generating PWM

    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 
    #include 
    #include 
    
    #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.


    1 members found this post helpful.

  17. #17
    Advanced Member level 3
    Points: 3,996, Level: 14

    Join Date
    Aug 2015
    Posts
    773
    Helped
    142 / 142
    Points
    3,996
    Level
    14

    Re: PIC18F Microcontroller - prescaler and postscaler in timer2 for generating PWM

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


    1 members found this post helpful.

  18. #18
    Junior Member level 2
    Points: 229, Level: 2

    Join Date
    Jan 2016
    Posts
    22
    Helped
    1 / 1
    Points
    229
    Level
    2

    Re: PIC18F Microcontroller - prescaler and postscaler in timer2 for generating PWM

    hello,
    I am using the PORTB pins of PIC for the communication so which prototype should be used?? StartI2C or SWStartI2C??(Reference MplabC18 libraries 2.4 and 3.4)

    Thankyou.



  19. #19
    Advanced Member level 3
    Points: 3,996, Level: 14

    Join Date
    Aug 2015
    Posts
    773
    Helped
    142 / 142
    Points
    3,996
    Level
    14

    Re: PIC18F Microcontroller - prescaler and postscaler in timer2 for generating PWM

    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 ?


    1 members found this post helpful.

    •   AltAdvertisment

        
       

  20. #20
    Junior Member level 2
    Points: 229, Level: 2

    Join Date
    Jan 2016
    Posts
    22
    Helped
    1 / 1
    Points
    229
    Level
    2

    Re: PIC18F Microcontroller - prescaler and postscaler in timer2 for generating PWM

    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.



--[[ ]]--