Shop OBEX P1 Docs P2 Docs Learn Events
Problem with differential counter modules — Parallax Forums

Problem with differential counter modules

EnriqueEnrique Posts: 90
edited 2012-09-05 11:53 in Propeller 1
Hello,

I am going through the Propeller Education Kit Labs but coding in C instead of Spin and I have encountered a problem in chapter 7, Counter Modules and Circuit Applications Lab.

I hooked up a LED to pin 4 and another one to pin 5 and I am trying to change their brightness using differential duty mode. When I run the program LED 4 works without any problem but LED 5 doesn't turn on at all. I checked the LED that refuses to cooperate and pin 5 of the Propeller and they both work.

Here is the code
/**
 * @file LedDutySweep.c
 * This is the main LedDutySweep program start point.
 */

/**
 * Main program function.
 */

#include <propeller.h>
#include "pin.h"
#include "counter.h"

int main(void)
{
    const int scale=16777216;
    int duty;
    int delta;

    // Prepare the CTRA register
    ctraSetMode(DUTY_DIFFERENTIAL);
    ctraSetApin(4);
    ctraSetBpin(5);
    pinOutput(4);
    pinOutput(5);

    delta=1;
    duty=100;

    //
    while(1)
    {
        waitcnt(CLKFREQ/100+CNT);
        FRQA=duty*scale;
        duty+=delta;
        if (duty==255)
            delta=-1;
        if (duty==100)
            delta=1;
    }
    
    return 0;
}

I am attaching counter.h and counter.c for those of you who want to duplicate the problem.

Thanks,
Enrique

Comments

  • jazzedjazzed Posts: 11,803
    edited 2012-09-05 08:45
    Hi Enrique​.

    At first glance I don't see why there would be a problem. I trust that there is an issue of course.

    Sorry my programming time is limited today (and any Wednesday). I'll try the example tomorrow if someone else doesn't get to it first.

    --Steve
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2012-09-05 09:10
    I don't know how C handles math overflow during multiplication, but 16777216 (0x1000000) times anything over 127 will produce a negative number, which is okay unless C intervenes. Try just shifting duty left by 24 bits instead and see what happens:
    FRQA = duty << 24
    

    -Phil
  • EnriqueEnrique Posts: 90
    edited 2012-09-05 11:04
    Phil

    The shifting didn't help.

    Enrique
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2012-09-05 11:23
    Here's the equivalent program in Spin:
    CON
    
      _clkmode      = xtal1 + pll16x
      _xinfreq      = 5_000_000
    
      scale         = 16777216
    
    PUB  start | duty, delta
    
      ctra := %00111 << 26 | 5 << 9 | 4
      dira[4..5]~~
      delta := 1
      duty := 100
    
      repeat
        waitcnt(clkfreq / 100 + cnt)
        frqa := duty * scale
        duty += delta
        if (duty == 255)
          delta := -1
        if (duty == 100)
          delta := 1
    

    It works on my Prop demo board; but, because the duty cannot go lower than 100, the LED connected to pin 5 glows only very dimly and very briefly. If you let the duty go to zero, your pin 5 LED might show more enthusiasm.

    -Phil
  • EnriqueEnrique Posts: 90
    edited 2012-09-05 11:53
    Phil,

    You are right, when I let duty go down to 0 both LEDs work.

    Thanks,
    Enrique
Sign In or Register to comment.