Stuck on porting code to 18F4520


Results 1 to 40 of 43

Threaded View

  1. #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 18:40.

Similar Threads

  1. Porting code to new PIC
    By malc-c in forum mel PIC BASIC Pro
    Replies: 12
    Last Post: - 31st December 2010, 23:20
  2. Kind of stuck
    By gti_uk in forum mel PIC BASIC Pro
    Replies: 6
    Last Post: - 31st May 2009, 20:45
  3. Getting Stuck in loop
    By Frozen001 in forum mel PIC BASIC Pro
    Replies: 22
    Last Post: - 19th November 2008, 15: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, 19:21
  5. Replies: 1
    Last Post: - 8th May 2008, 00:52

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