Port indexing & PWM for LED fade


Closed Thread
Results 1 to 10 of 10

Hybrid View

  1. #1
    Join Date
    May 2013
    Location
    australia
    Posts
    2,656


    Did you find this post helpful? Yes | No

    Default Re: Port indexing & PWM for LED fade

    from the book :- pwm pin,duty,cycle [ it says pin can be a constant or var from 0 to 15] whwre portb.0 = 0
    therefore this may work , I have not tested it

    for Avar=0 to 7
    cycle=4
    for Duty=0 to 220 step 1
    pwm Avar,Duty,cycle
    next Duty
    cycle=1
    for Duty=220 to 0 step -2
    pwm Avar,Duty,cycle
    next Duty
    low Avar ;end of routine

  2. #2
    Join Date
    Jan 2009
    Location
    Alabama,USA
    Posts
    232


    Did you find this post helpful? Yes | No

    Default Re: Port indexing & PWM for LED fade

    Richard, it worked, Thanks, although it raises as many questions as it answers.

    This works

    Code:
            for Avar= 0 to 7   
                    cycle=4    ;cycle is number of cycles of pulse    
                for Duty=0 to 220 step 1
                    pwm Avar,Duty,cycle
                    next Duty
                    cycle=2    ;cycle is number of cycles of pulse
                for Duty=220 to 0 step -2
                    pwm Avar,Duty,cycle
                    next Duty
                    low Avar
            next Avar
    This works, on PortB, why? This should work on PortA. PortB, I tought, would be 8-15.
    Yes I had used ‘Symbol Port_Pin=PortB’ which would set PortB to 0-7 but now I’m not using Port_Pin in the routine. I remember there is a file somewhere that sets the port numbers but I can ‘t remember what or where it is. I may have changed the default numbering over the years. Oh well, it works and now and I’m happy. I’m surprised I had not seen an example for PWM that used this method to cycle through LEDs. Too easy! thank you both for your input!


    Code:
    ; 16F886
    @ DEVICE INTRC_OSC,MCLR_ON,BOD_OFF,WDT_OFF,LVP_OFF
    
    define  osc 8
    ;------[ Set Hardware ]------------------------------------------------------
        OSCCON=$70    '$70=8MHz, $60=4MHz 
        ANSEL=$00
        ANSELH=$0    
        ADCON0=$00
        CM1CON0=0 
        CM2CON0=0
        OPTION_REG=0    ; INTOC RB0 set to interrupt on falling edge
        
    	PORTA=%00000000
    	PORTB=%00000000
    	PORTC=%00000000     ' RAC6&7 set with Hserout command
    	TRISA=%00000011      ' PORTA RA0 & RA1 input all other output
    	TRISB=%00000000      ' PORTB all set to Onput
                  TRISC=%00000000      ' PortC     RC7&RC6 for hserout
    ;--[ VARIBLES, CONSTATNS ]-------------------------------------------------
    ;  Symbol Port_Pin=PortB   ; To define PortB port numbers start at RB.0    
        Avar    var byte   
        Duty    var byte            
        Cycle   var byte    
    ;--[ Main Program ]-------------------------------------------------------------
    Start:
        gosub FadeTest
         
    ;--[ Routine for fade on and fade off LEDs]------------------------------------
    FadeTest:
            for avar = 0 to 7   
                    cycle=4    ;cycle is number of cycles of pulse    
                for Duty= 0 to 220 step 1
                    pwm Avar,Duty,cycle
                    next Duty
                    cycle=2    ;cycle is number of cycles of pulse
                for Duty= 220 to 0 step -2
                    pwm Avar,Duty,cycle
                    next Duty
                    low Avar
            next Avar
        Return  
             end

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


    Did you find this post helpful? Yes | No

    Default Re: Port indexing & PWM for LED fade

    Hi All,
    I tried this just the way it's published to no avail, but did manage to make it work after tinkering a bit. I used a PIC Demo board featuring a PIC16F690 and the 4 LEDs on Port C as follows:
    Code:
    #CONFIG
        __CONFIG _INTRC_OSC_NOCLKOUT & _WDT_ON & _PWRTE_ON & _MCLRE_ON & _WDT_ON
    
    #ENDCONFIG
    DEFINE OSC 4
    TRISA = 0
    TRISB = 0
    TRISC = 0
    
    
    
    ;------[ Set Hardware ]------------------------------------------------------
        OSCCON=$70    '$70=8MHz, $60=4MHz 
        ANSEL=$00
        ANSELH=$0    
        ADCON0=$00
        CM1CON0=0 
        CM2CON0=0
       
        
    	PORTA=%00000000
    	PORTB=%00000000
    	PORTC=%00000000     ' RAC6&7 set with Hserout command
    	
    	TRISA=%00000011      ' PORTA RA0 & RA1 input all other output
    	TRISB=%00000000      ' PORTB all set to Onput
        TRISC=%00000000      ' PortC     RC7&RC6 for hserout
       
        Avar    var byte  
        Duty    var byte            
        Cycle   var byte    
    ;--[ Main Program ]-------------------------------------------------------------
    Start:
        gosub FadeTest
    goto start
         
    ;--[ Routine for fade on and fade off LEDs]------------------------------------
    FadeTest:
            for avar = 8 to 11
            PortC = avar << 2 &%00000001 ;& OP damps a leading on signal   
                    cycle=4    ;cycle is number of cycles of pulse    
                for Duty= 0 to 220 step 1
                    pwm Avar,Duty,cycle
                    next Duty
                    cycle=3    ;cycle is number of cycles of pulse
                for Duty= 220 to 0 step -2
                    pwm Avar,Duty,cycle
                    next Duty
                   
            next Avar
             Avar = 8
        Return  
             end
    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.

  4. #4
    Join Date
    Jan 2005
    Location
    Montreal, Quebec, Canada
    Posts
    3,172


    Did you find this post helpful? Yes | No

    Default Re: Port indexing & PWM for LED fade

    Just a comment on logic:

    Code:
    Start:
        gosub FadeTest
        goto Start   <---- missing
    Robert

Similar Threads

  1. Help with LED driver fade on/off
    By dfort in forum mel PIC BASIC Pro
    Replies: 1
    Last Post: - 25th July 2009, 00:39
  2. PWM and fade effect
    By ruijc in forum mel PIC BASIC Pro
    Replies: 8
    Last Post: - 10th July 2009, 10:58
  3. Port Indexing
    By Ioannis in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 25th September 2006, 12:33
  4. LED fade without PWM command
    By Nick in forum mel PIC BASIC
    Replies: 7
    Last Post: - 29th June 2005, 20:56
  5. Fade out an LED using PWM?
    By RossW in forum mel PIC BASIC Pro
    Replies: 27
    Last Post: - 6th August 2004, 19:59

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