PICBasic newbie problem


Closed Thread
Results 1 to 33 of 33

Hybrid View

  1. #1
    skimask's Avatar
    skimask Guest


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by Bruce View Post
    And: what : makes : your : coding : style : different : than : anyone : elses :....;o}
    I like to think of it in these terms:
    My code takes up less space on the screen...
    Most other's code takes up less space in the brain...
    :::::::::::::::::::::::::::::::::::::::::::::::::: :::::::::

  2. #2
    Join Date
    Aug 2006
    Location
    Look, behind you.
    Posts
    2,818


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by skimask View Post
    I like to think of it in these terms:
    My code takes up less space on the screen...
    Most other's code takes up less space in the brain...
    :::::::::::::::::::::::::::::::::::::::::::::::::: :::::::::
    I'm : having : a : stack : overflow :
    Last edited by Archangel; - 9th February 2008 at 20:35.
    If you do not believe in MAGIC, Consider how currency has value simply by printing it, and is then traded for real assets.
    .
    Gold is the money of kings, silver is the money of gentlemen, barter is the money of peasants - but debt is the money of slaves
    .
    There simply is no "Happy Spam" If you do it you will disappear from this forum.

  3. #3
    skimask's Avatar
    skimask Guest


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by Joe S. View Post
    I'm : having : a : stack : overflow :
    Me too :::::::::::::::::::)))))))))))))))))))))

    At any rate, just to prove I'm not an _ _ _ (insert word here)...here's some cut down 3 channel code I use on a 16F628A running at 20Mhz.
    Interrupt driven 8 bit, 76Hz software PWM for RGB.
    You got a timer (Timer 0) running as fast as it can, interrupting at 19,531.25 Hz. That increments a byte counter, pwmcount.
    You got 3 duty cycle registers (reddcr, greendcr, bluedcr). If the individual duty cycle register is less than the pwmcounter, the LED stays on, if not the LED goes out.
    Change the DCRs in the mainloop and it should work.
    Like I said, this is cut down code. The full code works just fine for me, but I took out all of the serial interface code, the 5 button interface code and the serial LCD code. Might work, might not. Pay attention to how the hardware hooks up (3 LEDs on PortA, that's it).
    The code should fade the 3 LEDs each in turn in about one second from off to full brightness, over and over again, 3 seconds for all 3 LEDs, wash lather rinse repeat.

    But if you can't make an LED blink in the first place, DO THAT BEFORE YOU TRY THIS!

    Code:
    'PWM loop is @ 19,531.25 hz, w/ 256 cnt res, Freq of PWM is 76.2939hz
    @ DEVICE PIC16F628A , HS_OSC , WDT_OFF , PWRT_ON , MCLR_ON , BOD_ON , LVP_OFF , PROTECT_OFF	'HS 20mhz osc, watchdog off, powerup timer on, mclr external, brown out detect on, low volt program off , code protect off for now
    resetplaceholder:
    DEFINE		OSC		20	'20mhz
    DEFINE		NO_CLRWDT	1	'don't clear the watchdog timer, I'm not using it anyways
    DISABLE				'disable software interrupt checks
    CLEAR					'clear out the ram and do a software 'reset'
    redled var porta.0:greenled var porta.1:blueled var porta.2:chx var porta:reddcr var byte:greendcr var byte:bluedcr var byte:ledtemp var byte:pwmcount var byte
    temp var byte : temp2 var word
    startupholder:	goto skipsubs		'skip over all the commonly used subroutines
    ON INTERRUPT GOTO INTHANDLER
    DISABLE INTERRUPT
    INTHANDLER:	if intcon.2 = 1 then						'if tmr0 rolled over
    			intcon.2 = 0						'reset timer 0 overflow flag
    			if pwmcount = 0 then				'if pwmcounter has rolled over
    				if reddcr <> 0 then ledtemp.0 = 1	'turn off red leds if dcr is 0 (dcr = duty cycle register for LED), otherwise turn it on
    				if greendcr <> 0 then ledtemp.1 = 1	'turn off green leds if dcr is 0
    				if bluedcr <> 0 then ledtemp.2 = 1	'turn off blue leds if dcr is 0
    				chx = ledtemp		'update port pins of led status (on or off), can do this because all the LEDs are on the same port
    				endif
    			endif
    			if reddcr < pwmcount then ledtemp.0 = 0	'if the channel's dcr (duty cycle register) is less than the pwmcounter, turn that channel off, otherwise, leave it on ( < rather than <=, fixes problem with pulsing at high rates)
    			if greendcr < pwmcount then ledtemp.1 = 0
    			if bluedcr < pwmcount then ledtemp.2 = 0
    			chx = ledtemp : pwmcount = pwmcount + 1	'update the port pins and increment the pwmcount
    		endif
    intfinish:	RESUME
    'commonly used subroutines start here
    alloff:		reddcr = 0 : greendcr = 0 : bluedcr = 0 : return	'turn off all leds thru dcr's
    allon:		reddcr = 255 : greendcr = 255 : bluedcr = 255 : return     'turn on all leds
    skipsubs: option_reg=8:pie1=$20:trisa=0:porta=0:trisb=$ef:portb=$10:t1con=0:t2con=0:cmcon=7:ccp1con=0:vrcon=0:pir1.5=0:ledtemp=0:redled=0:blueled=0
                   greenled = 0 : intcon = $e0
    ENABLE INTERRUPT
    mainloop:
    for temp=0 to 255:reddcr=temp:for temp2=0 to 2603:pauseus 1:next temp2:next temp:for temp=0 to 255:greendcr=temp:for temp2=0 to 2603:pauseus 1:next temp2
    next temp:for temp=0 to 255:bluedcr=temp:for temp2=0 to 2603:pauseus 1:next temp2:next temp:goto mainloop	'do it over again
    END
    And for those that don't like colons...
    Code:
    'PWM loop is @ 19,531.25 hz, w/ 256 cnt res, Freq of PWM is 76.2939hz
    @ DEVICE PIC16F628A , HS_OSC , WDT_OFF , PWRT_ON , MCLR_ON , BOD_ON , LVP_OFF , PROTECT_OFF	'HS 20mhz osc, watchdog off, powerup timer on, mclr external, brown out detect on, low volt program off , code protect off for now
    resetplaceholder:
    DEFINE		OSC		20	'20mhz
    DEFINE		NO_CLRWDT	1	'don't clear the watchdog timer, I'm not using it anyways
    DISABLE				'disable software interrupt checks
    CLEAR					'clear out the ram and do a software 'reset'
    redled var porta.0
    greenled var porta.1
    blueled var porta.2
    chx var porta
    reddcr var byte
    greendcr var byte
    bluedcr var byte
    ledtemp var byte
    pwmcount var byte
    temp var byte
    temp2 var word
    startupholder:
    goto skipsubs		'skip over all the commonly used subroutines
    ON INTERRUPT GOTO INTHANDLER
    DISABLE INTERRUPT
    INTHANDLER:
    if intcon.2 = 1 then						'if tmr0 rolled over
    intcon.2 = 0						'reset timer 0 overflow flag
    if pwmcount = 0 then				'if pwmcounter has rolled over
    if reddcr <> 0 then
    ledtemp.0 = 1	'turn off red leds if dcr is 0 (dcr = duty cycle register for LED), otherwise turn it on
    endif
    if greendcr <> 0 then
    ledtemp.1 = 1	'turn off green leds if dcr is 0
    endif
    if bluedcr <> 0 then
    ledtemp.2 = 1	'turn off blue leds if dcr is 0
    endif
    chx = ledtemp		'update port pins of led status (on or off), can do this because all the LEDs are on the same port
    endif
    endif
    if reddcr < pwmcount then
    ledtemp.0 = 0
    endif
    'if the channel's dcr (duty cycle register) is less than the pwmcounter, turn that channel off, otherwise, leave it on ( < rather than <=, fixes problem with pulsing at high rates)
    if greendcr < pwmcount then
    ledtemp.1 = 0
    endif
    if bluedcr < pwmcount then
    ledtemp.2 = 0
    endif
    chx = ledtemp
    pwmcount = pwmcount + 1	'update the port pins and increment the pwmcount
    endif
    intfinish:
    RESUME
    'commonly used subroutines start here
    alloff:
    reddcr = 0
    greendcr = 0
    bluedcr = 0
    return	'turn off all leds thru dcr's
    allon:
    reddcr = 255
    greendcr = 255
    bluedcr = 255
    return     'turn on all leds
    skipsubs:
    option_reg = 8
    pie1 = $20
    trisa = 0
    porta = 0
    trisb = $ef
    portb = $10
    t1con = 0
    t2con = 0
    cmcon = 7
    ccp1con = 0
    vrcon = 0
    pir1.5 = 0
    ledtemp = 0
    redled = 0
    blueled = 0
    greenled = 0
    intcon = $e0
    ENABLE INTERRUPT
    mainloop:
    for temp = 0 to 255
    reddcr = temp
    for temp2 = 0 to 2603
    pauseus 1
    next temp2
    next temp
    for temp = 0 to 255
    greendcr = temp
    for temp2 = 0 to 2603
    pauseus 1
    next temp2
    next temp
    for temp = 0 to 255
    bluedcr = temp
    for temp2 = 0 to 2603
    pauseus 1
    next temp2
    next temp
    goto mainloop	'do it all over again
    END
    Last edited by skimask; - 10th February 2008 at 00:17.

  4. #4
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959


    Did you find this post helpful? Yes | No

    Default

    3 channels ...

    No, No, No!

    You said you could do 48 channels.
    That's what they want.

    Frankly, I'd like to see 48 chanels of 8-bit software PWM too.
    <br>
    DT

  5. #5
    skimask's Avatar
    skimask Guest


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by Darrel Taylor View Post
    3 channels ...
    No, No, No!
    You said you could do 48 channels.
    That's what they want.
    Frankly, I like to see 48 chanels of 8-bit software PWM too.
    <br>
    Well, the basics are there (as you well know)...
    And the 48 channels...I'll post some differences:
    18F8722 vs. 16F628A
    40Mhz vs. 20Mhz
    Timer 0 prescale is 1:2 instead of 1:1, thereby keeping the same 76Hz Freq of PWM...
    ...got 512 cycles to work with instead of 256. 48 channels in 512 cycles = 10 cycles per channel with even some extra leftover.
    Code is practically the same, except:
    PWM 0-7 on PortB (along with PGC/PGD/PGM, LVP disabled)
    PWM 8-15 on PortD (serial comm's on PortC, along with a couple of buttons)
    PWM 16-23 on PortE
    PWM 24-31 on PortF
    PWM 32-39 on PortH
    PWM 40-47 on PortJ
    The rest of the pins are either used for buttons, indicators, or whatever, the usual crap.

    At first I had a heck of a time finding enough cycles to run everything thru the interrupt loop and not miss any interrupts, that's when I figured out to keep all of the PWM channels in blocks of 8, byte writes to each port instead of bit writes saved just enough time to make it work reliably and leave a bit of time leftover to actually have a MAINLOOP to do other things. There's a few other shortcuts in the interrupt loop, such as checking the high bit of each DCR instead of checking the whole thing when doing the comparisons. That's one of the places where the PWM jitters a bit (I never said it was a ROCK SOLID 48 channels!). If too many of the channels have to take the jump where it sets or resets the pin, the PWM for all of them hiccups a bit. Worst case scenario is when all DCRs for each channel are exactly the same, they all have to take the jump to reset the pins. But if they're spread out, it's not bad at all. I can even trace it on the 'scope and only get a couple of us of jitter.

    THERE! Now everybody knows all of my secrets!

    You know, come to think of it...this might be one of those things just would go good with that little 'code optimization' exercise that went on awhile back.
    Get an '8722 to use every available pin for software driven PWM'ing of LEDs, while using the serial port for control at high speed, and still have solid PWM.
    Last edited by skimask; - 10th February 2008 at 04:04.

  6. #6
    Join Date
    Aug 2006
    Location
    Look, behind you.
    Posts
    2,818


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by skimask View Post
    Me too :::::::::::::::::::)))))))))))))))))))))

    At any rate, just to prove I'm not an _ _ _ (insert word here)...
    HMMMMMMMMM . . . . not an _ _ _ . . . what could that be . . ( Joe mutters to himself with a big grin on his ugly mug) E G G . . . no that's too obvious . . D O G . . . perhaps, no . . . HMMMMMM , what could it be? D O N K E Y? No too many letters . . . HMMMMMMM . . . What could it be . . . ? HE HE HE HE HE HE . . . .
    Last edited by Archangel; - 10th February 2008 at 05:40.
    If you do not believe in MAGIC, Consider how currency has value simply by printing it, and is then traded for real assets.
    .
    Gold is the money of kings, silver is the money of gentlemen, barter is the money of peasants - but debt is the money of slaves
    .
    There simply is no "Happy Spam" If you do it you will disappear from this forum.

  7. #7
    skimask's Avatar
    skimask Guest


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by Joe S. View Post
    HMMMMMMMMM . . . . not an _ _ _ . . .
    I'll even give you a hint there...
    Since I wrote 'not AN _ _ _' vs. 'not A _ _ _'
    then it can be assumed that the 3 letter word does in fact start with a vowel...

  8. #8
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by skimask View Post
    ...
    Since I wrote 'not AN _ _ _' vs. 'not A _ _ _'
    then it can be assumed that the 3 letter word does in fact start with a vowel...
    I had come up with so many possibilities
    Especially after reading this ...

    <a href="http://www.dailywritingtips.com/using-a-and-an-before-words/">http://www.dailywritingtips.com/using-a-and-an-before-words/</a>
    <br>
    DT

  9. #9
    Join Date
    Aug 2006
    Location
    Look, behind you.
    Posts
    2,818


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by skimask View Post
    I'll even give you a hint there...
    Since I wrote 'not AN _ _ _' vs. 'not A _ _ _'
    then it can be assumed that the 3 letter word does in fact start with a vowel...
    hmmmmmmmm . . . not AN . . . EAR . . . OWL . . . OAR . . . ? Be very careful if you pick one because there's lots of 3 letter words you do not want to be Lucky for me you are <font color=red>not</font color> AN overly sensitive drama queen ! and allow me to pick on you a little . . . all in fun though!
    Last edited by Archangel; - 10th February 2008 at 08:02.
    If you do not believe in MAGIC, Consider how currency has value simply by printing it, and is then traded for real assets.
    .
    Gold is the money of kings, silver is the money of gentlemen, barter is the money of peasants - but debt is the money of slaves
    .
    There simply is no "Happy Spam" If you do it you will disappear from this forum.

  10. #10
    skimask's Avatar
    skimask Guest


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by Darrel Taylor View Post
    I had come up with so many possibilities
    Especially after reading this ...
    <a href="http://www.dailywritingtips.com/using-a-and-an-before-words/">http://www.dailywritingtips.com/using-a-and-an-before-words/</a>
    <br>
    Point taken.

    Quote Originally Posted by Joe S. View Post
    hmmmmmmmm . . . not AN . . . EAR . . . OWL . . . OAR . . . ? Be very careful if you pick one because there's lots of 3 letter words you do not want to be Lucky for me you are not AN overly sensitive drama queen ! ans allow me to pick on you a little . . . all in fun though!
    APE...AXE...EMU...UNK...AKA...
    Drama Queen!!! WTH?
    Talk about getting off track...

  11. #11
    Join Date
    Nov 2003
    Location
    Wellton, U.S.A.
    Posts
    5,924


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by skimask View Post
    I'll even give you a hint there...
    Since I wrote 'not AN _ _ _' vs. 'not A _ _ _'
    then it can be assumed that the 3 letter word does in fact start with a vowel...
    I found another hint
    The High Colonic Master...yep...that's me...
    and another
    And for those that don't like colons...
    So I used Google and came up with this
    http://en.wikipedia.org/wiki/Colon_(anatomy)

    These puzzles are fun, when can we do another?

    Sorry skimask, I could not help myself
    Dave
    Always wear safety glasses while programming.

Similar Threads

  1. Newbie? Problem with LCD
    By lew247 in forum mel PIC BASIC Pro
    Replies: 10
    Last Post: - 7th December 2009, 19:48
  2. Picbasic Newbie Problem
    By Stargazer3141 in forum mel PIC BASIC
    Replies: 4
    Last Post: - 21st August 2007, 17:40
  3. Problem on writing EEPROM in Winpic800 with picbasic pro
    By selimkara in forum mel PIC BASIC Pro
    Replies: 1
    Last Post: - 4th May 2007, 16:33
  4. PicBasic Pro Math Problem??
    By Glen65 in forum mel PIC BASIC Pro
    Replies: 4
    Last Post: - 5th March 2006, 04:36
  5. DMX & PicBasic coding problem
    By magicmarty in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 20th September 2004, 15:35

Members who have read this thread : 0

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts