• AVR Freaks

Hot!PWM and ADC am i doing this correctly?

Author
Tinkerer of Things
New Member
  • Total Posts : 4
  • Reward points : 0
  • Joined: 2019/03/29 06:32:05
  • Location: 0
  • Status: offline
2019/04/21 02:49:37 (permalink)
0

PWM and ADC am i doing this correctly?

Help me out here I'm at a loss!

I am trying to set my PWM1 duty cycle using an ADC result. What I want to do is to have a preset Duty cycle then when I press a button the DC updates to whatever my ADC is and then changes back to the preset DC.

Now its not working correctly and I'm not sure if its the part where I selected adcon0 and adcon1 bits or what the error is. Please take a look.

What I find the code does is it ignores my if statement and simply updates the PWM Duty cycle all the time. How can I make this work correctly?
 

unsigned int ADCresult=0; //declared variables
unsigned int PWM = 0;
 
void main()
{
 PortA = 0; //clear port a
 TRISA0_bit = 1; //set A0 as input
 PortC = 0; //clear port c
 TRISC2_bit=0; //set C2 as output
 PortE = 0; //clear port e
 TrisE1_bit= 1; //set E1 as input

 ADCON0=0b00000001; //A/D converter module is powered up
 ADCON1=0b11001110; // right justified, fosc/4 , and only AN0 is analog
 CMCON=7; //disabled comparator mode

 ADC_Init(); //initialize adc
 PWM1_Init(5000); //initialize pwm1
 PWM1_Start(); //start pwm1
 
 while(1) //while 1 loop forever
 {
  if(RE1_bit==0) //if RE1 is brought low
   {
    Delay_ms(50); //software de-bounce
    ADCresult = ADC_Get_Sample(0);
    PWM= ADCresult*255.0/1023.0;
    PWM1_Set_Duty(PWM); //update PWM DC obtained from ADC on AN0
    Delay_ms(3000); //use updated PWM DC
    }
   PWM1_Set_Duty(250); //Revert back to near max DC
   while(RE1_bit==1); //Pause here till RE1 is brought low again
  }
}

 
just for demonstration i attached a button for RE1 ; motor set to 3V for CCP1 and 5V max analog for AN0

Attached Image(s)

#1

10 Replies Related Threads

    pcbbc
    Super Member
    • Total Posts : 997
    • Reward points : 0
    • Joined: 2014/03/27 07:04:41
    • Location: 0
    • Status: online
    Re: PWM and ADC am i doing this correctly? 2019/04/22 08:52:06 (permalink)
    0
    Baby steps. Start by flashing a LED when you press the button. Otherwise you don’t know if it is “ignoring your button” or you just aren’t programming the PWM module correctly.
    Then more to changing the DC to a difffernt (fixed) value when you press the button.
    Then do the ADC step.

    Where is the missing code for PWM1_Set_Duty, etc?
    What device are you using?

    This is fairly pointless as PWM and ADCresult are integers:
    PWM= ADCresult*255.0/1023.0;
    What range does ADCresult have? For most values this formula is, for all intents and purposes, dividing by 4 (especially when you consider the likely accuracy of ADC results).
    #2
    jack@kksound
    code tags!
    • Total Posts : 3188
    • Reward points : 0
    • Joined: 2014/05/14 10:03:19
    • Location: 0
    • Status: offline
    Re: PWM and ADC am i doing this correctly? 2019/04/22 09:01:34 (permalink)
    0
    It is always a good idea to post ALL the code and to specify which compiler you are using (version also).
    #3
    jack@kksound
    code tags!
    • Total Posts : 3188
    • Reward points : 0
    • Joined: 2014/05/14 10:03:19
    • Location: 0
    • Status: offline
    Re: PWM and ADC am i doing this correctly? 2019/04/22 09:03:31 (permalink)
    0
    What device are you using?
    From the attached photo it appears to be a 16F877a.
    #4
    pcbbc
    Super Member
    • Total Posts : 997
    • Reward points : 0
    • Joined: 2014/03/27 07:04:41
    • Location: 0
    • Status: online
    Re: PWM and ADC am i doing this correctly? 2019/04/22 09:45:05 (permalink)
    0
    jack@kksound
    What device are you using?
    From the attached photo it appears to be a 16F877a.
    Ah, yes the PIC device of choice ;)

    I did actually spot that after posting.
    #5
    Tinkerer of Things
    New Member
    • Total Posts : 4
    • Reward points : 0
    • Joined: 2019/03/29 06:32:05
    • Location: 0
    • Status: offline
    Re: PWM and ADC am i doing this correctly? 2019/04/22 10:59:17 (permalink)
    0
    jack@kksound
    It is always a good idea to post ALL the code and to specify which compiler you are using (version also).


    I posted all the code. And its mikroc version 7.2 and used proteus 8 to simulate
    #6
    Tinkerer of Things
    New Member
    • Total Posts : 4
    • Reward points : 0
    • Joined: 2019/03/29 06:32:05
    • Location: 0
    • Status: offline
    Re: PWM and ADC am i doing this correctly? 2019/04/22 11:03:50 (permalink)
    0
    pcbbc
    Baby steps. Start by flashing a LED when you press the button. Otherwise you don’t know if it is “ignoring your button” or you just aren’t programming the PWM module correctly.
    Then more to changing the DC to a difffernt (fixed) value when you press the button.
    Then do the ADC step.

    Where is the missing code for PWM1_Set_Duty, etc?
    What device are you using?

    This is fairly pointless as PWM and ADCresult are integers:
    PWM= ADCresult*255.0/1023.0;
    What range does ADCresult have? For most values this formula is, for all intents and purposes, dividing by 4 (especially when you consider the likely accuracy of ADC results).

    I believe adc result is from 0-1023 and pwm is from 0-255.
     
    I did try out what you said it the code seems to just "ignore" my if button is pressed part.
     
    As for "Where is the missing code for PWM1_Set_Duty, etc?" im not sure i understand sorry.
    #7
    dan1138
    Super Member
    • Total Posts : 3097
    • Reward points : 0
    • Joined: 2007/02/21 23:04:16
    • Location: 0
    • Status: offline
    Re: PWM and ADC am i doing this correctly? 2019/04/22 12:39:00 (permalink)
    0
    Tinkerer of Things
    I posted all the code. And its mikroc version 7.2 and used Proteus 8 to simulate

    Most of the members on this forum do not use the MikroElektronika IDE or compilers.

    There are several reasons for this. Most common is that the MikroC IDE seems to hide the configuration setting that are frequently where new PIC developers encounter problems.

    A lot of forum members will avoid even trying to answer questions about projects using MikroC because it can be too hard to provide a complete project that just works for the new kid.

    Many times a new PIC developer will get hung up with some trivial settings issue in the IDE, compiler configuration or PIC configuration words.

    The code you posted:
    unsigned int ADCresult = 0;     // declared variables
    unsigned int PWM = 0;

    void main()
    {
        PortA = 0;                  // clear port a
        TRISA0_bit = 1;             // set A0 as input
        PortC = 0;                  // clear port c
        TRISC2_bit=0;               // set C2 as output
        PortE = 0;                  // clear port e
        TrisE1_bit= 1;              // set E1 as input
            
        ADCON0=0b00000001;          // A/D converter module is powered up
        ADCON1=0b11001110;          // right justified, fosc/4 , and only AN0 is analog
        CMCON=7;                    // disabled comparator mode
            
        ADC_Init();                 // initialize adc
        PWM1_Init(5000);            // initialize pwm1
        PWM1_Start();               // start pwm1
            
        while(1)                    // while 1 loop forever
        {
            if(RE1_bit==0)          // if RE1 is brought low
            {
                Delay_ms(50);       // software de-bounce
                ADCresult = ADC_Get_Sample(0);
                PWM= ADCresult*255.0/1023.0;
                PWM1_Set_Duty(PWM); // update PWM DC obtained from ADC on AN0
                Delay_ms(3000);     // use updated PWM DC
            }
            PWM1_Set_Duty(250);     // Revert back to near max DC
            while(RE1_bit==1);      // Pause here till RE1 is brought low again
        }
    }

    Leaves too many things about the ADC implementation uncertain.
    These are things you should be definite about:
    1) The target PIC part number. By implication this is found in the Proteus schematic you posted. It is more helpful to put a comment line in the code like: "// Target: PIC16F877A"

    2) The PIC you selected can use a system oscillator frequency up to 20MHz. For someone to be able to help you by checking that you have configured the ADC clock it is important to know your actual value.

    3) Your code calls MikroC functions: ADC_Init(), ADC_Get_Sample(), PWM1_Init(), PWM1_Start(), PWM1_Set_Duty(). It is not obvious what these function do in detail or if you are using them correctly.
    post edited by dan1138 - 2019/04/22 12:40:34
    #8
    Tinkerer of Things
    New Member
    • Total Posts : 4
    • Reward points : 0
    • Joined: 2019/03/29 06:32:05
    • Location: 0
    • Status: offline
    Re: PWM and ADC am i doing this correctly? 2019/04/22 22:39:05 (permalink)
    0
    dan1138
    Tinkerer of Things
    I posted all the code. And its mikroc version 7.2 and used Proteus 8 to simulate

    Most of the members on this forum do not use the MikroElektronika IDE or compilers.

    There are several reasons for this. Most common is that the MikroC IDE seems to hide the configuration setting that are frequently where new PIC developers encounter problems.

    A lot of forum members will avoid even trying to answer questions about projects using MikroC because it can be too hard to provide a complete project that just works for the new kid.

    Many times a new PIC developer will get hung up with some trivial settings issue in the IDE, compiler configuration or PIC configuration words.

    The code you posted:
    unsigned int ADCresult = 0;     // declared variables
    unsigned int PWM = 0;

    void main()
    {
        PortA = 0;                  // clear port a
        TRISA0_bit = 1;             // set A0 as input
        PortC = 0;                  // clear port c
        TRISC2_bit=0;               // set C2 as output
        PortE = 0;                  // clear port e
        TrisE1_bit= 1;              // set E1 as input
            
        ADCON0=0b00000001;          // A/D converter module is powered up
        ADCON1=0b11001110;          // right justified, fosc/4 , and only AN0 is analog
        CMCON=7;                    // disabled comparator mode
            
        ADC_Init();                 // initialize adc
        PWM1_Init(5000);            // initialize pwm1
        PWM1_Start();               // start pwm1
            
        while(1)                    // while 1 loop forever
        {
            if(RE1_bit==0)          // if RE1 is brought low
            {
                Delay_ms(50);       // software de-bounce
                ADCresult = ADC_Get_Sample(0);
                PWM= ADCresult*255.0/1023.0;
                PWM1_Set_Duty(PWM); // update PWM DC obtained from ADC on AN0
                Delay_ms(3000);     // use updated PWM DC
            }
            PWM1_Set_Duty(250);     // Revert back to near max DC
            while(RE1_bit==1);      // Pause here till RE1 is brought low again
        }
    }

    Leaves too many things about the ADC implementation uncertain.
    These are things you should be definite about:
    1) The target PIC part number. By implication this is found in the Proteus schematic you posted. It is more helpful to put a comment line in the code like: "// Target: PIC16F877A"

    2) The PIC you selected can use a system oscillator frequency up to 20MHz. For someone to be able to help you by checking that you have configured the ADC clock it is important to know your actual value.

    3) Your code calls MikroC functions: ADC_Init(), ADC_Get_Sample(), PWM1_Init(), PWM1_Start(), PWM1_Set_Duty(). It is not obvious what these function do in detail or if you are using them correctly.


    Greetings Dan1138 thanks for the response...i would appreciate it if you could tell me where to begin then, as i am still just a beginner as it is so let me get into good habits and use the preferred methods. If not MikroC IDE what is a good option?
    #9
    pcbbc
    Super Member
    • Total Posts : 997
    • Reward points : 0
    • Joined: 2014/03/27 07:04:41
    • Location: 0
    • Status: online
    Re: PWM and ADC am i doing this correctly? 2019/04/23 00:08:26 (permalink)
    +1 (1)
    Tinkerer of Things I did try out what you said it the code seems to just "ignore" my if button is pressed part.

    Get rid of all of this code. Start with a LED flashy. Does that work?
    Modify the LED flashy to only flash, or do a different or slower pattern, when you press a button. Does that work?
    i.e. Can you detect a button press at all?

    As for "Where is the missing code for PWM1_Set_Duty, etc?" im not sure i understand sorry.

    Sorry, you are using MikroElektronika libraries.
    As others have pointed out, most people around here are unfamiliar with that environment, and roll their own code on 8 bit parts.
    Could well be some aspect of MikroElektronika (missing call, incorrect usage) that we are not familiar with.
    #10
    dan1138
    Super Member
    • Total Posts : 3097
    • Reward points : 0
    • Joined: 2007/02/21 23:04:16
    • Location: 0
    • Status: offline
    Re: PWM and ADC am i doing this correctly? 2019/04/23 10:59:37 (permalink)
    0
    Tinkerer of Things

    Greetings Dan1138 thanks for the response...i would appreciate it if you could tell me where to begin then, as i am still just a beginner as it is so let me get into good habits and use the preferred methods. If not MikroC IDE what is a good option?

    As a beginner there are no easy options. To continue with MikroC you will be better served using the MikroElektronika users forum.

    The members on this forum use the Microchip MPLABX and XC8 tools on a WindowsOS though Linux and MacOS are seeing more representation.

    For your case it seems that you need to focus on a project that is simpler.

    If you are still committed to this project you will need to CLEARLY describe ALL of behaviors your application must perform.

    From what you have posted thus far you have not been clear enough about what results are required.

    It seems to me that you have mixed the how and what you are doing together. More experience will solve issues with how to use the tools you have chosen. It is essential to have a complete understanding what needs to be accomplish.

    My suggestion is for you to focus on the what rather than the how of your application.
    #11
    Jump to:
    © 2019 APG vNext Commercial Version 4.5