Skip to main content
Visitor II
June 24, 2004
Question

software PWM

  • June 24, 2004
  • 5 replies
  • 888 views
Posted on June 24, 2004 at 02:25

software PWM

    This topic has been closed for replies.

    5 replies

    Visitor II
    June 22, 2004
    Posted on June 22, 2004 at 07:36

    Hi,

    I'm trying to generate code in a cheap ST7LITE09 with 3 sofware PWM's. But it doesn't work and i can't find the mistake.

    Any idea's.

    This is the code:

    #include ''Io7FLite0.h'' // ST7Flite0 memory and registers mapping

    #include ''lib_bits.h'' // Predifined libraries working at bit level

    #include ''bitdef.h'' // This file defines the names of the bits

    int tijd,teller;

    int pwm1,pwm2,pwm3;

    void main(void)

    {

    LTCSR=0x10;

    ATCSR=0x12;

    PBDDR=0x07;

    PBOR=0x07;

    PBDR=0x00;

    teller = 0;

    pwm1 = 125; // write testvalue into pwm (50% dutycycle)

    pwm2 = 125;

    pwm3 = 125;

    _asm(''rim'');

    while(0)

    {

    }

    }

    @interrupt void AR_timer(void)

    {

    if (teller == 0)

    {

    SetBit(PBDR,0);

    SetBit(PBDR,1);

    SetBit(PBDR,2);

    }

    if (teller == pwm1)

    ClrBit(PBDR,0);

    if (teller == pwm2)

    ClrBit(PBDR,1);

    if (teller == pwm3)

    ClrBit(PBDR,2);

    teller++;

    tijd = ATCSR; // Reset timer flag

    }

    Thanks in advance!
    Visitor II
    June 22, 2004
    Posted on June 22, 2004 at 10:14

    Below is a simple 3 channel pwm - freq 192Hz 0-100Duty.

    Alter the g_cCounter reload value to adjust resolution/speed, eg.

    change reload g_cCounter with 20 will alter pwm freq to approx 960Hz, the resolution will be much less - 20 steps rather than 100.

    Adjust to suit your project.

    unsigned char g_cCounter;

    counter for motor pwm

    volatile unsigned char g_cPwms[3]; // Motor PWM speeds

    void main(void)

    {

    g_cPwms[0] = 50;

    g_cPwms[1] = 30;

    g_cPwms[2] = 50;

    g_cCounter = 0;

    ATR = 0xE5F;

    ATCSR = 0x12;

    PBDDR=0x07;

    PBOR=0x07;

    PBDR=0x00;

    rim();

    while(1);

    }

    @interrupt void AR_timer(void)

    {

    if( g_cCounter >= g_cPwms[0] )

    ClrBit( PBDR, 0 );

    else

    SetBit( PBDR, 0 );

    if( g_cCounter >= g_cPwms[1] )

    ClrBit( PBDR, 1 );

    else

    SetBit( PBDR, 1 );

    if( g_cCounter >= g_cPwms[2] )

    ClrBit( PBDR, 2 );

    else

    SetBit( PBDR, 2 );

    if( ++g_cCounter >= 100 )

    g_cCounter = 0;

    ATCSR; // clear OVF flag

    }

    One point to remember as this device does not have nested interrupts, the ar timer interrupt is low priority - any other interrupt will seriously interfere with the software pwm above.

    Regards

    sjo

    [ This message was edited by: sjo on 22-06-2004 13:49 ]
    Visitor II
    June 22, 2004
    Posted on June 22, 2004 at 12:23

    one important difference between sjo's code and yours (although I'm not sure that's where your problem is coming from) is the use of the ''volatile'' keyword. More details at http://www.stmcu.com/forums-cat-1665-1.html

    Regards,

    Luca

    [ This message was edited by: _luca on 22-06-2004 15:53 ]
    Visitor II
    June 23, 2004
    Posted on June 23, 2004 at 05:44

    Hi,

    I have changed my code in the interrupt module to the code Sjo gave, and it works!

    I think the problem was that when i write 'teller++' and teller = 255, it does not become 0 (as i thought), but it stays

    I'm not sure this is the cause of the trouble, but i'll experiment with it when i have more time.

    Thanks for your help!!!

    Visitor II
    June 24, 2004
    Posted on June 24, 2004 at 02:25

    sjo,

    If I want to make the PWM frequency higher to 10K/20K, if your software can

    do this?

    best regards

    mcuinterest