PWM pins in jumpers

All technical discussions and projects around startKIT
Schatz143
Member++
Posts: 31
Joined: Mon Jan 20, 2020 9:54 am

Post by Schatz143 »

Code: Select all



#include <xs1.h>
#include <platform.h>
#include <gpio.h>
#include <timer.h>
#include <stdio.h>

out port d1 = XS1_PORT_1E;   /* SIGNAL AT D12 */
out port d2 = XS1_PORT_1F;   /*SIGNAL AT D13*/
clock clk = XS1_CLKBLK_1;


void dimm4(out port p)
{
    timer tmr;
    int port_count;
    int dim_period = 250*60000;
    int dim_value = dim_period*0.5;
    int state = 0;
    tmr :> port_count;
    port_count += dim_period;
    while (1) {
        select {
            case tmr when timerafter(port_count) :> void:
                p @ port_count <: state;
                state = !state;
                if (state)
                    port_count += dim_period - dim_value;
                else
                    port_count += dim_value;
            break;
        }
    }
}

void dimm3(out port p)
{
    int port_count;
    int dim_value = 3000000;
    int dim_period = 6000000;

    configure_clock_rate(clk, 25, 1);
    configure_out_port(p, clk, 0);
    start_clock(clk);

    p <: 0 @ port_count;
    port_count += dim_period;

    while (1)
    {
        p @ port_count <: 0;
        p @ port_count + (dim_period-dim_value) <: 1;
        port_count += dim_period;
    }
}


int main ()
{
    par
    {
        dimm4(d1);
        dimm3(d2);
    }
    return 0;
}










I tried this code above and found both signals at 1E(D12 pin on kit) and 1F(D13) but they are with fixed 50 % dutycycle.Is there anyway i can vary dutycycle from 0 to 100% ? and for a frequency of 10KHz or 20KHz?
User avatar
migueljds
Verified
Experienced Member
Posts: 96
Joined: Thu Dec 10, 2009 7:08 pm

Post by migueljds »

The old projects and Q&A sections of this site was moved into the forum a long while back.

http://www.xcore.com/projects/simple-servo-driver has been moved here https://www.xcore.com/viewtopic.php?f=21&t=5437 (in the Projects category)
and
http://www.xcore.com/questions/2848/i-require-8-pwm has been moved here https://www.xcore.com/viewtopic.php?f=47&t=3913 (in the Q&A category)

I've added a redirects now for these two links, so it takes you there automatically.
Schatz143
Member++
Posts: 31
Joined: Mon Jan 20, 2020 9:54 am

Post by Schatz143 »

Hi migueljds!.
Thanks for the links.I read the link for 8 pwm but all of those have a fixed dutycycle( would stand corrected if am wrong) and for the servo driver link i got an error while using the code .
Would be much appreciated if i can actually change the duty cycle of PWM signal generated in the above written code.
I would be glad if anyone can point out the way to go achieve varying dutycycle .
Regards
User avatar
mon2
XCore Legend
Posts: 1913
Joined: Thu Jun 10, 2010 11:43 am

Post by mon2 »

Hi Schatz143.

If you change the value of dim_period, you should be able to achieve your goal.

Code: Select all

int dim_period = 250*60000;
change to:

Code: Select all

int dim_period = 125*60000;
or

Code: Select all

int dim_period = 500*60000;
Schatz143
Member++
Posts: 31
Joined: Mon Jan 20, 2020 9:54 am

Post by Schatz143 »

Hi mon2!
I would like to have 10kHz frequency at the end.
And also the statement here i cannot understand "port_count += dim_period"
isnt port count is in binary? how can we assign dim_period?
Also these statements in the while loop i could not understand properly.
Can you please comment on the code?

Code: Select all


while (1) {
        select {
            case tmr when timerafter(port_count) :> void:
                p @ port_count <: state;
                state = !state;
                if (state)
                    port_count += dim_period - dim_value;
                else
                    port_count += dim_value;
            break;
        }



User avatar
akp
XCore Expert
Posts: 580
Joined: Thu Nov 26, 2015 11:47 pm

Post by akp »