Stuck on porting code to 18F4520


Closed Thread
Page 1 of 2 12 LastLast
Results 1 to 40 of 43
  1. #1
    Join Date
    Oct 2009
    Posts
    583

    Default Stuck on porting code to 18F4520

    Guys,

    I came across a post where the internal timer is used to display the time rather than a DS1307 (as I'm having issues reading a DS1307 for some reason as per my other post). Here is the thread in question http://www.picbasic.co.uk/forum/showthread.php?t=2129

    I've followed the notes on converting the code to run on a 20 mhz xtal, but there is one command that comes up with a syntax error
    Code:
    OPTION_REG=%10000111   'weak pullups off, TMRO prescale = 256
    If this is commented out the code compiles for an 18F4520

    Here is the code in full

    Code:
    ASM 
      __CONFIG    _CONFIG1H, _OSC_HSPLL_1H
      __CONFIG    _CONFIG2L, _PWRT_ON_2L  
      __CONFIG    _CONFIG2H, _WDT_ON_2H & _WDTPS_512_2H
      __CONFIG    _CONFIG3H, _MCLRE_ON_3H & _LPT1OSC_OFF_3H & _PBADEN_OFF_3H  
      __CONFIG    _CONFIG4L, _LVP_OFF_4L & _XINST_OFF_4L
    ENDASM
    
    DEFINE  OSC 48  ; config settings 18F4520, 20mhz crystal
    ADCON1 = $0F
    clear
    
    
    DEFINE LCD_DREG  PORTB           ' LCD Data port
    DEFINE LCD_DBIT  0               ' starting Data bit (0 or 4)
    DEFINE LCD_EREG  PORTB           ' LCD Enable port
    DEFINE LCD_EBIT  5               '     Enable bit  (on EasyPIC 5 LCD)
    DEFINE LCD_RSREG PORTB           ' LCD Register Select port
    DEFINE LCD_RSBIT 4               '     Register Select bit   (on EasyPIC 5 LCD)
    DEFINE LCD_BITS  4               ' LCD bus size (4 or 8 bits)
    DEFINE LCD_LINES 4               ' number of lines on LCD
    DEFINE LCD_COMMANDUS 2000        ' Command delay time in us 
    DEFINE LCD_DATAUS 50             ' Data delay time in us 
    
    
    CMCON=7                'all digital 
    'GPIO=0
    'TRISIO=0
    T0CON = %00000001
    OPTION_REG=%10000111   'weak pullups off, TMRO prescale = 256
    INTCON=0               'interrupts off
    '**********************************************************
    HzTimer VAR Word         '1/2 second counter (2 Hz)    
    HH VAR Byte  ' Hours 0-23
    MM VAR ByTE  ' Minutes 0-59
    SS VAR Byte  ' Seconds 0-59
    col VAR Byte  ' colon 1=on, 0=0ff
    
    HzTimer=$7A12        'for 1/2 Sec
    HH=12:MM=0:SS=0:col=0 'initial conditions (12:00 O'clock noon)
    '**********************************************************
    Main:
    
    ClockLoop: IF intcon.2=0 THEN ClockLoop	'Poll for TMRO overflow
    INTCON.2=0 ' Clear TMRO overflow flag
    
    HzTimer=HzTimer-$1000	'decrement timer count
    
    IF HzTimer < $1000  THEN
        IF Col=10 THEN    ' update time'
            SS=SS+1
            'GPIO.1=0    ' Turn off GP1
            IF SS=60 THEN   ' minute
                SS=0 
                MM=MM+1
                IF MM=60 THEN    ' hour            
                    MM=0
                    HH=HH+1
                    IF HH=24 THEN HH=0
                    'IF HH=20 then GPIO.0 = 1    ' Turn on GP0 at 8:00pm
                   ' GPIO.1=1    ' Turn On  GP1 for 1 second on the hour
                ENDIF                           ' Adjust these to meet your needs
                'if MM=15 then GPIO.0 = 0        ' Turn off GP0 at 8:15pm  
            ENDIF
        ENDIF
        Col=Col+1
    
        HzTimer=HzTimer+$7A12	' Add 0.5 seconds to count
    ELSE
        ' Do something here but must be less than 65.5 mSec
    ENDIF
          if Col=11 then 
        Col=1
        endif
        
        LCDOut $FE,$D4,HH,MM,SS
    GOTO  Main 'forever
    ' **************************************************************
    END
    Can anyone advise how to set up the timer and prescaler settings as per the original code for the 12F chip ?

    Cheers

    Malcolm

  2. #2
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,516


    Did you find this post helpful? Yes | No

    Default Re: Stuck on porting code to 18F4520

    Hi,
    There is no OPTION-register on the 18F4520, TMR0 has its own dedicated configuration register. It's described in the datasheet in greater detail than I'm able to do here.

    If you're running at 20MHz then you should also probably tell the compiler that by changing the appropriate DEFINE (which is now at 48MHz).

    /Henrik.

  3. #3
    Join Date
    Oct 2009
    Posts
    583


    Did you find this post helpful? Yes | No

    Default Re: Stuck on porting code to 18F4520

    Henrik, thanks for the suggestion.

    I'm terrible at understanding datasheets, so can you confirm if I've understood this ?

    The Timer0 module is controlled by the T0CON register,bit 0-2 sets the pre-scaler, so to get the pre-scaler value 256 required these bits are set to 1. Looking at the the other options would setting this register to 1110111 be correct to

    [1]enable timer0, [1]set to 8bit timer, [1]use the external xtal, [0]assign to use prescaller and use the output, [111]set prescaler value to 256

    Regards

    Malcolm


    EDIT - I've tried DEFINE OSC 20 and all I get on the LCD display are two lines of solid blocks (which normally happens when the pic doesn't run) - or is it because there is no time for the PIC to action the LCDout instruction because there is no time between the timer tripping over ?
    Last edited by Scampy; - 28th October 2013 at 21:10.

  4. #4
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,516


    Did you find this post helpful? Yes | No

    Default Re: Stuck on porting code to 18F4520

    Hi,
    There are only 7 bits in your example, you want to set it to use the instruction clock as the source, I think this shoukd do it.
    Code:
    T0CON = %11000111  'TMR0 ON, 8bit mode, instruction clock as source, prescaler assigned to TMR0, 256:1 ratio
    When you run the PIC at 20MHz but tell the compiler it's being run at 48MHz all the timings will be off by a factor of 2.4.
    A PAUSE 10 will actually be 24ms instead of 10ms. The same thing happens with ALL other commands that relies on software timing, including LCD-out. The fact that the display does NOT work when the compiler uses the correct OSC value to calculate the timings suggests that the LCD is slower than "normal". Try increasing the
    Code:
    DEFINE LCD_COMMANDUS 2000        ' Command delay time in us 
    DEFINE LCD_DATAUS 50             ' Data delay time in us
    And add a bit of delay at the start of the code to allow the LCD to start up properly.

    /Henrik.

  5. #5
    Join Date
    Oct 2009
    Posts
    583


    Did you find this post helpful? Yes | No

    Default Re: Stuck on porting code to 18F4520

    Hi Henrik,

    I already had the LCD delays in place, but added an initial two second pause and clear before the main code. I now have the time displayed on the LCD using the following code

    Code:
    ASM 
      __CONFIG    _CONFIG1H, _OSC_HS_1H
      __CONFIG    _CONFIG2L, _PWRT_ON_2L  
      __CONFIG    _CONFIG2H, _WDT_ON_2H & _WDTPS_512_2H
      __CONFIG    _CONFIG3H, _MCLRE_ON_3H & _LPT1OSC_OFF_3H & _PBADEN_OFF_3H  
      __CONFIG    _CONFIG4L, _LVP_OFF_4L & _XINST_OFF_4L
    ENDASM
    
    DEFINE  OSC 20  ; 48 was old config settings 18F4520, 20mhz crystal
    ADCON1 = $0F
    clear
    
    
    DEFINE LCD_DREG  PORTB           ' LCD Data port
    DEFINE LCD_DBIT  0               ' starting Data bit (0 or 4)
    DEFINE LCD_EREG  PORTB           ' LCD Enable port
    DEFINE LCD_EBIT  5               '     Enable bit  (on EasyPIC 5 LCD)
    DEFINE LCD_RSREG PORTB           ' LCD Register Select port
    DEFINE LCD_RSBIT 4               '     Register Select bit   (on EasyPIC 5 LCD)
    DEFINE LCD_BITS  4               ' LCD bus size (4 or 8 bits)
    DEFINE LCD_LINES 4               ' number of lines on LCD
    DEFINE LCD_COMMANDUS 2000        ' Command delay time in us 
    DEFINE LCD_DATAUS 50             ' Data delay time in us 
    
    LED var portA.7
    
    
    
    CMCON=7                'all digital 
    T0CON = %11000111
    INTCON=0               'interrupts off
    '**********************************************************
    HzTimer VAR Word         '1/2 second counter (2 Hz)    
    HH VAR Byte  ' Hours 0-23
    MM VAR ByTE  ' Minutes 0-59
    SS VAR Byte  ' Seconds 0-59
    col VAR Byte  ' colon 1=on, 0=0ff
    
    HzTimer=$7A12        'for 1/2 Sec
    HH=12:MM=0:SS=0:col=0 'initial conditions (12:00 O'clock noon)
    '**********************************************************
    
    LCDOUT $FE,1:FLAGS=0:PAUSE 250:LCDOUT $FE,1:PAUSE 250 ' Initialize LCD 
    
    Main:
    
    ClockLoop: IF intcon.2=0 THEN ClockLoop	'Poll for TMRO overflow
    INTCON.2=0 ' Clear TMRO overflow flag
    
    HzTimer=HzTimer-$1000	'decrement timer count
    
    IF HzTimer < $1000  THEN
        IF Col=10 THEN    ' update time'
            SS=SS+1
            LED =0    ' Turn off GP1
            IF SS=60 THEN   ' minute
                SS=0 
                MM=MM+1
                IF MM=60 THEN    ' hour            
                    MM=0
                    HH=HH+1
                    IF HH=24 THEN HH=0
                    IF HH=20 then LED = 1    ' Turn on GP0 at 8:00pm
                   LED =1    ' Turn On  GP1 for 1 second on the hour
                ENDIF                           ' Adjust these to meet your needs
                if MM=15 then LED = 0        ' Turn off GP0 at 8:15pm  
            ENDIF
        ENDIF
        Col=Col+1
    
        HzTimer=HzTimer+$7A12	' Add 0.5 seconds to count
    ELSE
        ' Do something here but must be less than 65.5 mSec
    ENDIF
          if Col=11 then 
        Col=1
        endif
       
        LCDOut $FE,$D4,#HH,":",#MM,":",#SS
        
    GOTO  Main 'forever
    ' **************************************************************
    END
    I changed one of the defines in the config as it was using the PLL option so setting this to just HS resolved the OSC setting.

    The display is crude and needs some tiding up to overdraw double digits (the 9 in 59 is left on the screen so when it counts 1,2,3 seconds it displays 19,29,39 etc) but I can work on that. I just hope I have enough time between the overflows to do the rest of the code, such as fade in LEDs over a period of time etc.

    Thanks for your help. - here's the code I'm now using which works on an 18F4520 with a 20mhz xtal



    Thanks once again for your help...

  6. #6
    Join Date
    Oct 2009
    Posts
    583


    Did you find this post helpful? Yes | No

    Default Re: Stuck on porting code to 18F4520

    Its been a long day....
    Code:
        IF HH = 20 and MM = 0 then 
        LED = 1  
        endif
        lcdout $FE,$80,#LED
    for some reason when the LCD displays 20:00:00 the LED value still remains 0 and the LED fails to light

    Time for bed... now even the simple stuff won't work !

  7. #7
    Join Date
    May 2013
    Location
    australia
    Posts
    2,383


    Did you find this post helpful? Yes | No

    Default Re: Stuck on porting code to 18F4520

    try
    IF (HH = 20) and (MM = 0) then

  8. #8
    Join Date
    Oct 2009
    Posts
    583


    Did you find this post helpful? Yes | No

    Default Re: Stuck on porting code to 18F4520

    Henrik / Richard, need some further help.

    I've incorporated the time code into a fresh version of the existing LED controller and I'm still having issues. not with the timing per-say, but with the actions when conditions are met.

    The idea is simply that when the on time for the LEDs match the current time generated by the clock routine, the LCD displays the first state (Dusk) and then ramps up the PWM steps from 0 to 255 over a set period which could be programmable from 0 - 120 minutes. Then the reverse when the programmed off time matches the current time generated by the clock routine.

    I'm using the hardware PWM function of the 18F4520, and the duty cycle is set by B_PWM. So for example lets say the LEDs are to ramp up over 10 minutes. The duty cycle would equate to 600 seconds divided 255 = 2.35 seconds per step. However I can't used the pause option to delay the steps in the B_PWM=B_PWM+1 as this stops the clock running.

    So then when compiled the condition would be met, be that by using a counter to convert the time to a word variable, or simply matching the hours and minutes, the ramp up would go from 0 to 100% (0 - 255) in the space of a couple of seconds, but because the condition still applied until one minute had elapsed the ramping up would cycle until 1 minute had passed. Matching the times to include seconds (ie 20:00:00) did the same, but the ramp up only reached 30% as the condition changed due to 1 second had elapsed and the condition no longer matched.

    My current controller uses a DS1307 as the clock, but for some reason, it's time checking is getting screwed and the lights are coming on at odd times of the day, or ramping up and down at random. Therefore using the timer0 option for the clock seemed the ideal option (and the accuracy is very good using the 20Mhz xtal).

    Here's the code: (ignore some of the variables, they were used in the old code and might form part of the programming options to set the times and brightness etc, but currently may not have any bearing on the code)

    Code:
    ASM 
      __CONFIG    _CONFIG1H, _OSC_HS_1H
      __CONFIG    _CONFIG2L, _PWRT_ON_2L  
      __CONFIG    _CONFIG2H, _WDT_ON_2H & _WDTPS_512_2H
      __CONFIG    _CONFIG3H, _MCLRE_ON_3H & _LPT1OSC_OFF_3H & _PBADEN_OFF_3H  
      __CONFIG    _CONFIG4L, _LVP_OFF_4L & _XINST_OFF_4L
    ENDASM
    
    DEFINE  OSC 20  ; 48 was old config settings 18F4520, 20mhz crystal
    ADCON1 = $0F
    clear
    
    DEFINE  WRITE_USED  1
    
    DEFINE LCD_DREG  PORTB           ' LCD Data port
    DEFINE LCD_DBIT  0               ' starting Data bit (0 or 4)
    DEFINE LCD_EREG  PORTB           ' LCD Enable port
    DEFINE LCD_EBIT  5               '     Enable bit  (on EasyPIC 5 LCD)
    DEFINE LCD_RSREG PORTB           ' LCD Register Select port
    DEFINE LCD_RSBIT 4               '     Register Select bit   (on EasyPIC 5 LCD)
    DEFINE LCD_BITS  4               ' LCD bus size (4 or 8 bits)
    DEFINE LCD_LINES 4               ' number of lines on LCD
    DEFINE LCD_COMMANDUS 2000        ' Command delay time in us 
    DEFINE LCD_DATAUS 50             ' Data delay time in us 
    
    DecButton var PORTA.0	         ' Press to Decrement Button
    SetButton var PORTA.1            ' Press to Set/memorise Button
    IncButton var PORTA.2	         ' Press to Increment Button
    
    H_butt   VAR PORTA.2
    M_butt   VAR PORTA.0
    S_butt   VAR PORTA.1
    
    	
    	CounterA var byte	' General purpose Variable
    	CounterB var byte	' General purpose Variable
    	CounterC var byte	' General purpose Variable
    	CounterD var byte	' General purpose Variable
    
    	
    '****************************************************************
    ' menu variables - used to store menu options
    	
    lightsetHR          VAR WORD[2]                   
      lightsetHR1       VAR lightsetHR[0]
      lightsetHR2       VAR lightsetHR[1]
      
    lightsetMN          VAR WORD[2]                   
      lightsetMN1       VAR lightsetMN[0]
      lightsetMN2       VAR lightsetMN[1]
      
    lightoffHR          VAR WORD[2]                   
      lightoffHR1       VAR lightoffHR[0]
      lightoffHR2       VAR lightoffHR[1]
      
    lightoffMN          VAR WORD[2]                   
      lightoffMN1       VAR lightoffMN[0]
      lightoffMN2       VAR lightoffMN[1]  
      
    fadesetHR          VAR WORD[2]                   
      fadesetHR1       VAR fadesetHR[0]
      fadesetHR2       VAR fadesetHR[1]
      
    fadesetMN          VAR WORD[2]                   
      fadesetMN1       VAR fadesetMN[0]
      fadesetMN2       VAR fadesetMN[1]
      
    fadeoutHR          VAR WORD[2]                   
      fadeoutHR1       VAR fadeoutHR[0]
      fadeoutHR2       VAR fadeoutHR[1]
      
    fadeoutMN          VAR WORD[2]                   
      fadeoutMN1       VAR fadeoutMN[0]
      fadeoutMN2       VAR fadeoutMN[1]  
      
    maxbright          var byte
                     
     
    
    '****************************************************************
    ' LED variables
    ' Whites:
    W_Min           var word    'Min Light value
    W_Max           var word    'Max Light value
    W_Fade_In       var word    'Fade in duration
    W_Fade_Out      var word    'Fade out duration
    W_On_Time_H     var word    'What time to turn the lights on
    W_On_Time_M     var word    'What time to turn the lights on
    W_Off_Time_H    var word    'Time to turn lights off
    W_Off_Time_M    var word    'Time to turn lights off
    
    ' Blues:
    B_Min           var word    'Min Light value
    B_Max           var word    'Max Light value
    B_Fade_In       var word    'Fade in duration
    B_Fade_Out      var word    'Fade out duration
    B_On_Time_H     var word    'What time to turn the lights on
    B_On_Time_M     var word    'What time to turn the lights on
    B_Off_Time_H    var word    'Time to turn lights off
    B_Off_Time_M    var word    'Time to turn lights off
    
    'Running fade variables
    B_FadeIn_Time   var word    'LIVE fade delay time seconds
    B_FadeOut_Time  var word
    W_FadeIn_Time   var word
    W_FadeOut_Time  var word
    B_Cnt           var word    'Seconds counter for the timing of adjusting the PWM
    W_Cnt           var word
    B_Tick          var word
    W_Tick          var word
    
    'PWM Variables where the current output values are stored
    W_PWM           var word    'Whites
    B_PWM           var word    'Royal Blues
    
    '****************************************************************
    Blue_Day_Cycle      var word    'Store for which part of the day cycle we are in
    White_Day_Cycle     var word
    
    ' Constants for the current running mode cycle for the lights
    DAWN    con     0
    DAY     con     1
    DUSK    con     2
    NIGHT   con     3
    
    '****************************************************************
    'switches
    
    
    tog1 var  byte
    
    
    '****************************************************************
    'Expendable variables used for misc calculations etc
    Vb_1            var word
    Vb_2            Var word
    Vb_3            var word
    Vb_4            var word
    
    Power           var bit
    
    '****************************************************************
    ' Include files and configure timers
    
    DEFINE WRITE_INT 1
    INCLUDE "DT_INTS-18.bas"                        ; Base Interrupt System
    INCLUDE "ReEnterPBP-18.bas"
    ASM
    INT_LIST  macro    ; IntSource,          Label,  Type, ResetFlag?    
            INT_Handler     TMR1_INT,  _MyTimer,   PBP,  yes
        endm
        INT_CREATE               ; Creates the interrupt processor
    endasm
    T0CON = %11000111
    
    @   INT_ENABLE   TMR1_INT                       ; enable Timer1 interrupts
    'T2CON = %00000001                               ; free-running, 1:1 prescaler
    
    
    ENABLE DEBUG
    
    '****************************************************************
    'DS18B20 setting
    
    DQ              VAR     PORTA.5         ' One-wire data pin
    temperature     VAR     WORD            ' Temperature storage
    count_remain    VAR     BYTE            ' Count remaining
    count_per_c     VAR     BYTE            ' Count per degree C
    
    '****************************************************************
    'analog settings
    
    ADCON0 = 0                      'Set ADCON0
    ADCON1 = %00001111              'Set D i/o
    CMCON = 7                       'Disable Comparators
    
    PR2 = 249 
    '****************************************************************
    'Port settings
    
    CCP1CON = %00001100             ' 
    CCP2CON = %00001100             '     
    TRISA  = %11101111              'set PORTA as all output apart from 0,1,2
    TRISB  = %00000011 
    TRISD  = %00000011              'set PORTB as all output apart from 0&1
                       
    '****************************************************************
    'Varibles 
    
    GIE               VAR INTCON.7
    TempWD            VAR WORD                      ' temporary WORD variable
    Bvar              VAR BYTE                      ' temporary BYTE variable
    TempStr           VAR BYTE[8]
    EditChannel       VAR BYTE
    option            var byte
    Hours             var byte
    Minutes           var byte
    nitetemp          var word
    fn                var byte
    cn                var byte
    AllZeros          VAR Bit
    
    Tick_Tmr          var byte
    
    Value             VAR WORD
    Sensor	          VAR BYTE
    viv               var byte
    
    counterX           var byte
    
    Counter1 var word
    set1 var word
    set2 var word
    
    ButtonRepeat con 200	' Timeperiod (in mS for Edit Buttons auto-repeat
    				            ' should you want to hold them down...
    
    '****************************************************************
    'Data presets 
    
    
    
    data @0
        data    14      'Whites ON Time HOURS 
        data    00       'Whites ON Time MINS  
        data    20      'Whites OFF Time HOURS
        data    30       'Whites OFF Time MINS
        data    0       'Whites Fade IN duration HOURS
        data    10      'Whites Fade IN duration MINS
        data    0       'Whites Fade OUT duration HOURS
        data    15       'Whites Fade OUT duration MINS
        data    0       'Whites MIN Intensity %
        data    80      'Whites MAX Intensity %
    
        data    14      'Blues ON Time HOURS
        data    05       'Blues ON Time MINS
        data    20      'Blues OFF Time HOURS
        data    45       'Blues OFF Time MINS
        data    0       'Blues Fade IN duration HOURS
        data    05       'Blues Fade IN duration MINS
        data    0       'Blues Fade OUT duration HOURS
        data    10       'Blues Fade OUT duration MINS
        data    0       'Blues MIN Intensity %
        data    100      'Blues MAX Intensity %
    
    '**************************************************************** 
    'Read EEPROM on Power-Up
    ' gosub Read_eeprom
      
    '****************************************************************
    ;Initialization
    init2:
    
    LCDOUT $FE,1:FLAGS=0:PAUSE 250:LCDOUT $FE,1:PAUSE 250 ' Initialize LCD
    LCDOUT  $FE,1
    lcdout  $FE,$C0,"    Version T    "
    pause   2000   'wait 2 seconds so they can see it
    lcdout  $FE,1   'Clear the screen
    'gosub   read_eeprom     'Read in the data needed for the lighting periods 
    option=1   
    
    tog1 =0
    init:
    b_cnt = 0
    w_cnt = 0
    
    'gosub  Calc_Fade
    'gosub check_lighting    
    
     HzTimer VAR Word         '1/2 second counter (2 Hz)    
    HH VAR Byte  ' Hours 0-23
    MM VAR ByTE  ' Minutes 0-59
    SS VAR Byte  ' Seconds 0-59
    col VAR Byte  ' colon 1=on, 0=0ff
    S1H var byte
    S1M var byte
    S1S var byte
    S2H var byte
    S2M var byte
    S2S var byte
    
    HzTimer=$7A12        'for 1/2 Sec
    HH=19:MM=58:SS=58:col=0 'initial conditions (12:00 O'clock noon)
    
    S1H=20:S1M=0:S1S=0
    S2H=20:S2M=30:S2S=0
    
    
    '****************************************************************     
    ;************        Main Program Loop              *************
    '****************************************************************
    Main:
    
    ClockLoop: IF intcon.2=0 THEN ClockLoop	'Poll for TMRO overflow
    INTCON.2=0 ' Clear TMRO overflow flag
    
    HzTimer=HzTimer-$1000	'decrement timer count
    
    IF HzTimer < $1000  THEN
    IF Col=10 THEN    ' update time'
    SS=SS+1
            
    IF SS=60 THEN   ' minute
    SS=0 
    MM=MM+1
    IF MM=60 THEN    ' hour            
    MM=0
    HH=HH+1
    IF HH=24 THEN HH=0
    ENDIF                           
    counter1 = (HH*60)+MM
    ENDIF
    ENDIF
    Col=Col+1
    
    HzTimer=HzTimer+$7A12	' Add 0.5 seconds to count
    ELSE
        ' Do something here but must be less than 65.5 mSec
    ENDIF
    if Col=11 then 
    Col=1
    endif
       
    LCDOut $FE,$D4,#HH DIG 1,#HH DIG 0,":",#MM DIG 1,#MM DIG 0,":",#SS DIG 1,#SS Dig 0 
    
    
    
    If SetButton=0 then                                 ; jump to programming
    goto mainmenu
    endif
    
    
    If S1H=HH and S1M=MM and S1S=SS then      
    B_Fade_in = 5
    lcdout $FE,$80+13,"DAWN "
    B_PWM=B_PWM+1
    If B_PWM=B_MAX then 
    lcdout $FE,$80+13,"DAY "
    endif 
    If Counter1 =set2 then 
    B_PWM = B_PWM-1
    lcdout $FE,$80+13,"DUSK "
    endif
    Endif
    
        hpwm 1,W_PWM,200        
        hpwm 2,B_PWM,200
    
    If (B_PWM * 100)/255 = 100 then
    lcdout $FE,$80,"BLUES  ",dec3 (B_PWM *100)/255,"%"
    endif
    
    If (B_PWM * 100)/255 =0 or (B_PWM * 100)/255 <10 then
    lcdout $FE,$80,"BLUES  ",dec1 (B_PWM *100)/255,"% "
    endif
    
    if (B_PWM * 100)/255 >=10 and (B_PWM * 100)/255 <100 then
    lcdout $FE,$80,"BLUES  ",dec2 (B_PWM *100)/255,"%  "
    endif
    
    If (W_PWM*100)/255 >= 100 then
    lcdout $FE,$C0,"WHITES ",dec3 (W_PWM *100)/255,"%"
    endif
    
    If (W_PWM * 100)/255 <=0 or (W_PWM * 100)/255 <10 then
    lcdout $FE,$C0,"WHITES ",dec1 (W_PWM *100)/255,"% "
    endif
    
    if (W_PWM * 100)/255 >=10 AND (W_PWM * 100)/255 <100 then
    lcdout $FE,$C0,"WHITES ",dec2 (W_PWM *100)/255,"%  "
    endif
    
    'Get and display the temperature
    
        OWOUT DQ, 1, [$CC, $44]                 ' Start temperature conversion
        OWOUT DQ, 1, [$CC, $BE]                 ' Read the temperature
        OWIN DQ, 0, [temperature.LOWBYTE, temperature.HIGHBYTE]
        temperature = temperature */ 1600
        lcdout $FE,$D4+11,"TEMP ",DEC(temperature / 100),$DF,"C"
    	
    
    
        
    GOTO Main
    Last edited by Scampy; - 29th October 2013 at 19:40.

  9. #9
    Join Date
    May 2013
    Location
    australia
    Posts
    2,383


    Did you find this post helpful? Yes | No

    Default Re: Stuck on porting code to 18F4520

    in the posted code where is your isr "MyTimer" ?

    in the temperature routine you do not wait for the ds1820 to perform the conversion
    \'Get and display the temperature

    OWOUT DQ, 1, [$CC, $44] ' Start temperature conversion

    program usually loops here till conversion complete {about 700ms for 12bit)

    OWOUT DQ, 1, [$CC, $BE] ' Read the temperature
    OWIN DQ, 0, [temperature.LOWBYTE, temperature.HIGHBYTE]
    temperature = temperature */ 1600
    lcdout $FE,$D4+11,"TEMP ",DEC(temperature / 100),$DF,"C"

  10. #10
    Join Date
    Oct 2009
    Posts
    583


    Did you find this post helpful? Yes | No

    Default Re: Stuck on porting code to 18F4520

    Richard, the MyTimer was left over from the previous code, and was used to scale the timing of the pwm as in the version of PBP there is no floating point maths. For example if the calculation gave the duration as 4.456 seconds, this would be rounded down to 4 seconds, so over the duration it would loose 255 x 0.456 or almost two minutes. In real terms that's not a problem, but the previous author used that to perform its function. Here is the routine

    Code:
    ' Timer used to scale the PWM pulse
     MyTimer:
        Tick_Tmr = Tick_Tmr + 1
        if Tick_Tmr >180 then     ;179
            Tick_Tmr = 0
            b_cnt = b_cnt + 1
            W_cnt = W_cnt + 1
        endif  
    @  INT_RETURN
    The temperature routine was an example found on the net and works well...

  11. #11
    Join Date
    Oct 2009
    Posts
    583


    Did you find this post helpful? Yes | No

    Default Re: Stuck on porting code to 18F4520

    OK I've reverted to using a counter to dictate when each case statement is reached. The only issue i have is that for the life of me I can't work out how to slow the B_PWM=B_PWM+1 down so the PWM goes from 0 to 255 over the course of 30 minutes without stopping the clock running (such as would happen if a pause statement was used).

    To make this simple I'm going to remove any user programming for now just to make things simple. If anyone can help with some code to assist me in achieving this delay between increasing / decreasing the PWM once every seven seconds without upsetting the clock it would be most appreciated. The timings in the code below are for testing only, so I'm not waiting for things to happen

    Code:
    If Counter1 < 1200 then
    Blue_day_cycle = NIGHT
    endif
    
    if Counter1 = 1200 then
    Blue_day_cycle = DAWN
    endif
    
    if counter1 =1202 then
    Blue_day_cycle = DUSK
    endif
    
      '*** Do BLUE daily cycle
        select case Blue_Day_Cycle
        case DAWN 
        lcdout $FE,$80+13,"DAWN "
                B_PWM = B_PWM+1
            if B_PWM = b_max then
                Blue_Day_Cycle = DAY
            endif
        case DAY
        lcdout $FE,$80+13,"DAY  "
                Blue_Day_Cycle = DUSK
        CASE DUSK
        lcdout $FE,$80+13,"DUSK "
            if  B_PWM > B_Min then
                B_PWM = B_PWM - 1
            endif
            if B_PWM = b_min then 
                Blue_Day_Cycle = NIGHT
            endif
        case NIGHT
        lcdout $FE,$80+13,"NIGHT"
            if HH = S2H and MM = S2M then
                Blue_Day_Cycle = DAWN
                b_cnt = 0
            endif 
        end select
    Last edited by Scampy; - 30th October 2013 at 16:37. Reason: added code

  12. #12
    Join Date
    May 2013
    Location
    australia
    Posts
    2,383


    Did you find this post helpful? Yes | No

    Default Re: Stuck on porting code to 18F4520

    why not use
    if seconds//7= 0 then
    B_PWM=B_PWM+1
    endif

  13. #13
    Join Date
    Oct 2009
    Posts
    583


    Did you find this post helpful? Yes | No

    Default Re: Stuck on porting code to 18F4520

    Hi Richard,

    That gives me something similar to

    If SS = 7 or SS=14 or SS=21

    Which results in the value reaching 255 in around 4 segments. The code converts 0 - 255 into percentage (0 - 100%) on the LCD. Running your suggestion, whilst the seconds are displayed the B_PWM=B_PWM+1 runs, so when it starts it reads 29%, then at 7 seconds 59% 89% at 14 and 100% by 22 seconds, pausing between each multiple of seven, if that makes sense ?

    Malcolm

  14. #14
    Join Date
    May 2013
    Location
    australia
    Posts
    2,383


    Did you find this post helpful? Yes | No

    Default Re: Stuck on porting code to 18F4520

    try

    if ss//7= 0 then
    if ss != old_ss then
    B_PWM=B_PWM+1
    old_ss=ss
    endif
    endif
    Last edited by richard; - 31st October 2013 at 02:46. Reason: typo

  15. #15
    Join Date
    Oct 2009
    Posts
    583


    Did you find this post helpful? Yes | No

    Default Re: Stuck on porting code to 18F4520

    Richard, thanks for your help - that works a treat. I've changed the value in the first line for testing, but using the following code it changes from night to dawn, ramps up to 100%, changes to day, then to dusk at the set time, and fades down to 0% and changes to night - in 12 minutes. I'll reset the value to 7 and see how long the cycle lasts.

    Code:
    case DAWN 
    lcdout $FE,$80+13,"DAWN "   
    if ss//1= 0 then
    if ss != old_ss then
    B_PWM=B_PWM+1
    old_ss=ss
    endif
    endif
    if B_PWM = b_max then
    Blue_Day_Cycle = DAY
    endif
    
    case DAY
    lcdout $FE,$80+13,"DAY  "
    if counter1 =1208 then
    Blue_day_cycle = DUSK
    endif
        
    CASE DUSK
    lcdout $FE,$80+13,"DUSK "
    if ss//1= 0 then
    if ss != old_ss then
    B_PWM=B_PWM-1
    old_ss=ss
    endif
    endif
    if B_PWM = b_min then 
    Blue_Day_Cycle = NIGHT
    endif
    
    case NIGHT
    lcdout $FE,$80+13,"NIGHT"
    if HH = S2H and MM = S2M then
    Blue_Day_Cycle = DAWN
    endif
    Now as I want to learn from this experience, can you please explain the use of the double forward slash and exclamation mark

  16. #16
    Join Date
    May 2013
    Location
    australia
    Posts
    2,383


    Did you find this post helpful? Yes | No

    Default Re: Stuck on porting code to 18F4520

    its all in the manual ,// is modulo and ! is not

    ss//7 =0 :- means if ss is divided by 7 it's the remainder that is tested to be equal to 0 , ie 15/7 = 2 with a remainder of 1 whereas 21//7 = 3 with a remainder of 0
    the != :- means simply not equal to

  17. #17
    Join Date
    Oct 2009
    Posts
    583


    Did you find this post helpful? Yes | No

    Default Re: Stuck on porting code to 18F4520

    Richard, I've stumbled on a small issue which I would like some further assistance with if possible.

    I have two channels, one for blue LEDs and the other for Whites. I've replicated the working CASE statements for the white, but I can't seem to find a solution to running these case statements independently. I need to be able to have the two channels working at different times, for example both the Blues and Whites come on at 14:00 hrs, the Whites ramp up at a different rate than the blues (as set by the //7 part of the code) and then whites fade down at 20:00 hrs, followed by the blues at 20:15.

    If I comment out the section under blue select the whites run fine, and vice-verca, but whilst the LCD displays a change in case (ie from NIGHT to DAWN) for both white and blue channels, only one (the blues) ramps up the LED and displays the increasing percentage on the LCD. Here's the code (now tidied up with the non used variables etc removed)

    Code:
    '*******************************************************************************
    ' Variables
    '*******************************************************************************
    
    DecButton var PORTA.0	         ' Press to Decrement Button
    SetButton var PORTA.1            ' Press to Set/memorise Button
    IncButton var PORTA.2	         ' Press to Increment Button
    
    H_butt   VAR PORTA.2
    M_butt   VAR PORTA.0
    S_butt   VAR PORTA.1
    
    Counter1 var word
    
    ' LED variables - Whites:
    W_Min           var word    'Min Light value
    W_Max           var word    'Max Light value
    
    ' LED variables - Blues:
    B_Min           var word    'Min Light value
    B_Max           var word    'Max Light value
    
    'PWM Variables where the current output values are stored
    W_PWM           var word    'Whites
    B_PWM           var word    'Royal Blues
    
    HzTimer VAR Word         '1/2 second counter (2 Hz)    
    HH VAR Byte  ' Hours 0-23
    MM VAR ByTE  ' Minutes 0-59
    SS VAR Byte  ' Seconds 0-59
    col VAR Byte  ' colon 1=on, 0=0ff
    
    old_ss var byte
    
    '*******************************************************************************
    Blue_Day_Cycle      var word    'Store for which part of the day cycle we are in
    White_Day_Cycle     var word
    
    ' Constants for the current running mode cycle for the lights
    DAWN    con     0
    DAY     con     1
    DUSK    con     2
    NIGHT   con     3
    
    '*******************************************************************************
    ;Initialization
    
    LCDOUT $FE,1:FLAGS=0:PAUSE 250:LCDOUT $FE,1:PAUSE 250 ' Initialize LCD
    LCDOUT  $FE,1
    lcdout  $FE,$C0,"    Version R    "
    pause   2000   'wait 2 seconds so they can see it
    lcdout  $FE,1   'Clear the screen
       
    HzTimer=$7A12        'for 1/2 Sec
    HH=19:MM=59:SS=50:col=0 'initial conditions (19:59 )
    
    B_MAX=255
    W_MAX=255
    
    '*******************************************************************************
    '************                       Main Program Loop              *************
    '*******************************************************************************
    Main:
    
    ClockLoop: IF intcon.2=0 THEN ClockLoop	'Poll for TMRO overflow
    INTCON.2=0 ' Clear TMRO overflow flag
    
    HzTimer=HzTimer-$1000	'decrement timer count
    
    IF HzTimer < $1000  THEN
    IF Col=10 THEN    ' update time'
    SS=SS+1
            
    IF SS=60 THEN   ' minute
    SS=0 
    MM=MM+1
    IF MM=60 THEN    ' hour            
    MM=0
    HH=HH+1
    IF HH=24 THEN HH=0
    ENDIF                           
    counter1 = (HH*60)+MM
    ENDIF
    ENDIF
    Col=Col+1
    
    HzTimer=HzTimer+$7A12	' Add 0.5 seconds to count
    ELSE
        ' Do something here but must be less than 65.5 mSec
    ENDIF
    if Col=11 then 
    Col=1
    endif
       
    LCDOut $FE,$D4,#HH DIG 1,#HH DIG 0,":",#MM DIG 1,#MM DIG 0,":",#SS DIG 1,#SS Dig 0 
    
    If Counter1 < 1200 then
    Blue_day_cycle = NIGHT
    endif
    
    if Counter1 = 1200 then
    Blue_day_cycle = DAWN
    endif
    
    If Counter1 < 1200 then
    White_day_cycle = NIGHT
    endif
    
    if Counter1 = 1200 then
    White_day_cycle = DAWN
    endif
    
    
    '*** Do BLUE daily cycle
    select case Blue_Day_Cycle
        
    case DAWN 
    lcdout $FE,$80+13,"DAWN "   
    if ss//1= 0 then
    if ss != old_ss then
    B_PWM=B_PWM+1
    old_ss=ss
    endif
    endif
    if B_PWM = b_max then
    Blue_Day_Cycle = DAY
    endif
    
    case DAY
    lcdout $FE,$80+13,"DAY  "
    if counter1 =1210 then
    Blue_day_cycle = DUSK
    endif
        
    CASE DUSK
    lcdout $FE,$80+13,"DUSK "
    if ss//1= 0 then
    if ss != old_ss then
    B_PWM=B_PWM-1
    old_ss=ss
    endif
    endif
    if B_PWM = b_min then 
    Blue_Day_Cycle = NIGHT
    endif
    
    case NIGHT
    lcdout $FE,$80+13,"NIGHT"
    B_PWM=B_min
    end select
    
    
    '*** Do WHITE daily cycle
    select case White_Day_Cycle
        
    case DAWN 
    lcdout $FE,$C0+13,"DAWN "   
    if ss//1= 0 then
    if ss != old_ss then
    W_PWM = W_PWM+1
    old_ss=ss
    endif
    endif
    if W_PWM = W_max then
    White_Day_Cycle = DAY
    endif
    
    case DAY
    lcdout $FE,$C0+13,"DAY  "
    if counter1 =1210 then
    White_day_cycle = DUSK
    endif
        
    CASE DUSK
    lcdout $FE,$C0+13,"DUSK "
    if ss//1= 0 then
    if ss != old_ss then
    W_PWM=W_PWM-1
    old_ss=ss
    endif
    endif
    if W_PWM = W_min then 
    White_Day_Cycle = NIGHT
    endif
    
    case NIGHT
    lcdout $FE,$C0+13,"NIGHT"
    W_PWM=W_min
    end select
    
        hpwm 1,W_PWM,200        
        hpwm 2,B_PWM,200
    
    If (B_PWM * 100)/255 = 100 then
    lcdout $FE,$80,"BLUES  ",dec3 (B_PWM *100)/255,"%"
    endif
    
    If (B_PWM * 100)/255 =0 or (B_PWM * 100)/255 <10 then
    lcdout $FE,$80,"BLUES  ",dec1 (B_PWM *100)/255,"% "
    endif
    
    if (B_PWM * 100)/255 >=10 and (B_PWM * 100)/255 <100 then
    lcdout $FE,$80,"BLUES  ",dec2 (B_PWM *100)/255,"%  "
    endif
    
    If (W_PWM*100)/255 >= 100 then
    lcdout $FE,$C0,"WHITES ",dec3 (W_PWM *100)/255,"%"
    endif
    
    If (W_PWM * 100)/255 <=0 or (W_PWM * 100)/255 <10 then
    lcdout $FE,$C0,"WHITES ",dec1 (W_PWM *100)/255,"% "
    endif
    
    if (W_PWM * 100)/255 >=10 AND (W_PWM * 100)/255 <100 then
    lcdout $FE,$C0,"WHITES ",dec2 (W_PWM *100)/255,"%  "
    endif
    
    'Get and display the temperature
    
        OWOUT DQ, 1, [$CC, $44]                 ' Start temperature conversion
        OWOUT DQ, 1, [$CC, $BE]                 ' Read the temperature
        OWIN DQ, 0, [temperature.LOWBYTE, temperature.HIGHBYTE]
        temperature = temperature */ 1600
        lcdout $FE,$D4+11,"TEMP ",DEC(temperature / 100),$DF,"C"
      
    GOTO Main 
    
    ''****************************************************************     
    ';************        Main Program Loop END          *************
    ''****************************************************************
    Any suggestions ?

  18. #18
    Join Date
    May 2013
    Location
    australia
    Posts
    2,383


    Did you find this post helpful? Yes | No

    Default Re: Stuck on porting code to 18F4520

    you need an old_ss_blue and a old_ss_white so the channels can be independent

  19. #19
    Join Date
    Oct 2009
    Posts
    583


    Did you find this post helpful? Yes | No

    Default Re: Stuck on porting code to 18F4520

    magic works a treat - Thanks Richard

  20. #20
    Join Date
    Oct 2009
    Posts
    583


    Did you find this post helpful? Yes | No

    Default Re: Stuck on porting code to 18F4520

    Richard, need some further assistance please.

    I want to be able to set the delay that the LEDs fade up and down via a menu option on the LCD. To make the interface user friendly I would like to enter the period in which the LEDs fade to the pre set brightness in the form of hours and minutes rather than "level 1, 2, 3" etc where that value is used in the SS/1 or SS/2 etc. Can you advise on how to convert time delay into these values. EG Delay Hours (DH) and delay minutes (DM) gets converted into //white_delay. So for example I want the whites to ramp up in 30 minutes DM gets converted into the value after SS/ (which I'm guessing will be around 5 or 6 ?)

  21. #21
    Join Date
    May 2013
    Location
    australia
    Posts
    2,383


    Did you find this post helpful? Yes | No

    Default Re: Stuck on porting code to 18F4520

    the ramp up time in seconds is 255*"the modulo divider"
    ie for a modulo 7 time = 255*7 = 1785 seconds (29.75 minutes)
    the modulo range is 1 to 59 in your present scheme

  22. #22
    Join Date
    Oct 2009
    Posts
    583


    Did you find this post helpful? Yes | No

    Default Re: Stuck on porting code to 18F4520

    Richard, many thanks for your continued support. That gives me something to work with

  23. #23
    Join Date
    Oct 2009
    Posts
    583


    Did you find this post helpful? Yes | No

    Default Re: Stuck on porting code to 18F4520

    guys, I have a strange issue which I can't seem to resolve - It's probably my maths, but on paper it seems to work out, but falls over when running.

    I have 4 word variables for setting the fade in and fade out duration of the two LEDs, the duration between steps in the duty cycle stems from the assistance I had above. Here's the code

    Code:
    blue_delay_in = (((fadesetHR[0]*60)+fadesetMN[0])*60)/255   'takes hours and minutes, converts to minutes, then converts to seconds
    white_delay_in = (((fadesetHR[1]*60)+fadesetMN[1])*60)/255  'takes hours and minutes, converts to minutes, then converts to seconds
    blue_delay_out = (((fadeoutHR[0]*60)+fadeoutMN[0])*60)/255  'takes hours and minutes, converts to minutes, then converts to seconds
    white_delay_out = (((fadeoutHR[1]*60)+fadeoutMN[1])*60)/255 'takes hours and minutes, converts to minutes, then converts to seconds
    This works fine if the delay is less than an hour. Setting a delay of 0 hours and 59 minutes gives a value for the variable of 13, which is close to the 13.88 seconds when using a calculator. But if I set the duration to 1 hour and zero minutes the value for the variables is shown as 0 and the leds remain at the max setting. My maths suggest it should be 14. (1 hr * 60 = 60 minutes, add zero minutes, total minutes still = 60. * 60 to convert to seconds = 3600 seconds / 255 = 14.11s delay)

    I'm using the following to display the value for these variables, but can't see why it would be that which relates to the issue
    Code:
    lcdout $FE,$80,"B.FO ",DEC blue_delay_out,"   W.FO ",dec white_delay_out
    Any Ideas guys ?

  24. #24
    Join Date
    Jan 2005
    Location
    Montreal, Quebec, Canada
    Posts
    2,588


    Did you find this post helpful? Yes | No

    Default Re: Stuck on porting code to 18F4520

    Is your variable a byte or word?

    60 x 60 = 3600

    Not sure things will go as expected before dividing by 255 if using a byte (guessing here).

    Robert



    Edit: That's not it: 59 minutes x 60 = 3540 seconds and that works...


    Edit some more: Got it, your brackets are screwed up.


    Code:
    (((fadeoutHR[1]*60)+fadeoutMN[1])*60)/255
    
    |
    |
    V
    
    ((fadeoutHR[1]*60)+(fadeoutMN[1]*60))/255
    Last edited by Demon; - 16th December 2013 at 04:35.

  25. #25
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,516


    Did you find this post helpful? Yes | No

    Default Re: Stuck on porting code to 18F4520

    Robert,
    I don't think that's it.
    With your version of the formula one hour "is worth" as much as one minute. Try it out with 1h 0min, then try it again with 0h 1min - if I'm not mistaken you'll get the same result.

    Not that *I* see any real problem with the original formula but you could try:
    Code:
    blue_delay_in VAR WORD
    blue_delay_in = (fadesetHR[0] * 3600 + fadesetMN[0] * 60) / 255
    /Henrik.

  26. #26
    Join Date
    Oct 2009
    Posts
    583


    Did you find this post helpful? Yes | No

    Default Re: Stuck on porting code to 18F4520

    Morning guys,

    Thanks for the suggestions. Yes the variable is a word, sorry should of pointed that out.

    I haven't tried the suggestions yet, but wondered if Henrik's suggestion could be modified to
    Code:
    blue_delay_in = (fadesetHR[0] * 3600) + (fadesetMN[0] * 60) / 255
    In my mind this will work out the seconds for both hours and minutes then add them together and divide the result by 255 ??

    I never have been good at calculations with lots of brackets when programming !!

  27. #27
    Join Date
    Oct 2009
    Posts
    583


    Did you find this post helpful? Yes | No

    Default Re: Stuck on porting code to 18F4520

    Tried both Henriks version and my revised version and both give a zero value for the variable when the delay is set to 1 hr. Works fine if set to 59 minutes.

    Thinking it might be something to do with the menu option to set the delay times, I used the data statements that contain preset settings to set the delay to 1 hr and still got zero for the result.

    It seems PBP must handle bracketed equations different to "normal"

    EDIT - Most illogical captain...

    I tried it without the brackets
    Code:
    blue_delay_in = fadesetHR[0] * 3600 + fadesetMN[0] * 60 / 255
    Setting the delay to 1 hour gives a value of 14 for the variable !!!!!
    Last edited by Scampy; - 16th December 2013 at 11:32.

  28. #28
    Join Date
    Jan 2005
    Location
    Montreal, Quebec, Canada
    Posts
    2,588


    Did you find this post helpful? Yes | No

    Default Re: Stuck on porting code to 18F4520

    Darn, Henrik is right, again. Shoot, my formula makes things worse.

    I was sure I figured a math problem, oh well, I suck.

    Robert

  29. #29
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,516


    Did you find this post helpful? Yes | No

    Default Re: Stuck on porting code to 18F4520

    Hi,
    In my mind this will work out the seconds for both hours and minutes then add them together and divide the result by 255 ??
    I believe that's exactly what mine does. Multiplication and division has precedence over addition and subtraction. So mine will take the hours and multiply that by 3600, then it'll take the minutes and multiply those by 60. It will then add those two results together because they are enclosed within brackets and THEN divide that result by 255 - which I believe is what you want.

    /Henrik.

  30. #30
    Join Date
    Oct 2009
    Posts
    583


    Did you find this post helpful? Yes | No

    Default Re: Stuck on porting code to 18F4520

    Quote Originally Posted by Demon View Post
    Darn, Henrik is right, again. Shoot, my formula makes things worse.

    I was sure I figured a math problem, oh well, I suck.

    Robert
    Robert... it's fine, just put it down to recovering from the party

  31. #31
    Join Date
    Oct 2009
    Posts
    583


    Did you find this post helpful? Yes | No

    Default Re: Stuck on porting code to 18F4520

    Guys, see my edited post above - it works without the brackets - why I have no idea !!

  32. #32
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,516


    Did you find this post helpful? Yes | No

    Default Re: Stuck on porting code to 18F4520

    OK, seems you've got it going now but during that time I tried "my version" of the formula with the following code:
    Code:
    PsuedoSeconds VAR WORD
    Hours VAR WORD
    Minutes VAR WORD
    
    Start:
        HSEROUT["Program start",13,13]
    
        Minutes = 30
        Hours = 0
        GOSUB Calculate
        GOSUB PrintResult
        
        Minutes = 59
        Hours = 0
        GOSUB Calculate
        GOSUB PrintResult
        
        Minutes = 0
        Hours = 1
        GOSUB Calculate
        Gosub PrintResult
        
        Hours = 2
        Minutes = 15
        GOSUB Calculate
        Gosub PrintResult
    
        Hours = 3
        Minutes = 59
        GOSUB Calculate
        Gosub PrintResult
                
        Hours = 10
        Minutes = 0
        GOSUB Calculate
        Gosub PrintResult
        
        Pause 100
        
    END
    
    Calculate:
        PsuedoSeconds = (Hours * 3600 + Minutes * 60) / 255
    RETURN
    
    PrintResult:
        HSEROUT[DEC2 Hours, ":", DEC2 Minutes, " - ", DEC PsuedoSeconds,13]
    RETURN
    And it outputs:
    Code:
    Program start
    
    00:30 - 7
    00:59 - 13
    01:00 - 14
    02:15 - 31
    03:59 - 56
    10:00 - 141
    Which seems correct to me.

    /Henrik.

  33. #33
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,516


    Did you find this post helpful? Yes | No

    Default Re: Stuck on porting code to 18F4520

    I have no idea what you're doing.....
    I've tried three versions of the formula and the only one to give correct results are the one I posted. Please see the following code and the results:
    Code:
    PsuedoSeconds VAR WORD
    Hours VAR WORD
    Minutes VAR WORD
    
    Start:
        HSEROUT["Program start",13,13]
    
        Minutes = 30
        Hours = 0
        GOSUB Calculate
        
        Minutes = 59
        Hours = 0
        GOSUB Calculate
        
        Minutes = 0
        Hours = 1
        GOSUB Calculate
      
        Hours = 2
        Minutes = 15
        GOSUB Calculate
    
        Hours = 3
        Minutes = 59
        GOSUB Calculate
        
        Hours = 10
        Minutes = 0
        GOSUB Calculate
       
        Pause 100
        
    END
    
    Calculate:
        PsuedoSeconds = (Hours * 3600 + Minutes * 60) / 255
        HSEROUT["Version 1: "]
        GOSUB PrintResult
        
        PsuedoSeconds = (Hours * 3600) + (Minutes * 60) / 255
        HSEROUT["Version 2: "]
        GOSUB PrintResult
        
        PsuedoSeconds = Hours * 3600 + Minutes * 60 / 255
        HSEROUT["Version 3: "]
        GOSUB PrintResult
        
        HSEROUT[13]
    RETURN
    
    PrintResult:
        HSEROUT[DEC2 Hours, ":", DEC2 Minutes, " - ", DEC PsuedoSeconds,13]
    RETURN
    Result:
    Code:
    Program start
    
    Version 1: 00:30 - 7
    Version 2: 00:30 - 7
    Version 3: 00:30 - 7
    
    Version 1: 00:59 - 13
    Version 2: 00:59 - 13
    Version 3: 00:59 - 13
    
    Version 1: 01:00 - 14
    Version 2: 01:00 - 3600
    Version 3: 01:00 - 3600
    
    Version 1: 02:15 - 31
    Version 2: 02:15 - 7203
    Version 3: 02:15 - 7203
    
    Version 1: 03:59 - 56
    Version 2: 03:59 - 10813
    Version 3: 03:59 - 10813
    
    Version 1: 10:00 - 141
    Version 2: 10:00 - 36000
    Version 3: 10:00 - 36000
    As you can see, the only one to give correct results are the first one. Why, because the other two both will take the result of the Minutes*60 divided by 255 and add THAT result to Hours*60 while the first (correct) one will divide the sum of the multiplications by 255. How you are getting the correct results from the formula without any parenthesis is beyond me.

    /Henrik.

  34. #34
    Join Date
    Oct 2009
    Posts
    583


    Did you find this post helpful? Yes | No

    Default Re: Stuck on porting code to 18F4520

    I found the cause. I had duplicated the fade in / fade out lines within another subroutine which was being called and thus overwriting the changes made - I guess that's what happens when you spend too long coding into the wee hours of the night !! - Mistakes happen

  35. #35
    Join Date
    Oct 2009
    Posts
    583


    Did you find this post helpful? Yes | No

    Default Re: Stuck on porting code to 18F4520

    Hi Guys,

    I've stumbled on a small issue that is doing my head in.

    Whilst using 8 bit resolution for the fading (ie 0 - 255) the code works and allows a minimum fade up / down time of 5 minutes, I now want to increase the resolution to 4095 steps.

    Code:
    blue_delay_in = fadesetHR[0] * 3600 + fadesetMN[0] * 60 / 255
    white_delay_in = fadesetHR[1] * 3600 + fadesetMN[1] * 60 / 255
    blue_delay_out = fadeoutHR[0] * 3600 + fadeoutMN[0] * 60 / 255
    white_delay_out = fadeoutHR[1] * 3600 + fadeoutMN[1] * 60 / 255
    If the above is changed by removing the 255 at the end of each line and replacing that with 4095 it means the minimum fade in/out time is now 68 minutes as anything less results in an FP decimal which isn't supported by PBP

    The resulting value for blue_fade_in etc is then used in the case statements to perform the PWM

    Code:
    case DAWN 
    lcdout $FE,$80+15,"DAWN "   
    if ss//blue_delay_in = 0 then
    if ss != old_ss_blue then
    B_PWM=B_PWM+1
    old_ss_blue=ss
    endif
    endif
    if B_PWM = B_Mid then
    Blue_Day_Cycle = MORN
    endif
    Is there anything I can do to the code as it stands to get the timing so that I can use the 4095 resolution, and still have fade in/out times of 5 minutes or more rather than 68 minutes.

  36. #36
    Join Date
    Jan 2005
    Location
    Montreal, Quebec, Canada
    Posts
    2,588


    Did you find this post helpful? Yes | No

    Default Re: Stuck on porting code to 18F4520

    Could this be a byte VS word issue?

    Sorry, falling asleep.

    Robert

  37. #37
    Join Date
    Oct 2009
    Posts
    583


    Did you find this post helpful? Yes | No

    Default Re: Stuck on porting code to 18F4520

    Hi Rob,

    Yes I've changed the variable from byte to word.

  38. #38
    Join Date
    May 2013
    Location
    australia
    Posts
    2,383


    Did you find this post helpful? Yes | No

    Default Re: Stuck on porting code to 18F4520

    if I remember correctly your fadein loop runs at 1 sec intervals therefore , 5 minutes = 300 seconds
    increment is :- 4095/300 = 13.65
    so the loop must inc the pwm value by 13 or 14

    case DAWN
    lcdout $FE,$80+15,"DAWN "
    if ss//blue_delay_in = 0 then
    if ss != old_ss_blue then
    B_PWM=B_PWM+13
    old_ss_blue=ss
    endif
    endif
    if B_PWM = B_Mid then
    Blue_Day_Cycle = MORN
    endif

  39. #39
    Join Date
    Oct 2009
    Posts
    583


    Did you find this post helpful? Yes | No

    Default Re: Stuck on porting code to 18F4520

    Hi Richard,

    Surely it should be 300/4095 rather than the way suggested. I need 4095 increments of 0.073s to fade from 0 to 4095 (0.073*4095 = 298.935)

    Also, if B_PWM= B_PWM+13 then in essence that would only give me a resolution of 315 steps or there about and not the 4095 I'm after would it not ?

  40. #40
    Join Date
    May 2013
    Location
    australia
    Posts
    2,383


    Did you find this post helpful? Yes | No

    Default Re: Stuck on porting code to 18F4520

    you could do it that way but doesn't your main timing loop increment in seconds ?

    and the pwm value would go from 0 to 3900 in 300 x 13 increments
    Last edited by richard; - 23rd May 2014 at 04:02.

Similar Threads

  1. Porting code to new PIC
    By malc-c in forum mel PIC BASIC Pro
    Replies: 12
    Last Post: - 1st January 2011, 00:20
  2. Kind of stuck
    By gti_uk in forum mel PIC BASIC Pro
    Replies: 6
    Last Post: - 31st May 2009, 21:45
  3. Getting Stuck in loop
    By Frozen001 in forum mel PIC BASIC Pro
    Replies: 22
    Last Post: - 19th November 2008, 16:46
  4. Any idea's on porting this to PBP?
    By Ryan7777 in forum mel PIC BASIC Pro
    Replies: 20
    Last Post: - 10th October 2008, 20:21
  5. Replies: 1
    Last Post: - 8th May 2008, 01:52

Members who have read this thread : 1

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