News:

PROTON pic BASIC Compilers for PIC, PIC24, dsPIC33

Main Menu

British summer time problem

Started by RGV250, Oct 30, 2021, 01:06 PM

Previous topic - Next topic

RGV250

Hi,
A big thanks to Charlie for the initial code and the link to the website. The liver can take a rest now as I do not need the power of beer to work it out:-)

Procedures are brilliant, like subroutines on steroids. They really do tidy up the code but I am still learning with them. Structures could be interesting.

I noticed a small issue with the code, I am still learning to put the variable type in front and when I changed the year from byte to word I did not change the b to w.
On that point, I believe there was a thread where people gave their methods but cannot find it.

Just wondering if it is worth putting on the Wiki if anyone else might be interested.

Bob

John Drew

#21
G'day all,
As a fun thing to do, I wrote the following to determine if a date should have Daylight Saving added or not.
It is a fresh look and does not use any of the algorithms on the web. It only uses integer calculations.The result is in the variable DStime=1 for on, 0 for off.
I've checked it in ISIS and all seems well. Essentially it calculates days from a fixed date (1/1/2017) which happens to have Sunday on the 1st of January.
It could easily be expanded to output the days of the week, but this isn't necessary for Daylight Saving. I have not allowed for 2100 although an easy fix. Let's face it, none of us will be using PICs in 2100!
Cheers from Oz,
John

' Daylight saving in UK. It works by calculating days from 1/1/2017
' written in Positron by John Drew VK5DJ - Version 1.0.0 November 2021 - for 16F1827
;-------------------------------------------------------------------------------
;**** Added by Fuse Configurator ****
; Use the Fuse Configurator plug-in to change these settings

Device = 16F1827
Declare Xtal = 32
Declare Reminders Off
@ CONFIG_REQ = 0 ; Override Compiler's configuration settings
Asm-
__Config _Config1, 0x0FFC ;FOSC_INTOSC & WDTE_ON & PWRTE_OFF & MCLRE_ON & CP_OFF & CPD_OFF & BOREN_ON & CLKOUTEN_OFF & IESO_OFF & FCMEN_OFF
__Config _Config2, 0x1FFF ;WRT_OFF & PLLEN_ON & STVREN_ON & BORV_LO & LVP_OFF
Endasm-
Declare Reminders On

;**** End of Fuse Configurator Settings ****
;-------------------------------------------------------------------------------

Declare Bootloader off
Declare Dead_Code_Remove = On
All_Digital=TRUE
TRISB=%00000000                              ' all outputs)

TRISA=%00100000                              ' all ouputs except mclr

Symbol WATCH WDTCON              'watchdog
'Symbol RBPU OPTION_REG.7         'weak pullups

WATCH =  %00100101               'WDT set for 256 seconds  (4 min 16secs)
OSCCON = %11110000               'set the internal oscilator to 4MHz      was 01101010
OPTION_REG.7 = 1                 'disable weak pullups
           
'set up the LCD
Declare LCD_Type 0                            'text type
Declare LCD_DTPin PORTB.4                'assigns data lines to B4..7
Declare LCD_ENPin PORTB.3                'enable pin
Declare LCD_RSPin PORTB.2                'RS line pin
Declare LCD_Interface 4                      '4 or 8 line interface
Declare LCD_Lines 2                        'lines in the display
Declare LCD_DataUs 255                  'delay data to allow for slow LCD
Declare LCD_CommandUs 65000              'add a delay for commands, normally 2000

'Global variables
Dim Month     As Byte
Dim Totaldays As Word
Dim Day       As Word
Dim Year      As Word
Dim HoldDay   As Word
Dim DStime    As Word
Dim MarchDay  As Word
Dim NovDay    As Word
Dim Today     As Word

GoTo Main

Proc GetDates()                           ' convert NMEA strings to word,byte,year
     'here you will dismantle the NMEA string and assign day, month and year
     Day= 3                               'these are test values
     Month= 7
     Year= 2021
EndProc

'get day number from 1/1/17 which is the last date to have Sunday on the 1st of the month

Proc GetDayNo(dDate As Word, mDate As Byte, yDate As Word), Word 
Dim LeapDays   As Word
Dim Days       As Word
Dim TempDays   As Word
Dim YearsCount As Word
Dim Tempyears  As Word

    Leapdays = 0                        'reset values
    TempDays = 0
    For YearsCount = 2017 To yDate
      Tempyears = YearsCount - 2000     ' leap years and inc leapdays
      If Tempyears // 4 = 0 Then
         Inc (leapdays)                 ' leap days
      EndIf
    Next

    TempYears = yDate - 2017            ' how many years from 2017
    Days = (365* TempYears) + leapdays  ' days from year data
    'now get days till end of last month
    Tempdays = LookUpL mDate,[0,0,31,59,90,120,151,181,212,243,273,304,334]
    Totaldays = Days + TempDays + dDate -1       
    Result = Totaldays                   ' total from 1/1/2017 including today
EndProc

'get the last Sunday of the month as Daylight saving changeover day in UK
Proc LastSunday(bMth As Byte, yDate As Word), Word
Dim TempDay As Byte
Dim dSave As Word
    For TempDay = 1 To 31
        Totaldays = GetDayNo(TempDay,bMth,yDate)
        If Totaldays // 7 = 0 Then
           dSave = Totaldays             ' find Sundays, overwrites dSave until
        EndIf                            ' last Sunday
    Next                                               
    Result = dSave                       ' assign to dates
EndProc

Main:     
     Cls
     Clrwdt
     HoldDay = 0
     GetDates()                                  ' gets integer dates from NMEA string

'get todays day number
     Today = GetDayNo(Day,Month,Year)            ' get todays day number 
'get March day number   
     MarchDay = LastSunday(3,Year)               ' get March of this year last Sunday
'get November day number
     NovDay= LastSunday(11,Year)                 ' get November of this year last Sunday

     Print At 1,1,Dec Today, " ",Dec MarchDay," ",Dec NovDay ' for test purposes only. Gives 3 relevant day numbers
     If Today >= MarchDay And Today < NovDay Then
         DStime = 1                              ' use DStime = 1to add 1 hour to current times
         Print At 2,1,"DStime on"                ' test purpose only
     Else
         DStime = 0                              ' do not add 1 hr.
         Print At 2,1,"DStime off"               ' test purpose only
     EndIf
End