Shop OBEX P1 Docs P2 Docs Learn Events
SX48 PWM Problem — Parallax Forums

SX48 PWM Problem

djh82ukdjh82uk Posts: 193
edited 2007-07-17 00:21 in General Discussion
Heya Guys

I tried the example from the help file on PWM, and I get the an erro saying TrisB is not defined. Any Ideas?
Somebody said...

'
' Program Description
'
'
' Uses PWM through an RC network to create an analog voltage.

'
' Device Settings
'

DEVICE SX28, OSC4MHZ, TURBO, STACKX, OPTIONX
FREQ 4_000_000
ID "PWM"

'
' IO Pins
'

DAC VAR RB.0 ' output to RC network

'
' Variables
'

angle VAR Byte
duty VAR Byte

tmpB1 VAR Byte
tmpB2 VAR Byte

' =========================================================================
PROGRAM Start
' =========================================================================

'
' Subroutine Declarations
'

SIN SUB 1 ' returns sine of angle
COS SUB 1 ' returns cosine of angle

'
' Program Code
'

Start:
DO
FOR angle = 0 TO 255 ' sweep through angles
duty = SIN angle ' get sine of angel
duty = duty + 127 ' bias to 2.5 v
PWM DAC, duty, 1 ' charge RC network
NEXT
LOOP

'
' Subroutine Code
'

' Use: value = SIN angle
' -- "value" returned as signed byte (-127 to 127)
' -- angle is expressed in Brads (0 - 255)

SIN:
tmpB1 = __PARAM1 ' get angle
tmpB2 = tmpB1 ' make copy
IF tmpB2.6 = 1 THEN ' in 2nd or 4th quadrant?
tmpB1 = 0 - tmpB1 ' yes, move to 1st/3rd
ENDIF
tmpB1.7 = 0 ' reduce to 1st quadrant
READ SineTable + tmpB1, tmpB1 ' read sine
IF tmpB2.7 = 1 THEN ' was angle in 3rd/4th?
tmpB1 = 0 - tmpB1 ' yes, adjust
ENDIF
RETURN tmpB1

'

' Use: value = COS angle
' -- "value" returned as signed byte (-127 to 127)
' -- angle is expressed in Brads (0 - 255)

COS:
tmpB1 = __PARAM1 + $40 ' get angle (adjust phase)
tmpB1 = SIN tmpB1 ' call sine table
RETURN tmpB1


' =========================================================================
' User Data
' =========================================================================

SineTable:
DATA 000, 003, 006, 009, 012, 016, 019, 022
DATA 025, 028, 031, 034, 037, 040, 043, 046
DATA 049, 051, 054, 057, 060, 063, 065, 068
DATA 071, 073, 076, 078, 081, 083, 085, 088
DATA 090, 092, 094, 096, 098, 100, 102, 104
DATA 106, 107, 109, 111, 112, 113, 115, 116
DATA 117, 118, 120, 121, 122, 122, 123, 124
DATA 125, 125, 126, 126, 126, 127, 127, 127
DATA 127

Comments

  • BeanBean Posts: 8,129
    edited 2007-07-12 15:30
    It's TRIS_B

    Bean.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Teacher: What is the difference between ignorance and apathy ?
    Student: I don't know and I don't care
    Teacher: Correct !
    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    www.hittconsulting.com
    ·
  • djh82ukdjh82uk Posts: 193
    edited 2007-07-12 19:18
    Sorry I typed it from memory (not good)

    it still does not mentions tris_b in the example from the manual however

    Anyone advise?

    Sorry to be a pain

    Thanks

    DJH
  • Sparks-R-FunSparks-R-Fun Posts: 388
    edited 2007-07-12 19:29
    I copied what you posted and it compiled without errors for me. What version of SX/B are you using? You can find the latest version in a “sticky post” at the top of this forum.

    - Sparks
  • djh82ukdjh82uk Posts: 193
    edited 2007-07-12 22:19
    heya

    Newest version, downloaded 2 days ago [noparse]:([/noparse]

    Got it working now, was a bug with the ide, copied the code, restarted pasted and it worked.

    Now I cannot get it to simply dim an led now [noparse]:([/noparse]

    regardless of duty being 10 or 250, led is same brightness, is any extra circuitry required? as with the picaxe, you just use an led, and a resistor.

    DJH
  • PJAllenPJAllen Banned Posts: 5,065
    edited 2007-07-12 23:31
    Is the LED connected to RB.0?

    You have: PWM DAC, duty, 1 ' charge RC network

    RB.0 will PWM for 1ms.· That's not long enough to see anything.· Are you figuring it's going to stay at the charged voltage so that you can base your brightness on it?· That won't work like that.· You can vary brightness as long as the pin is (actively) PWM'ing, but the cap in the LP filter will drain straight away once the PWM stops.· I'm thinking that you'll have to use an op-amp, as a sample & hold.



    PWM Pin, Duty, Duration

    Post Edited (PJ Allen) : 7/12/2007 11:45:13 PM GMT
  • djh82ukdjh82uk Posts: 193
    edited 2007-07-13 14:28
    Ah sorry i stole that code from the pwm example.

    I did try just pwm with a duty between 1 and 255 and the duration as 1000

    the led comes on but thats it no variance in brightness, always the same brightness regardless of duty.

    Also can this not be done with just a reisitor and led as on the picaxe series of chips (basically a pic chip with a bootloader)

    Thanks

    DJH
  • BeanBean Posts: 8,129
    edited 2007-07-13 15:34
    DJH,
    · You need to create the simplest program that demonstrates the problem.

    · If you just want PWM then there is no reason that "PWM pin, value, duration" won't work. value and duration must be from 0 to 255. This is all right in the help file. Duration cannot be 1000.
    Bean.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Teacher: What is the difference between ignorance and apathy ?
    Student: I don't know and I don't care
    Teacher: Correct !
    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    www.hittconsulting.com
    ·
  • djh82ukdjh82uk Posts: 193
    edited 2007-07-13 16:44
    ah Smile. good point bean thank you

    I spent so long messing with it that I forgot about that, I did also try it with 100

    Will try it again

    DJH
  • djh82ukdjh82uk Posts: 193
    edited 2007-07-13 16:49
    ok this has the same problem led just stays on (obviously this is not all the code, the rest is just declarations)

    Start:
    DO
    FOR duty = 0 TO 255 ' sweep through angles
    PWM DAC, duty, 250 ' charge RC network
    NEXT
    LOOP


    Nobody has said if i can even do this with just an led and a resistor?
  • PJAllenPJAllen Banned Posts: 5,065
    edited 2007-07-13 17:09
    OK -- Yes, all you need is a resistor and an LED.

    This program works (the LED starts OFF and then gradually increases to full ON), I am watching it right before me·--
    DEVICE sx28, oschs3, TURBO, OPTIONX
    IRC_CAL  IRC_SLOW
    FREQ     75_000_000
    
    PROGRAM  Start
     
    duty  var  byte
     
    TRIS_A = $00
     
    Start:
    DO
    FOR duty = 0 TO 255 ' sweep through angles
    PWM RA.0, duty, [color=red]50[/color]  ' [color=red]speed of change[/color]
    NEXT
    LOOP
    

    I am using RA.0 for the output, and I am not using a·capacitor or RC network at all (and I don't know why you are.)·

    You will have to change the FREQ directive to match your time-base.

    Update -- added drawing and SXB program

    Post Edited (PJ Allen) : 7/13/2007 5:42:02 PM GMT
    292 x 222 - 5K
  • BeanBean Posts: 8,129
    edited 2007-07-13 17:44
    Okay I forgot to mention. Make the program as simple as possible. But then post the ENTIRE program. There are many things you could have wrong in the parts you haven't posted.

    You got to help us help you...

    Bean.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Teacher: What is the difference between ignorance and apathy ?
    Student: I don't know and I don't care
    Teacher: Correct !
    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    www.hittconsulting.com
    ·
  • djh82ukdjh82uk Posts: 193
    edited 2007-07-13 18:16
    Thanks guys

    been a hard day, Im not using a cap or RC network, those comments were left over from the code taken from the manual.

    I am now trying this code but I just get a "Tris_a not defined" error.
    Somebody said...

    DEVICE sx48, OSC4MHZ
    IRC_CAL IRC_SLOW
    FREQ 4_000_000
    PROGRAM Start

    duty var byte

    TRIS_A = $00

    Start:
    DO
    FOR duty = 0 TO 255 ' sweep through angles
    PWM RA.0, duty, 50 ' charge RC network
    NEXT
    LOOP
  • djh82ukdjh82uk Posts: 193
    edited 2007-07-13 18:20
    hmm when it tries to compile it opens a *.src file and says that tris_a is not defined and it highlights a line in the assembly, if i just delete that line it works :S
    Somebody said...

    MOV FSR,#__TRISA

    thats the line I deleted.

    Any ideas why this would happen? Sorry Ive been getting used to the propeller, but need to use the SX for it's larger number of outputs and cheaper price.

    Thanks

    DJH
  • BeanBean Posts: 8,129
    edited 2007-07-13 18:51
    Okay, now we are getting somewhere !

    It seems you have found a bug in the compiler. This can be corrected by adding a line: __TRISA CON __TRIS

     
    DEVICE sx48, OSC4MHZ
    IRC_CAL IRC_SLOW
    FREQ 4_000_000
    
     
    __TRISA  CON __TRIS
     
    PROGRAM Start
    
    duty var byte
    
    TRIS_A = $00
    
    Start:
    DO
      FOR duty = 0 TO 255  ' sweep through angles
        PWM RA.0, duty, 50 ' charge RC network
      NEXT
    LOOP
    
    


    I will make sure this is corrected in the next release of the compiler.

    Thanks,

    Bean.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Teacher: What is the difference between ignorance and apathy ?
    Student: I don't know and I don't care
    Teacher: Correct !
    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    www.hittconsulting.com


    Post Edited (Bean (Hitt Consulting)) : 7/13/2007 7:22:41 PM GMT
  • djh82ukdjh82uk Posts: 193
    edited 2007-07-13 19:58
    woohoo my inadequencies as a programmer came in helpful? smile.gif

    Thanks for all of your help also, i do not mean to be a pain, once I pick things up I will bother you all a bit less [noparse]:)[/noparse]
  • djh82ukdjh82uk Posts: 193
    edited 2007-07-13 20:04
    hmm if I try to program the below code with or without the TrisA = nothing happens, no error box pops up as usual or anything, just "SX/B Error" in the bottom of the application.
    Somebody said...

    DEVICE sx48, OSC4MHZ
    IRC_CAL IRC_SLOW
    FREQ 4_000_000
    PROGRAM Start

    duty var byte
    cntr var byte
    __TRISA CON __TRIS

    'TRIS_A = $00

    Start:
    duty = 255
    DO
    FOR cntr = 0 TO 255 ' sweep through angles
    duty = duty - 1
    PWM RA, duty, 50 ' charge RC network
    NEXT
    low ra.0
    pause 10000
    LOOP
  • BeanBean Posts: 8,129
    edited 2007-07-13 20:47
    You have not specifed a pin for PWM. You have RA which is a whole port.
    You don't have to set the pin to an output, PWM will do that for you.

    and you can just use

    FOR duty = 255 TO 0 STEP -1

    Bean.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Teacher: What is the difference between ignorance and apathy ?
    Student: I don't know and I don't care
    Teacher: Correct !
    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    www.hittconsulting.com
    ·
  • jesse_in_venicejesse_in_venice Posts: 5
    edited 2007-07-14 21:29
    i've been using the r/c network in the example, w/ both a 1uf cap & .1 ufd cap (& changing the charge time from 10 to 1..). both configurations w/ a 10K resistor.. What I've addad is a 2n7000 series J-mosfet on the output & the hold time is outragous !!! I can send a single PWM instruction & the circuit will hold the val for many MINUTES.. not secs or millsec's..

    in trying to cut down code space, however, I've hit a snag... I need to be able to specify the PIN in the PWM command as a variable... I'm sending pulses out each RA & RB port bit ( 16 seperate PWM commands..yuk) & want to use a generic 'send' subroutine, passing pin & duration as params... any ideas??

    thanks,
    jesse in venice..
  • James NewtonJames Newton Posts: 329
    edited 2007-07-17 00:09
    Just a quick note to say how impressed I am with Bean for his work on the SX/B compiler and with Parallax for sponsoring it. People who program in BASIC rather than ASM or C really owe you guys a debt of gratitude. Same day help with programming, bug report and fix, and quick turns on compiler versions are sadly lacking in this industry.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    ---
    James Newton, Host of SXList.com
    james at sxlist,com 1-619-652-0593 fax:1-208-279-8767
    SX FAQ / Code / Tutorials / Documentation:
    http://www.sxlist.com Pick faster!



  • djh82ukdjh82uk Posts: 193
    edited 2007-07-17 00:21
    I did not realise Bean works on the SX/B Compiler

    Thank you, would never have tried out the SX chips without it [noparse]:)[/noparse]

    DJH
Sign In or Register to comment.