• AVR Freaks

Hot!4 Channel PWM conditioner - Need Help PIC16F1578

Author
Mobiz
New Member
  • Total Posts : 7
  • Reward points : 0
  • Joined: 2019/06/07 18:27:47
  • Location: 0
  • Status: offline
2019/06/10 12:15:01 (permalink)
0

4 Channel PWM conditioner - Need Help PIC16F1578

Hi, 
I am using the PIC16F1578 and trying to take in 4 PWM channels and modify the duty cycle for each channel (divide by 2), then output each channel again as a PWM. I have seen some examples of how people use the CCP registers, but from what it seems this chip does not have one, at least from reading the datasheet. In addition, many of the examples I have seen are for 1 channel, and I'm not entirely sure how to expand to 4. 
 
Each PWM is a 1Khz signal, of varying input duty cycles from 0 to 100%. If there is slight phase separation between the channels it doesn't matter too much. 
 
I am new to using PIC micros and dont have much experience with them. Any help would be greatly appreciated!!!
 
 
Thanks,
Mobiz
 
 
#1

12 Replies Related Threads

    ric
    Super Member
    • Total Posts : 23829
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: offline
    Re: 4 Channel PWM conditioner - Need Help PIC16F1578 2019/06/10 19:29:45 (permalink)
    0
    As you say, there's no CCP modules.
    Therefore, THIS PIC does not appear to be a suitable device to satisfy your stated aim. Can you change?
     
     

    I also post at: PicForum
    Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
    NEW USERS: Posting images, links and code - workaround for restrictions.
    To get a useful answer, always state which PIC you are using!
    #2
    Mobiz
    New Member
    • Total Posts : 7
    • Reward points : 0
    • Joined: 2019/06/07 18:27:47
    • Location: 0
    • Status: offline
    Re: 4 Channel PWM conditioner - Need Help PIC16F1578 2019/06/10 19:37:41 (permalink)
    0
    Yes I willinging to change. What would you recommend. 
     
    I have a curiosity board to prototype with so any 20pin DIP for now would do, I plan to spin a PCB for this eventually. 
    #3
    ric
    Super Member
    • Total Posts : 23829
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: offline
    Re: 4 Channel PWM conditioner - Need Help PIC16F1578 2019/06/10 19:59:56 (permalink)
    0
    I'm not sure if there's any 20 pin PICs that will do it.
    If you could go to 28 pin, a PIC16F1778 probably would do.
    Have a poke around at https://www.microchip.com/maps/microcontroller.aspx
     

    I also post at: PicForum
    Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
    NEW USERS: Posting images, links and code - workaround for restrictions.
    To get a useful answer, always state which PIC you are using!
    #4
    davea
    Senior Member
    • Total Posts : 152
    • Reward points : 0
    • Joined: 2016/01/28 13:12:13
    • Location: 0
    • Status: offline
    Re: 4 Channel PWM conditioner - Need Help PIC16F1578 2019/06/10 20:11:58 (permalink)
    0
    is this for a H-bridge ?
    can all PWM's start at the same time ?
    #5
    Mobiz
    New Member
    • Total Posts : 7
    • Reward points : 0
    • Joined: 2019/06/07 18:27:47
    • Location: 0
    • Status: offline
    Re: 4 Channel PWM conditioner - Need Help PIC16F1578 2019/06/10 20:18:44 (permalink)
    0
    Hi davea,
     
    This is not for an H bridge, this is for an LED system. The PWMs can start at the same time and it actually better if it does. 
    #6
    ric
    Super Member
    • Total Posts : 23829
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: offline
    Re: 4 Channel PWM conditioner - Need Help PIC16F1578 2019/06/10 20:22:04 (permalink)
    0
    How many bits resolution do you require?
    You might get away with using CCPs for the input measurement, and bit-bang for the outputs.
     

    I also post at: PicForum
    Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
    NEW USERS: Posting images, links and code - workaround for restrictions.
    To get a useful answer, always state which PIC you are using!
    #7
    pcbbc
    Super Member
    • Total Posts : 1310
    • Reward points : 0
    • Joined: 2014/03/27 07:04:41
    • Location: 0
    • Status: offline
    Re: 4 Channel PWM conditioner - Need Help PIC16F1578 2019/06/10 20:25:07 (permalink)
    0
    Trouble is CC/PWM modules are shared, and you appear to require 4 CC to do the incoming sampling and 4 PWM to do the outbound waveform generation. So 8 total.
    Do any of these share the same time base? If not that may be a problem also.

    As far as the 16F family goes there is the PIC16(L)F1526/7 which has 10 CCP modules, but it is 64-PIN TQFP or 64-PIN QFN package.

    Perhaps you are able to multiplex the 4 PWM inputs, and so (with some accepted latency) use only one CC module and 4 PWM for output? Of if high frequency or accuracy not required, do software PWM?

    I’m not an expert on the entire microchip range, but from some quick research this seems to be outside of the scope of the PIC16 family for a single device. But you can start with the MAPS part selection here and investigate yourself.
    #8
    Mobiz
    New Member
    • Total Posts : 7
    • Reward points : 0
    • Joined: 2019/06/07 18:27:47
    • Location: 0
    • Status: offline
    Re: 4 Channel PWM conditioner - Need Help PIC16F1578 2019/06/10 20:27:46 (permalink)
    0
    How about this MCU PIC18F24K42, not sure if this is overkill but... The chip has 4 CCPs, and 4 PWM channels. 
     
    Pg.2 
    • Four Capture/Compare/PWM (CCP) modules
    • Four 10-bit Pulse-Width Modulators (PWMs)
     
    The incoming is from a DALI-PWM Signal Converter DAP-04 series. So it could be possible they share a timebase. The frequency is only 1Khz which is pretty slow, I'd imagine its ok if there is some delay with the CCP multiplexed since it would just show a very small lag in actual dimming response. 
     
     
     
     
    post edited by Mobiz - 2019/06/10 20:32:48
    #9
    Mobiz
    New Member
    • Total Posts : 7
    • Reward points : 0
    • Joined: 2019/06/07 18:27:47
    • Location: 0
    • Status: offline
    Re: 4 Channel PWM conditioner - Need Help PIC16F1578 2019/06/16 20:19:22 (permalink)
    0
     
    I just wanted to give an update and also ask for some assistance. I got the PWM read to work using a gated timer for 1 channel. I am able to take in the PWM and output the same frequency PWM with 1/2 the duty cycle. 
     
    Now my issue is how to make it multichannel. The update rate doesn't matter too much as long as it's above 60Hertz. So I am thinking some type of muxing system for the input channels. The chip has two gated timers, but I dont think id need to use more than 1.  
     
    Any ideas on how I can get this thing to be muxed for the input channels? Everything I have tried so far just produces garbage. FYI this is on the PIC16F1578. 
     

    Pictures attached of a single channel working. Any help to make it multi channel will be appreciated!
     
    uint16_t Get_Pulse(void);
    unsigned int  Pulse;

    void main(void)
    {
    SYSTEM_Initialize();
    PWM1_Start();
    TMR1_Initialize();
    TMR1_StartSinglePulseAcquisition();
    SYSTEM_Initialize();

    while (1)
    {
    Pulse = Get_Pulse();
    PWM1_DutyCycleSet(Pulse/8);
    PWM1_PeriodSet(4000);
    PWM1_LoadBufferSet();

    }
    }
     
    Get Pulse: 
    uint16_t Get_Pulse(){
    unsigned int tm1;
    T1GCON = 0xD8; //enable timer1 gate, set toggle mode off
    T1CON = 0x45; //set clock to Fosc/4
    if (PIR1bits.TMR1GIF==1){
    tm1 = (TMR1L|(TMR1H << 8)); // take timer register value
    PIR1bits.TMR1GIF = 0; //reset timer1 gate flag
    TMR1H = 0x00; //clear timer registers
    TMR1L = 0x00;
    }
    return tm1;
    }
     
    #10
    ric
    Super Member
    • Total Posts : 23829
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: offline
    Re: 4 Channel PWM conditioner - Need Help PIC16F1578 2019/06/16 20:21:45 (permalink)
    +1 (1)
    You never answered this question
    ric
    How many bits resolution do you require?
    You might get away with using CCPs for the input measurement, and bit-bang for the outputs.


    I also post at: PicForum
    Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
    NEW USERS: Posting images, links and code - workaround for restrictions.
    To get a useful answer, always state which PIC you are using!
    #11
    Mobiz
    New Member
    • Total Posts : 7
    • Reward points : 0
    • Joined: 2019/06/07 18:27:47
    • Location: 0
    • Status: offline
    Re: 4 Channel PWM conditioner - Need Help PIC16F1578 2019/06/16 20:40:39 (permalink)
    0
    I need 8 Bits of resolution.
    #12
    Mobiz
    New Member
    • Total Posts : 7
    • Reward points : 0
    • Joined: 2019/06/07 18:27:47
    • Location: 0
    • Status: offline
    Re: 4 Channel PWM conditioner - Need Help PIC16F1578 2019/06/18 19:36:15 (permalink)
    0
    Ok, so it's confirmed I need 8 Bits of resolution. 
     
    I have gotten 4 channels kinda working on the PIC16F1578. Problem is if I switch up the duty cycle of the inputs it goes crazy. The 4 channels only works if the duty cycle is smallest to greatest, in the way it is being checked. Image attached of the scenario when it works. 
    For example:
    Ch1 25% 
    Ch2 50% 
    Ch3 75%
    Ch4 100%
    everything works
     
    Ch1 100% 
    Ch2 50% 
    Ch3 75% 
    ...
    everything breaks. 
     
    I am starting to think it is possible to do the divider on the PIC16F1578, assuming the channels are demuxed right. I am still using a gated timer, below is the main. 
     
    #include "mcc_generated_files/mcc.h"
    uint16_t Get_Period(void);
    uint16_t Get_Pulse();
    void Get_meas();
    uint16_t Get_Pulse_IOC(void);
    unsigned int Period,flag,pulse[4],tm1;

    void main(void)
    {
    SYSTEM_Initialize();
    PWM1_Start();
    PWM2_Start();
    PWM3_Start();
    PWM4_Start();
    TMR1_Initialize();
    TMR1_StartSinglePulseAcquisition();
    TMR0_Initialize();
    TMR2_Initialize();

    while (1)
    {
    PWM1_LoadBufferSet();
    PWM2_LoadBufferSet();
    PWM3_LoadBufferSet();
    PWM4_LoadBufferSet();

    if (INTCONbits.TMR0IF){
    Get_meas();
    TMR0_ISR();
    }
    }
    }
    // this is where the duty cycle is measured based on the timer interrupt. Flag is incremented to demux. 
    void Get_meas(){
    if (flag>=4){flag=0;}
    switch (flag)
    {
    case 0x0: T1GPPS = 0x16; //RC7 > PWM1
    break;
    case 0x1: T1GPPS = 0x13; //RC6 > PWM2
    break;
    case 0x2: T1GPPS = 0x14; //RC3 > PWM3
    break;
    case 0x3: T1GPPS = 0x17; //RC4 > PWM4
    break;
    default: flag = 0;
    INTCONbits.IOCIE = 0;
    INTCONbits.IOCIF = 0;
    }
    pulse[flag] = Get_Pulse();
    PWM1_DutyCycleSet(pulse[0]/8);
    PWM2_DutyCycleSet(pulse[1]/8);
    PWM3_DutyCycleSet(pulse[2]/8);
    PWM4_DutyCycleSet(pulse[3]/8);
    flag++;
    }
    uint16_t Get_Pulse(){
    //T1GPPS = pin; //attach pin
    T1GCON = 0xD8; //enable timer1 gate, set toggle mode off
    T1CON = 0x45; //set clock to Fosc/4
    T2CONbits.TMR2ON = 1;//start timer2
    do {
    if (PIR1bits.TMR1GIF==1){
    tm1 = (TMR1L|(TMR1H << 8)); // take timer register value
    PIR1bits.TMR1GIF = 0; //reset timer1 gate flag
    TMR1H = 0x00; //clear timer registers
    TMR1L = 0x00;
    return tm1;
    }
    }
    while (!PIR1bits.TMR2IF);
    TMR2_ISR();
    return 0;

    }
     
     
    Thanks
    #13
    Jump to:
    © 2019 APG vNext Commercial Version 4.5