Sat s timerom

Rasprava o PIC mikrokontrolerima, PIC projekti i drugo vezano za PIC-eve...

Moderators: pedja089, stojke369, [eDo], trax

Post Reply
Marin (Kemija)
Posts: 8
Joined: 04-08-2007, 09:06

Sat s timerom

Post by Marin (Kemija) »

Pozdrav!

Zanimalo bi me ima li tko rutinu za sat, znači namjestim mu vrijeme i datum, i onda da mogu mu recimo namjesitit datum i vrijeme kad da upali relej......ili ići još sa onim DS1302 kao real time clock?


Inače radim u PICBASIC pro.....i 16F877 koristim..


Pa ako netko ima kakav primjer!

tnx!
Ljupco
Penjem se :)
Penjem se :)
Posts: 98
Joined: 06-03-2008, 13:27

Super tema!

Post by Ljupco »

Imam ja sat, ali nisam ga isprobao, ali kako da se umetne u programu da se ukluci relej i sto je najbitnije treba da se programira kad da ukljuci relej, trebalo bi malo da mozgamo. JEL TI HOCES U KRACE VREME, NA PROMER SAT, DVA, DAN, ILI VISE DANA NA ODREDJENI DATUM DA UKLJUCI RELEJ, AJDE AKO MOZES OBJASNI MI. I ako imas PicBsic Pro full verziju ajde molim te postavi je ovde jer ja imam samo neku ogranicenu verziju. POZZ
User avatar
ShOkRe
Pravo uznapredovao :)
Pravo uznapredovao :)
Posts: 468
Joined: 10-01-2007, 20:55
Location: Pula

writing via ipaq

Post by ShOkRe »

imam ja, radio sam si sam jer nisam mogo naci gotovo, ali nazalost zauzet sam da nestignem ovih dana.... ako ti je hirno.... ali probacu za vikend.... od 6 sam na nogama i evo sad sam dosao doma sa posla, lego u krevetu i tipkam po telefonu (ppc-u) :-(
Marin (Kemija)
Posts: 8
Joined: 04-08-2007, 09:06

Post by Marin (Kemija) »

E hvala, aj ako možeš pa baci tu ili na PM, mail kako ti draže...!
User avatar
ShOkRe
Pravo uznapredovao :)
Pravo uznapredovao :)
Posts: 468
Joined: 10-01-2007, 20:55
Location: Pula

Post by ShOkRe »

sorry, nikako nemogu naci fuck'n filez... neznam na kom kompu su mi, vidjet cu sutra na poslu da mi nisu u trecem lap-u....

evo ti za prvu pomoc neki primjer koji sam nasao ali nije moj....

Code: Select all

'****************************************************************
'*  Name    : şat1.BAS                                          *
'*  Author  : Tomislav Pusec                                    *
'*  Notice  : Copyright (c) 2005 Tom's Lab                      *
'*          : All Rights Reserved                               *
'*  Date    : 21.4.2005.                                        *
'*  Version : 1.0                                               *
'*  Notes   :                                                   *
'*          :                                                   *
'****************************************************************
    define LOADER_USED 1	'izbaciti ako ne koristimo bootloader
    define OSC 20		
    define LCD_EREG PORTC	
    define LCD_EBIT 1   

'--varijable termometra,-izbaciti ako ne koristimo---------------
    DQ var PORTC.0
    busy var bit
    R_Temp var word
    TempC var word
    float var word
    sign var byte
'================================================================
    
    Symbol sda = PORTC.2    'I2C komunikacija 
    Symbol scl = PORTC.3    'I2C komunikacija
    addr var Byte           'I2C komunikacija
    
    brojac var byte     'za dane alarma
    
    a1 var byte     'za button funkcije
    a2 var byte     'za button funkcije
    a3 var byte     'za button funkcije
    a4 var byte     'za button funkcije
    a5 var byte     'za button funkcije
    b1 var byte     'za button funkcije
    b2 var byte     'za button funkcije
    b3 var byte     'za button funkcije
    b4 var byte     'za button funkcije
    
    hi var byte     'Print rutina
    lo var byte     'Print rutina
    value var byte  'Print rutina
    
    stat var byte       'citanje status registra
    control var byte    'citanje alarm registra
    
    dan var byte        'u display
       
    sec var Byte        'za citanje lokacije 02 
    m_in var Byte       'za citanje lokacije 03
    hour var Byte       'za citanje lokacije 04
    day var Byte        'za citanje lokacije 05
    month var Byte      'za citanje lokacije 06
           
    year_pr var byte            'prikaz godine
    index var byte              'prikaz godine
    index_temp var byte         'prikaz godine
        
    dan_temp var byte           'u set_dan rutini (dan u tjednu)
    
    a_hour var byte             'sati za podesavanje alarma
    a_min var byte              'minute za podesavanje alarma
    a_sec var byte              'sekunde za podesavanje alarma
    a var byte    
    
    a_temph var byte            'podesavanje alarma,sati-temp
    a_temp10h var byte          'podesavanje alarma,desetice sata-temp
    a_tempm var byte            'podesavanje alarma,minute-temp
    a_temp10m var byte          'podesavanje alarma,desetice minuta-temp
    
    temph var byte              'podesavanje sati-temp
    temp10h var byte            'podesavanje sati,desetice-temp
    tempm var byte              'podesavanje minuta-temp
    temp10m var byte            'podesavanje minuta,desetice-temp
    tempmo var byte             'podesavanje mjeseci-temp
    temp10mo var byte           'podesavanje mjeseci,desetice-temp
    tempday var byte            
    temp10day var byte

'------------------definicija kvacice u alarm meniu-----------        
    lcdout $fe, %01000000
    lcdout %00000000
    lcdout %00000000
    lcdout %00000000
    lcdout %00000001
    lcdout %00000010
    lcdout %00010100
    lcdout %00001000
    lcdout %00000000

'-----------------definicija stupnja C------------------------
    lcdout $fe, %01001000
    lcdout %00000111
    lcdout %00000101
    lcdout %00000111
    lcdout %00000000
    lcdout %00000000
    lcdout %00000000
    lcdout %00000000
    lcdout %00000000

'-------------pocetni alarm:----------------------------------- 
    brojac = 0
    ADCON1=7
    low PORTA.5
    
    addr = 0
    I2CWrite sda, scl, $a2, addr, [$04]     'omoguci alarm
    addr = 8
    I2CWrite sda, scl, $a2, addr, [$20]     'weekday alarm ukljucen,int zabranjen
    addr = 9
    I2CWrite sda, scl, $a2, addr, [$00]     'stotinke sek alarma
    addr = 10
    I2CWrite sda, scl, $a2, addr, [$00]     'sekunde alarma
    addr = 11
    I2CWrite sda, scl, $a2, addr, [$59]     'minute alarma
    addr = 12
    I2CWrite sda, scl, $a2, addr, [$23]     'sati alarma

'--------------pocetno vrijeme:----------------------------------    
    addr = 2 
    I2CWrite sda, scl, $a2, addr, [$20]     'sekunde 
    addr = 3 
    I2CWrite sda, scl, $a2, addr, [$59]     'minute 
    addr = 4 
    I2CWrite sda, scl, $a2, addr, [$23]     'sati 
    addr = 5 
    I2CWrite sda, scl, $a2, addr, [$69]     'godina/datum 
    index=1
    index_temp=0
    year_pr=5
    addr = 6 
    I2CWrite sda, scl, $a2, addr, [$43]     'dan u tjednu/mjesec 

'--spremanje inicijalno podesenog alarma u varijable kako bismo--
'--prvim ulaskom u alarmni izbornik imali suvisle podatke--------
    addr = 10
    I2CRead sda, scl, $a2, addr, [a_sec] 
    addr = 11
    I2CRead sda, scl, $a2, addr, [a_min] 
    addr = 12
    I2CRead sda, scl, $a2, addr, [a_hour]     

'------------glavna petlja:---------------------------------------
loop: 
    a1=0:a2=0:a3=0:a4=0:a5=0
    b1=0:b2=0:b3=0:b4=0
    addr = 2                                    
    I2CRead sda, scl, $a2, addr, [sec]          'procitaj sekunde  
    addr = 3 
    I2CRead sda, scl, $a2, addr, [m_in]         'procitaj minute 
    addr = 4 
    I2CRead sda, scl, $a2, addr, [hour]         'procitaj sate 
    addr = 5 
    I2CRead sda, scl, $a2, addr, [day]          'procitaj datum i godinu
    addr = 6 
    I2CRead sda, scl, $a2, addr, [month]        'procitaj dan u tj i mjesec 
    
    
    button PORTB.1,0,15,122,a4,1,set_hour    'podesavanje sata 			T1
    button PORTB.2,0,255,0,a5,1,set_min      'podesavanje minuta		T2
    button PORTB.3,0,255,0,a3,1,set_dan      'podesavanje dana u tjednu		T3
    button PORTB.4,0,255,0,a1,1,set_datum    'podesavanje datuma-dan u mjesecu	T4
    button PORTB.5,0,255,0,a2,1,set_month    'podesavanje mjeseca		T5

    button PORTB.6,0,255,0,b1,1,alarm_toggle 'ukljuci/iskljuci alarm  		T6          
    button PORTB.7,0,255,0,b3,1,alarm_hour   'podesavanje sata alarma		T7
    button PORTC.5,0,255,0,b4,1,alarm_min    'podesavanje minute alarma		T8
    button PORTC.4,0,180,0,b2,1,alarm_day    'podesavanje dana alarma-izbornik	T9
          
display:
    index_temp.0=day.6
    index_temp.1=day.7
    if index <> index_temp then
        year_pr=year_pr+1
    endif 
    index=index_temp

'---------mjerenje temperature, izbaciti ako se ne koristi----------------
    Start_convert:
    owout DQ,1,[$CC,$44]
Wait_up:
    owin DQ,4,[busy]
    if busy = 0 then Wait_up
    owout DQ,1,[$CC,$BE]
    owin DQ,2,[R_Temp.lowbyte,R_Temp.highbyte]
    gosub convert_temp
    goto dalje
    
convert_temp:
    lcdout $fe,1
    sign="+"
    TempC=R_Temp.lowbyte>>1
    if R_Temp.0=1 then 
        Float=5
    else
        float=0
    endif
    lcdout sign,dec TempC,".",dec1 float,%00000001,"C"
    return
dalje:
'=========================================================================          
    value=hour
    gosub print
    lcdout " ",dec1 hi,dec1 lo,":"
    value=m_in
    gosub print
    lcdout dec1 hi, dec1 lo,":" 
    value=sec
    gosub print
    lcdout dec1 hi, dec1 lo
    
    lcdout $fe, $c0
    dan=month>>5                    'lokacija 06
    if dan=0 then lcdout "ned"
    if dan=1 then lcdout "pon"
    if dan=2 then lcdout "uto"
    if dan=3 then lcdout "sri"
    if dan=4 then lcdout "cet"
    if dan=5 then lcdout "pet"
    if dan=6 then lcdout "sub"
    lcdout "   "
    value=day & %00111111           'lokacija 05 
    Gosub print 
    lcdout dec1 hi,dec1 lo 
    Lcdout "/" 
    value=month & %00011111         'lokacija 06
    gosub print
    lcdout dec1 hi,dec1 lo    
    Lcdout "/20", dec2 year_pr 
    pause 400
    goto loop
 
'-----------obrada tipke T1----------------------
set_hour:				
    temph=hour & $0F
    temp10h=hour>>4
    temph=temph+1
    if temph>9 then 
        temph=0
        temp10h=temp10h+1
    endif
    if temph=4 AND temp10h=2 then
        temph=0
        temp10h=0
    endif
        
    hour.0=temph.0
    hour.1=temph.1
    hour.2=temph.2
    hour.3=temph.3
    hour.4=temp10h.0
    hour.5=temp10h.1
    hour.6=temp10h.2
    hour.7=temp10h.3
    addr = 4 
    I2CWrite sda, scl, $a2, addr, [hour]    
    value=hour
    gosub print
    lcdout $fe,$88,dec1 hi,dec1 lo
    goto display

'---------obrada tipke T2------------------------
set_min:				
    tempm=m_in & $0F
    temp10m=m_in>>4
    tempm=tempm+1
    if tempm>9 then 
        tempm=0
        temp10m=temp10m+1
    endif
    if tempm=0 AND temp10m=6 then
        tempm=0
        temp10m=0
    endif
        
    m_in.0=tempm.0
    m_in.1=tempm.1
    m_in.2=tempm.2
    m_in.3=tempm.3
    m_in.4=temp10m.0
    m_in.5=temp10m.1
    m_in.6=temp10m.2
    m_in.7=temp10m.3
    addr = 2 
    I2CWrite sda, scl, $a2, addr, [$00] 
    addr = 3 
    I2CWrite sda, scl, $a2, addr, [m_in]    
    goto display

'-------------obrada tipke T5---------------------
set_month:				
    tempmo=month & $0F
    temp10mo=(month & %00011111)>>4
    tempmo=tempmo+1
    if tempmo>9 then 
        tempmo=0
        temp10mo=temp10mo+1
    endif
    if tempmo=3 AND temp10mo=1 then
        tempmo=1
        temp10mo=0
    endif
    
    month.0=tempmo.0
    month.1=tempmo.1
    month.2=tempmo.2
    month.3=tempmo.3
    month.4=temp10mo.0
    addr = 6 
    I2CWrite sda, scl, $a2, addr, [month]    
    goto display
    
'-----------obrada tipke T4------------------------
set_datum:				
    day=day & %00111111
    temp10day=day>>4
    tempday=day & $0F
    tempday=tempday+1
    if tempday>9 then 
        tempday=0
        temp10day=temp10day+1
    endif
    if tempday=2 AND temp10day=3 then
        tempday=1
        temp10day=0
    endif
    
    day.0=tempday.0
    day.1=tempday.1
    day.2=tempday.2
    day.3=tempday.3
    day.4=temp10day.0
    day.5=temp10day.1
    
    day.6=index.0   'visak
    day.7=index.1   'visak
    
    addr = 5 
    I2CWrite sda, scl, $a2, addr, [day]    
    goto display
      
'----------------obrada tipke T3------------------
set_dan:				
    dan_temp=month>>5
    dan_temp=dan_temp+1
    if dan_temp>6 then dan_temp=0
    lcdout $fe,$c0
    if dan_temp=0 then lcdout "ned"
    if dan_temp=1 then lcdout "pon"
    if dan_temp=2 then lcdout "uto"
    if dan_temp=3 then lcdout "sri"
    if dan_temp=4 then lcdout "cet"
    if dan_temp=5 then lcdout "pet"
    if dan_temp=6 then lcdout "sub"
    month.5=dan_temp.0
    month.6=dan_temp.1
    month.7=dan_temp.2
    addr = 6
    I2CWrite sda, scl, $a2, addr, [month] 
    goto display

'---------------obrada tipke T6--------------------
alarm_toggle:				
    addr = 0 
    I2CRead sda, scl, $a2, addr, [stat]
    addr=8
    I2CRead sda, scl, $a2, addr, [control]
    if stat=$04 AND control=$20 then
        control=$A0
        high PORTA.5
        goto upis
    endif
    if stat=$06 then
        control=$20
        stat=$04
        low PORTA.5
        goto upis
    endif
    if stat=$04 AND control=$A0 then
        control=$20
        low PORTA.5    
        goto upis
    endif
    upis:
    addr=0
    I2CWrite sda, scl, $a2, addr, [stat]
    addr=8
    I2CWrite sda, scl, $a2, addr, [control]
    goto display

'------------obrada tipke T9------------------------
alarm_day:				
    lcdout $fe,1
    lcdout "alarm >"
    value=a_hour
    gosub print
    lcdout dec1 hi,dec1 lo,":"
    value=a_min
    gosub print
    lcdout dec1 hi,dec1 lo,":"
    value=a_sec
    gosub print
    lcdout dec1 hi,dec1 lo
    
    lcdout $fe,$c0
    dani_alarma:
    brojac=brojac+1
    select case brojac
        case 1
            a=%01111111         'svi dani
            lcdout "p",0,"u",0,"s",0,"c",0,"p",0,"s",0,"ne",0
        case 2                  'radni dani
            a=%00111110
            lcdout "p",0,"u",0,"s",0,"c",0,"p",0,"s ne"
        case 3                  'radni dani+subota
            a=%01111110
            lcdout "p",0,"u",0,"s",0,"c",0,"p",0,"s",0,"ne"
    case else
            brojac=0
            goto dani_alarma
    end select
    pause 500
    if PORTC.4=0 then alarm_day
    addr=$0E
    I2CWrite sda, scl, $a2, addr, [a]
    goto display

'-----------------obrada tipke T7---------------------
alarm_hour:				
    a_temph=a_hour & $0F
    a_temp10h=a_hour>>4
    a_temph=a_temph+1
    if a_temph>9 then 
        a_temph=0
        a_temp10h=a_temp10h+1
    endif
    if a_temph=4 AND a_temp10h=2 then
        a_temph=0
        a_temp10h=0
    endif
    if a_temp10h>2 then a_temp10h=0
    a_hour.0=a_temph.0
    a_hour.1=a_temph.1
    a_hour.2=a_temph.2
    a_hour.3=a_temph.3
    a_hour.4=a_temp10h.0
    a_hour.5=a_temp10h.1
    a_hour.6=a_temp10h.2
    a_hour.7=a_temp10h.3
    addr = 12 
    I2CWrite sda, scl, $a2, addr, [a_hour]  
    gosub ispis
    pause 300
    if PORTB.7=0 then alarm_hour    
    goto display
    
'----------obrada tipke T8-----------------------------------
alarm_min:				
    a_tempm=a_min & $0F
    a_temp10m=a_min>>4
    a_tempm=a_tempm+1
    if a_tempm>9 then 
        a_tempm=0
        a_temp10m=a_temp10m+1
    endif
    if a_tempm=0 AND a_temp10m=6 then
        a_tempm=0
        a_temp10m=0
    endif
    if a_temp10m>5 then a_temp10m=0
    a_min.0=a_tempm.0
    a_min.1=a_tempm.1
    a_min.2=a_tempm.2
    a_min.3=a_tempm.3
    a_min.4=a_temp10m.0
    a_min.5=a_temp10m.1
    a_min.6=a_temp10m.2
    a_min.7=a_temp10m.3
    addr = 11 
    I2CWrite sda, scl, $a2, addr, [a_min]  
    gosub ispis
    pause 300
    if PORTC.5=0 then alarm_min    
    goto display

'----------print rutina:-------------------------------------
print: 
    hi=value>>4
    lo=value & $0F 
    Return 

'----------rutina za ispis izbornika minuta i sati alarma----
ispis:
    lcdout $fe, 1
    lcdout "Alarm time:"
    lcdout $fe,$c0
    value=a_hour
    gosub print
    lcdout dec1 hi, dec1 lo 
    Lcdout ":" 
    value=a_min
    gosub print
    lcdout dec1 hi, dec1 lo 
    Lcdout ":" 
    value=a_sec
    gosub print
    lcdout dec1 hi, dec1 lo
    return         
end
 
  
    
User avatar
ShOkRe
Pravo uznapredovao :)
Pravo uznapredovao :)
Posts: 468
Joined: 10-01-2007, 20:55
Location: Pula

Post by ShOkRe »

ovo ti je od melanije :-)

Code: Select all

'****************************************************************
'*  Name    : UNTITLED.BAS                                      *
'*  Author  : [select VIEW...EDITOR OPTIONS]                    *
'*  Notice  : Copyright (c) 2006 [select VIEW...EDITOR OPTIONS] *
'*          : All Rights Reserved                               *
'*  Date    : 21.9.2006                                         *
'*  Version : 1.0                                               *
'*  Notes   :                                                   *
'*          :                                                   *
'****************************************************************
	'
	'	Project MN1307
	'	==============
	'	Embedded Software Version 1.00
	'	Target Processor PIC16F628-04/SP
	'	Target PCB - BB-01
	'	Created 12/06/02  
	'	Last Updated 12/06/02
	'	Written by Melanie Newman
	'
	'	DS1307 RTC Program
	'	Includes Setting the Date and Time
	'	   3 Buttons Control Setting...
	'		12/24 hour Clock Mode
	'		Year
	'		Month
	'		Day of Month (also accounting for Leap Years)
	'		Hour
	'		Minute
	'		Second
	'
	'	Written for 2 line 16 Character LCD
	'
	'	This program is designed to "hold your hand" and prevent you
	'	   from setting any invalid data combinations into the DS1307
	'
	'	It's a crude program, but demonstrates how to set the clock
	'	   with control buttons
	'
	'	You will be automatically returned to the Time & Date display if
	'	   you are in the Setup and don't touch any Buttons for 20 seconds
	'

	'
	'	Device Programming Options
	'	--------------------------
	@ DEVICE pic16F628, INTRC_OSC_NOCLKOUT
			' System Clock Options	
	@ DEVICE pic16F628, WDT_ON
			' Watchdog Timer
	@ DEVICE pic16F628, PWRT_ON
			' Power-On Timer
	@ DEVICE pic16F628, BOD_ON
			' Brown-Out Detect
	@ DEVICE pic16F628, MCLR_OFF
			' Master Clear Options (Internal)
	@ DEVICE pic16F628, LVP_OFF
			' Low-Voltage Programming
	@ DEVICE pic16F628, CPD_OFF
			' Data Memory Code Protect
	@ DEVICE pic16F628, PROTECT_OFF
			' Program Code Protection

	'
	'	Hardware Assignments
	'	--------------------
		'
		'	System LCD
		'	----------
		'	No Resistors required on any line
		'	RA0-RA3 Data bits DB0-DB3
		'	RA6 RS-bit
		'	RA7 E-Bit
		'
	Define LCD_DREG PORTA	' Port for LCD Data
	Define LCD_DBIT 0	' Use lower 4 bits of Port
	Define LCD_RSREG PORTA	' Port for RegisterSelect (RS) bit
	Define LCD_RSBIT 6	' Port Pin for RS bit
	Define LCD_EREG PORTA	' Port for Enable (E) bit
	Define LCD_EBIT 7	' Port Pin for E bit
	Define LCB_BITS 4	' Using 4-bit bus
	Define LCD_LINES 2	' Using 2 line Display
	Define LCD_COMMANDUS 2000
				' Command Delay (uS)
	Define LCD_DATAUS 50	' Data Delay (uS)
		'
		'	DS1307 Connections
		'	------------------
		'	10K Resistors required from PORTB.1 and PORTB.2 to Vdd
		'	PORTB.0 relies on weak pull-up's (for future use)
		'
	SQWpin var PORTB.0
		'	You don't actually need to wire this pin... but...
		'	If you're observant, you'll notice I've wired the DS1307's 
		'	SQW to RB0.  Rather than 'poll' and display the time/date
		'	as in this example code, you can have an interrupt
		'	on RB0 each second to do this.  In fact if you look at
		' 	my code for setting the DS1307 you'll see I've already
		'	programmed it for a 1 second 'Tick' for exactly this
		'	purpose...
	SCLpin var PORTB.1
	SDApin var PORTB.2
		'
		'	Setting Buttons
		'	---------------
		'	Buttons are connected between PORTB.4, 5 & 6 down to Vss
		'	No Resistors required - we're using weak pull-up's
		'
	DecButton var PORTB.4	' Press to Decrement Button
	SetButton var PORTB.5	' Press to Set/memorise Button
	IncButton var PORTB.6	' Press to Increment Button
		'
		'	Unused Ports
		'	------------	
		' RA4
		' RA5
		' RB3
		' RB7

	'
	'	EEPROM Locations
	'	----------------
	Data @0,74,97,110,70,101,98,77,97,114,65,112,114
		' Jan Feb Mar Apr
	Data 77,97,121,74,117,110,74,117,108,65,117,103
		' May Jun Jul Aug
	Data 83,101,112,79,99,116,78,111,118,68,101,99
		' Sep Oct Nov Dec
	Data 84,117,101,87,101,100,84,104,117,70,114,105
		' Tue Wed Thu Fri
	Data 83,97,116,83,117,110,77,111,110
		' Sat Sun Mon

	'
	'	RAM Assignments and Variables
	'	-----------------------------
	CounterA var byte	' General purpose Variable
	CounterB var byte	' General purpose Variable
	CounterC var byte	' General purpose Variable
	CounterD var byte	' General purpose Variable
	RTCSec var byte		' Seconds
	RTCMin var byte		' Minutes
	RTCHour var byte	' Hours
	RTCWDay var byte	' Weekday
	RTCDay var byte		' Day
	RTCMonth var byte	' Months
	RTCYear var byte	' Year
	RTCCtrl var byte	' Control 
	SetTime var byte	' 12/24 Hour Clock
	SetSec var byte		' Seconds
	SetMin var byte		' Minutes
	SetHour var byte	' Hours
	SetDay var byte		' Day
	SetMonth var byte	' Months
	SetYear var byte	' Year
	TimeOut var word	' Variable for SetUp Menu Time-Out

	'
	'	Program Constants
	'	-----------------
	ButtonRepeat con 200	' Timeperiod (in mS for Edit Buttons auto-repeat
				' should you want to hold them down...
	
	'
	'	Start Program
	'	=============
	goto JumpStart

	'
	'	Subroutine Nest Area
	'	====================

	'
	'	Subroutine Converts back to BCD
	'	-------------------------------
		' CounterA is the entry variable
		' CounterB holds the converted BCD result on exit
ConvertBCD:
	CounterB=CounterA DIG 1
	CounterB=CounterB<<4
	CounterB=CounterB+CounterA DIG 0
	Return

	'
	'	Subroutine Displays Month at current Cursor Position
	'	----------------------------------------------------
		' CounterB holds Month (1-12) this value is destroyed by the routine
		' additionally uses CounterA and CounterD variables
DisplayMonth:
	CounterB=CounterB*3-3	' Convert BCD Month to EEPROM Start Address
DisplaySequence:
	For CounterA=CounterB to CounterB+2
				' Read and Display Month
		Read CounterA,CounterD
		LCDOut CounterD
		Next CounterA
	Return

	'
	'	Subroutine works out Number of Days in the Month
	'	------------------------------------------------
		' SetYear - entry variable containing year (0-99)
		' SetMonth - entry Variable Containing Month (1-12)
		' CounterA - exits with number of days
FindDays:
	LookUp SetMonth-1,[31,28,31,30,31,30,31,31,30,31,30,31],CounterA
		' Above line gives the 'usual' days of the Month 
		' Section below adjusts for Leap-Year
	If SetMonth=2 then
		If (SetYear&$03)=0 then CounterA=29
		endif
	Return
	
	'
	'	Subroutine waits until user reseases Set Button
	'	-----------------------------------------------
SetButtonRelease:
	LCDOut $FE,1
	While SetButton=0:Wend
	Pause 250	' Small pause to kill any Key-Bounce
	Return

	'
	'	End of Subroutine Nest Area
	'	---------------------------

	'
	'	Actual Start of Real Program
	'	============================
JumpStart:
	'
	'	Set Hardware Directions
	'	-----------------------
	CMCON=%00000111		' Disable Comparators
	TRISA=%00000000		' PORTA all set to Output 
	TRISB=%11111111		' PORTB all set to Input
	OPTION_REG.7=0		' Enable Weak Pull-Ups
	'
	'	Reset Hardware
	'	--------------
	Pause 200		' Timeout for LCD to settle

	'
	'	Time & Date Display Loop
	'	========================
ReDisplay:
	LCDOut $FE,1		' Clear LCD
ReDisplayLoop:
		'
		'	Read RTC
		'	--------
	I2CRead SDApin,SCLpin,$D0,$00,[RTCSec,RTCMin,RTCHour,RTCWDay,RTCDay,RTCMonth,RTCYear,RTCCtrl]
		'
		'	Decide if Setup Required/Wanted
		'	-------------------------------
	If RTCSec.7=1 then goto SetUpPreset
				' Hands-Up who noticed the fact that when the DS1307
				' powers-up initially from cold, and all it's
				' registers are potentially "unpredictable", that 
				' Seconds bit 7 is always high?
				' We can use this feature to automatically jump to
				' our 'Setup' routine on initial power-up if the user
				' forgets to keep 'Setup' depressed and so prevent
				' any 'undesriable' Time & Date displays...
	If SetButton=0 then
				' If Set Button is pressed, User will be taken to
				' the Setup Menu.  If the Set Button is pressed at
				' Power-Up, then the User will be taken to the Setup
				' Menu directly without any intermediate display
		Gosub SetButtonRelease
		goto Setup
		endif	
	'
	'	Display RTC
	'	-----------
	'	Displayed on 2-line 16 character LCD in the form...
	'		10:15:00 AM
	'		Wed 12 Jun 2002
	'	Note - AM/PM indicator only shows in 12 hour mode.
	'	Additionally there is Hours leading Zero Surpression in 12-Hour Mode
	'	   after-all, you don't display 07pm do you?
	'
		'
		'	Display Time on Line 1
		'	----------------------
	LCDOut $FE,$80
	If RTCHour.6=1 then
			' Work-Out 12 or 24 hour Display for Hours
		CounterA=(RTCHour>>4)&$01
		else
		CounterA=(RTCHour>>4)&$03
		endif
	CounterA=CounterA*10+(RTCHour&$0F)
	If RTCHour.6=1 then
			' Display Hours appropriately for 12 or 24 hour Mode 
		LCDOut #CounterA
		else
		LCDOut #CounterA Dig 1,#CounterA Dig 0
		endif
	LCDOut ":",#(RTCMin>>4)&$0F,#RTCMin&$0F,":"
	LCDOut #(RTCSec>>4)&$0F,#RTCSec&$0F," "
	IF RTCHour.6=1 then
		If RTCHour.5=1 then
			LCDOut "PM"
			else
			LCDOut "AM"
			endif
		endif
		'
		'	Display Day of Week on Line 2
		'	-----------------------------
			' Let's face it... the DS1307 doesn't calculate the Day of Week,
			' all it does is increment a number at each mignight from
			' 1 thru 7 and roll back to start from 1 all over again.
			' It's really up to you to work out the DOW and program it in.
			' This is a tough nut for integer math to do... see the
			' 'Calculate Day of Week' routine in the setup section.
	LCDOut " ",$FE,$C0
	CounterB=RTCWDay*3+33	' Convert BCD WeekDay to EEPROM Start Address
	Gosub DisplaySequence	' Display 3 character sequence from EEPROM
		'
		'	Display Date on Line 2
		'	----------------------
	LCDOut " ",#(RTCDay>>4)&$0F,#RTCDay&$0F," "
	CounterB=((RTCMonth>>4)&$0F)*10+(RTCMonth&$0F)
	Gosub DisplayMonth
	LCDOut " 20",#(RTCYear>>4)&$0F,#RTCYear&$0F,$FE,$80
	'
	Pause 250		' Repeat about 4 times/sec
	Goto ReDisplayLoop

	'
	'	Clock and Calandar Settings
	'	===========================
		'
		'	Initial Default Settings
		'	------------------------
		' Default setting preset to the moment this program first born
		'
SetupPreset:
	RTCSec=$00		' Seconds preset to 00
	RTCMin=$15		' Minutes preset to 15
	RTCHour=$13		' Hours preset to 13'00
	RTCWDay=$01		' Weekday preset to 01
	RTCDay=$12		' Day preset 12
	RTCMonth=$06		' Months preset to June
	RTCYear=$02		' Year preset to 2002
	RTCCtrl=$10		' Control preset to output 1 second 'Tick' on SQWpin 
		'
		'	Convert the Current BCD Data to Something easier to Edit
		'	--------------------------------------------------------
Setup:
	SetTime=RTCHour.6	' Determines 12/24 Hour mode
	If SetTime=1 then
				' Regardless of 12/24 options, SetHour always
				' contains the hours in 24 hour clock format
				' otherwise there's twice the amount of work
				' to keep track in the edit routines
		SetHour=(RTCHour>>4)&$01
				' if 12-hour mode
		else
		SetHour=(RTCHour>>4)&$03
				' if 24 hour mode
		endif
	SetHour=SetHour*10+(RTCHour&$0F)
	If SetTime=1 then
		If RTCHour.5=1 then
			If SetHour<12 then SetHour=SetHour+12
				' Add-in the AM/PM indicator
			else
			If SetHour=12 then SetHour=0
			endif
		endif
	SetMin=((RTCMin>>4)&$0F)*10+(RTCMin&$0F)
	SetSec=((RTCSec>>4)&$0F)*10+(RTCSec&$0F)
	SetYear=((RTCYear>>4)&$0F)*10+(RTCYear&$0F)
	SetMonth=((RTCMonth>>4)&$0F)*10+(RTCMonth&$0F)
	SetDay=((RTCDay>>4)&$0F)*10+(RTCDay&$0F)
		'
		'	Setup Menu Loop
		'	---------------
	CounterC=0		' Set to Start of Setup (seven steps from 0 to 6)
	TimeOut=0		' Zero the Time-Out counter
SetupLoop:
	LCDOut $FE,1,"Set "
		'
		'	First Display the Menu Prompts
		'	------------------------------
		'	This is a crude way of doing the entry Menu's but it was
		'	pretty fast and quite mindless to program this lunch-hour
		'
	If CounterC=0 then
		LCDOut "Mode"
		endif
	If CounterC=1 then 
		LCDOut "Hours"
		endif
	If CounterC=2 then
		LCDOut "Minutes"
		endif
	If CounterC=3 then 
		LCDOut "Seconds"
		endif
	If CounterC=4 then
		LCDOut "Year : 20"
		endif
	If CounterC=5 then
		LCDOut "Month"
		endif
	If CounterC=6 then
		LCDOut "Day"
		endif
	If CounterC<>4 then LCDOut " :"
		'
		'	Now Display the Data
		'	--------------------
SetupDisplayLoop:
			'
			' 	12/24 Hour Option
			'	-----------------
	If CounterC=0 then
		LCDOut $FE,$8B
		If SetTime=0 then
			LCDOut "24HR"
			else 
			LCDOut "12HR"
			endif
		LCDOut $FE,$8B
		endif
			'	Hours
			'	-----
			'	Hours are tricky because of the 12/24 & AM/PM bit
			'	so we have to reconvert 1-24 back to 1-12 plus AM/PM
	If CounterC=1 then
		CounterA=SetHour
		If SetTime=1 then
			LCDOut $FE,$8E
				' Display AM/PM bit
			If CounterA<12 then
				LCDOut "AM"
				else
				LCDOut "PM"
				endif
			If CounterA=0 then CounterA=12
			If CounterA>12 then CounterA=CounterA-12
			endif
		LCDOut $FE,$8C,#CounterA
		If CounterA<10 then LCDOut " "			
		LCDOut $FE,$8C
		endif
			'
			'	Minutes
			'	-------
	If CounterC=2 then
		LCDOut $FE,$8E,#SetMin
		If SetMin<10 then LCDOut " "
		LCDOut $FE,$8E
		endif
			'
			'	Seconds
			'	-------
	If CounterC=3 then
		LCDOut $FE,$8E,#SetSec
		If SetSec<10 then LCDOut " "
		LCDOut $FE,$8E
		endif
			'
			'	Year
			'	----
	If CounterC=4 then
		LCDOut $FE,$8D,#SetYear DIG 1,#SetYear Dig 0
		LCDOut $FE,$8D
		endif
			'
			'	Month
			'	-----
			'	Here's a nice touch to setting the month rather
			'	than the usual number entry method...
			'
	If CounterC=5 then
		LCDOut $FE,$8C
		CounterB=SetMonth
		Gosub DisplayMonth
		LCDOut $FE,$8C
		endif
			'
			'	Day
			'	---
	If CounterC=6 then
		LCDOut $FE,$8A,#SetDay
		If SetDay<10 then LCDOut " "
		LCDOut $FE,$8A
		endif
		'
		'	The Data-Entry Bit
		'	------------------
SetupEntryLoop:
			'
			'	Decrement Button Pressed
			'	------------------------
	If DecButton=0 then
				'
				'	12/24 Clock Selection
				'	---------------------
		If CounterC=0 then
			If SetTime=0 then 
				SetTime=1
				else
				SetTime=0
				endif
			endif
				'
				'	Decrement Hours
				'	---------------
		If CounterC=1 then
			If SetHour=0 then
				SetHour=23
				else
				SetHour=SetHour-1
				endif
			endif
				'
				'	Decrement Minutes
				'	-----------------
		If CounterC=2 then
			If SetMin=0 then
				SetMin=59
				else
				SetMin=SetMin-1
				endif
			endif
				'
				'	Decrement Seconds
				'	-----------------
				'	Kinda overkill to include setting seconds,
				'	but if you got codespace to burn...
		If CounterC=3 then
			If SetSec=0 then
				SetSec=59
				else
				SetSec=SetSec-1
				endif
			endif
				'
				'	Decrement Years
				'	---------------
		If CounterC=4 then
			If SetYear=0 then
				SetYear=99
				else
				SetYear=SetYear-1
				endif
			endif
				'
				'	Decrement Month
				'	---------------
		If CounterC=5 then
			If SetMonth=1 then
				SetMonth=12
				else
				SetMonth=SetMonth-1
				endif
					'
					' Changing the Month has a bearing on the
					' days, especially if you've got days preset
					' to 31 and you now select a month with fewer
					' days... so this bit here just prevents that
			Gosub FindDays
			If SetDay>CounterA then SetDay=CounterA
			endif
				'
				'	Decrement Days
				'	--------------
				'	Tricky one here... go work-out the days
				'	of the month first...
		If CounterC=6 then
			Gosub FindDays
			If SetDay=1 then
				SetDay=CounterA
				else
				SetDay=SetDay-1
				endif
			endif
		Pause ButtonRepeat
		TimeOut=0
		Goto SetUpDisplayLoop
		endif
			'
			'	Increment Button Pressed
			'	------------------------
			' 	This is just like Decrement but opposite...
	If IncButton=0 then
				'
				'	12/24 Clock Selection
				'	---------------------
		If CounterC=0 then
			If SetTime=1 then 
				SetTime=0
				else
				SetTime=1
				endif
			endif
				'
				'	Increment Hours
				'	---------------
		If CounterC=1 then
			If SetHour=23 then
				SetHour=0
				else
				SetHour=SetHour+1
				endif
			endif
				'
				'	Increment Minutes
				'	-----------------
		If CounterC=2 then
			If SetMin=59 then
				SetMin=0
				else
				SetMin=SetMin+1
				endif
			endif
				'
				'	Increment Seconds
				'	-----------------
				'	Kinda overkill to include setting seconds,
				'	but if you got codespace to burn...
		If CounterC=3 then
			If SetSec=59 then
				SetSec=0
				else
				SetSec=SetSec+1
				endif
			endif
				'
				'	Increment Years
				'	---------------
		If CounterC=4 then
			If SetYear=99 then
				SetYear=0
				else
				SetYear=SetYear+1
				endif
			endif
				'
				'	Increment Month
				'	---------------
		If CounterC=5 then
			If SetMonth=12 then
				SetMonth=1
				else
				SetMonth=SetMonth+1
				endif
					'
					' Changing the Month has a bearing on the
					' days, especially if you've got days preset
					' to 31 and you now select a month with fewer
					' days... so this bit here just prevents that
			Gosub FindDays
			If SetDay>CounterA then SetDay=CounterA
			endif
				'
				'	Increment Days
				'	--------------
				'	Tricky one here... go work-out the days
				'	of the month first...
		If CounterC=6 then
			Gosub FindDays
			If SetDay=>CounterA then
				SetDay=1
				else
				SetDay=SetDay+1
				endif
			endif
		Pause ButtonRepeat
		TimeOut=0
		Goto SetupDisplayLoop
		endif
			'
			'	Set Button Pressed
			'	------------------
	If SetButton=0 then
		CounterC=CounterC+1
				' Increment Menu Item
		TimeOut=0
		If CounterC>6 then
				' Save Data if all edit items exhaused
			LCDOut $FE,1,"Memorised"
				' Make the User feel happy
				'
				'	Save 12/24 Hours to BCD DS1307's Format
				'	---------------------------------------
			CounterA=SetHour
			If SetTime=1 then
				If CounterA>12 then CounterA=CounterA-12
				If CounterA=0 then CounterA=12
				endif
			Gosub ConvertBCD
			RTCHour=CounterB
					' Save the Hours Value
			If SetTime=1 then
				RTCHour.6=1
					' Save the 12 Hour Mode Flag
				If SetHour=>12 then RTCHour.5=1
					' Save the 'PM' Flag
				endif
				'
				'	Save Minutes
				'	------------
			CounterA=SetMin
			Gosub ConvertBCD
			RTCMin=CounterB
				'
				'	Save Seconds
				'	------------
			CounterA=SetSec
			Gosub ConvertBCD
			RTCSec=CounterB
				'
				'	Save Year
				'	---------
			CounterA=SetYear
			Gosub ConvertBCD
			RTCYear=CounterB
				'
				'	Save Month
				'	----------
			CounterA=SetMonth
			Gosub ConvertBCD
			RTCMonth=CounterB
				'
				'	Save Day
				'	--------
			CounterA=SetDay
			Gosub ConvertBCD
			RTCDay=CounterB
				'
				'	Calculate Day of Week & Save
				'	----------------------------
					' Melanie's fudge for calculating Days of Week
					' using PBP's integer math...
					' by the time someone's clock displays the
					' the wrong Day of Week, I'll hopefully have long
					' retired and be past caring...
					' I wouldn't however go stick this routine in a 
					' Nuclear Power Station to purge the reactor
					' every Monday morning without checking how far it
					' will work before the integer math overflows...
					' In my routine RTCWDay=1 is a Tuesday (the
					' start of my week) and continues sequentially
					' until RTCWDay=7 which is a Monday
			CounterA=SetYear+4
			CounterB=SetMonth
			If SetMonth<3 then
				CounterA=CounterA-1
				CounterB=CounterB+12
				endif
			CounterD=(SetDay+(153*CounterB-457)/5+365*CounterA+CounterA/4-CounterA/100+CounterA/400+2) MOD 7
			RTCWDay=CounterD+1
				'
				'	Do the Business
				'	---------------

			I2CWrite SDApin,SCLpin,$D0,$00,[RTCSec,RTCMin,RTCHour,RTCWDay,RTCDay,RTCMonth,RTCYear,RTCCtrl]
			Pause 1000
			Gosub SetButtonRelease
			Goto ReDisplay
			endif
		Gosub SetButtonRelease
		Goto SetupLoop	' Loop for Next Menu Item
		endif
		'
		'	Menu TimeOut Counter
		'	--------------------
	Pause 1			' Kill 1mS 
	TimeOut=TimeOut+1
	If TimeOut>20000 then goto ReDisplay
				' User paused too long
				' Return User to Time & Date Display
				' This will however bounce the User to the original
				' Setup Menu start point if the DS1307 has not 
				' been initialised
	Goto SetupEntryLoop	' Loop for Button Press

	'
	End
User avatar
ShOkRe
Pravo uznapredovao :)
Pravo uznapredovao :)
Posts: 468
Joined: 10-01-2007, 20:55
Location: Pula

Post by ShOkRe »

i još jedan...

Code: Select all

'****************************************************************
'*  Name    : UNTITLED.BAS                                      *
'*  Author  : [select VIEW...EDITOR OPTIONS]                    *
'*  Notice  : Copyright (c) 2006 [select VIEW...EDITOR OPTIONS] *
'*          : All Rights Reserved                               *
'*  Date    : 20.9.2006                                         *
'*  Version : 1.0                                               *
'*  Notes   :                                                   *
'*          :                                                   *
'****************************************************************
'****************************************************************
'*  Name    : ds1302_LCD_setup.BAS                              *
'*  Author  : [Omer Yildiz]                                     *
'*  Notice  : Copyright (c) 2006 [2005]                         *
'*          : All Rights Reserved                               *
'*  Date    : 02.02.2006                                        *
'*  Version : 1.0                                               *
'*  Notes   : This code is a modified version of the clock code posted to the MELabs PICBASIC Forums by CocaColaKid. Thanks to CocaColaKid for this post.
'*            It was not designed with a user interface by which time, date, etc. can be entered. The clock can only be set during the programming of PIC.
'
'            My modification adds a user interface so that the clock can be adjusted at any time.
'            The circuit is simple: two buttons for changing and setting the numbers, and a led that lights up when the user is in menu.

'           The values that you see on LCD are decimal values and once the last menu section (minute) is set then the decimal values that have been converted to HEX format 
'           are sent to DS1302. 

'           Converting Decimal values into HEX values is done in a long way. I would appreciate any information of a shorter way of doing it.

'           This code is of course for fr*e but if you post my nick name somewhere along with your design etc, that would hit my ego.

'****************************************************************


@ DEVICE INTRC_OSC, WDT_Off, PWRT_Off, MCLR_OFF,PROTECT_OFF
eeprom 1,[31,28,31,30,31,30,31,31,30,31,30,31]  ' Lets burn the eeprom for something useful. A kind of lookup table.

DEFINE LCD_EREG PORTA   ' Set LCD Enable port
DEFINE LCD_EBIT 7       ' Set LCD Enable bit

trisa = 0
trisb = 0 
input   portb.4
input   portb.5
'--------------------------------------------

clk     var portb.1 ' DS1302 Clock Pin
dq      var portb.2 ' DS1302 Data Pin
rst     var portb.3 ' DS1302 Reset Pin

CHNG    VAR portb.4 ' Increment button
SETIN   VAR portb.5 ' Set button
LGHT    var portb.7 ' Led lights up while user is in setup menu and goes off at exit.
'bcklght var porta.6 ' Back light on/off for LCD.

'-----------decimal value variables for menu display---------------

second  var byte
minute  var byte
hour    var byte
day     var byte
month   var byte
year    var byte
monthx  var byte 

'--------hex value variables for actual time display--------------

minutex  var byte
hourx    var byte
dayx     var byte
monthxx  var byte
yearx    var byte
RTC      var byte

'----------------------- Write Commands For DS1302 --------------------------

writectrl       con $8E ' Control byte
writeram        con $80 ' Write to RAM
writeprotect    con $00 ' Write-protect DS1302
writesec        con $80 ' Write seconds
writemin        con $82 ' Write minutes
writehour       con $84 ' Write hour
writedate       con $86 ' Write date
writemonth      con $88 ' Write month
writeyear       con $8C ' Write year

'------------------------- Read Commands For DS1302 -------------------------

readsec         con $81 ' Read seconds from DS1302
readmin         con $83 ' Read minutes from DS1302
readhour        con $85 ' Read hours from DS1302
readdate        con $87 ' Read date from DS1302
readyear        con $8D ' Read year from DS1302
readmonth       con $89 ' Read month from DS1302

'------------------------------ Time Variables ------------------------------

mem         var byte ' Temporary data holder
outbyte     var byte ' Second byte to ds1302
reg_adr     var byte ' First byte to DS1302
decx        var byte ' A swap variable
'a           var byte ' A Loop variable

'------------------------ Initial Settings For Ports ------------------------

begin:       'We get back here after everything is set.

low rst ' Set reset pin low
low clk ' Set clock pin low


'----------------------- Set Initial Time Variables -------------------------

start:
        if CHNG = 0 then        'goto SETYEAR 'also works alone but adds couple of years at first entrance...
            pause 200
            year=year-2         ' just a modification.
            goto SETYEAR        ' Start setting clock values.
        endif

        call readclk            ' go read DS1302
        
        lcdout $fe,1,   HEX2 hourx,":",HEX2 minutex,":",HEX2 second
        lcdout $fe,$c0, hex2 dayx, "/",hex2 monthxx,"/",hex2 yearx
        pause 100

goto start

'----------------------- Time Commands Subroutines --------------------------
readclk:

        reg_adr = readsec   'Read seconds
        gosub w_in
        second = mem
        
        reg_adr = readmin   'Read minutes
        gosub w_in
        minutex = mem
        
        reg_adr = readhour  'Read Hours
        gosub w_in
        hourx = mem
        
        reg_adr = readyear  'Read Year
        gosub w_in
        yearx = mem
        
        reg_adr = readdate  'Read Date
        gosub w_in
        dayx = mem
        
        reg_adr = readmonth 'Read Month
        gosub w_in
        monthxx = mem

        return
   
setup:
        reg_adr = writectrl         ' Set to control byte 
        outbyte = writeprotect      ' Set turn off protection
        gosub w_out                 ' Send both bytes
        
        reg_adr = writesec          ' Set to write seconds register
        outbyte = $00               ' Set to write 00 to seconds register in HEX format
        gosub w_out
        
        reg_adr = writemin
        outbyte = minutex            'Our minutex and others with x (except for monthx) are now in HEX format.
        gosub w_out
        
        reg_adr = writehour
        outbyte = hourx
        gosub w_out
        
        reg_adr = writedate
        outbyte = dayx
        gosub w_out
        
        reg_adr = writemonth
        outbyte = monthxx
        gosub w_out
        
        reg_adr = writeyear
        outbyte = yearx
        gosub w_out
        
        reg_adr = writectrl
        outbyte = writeprotect
        gosub w_out
        
        goto begin

w_in:
        mem = reg_adr               ' Set mem variable to reg_adr contents
        high rst                    ' Activate the DS1302
        shiftout dq,clk,0, [mem]    ' Send control byte
        shiftin dq,clk,1, [mem]     ' Retrieve data in from the DS1302
        low rst                     ' Deactivate DS1302
        return 

w_out:
        mem = reg_adr               ' Set mem variable to reg_adr contents
        high rst                    ' Activate the DS1302
        shiftout dq,clk,0, [mem]    ' Send control byte
        mem = outbyte               ' Set mem variable to outbyte contents
        shiftout dq,clk,0, [mem]    ' Send data stored in mem variable to DS1302
        low rst                     ' Deactivate DS1302
        return


chk:  'This check module can be modified as desired. It can be alot more detailed, or more logical, or much shorter with a for loop. But it works as is.

    if year=0 or year=4 or year=8 or year=12 or year=16	or year=20 or year=24 or year=28 or year=32 or year=36 or year=40 or year=44 or year=48 then 
        write 2,29              'Years with Feb=29 days (for 50 yrs.)
        pause 20
    else
        write 2, 28   
        pause 20
    endif
  
    return

'------------------------------------------------------------------------
    
set:                           
        
        if CHNG = 0 then        'change the minute on display
            pause 100
            minute=minute+1
            if minute>59 then minute=0
            call send
        endif
        
        IF setin = 0 then   
            decx=minute
            gosub convertion    'Go get the hex value tiger!
            minutex=rtc            
            low lght
            goto setup       
        endif    
        
        goto set


sethour:
        
        if CHNG = 0 then        'change the hour on display
            pause 100
            hour=hour+1
            if hour>23 then hour=0
            call send
        endif
        
        IF setin = 0 then       'Press to set the hour and goto next
            pause 100
            decx=hour
            gosub convertion
            hourx=rtc
            goto set
        endif    
        
        goto sethour

setday:
        
        if CHNG = 0 then        'change the day on display
            pause 100
            day=day+1
            if day>monthx then day=1
            call send
        endif
        
        IF setin = 0 then       'Press to set the day and goto next
            pause 100
            decx=day
            gosub convertion
            dayx=rtc            
            goto sethour
        endif   
        
        goto setday

setmonth:
        
        if CHNG = 0 then        'change the month on display
            pause 100
            month=month+1
            if month>12 then month=1
            call send
        endif
        
        IF setin = 0 then       'Press to set the month and goto next
            pause 100
            read month, monthx
            decx=month
            gosub convertion
            monthxx=rtc
            goto setday
        endif  
        
        goto setmonth
                        
setyear:
        
        high lght 
        
        if CHNG = 0 then        'change the year on display
            pause 100
            year=year+1
            if year>50 then year=0
            call send
        endif
        
        IF setin = 0 then      'Press to set the year and goto next
            pause 150
            call chk
            pause 20
            decx=year          
            gosub convertion    'Go get the hex value tiger!
            yearx=rtc         ' Now keep it and wait for setup.
            goto setmonth      
        endif  
     
        goto setyear

send:       'We see the values in decimal format; but after conversion, DS1302 is set with HEX format.

    LCDOut $FE,1,   dec2 Hour,":",dec2 Minute
    lcdout $fe,$c0, dec2 day,"/", dec2 month,"/",dec2 year
    return

convertion:

	RTC=decx DIG 1
	RTC=RTC<<4
	RTC=RTC+decx DIG 0
    return

[TabIndex]
Value=0
User avatar
ShOkRe
Pravo uznapredovao :)
Pravo uznapredovao :)
Posts: 468
Joined: 10-01-2007, 20:55
Location: Pula

Post by ShOkRe »

istina moj je ful jednostavniji, nije mi trebala nikakva perv, samo da kroz prolaz ocita koliko je sati i ako je dovoljno da starta.... i stimanje sata i bla bla, malo me zajebavalo kako da se namjesti dan u tjednu, ako znas datum i godinu (ima neka luda formula).... mislim da ima u mell.-inom primjeru...
User avatar
ShOkRe
Pravo uznapredovao :)
Pravo uznapredovao :)
Posts: 468
Joined: 10-01-2007, 20:55
Location: Pula

Post by ShOkRe »

eh da, skoro zaboravih, proleti malo po ovoj stranici, to je po meni najelegantnije/modernije rjesenje u pbp-u interupt driven....
Post Reply