PWM pins in jumpers

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

Re: PWM pins in jumpers

Postby Schatz143 » Fri Jan 24, 2020 1:35 pm

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
Experienced Member
Posts: 71
Joined: Thu Dec 10, 2009 7:08 pm

Postby migueljds » Fri Jan 24, 2020 2:45 pm

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 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 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

Postby Schatz143 » Fri Jan 24, 2020 3:14 pm

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: 1776
Joined: Thu Jun 10, 2010 11:43 am

Postby mon2 » Wed Jan 29, 2020 2:04 am

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

Postby Schatz143 » Tue Feb 04, 2020 3:08 pm

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
Respected Member
Posts: 428
Joined: Thu Nov 26, 2015 11:47 pm

Postby akp » Thu Feb 06, 2020 7:05 pm


Who is online

Users browsing this forum: No registered users and 3 guests