Shop OBEX P1 Docs P2 Docs Learn Events
Simple problem with program — Parallax Forums

Simple problem with program

ProfessorwizProfessorwiz Posts: 153
edited 2007-05-29 02:02 in General Discussion
Hello, I've been using PBasic and have just been going though my programs making them work with the SX, I've changed a simple one that flashes a couple led going though a transistor, and one just going though a 470ohm resistor.· The problem is I have 2 programs and one works and one doesn't and I can't see why the 1st doesn't.· My schematic is attached for the pin with the transistor.· Can anyone tell me what I'm overlooking?

Program 1:

'
' Device Settings
'
DEVICE········· SX28, OSCHS3, TURBO, STACKX, OPTIONX
IRC_CAL··IRC_SLOW
FREQ··········· 5_000_000
PROGRAM··start_point

'
' IO Pins
'
'pin0 to transistor and 3 leds
'pin1 to resistor and 1 led

'
' Variables
'
ctn·var·byte
'
TRIS_B=0
RB=0
·'
'··········· PROGRAM CODE
'
start_point:
·for ctn =1 to 10
··RB=%00000000
··pause 100
··RB=%11111111
··pause 100
·next
·goto start_point


Program #2
' =========================================================================
'
'·· File...... TEMPLATE.SXB
'·· Purpose... SX/B Programming Template
'·· Author....·
'·· E-mail....·
'·· Started...
'·· Updated...
'
' =========================================================================
'
' Program Description
'

'
' Device Settings
'
DEVICE········· SX28, OSCHS3, TURBO, STACKX, OPTIONX
IRC_CAL··IRC_SLOW
FREQ··········· 5_000_000
PROGRAM··start_point
'
' IO Pins
'
'Pin0 to transistor and 3leds
'Pin1 to resistor and 1 led
' Variables
'
CNT·VAR·BYTE
spd·var·byte
'
·' INTERRUPT
'
' Program Code
'
start_point:
·spd =·10
·TRIS_B=0
·RB=0

FOR CNT = 1 TO 250
·RB=%01111110
PAUSE spd
·RB=%11111101
PAUSE spd
NEXT
goto start_point

Comments

  • BeanBean Posts: 8,129
    edited 2007-05-26 12:08
    In the first program the "TRIS_B = 0" and "RB = 0" instructions will never get executed because the program starts at "start_point".
    If you define your pins you can set them as output without having to use "TRIS_".
    Program 1:
     
    ' -------------------------------------------------------------------------
    ' Device Settings
    ' -------------------------------------------------------------------------
    DEVICE   SX28, OSCHS3, TURBO, STACKX, OPTIONX
    IRC_CAL  IRC_SLOW
    FREQ     5_000_000
    PROGRAM  start_point
    
    ' -------------------------------------------------------------------------
    ' IO Pins
    ' -------------------------------------------------------------------------
    'pin0 to transistor and 3 leds
    'pin1 to resistor and 1 led 
     
    LEDS    PIN RB OUTPUT ' Port RB will be set to all outputs at startup
    
    ' -------------------------------------------------------------------------
    ' Variables
    ' -------------------------------------------------------------------------
    ctn var byte
     
    ' -------------------------------------------------------------------------
    '            PROGRAM CODE
    ' -------------------------------------------------------------------------
    start_point:
     for ctn = 1 to 10
      LEDS = %00000000
      pause 100
      LEDS = %11111111
      pause 100
     next
     goto start_point
    
    


    P.S. In your schematic you have the LEDs upside down, they won't light if they are connected as shown.

    Bean.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    “The United States is a nation of laws -· poorly written and randomly enforced.” - Frank Zappa

    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    www.hittconsulting.com


    Post Edited (Bean (Hitt Consulting)) : 5/26/2007 12:15:36 PM GMT
  • JonnyMacJonnyMac Posts: 9,214
    edited 2007-05-26 13:57
    The other problem with your schematic is that the resistors are not limiting the current through the LEDs and if you leave them on for a long time you may destroy them. I think you want a circuit more like what's attached (select appropriate values).
    317 x 442 - 5K
  • ProfessorwizProfessorwiz Posts: 153
    edited 2007-05-26 18:55
    I had asked the question before about the leds in with the transistor and no resistor when I was playing with the stamp. I'm basically following the schematic in the "What's a microcontroller" book page 265. It was my understanding that the transistor would not be fully saturated therefor did not need a resistor. In my drawing I just made an opps and had the leds the wrong way.

    Thanks for catching my mistake! I knew it was something foolish!

    Russ
  • JonnyMacJonnyMac Posts: 9,214
    edited 2007-05-26 19:02
    I would suggest the circuit above which does saturate the transistor -- that way you can turn the LEDs full on or dim them with any number of PWM techniques. Use a 1K series resistor and a 2N3904 and you'll be set. Select the current limiters based on the LED forward voltage plus about 0.3v for Vce.
  • ProfessorwizProfessorwiz Posts: 153
    edited 2007-05-26 21:13
    Yep, that's a bit much for a guy who had trouble turning on a LED..hehhe. So I should use the 1k resistor going into the 2n3904. If I'm using the SX Tech board and the Vdd, would ok? The LED's say the forward voltage is 3.0-3.2v. I'm just started to get into electronics and don't have a clue on how to calculate the proper voltage.
  • ProfessorwizProfessorwiz Posts: 153
    edited 2007-05-27 01:00
    Ok, I fixed the schematic I think.· Please let me know if you see something else wrong.· Thanks so much for all you help!·

    Russ
  • JonnyMacJonnyMac Posts: 9,214
    edited 2007-05-27 05:21
    With 470-ohm resistors you should get about 3.8 or so milliamps through your LEDs -- probably won't be very bright. The formula is pretty easy:

    iLed = (Vsource - Vled) / Rlimiter

    so...

    (5.0 - 3.2) / 470 = 0.0038

    If you wanted to get 10 mA through the LEDs you can do that by dropping to a 180 ohm resistor. Since you're just getting started, let me recommend you get a copy of the Forrest M. Mims book, "Getting Started In Electronics" -- it will be a very big help as you move forward with your experiments.
  • ProfessorwizProfessorwiz Posts: 153
    edited 2007-05-29 01:50
    I had that book about 20 years ago when I worked at Radio Shack, looks like I'll have to buy it again! I was reading though the info on those super bright leds and they'll take up to a max of 30 ma continous, so I'd be safe with even 100 ohm resistors if I use your formula correctly. That would only be 18 ma correct?
    Russ
  • JonnyMacJonnyMac Posts: 9,214
    edited 2007-05-29 02:02
    Yes, (5 - 3.2) / 100 = 0.018; 18 milliamps.
Sign In or Register to comment.