• AVR Freaks

Hot!RE: Assigning values to PDC0L and PDC0H

Author
Dracarys99
New Member
  • Total Posts : 8
  • Reward points : 0
  • Joined: 2019/05/23 22:22:45
  • Location: 0
  • Status: offline
2019/06/22 22:44:24 (permalink)
0

RE: Assigning values to PDC0L and PDC0H

Hi
for pic18f4431
I need to make some computations on the analog value input given to the ADC, & then assign the computed value to the duty cycle registers PDC0L AND PDC0H to give PWM output
i read the value from the adc as 256*ADRESH+ADRESL
made the computation
how to assign this to PDCoL and PDC0H?
#1

8 Replies Related Threads

    ric
    Super Member
    • Total Posts : 23859
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    RE: Assigning values to PDC0L and PDC0H 2019/06/24 22:19:11 (permalink)
    +1 (1)
    Assuming you have the value in a 16 bit variable named "pwmvalue"
    PDC0L = LOW_BYTE(pwmvalue)
    PDC0H = HIGH_BYTE(pwmvalue);

     
    Newer PIC devices have the registers arranged so you can just do "PDC0 = pwmvalue", but you can't in older devices like the PIC18F4431.
    post edited by ric - 2019/06/24 22:20:36

    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
    Dracarys99
    New Member
    • Total Posts : 8
    • Reward points : 0
    • Joined: 2019/05/23 22:22:45
    • Location: 0
    • Status: offline
    RE: Assigning values to PDC0L and PDC0H 2019/06/26 04:30:30 (permalink)
    0
    HI
    somehow, i think there is something wrong with the assignment in the following code:- (I'm not getting any output in the proteus simulation)
    ------------------------------------------
    #include<math.h>
    #include<xc.h>
    #include <pic18f4431.h>
    #include<stdio.h>
    #include<stdint.h>
    #include<ctype.h>

    #pragma config OSC=IRCIO, PWM4MX = RB5 //use internal oscillator, multiplex PWM4 pin onto RB5
    int R=5;
    float C=0.02;
    float dydx(float x, float y)
    {
    return((x-y)*2); //taking 500 ms so multiply by 2
    }

    float rungeKutta(float x0, float y0, int p, float h)
    {

    int n = (int)((p - x0) / h);
    //int n = 1; //if 1 step is needed
    float k1, k2, k3, k4, k5;

    float y = y0;
    for (int i=1; i<=n; i++)
    {

    k1 = h*dydx(x0, y);
    k2 = h*dydx(x0 + 0.5*h, y + 0.5*k1);
    k3 = h*dydx(x0 + 0.5*h, y + 0.5*k2);
    k4 = h*dydx(x0 + h, y + k3);
    // Update next value of y and x
    y = y + (1.0/6.0)*(k1 + 2*k2 + 2*k3 + k4);
    x0 = x0 + h;
    }
    return y;
    }
    void main(void){
    OSCCON= 0b11111111; //8Mhz oscillator
    TRISA= 0b00000001; //Set pin RA0 to input
    ANSEL0= 0b00000001; //Set RA0 to analog

    // avoid floating inputs
    ANSEL1= 0;
    LATA = 0;
    TRISB = 0;
    LATB = 0;
    TRISC = 0;
    LATC = 0;
    TRISD = 0;
    LATD = 0;
    TRISE = 0;
    LATE = 0;

    //ADC setup
    ADCON0 = 0b00000001; //single-shot mode A
    ADCON1 = 0b00000000; // AVdd and AVss for references
    ADCON2 = 0b10010100; //Fosc/4
    ADCON3 = 0b00000000; //no triggers
    ADCHS = 0b00000000; //Use AN0

    //PWM Setup
    PTPERH = 0b00000000;
    PTPERL = 0b11111110;
    PTCON0 = 0b00001000;
    PTCON1 = 0b10000000;
    PWMCON0 = 0b00010000;
    PWMCON1 = 0b00000000; //No special event triggers

    while(1){
    ADCON0bits.GO_DONE=1;
    while(ADCON0bits.GO_DONE){}
    int result; float res;
    int duty_high,duty_low,ans=0;
    result=0x0000; //next 4 steps is to hold result of conversion
    result =(ADRESH<<8)|ADRESL;
    res = rungeKutta(0,0,result,0.0002);
    // res = res/204.8;
    //res = res*(819.2);
    res = res*4;
    if(fabs(res - ceil(res))> fabs(res - floor(res)))
    ans = floor(res);
    else
    ans = ceil(res);
    //duty_low =res%256;
    //duty_high = (res - duty_low)>>8;

    PDC0H = HIGH_BYTE(ans);
    PDC0L = LOW_BYTE(ans);
    }
    } //end main()
     
     
    Please correct this
    #3
    ric
    Super Member
    • Total Posts : 23859
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    RE: Assigning values to PDC0L and PDC0H 2019/06/26 04:37:51 (permalink)
    +1 (1)
    Why test with something so complicated?
    Start with something that just sets a few arbitrary values, or outputs a staircase.
    Only when you get simple things working should you move up to more complex cases.
     
    oh and by the way, delete this line
    #include <pic18f4431.h>
    Never include the device specific file, xc.h has already done it for you.
    You almost certainly don't need stdio.h either. Get out of the Windows habit of just including every header file you can think of.
     
     

    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
    Dracarys99
    New Member
    • Total Posts : 8
    • Reward points : 0
    • Joined: 2019/05/23 22:22:45
    • Location: 0
    • Status: offline
    RE: Assigning values to PDC0L and PDC0H 2019/06/26 04:45:37 (permalink)
    0
    The first task was to take an input from the adc (given by a pot) and set the duty cycle of the pwm....This gave the pwm output as reqd...the next task is to take an adc value,make a computation on it(solve a differential equation) get the corresponding output and scale it to get pwm output.I think it's not getting assigned bcoz I checked the diff eqn output & that's right
    #5
    ric
    Super Member
    • Total Posts : 23859
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    RE: Assigning values to PDC0L and PDC0H 2019/06/26 05:16:31 (permalink)
    +1 (1)
    So where is your problem?
    Reading the ADC, scaling it, or writing to the PWM?
     

    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!
    #6
    Dracarys99
    New Member
    • Total Posts : 8
    • Reward points : 0
    • Joined: 2019/05/23 22:22:45
    • Location: 0
    • Status: offline
    RE: Assigning values to PDC0L and PDC0H 2019/06/26 05:53:55 (permalink)
    0
    I think the scaling part is incorrect....somehow it becomes 0 i guess...
    I can't find the problem Because it runs fine separately.
    I figured the aassignment isn't wrong because I gave res a random value and then it gave a pwm output
    post edited by Dracarys99 - 2019/06/26 06:55:46
    #7
    ric
    Super Member
    • Total Posts : 23859
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    RE: Assigning values to PDC0L and PDC0H 2019/06/26 13:31:04 (permalink)
    +1 (1)
    So single step your code in the debugger, and watch your intermediate values,
    or arrange a serial output, and output some debugging info.
     

    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!
    #8
    dan1138
    Super Member
    • Total Posts : 3229
    • Reward points : 0
    • Joined: 2007/02/21 23:04:16
    • Location: 0
    • Status: offline
    RE: Assigning values to PDC0L and PDC0H 2019/06/26 14:05:58 (permalink)
    +2 (2)
    Dracarys99
    somehow, i think there is something wrong with the assignment in the following code:- (I'm not getting any output in the proteus simulation)

    < see post #3 for code >

    Please correct this

    I appreciate that this is course work so may never be loaded in to real world hardware.

    There appears to be more wrong in your code than simple syntax errors.

    The Runge-Kutta method is computationally intensive. Other methods may achieve a solution in less real time.

    It seems your implementation may be flawed.

    Validate that your implementation of the "float rungeKutta(float x0, float y0, int p, float h)" function returns correct values for all valid input values.

    Note: The PIC18F4431 is not a good choice for computing floating point functions in real time.


    #9
    Jump to:
    © 2019 APG vNext Commercial Version 4.5