timer/tijdklok
-
- Berichten: 121
- Lid geworden op: 30 september 2008, 00:00
timer/tijdklok
even stukje uitleg
ik wil een tijdklok maken die het volgende moet doen:
4 timers die op tijdbasis werken
2 uitgangen die temperatuur gestuurd zijn
als tijdbasis heb ik een ds1307 met een ds32kHz en temperatuur een ds1621
microprocessor is een 16f877
display 4 x 20
en paar drukknoppen voor instellingen
nu mijn probleem ;
Ik krijg wel apart de temp op het display
maar niet samen met de tijd
weet iemand hiervoor een oplossing
ook om de timers in te stellen?
-
- Berichten: 121
- Lid geworden op: 30 september 2008, 00:00
[code]
DEVICE 16F877A ;We gebruiken een 16F877A type
CONFIG WDT_OFF, PWRTE_ON, LVP_OFF, HS_OSC
ALL_DIGITAL TRUE ;Alle ingangen digitaal
XTAL 20
DECLARE LCD_TYPE ALPHA
DECLARE LCD_INTERFACE 4
DECLARE LCD_ENPIN PORTd.2
DECLARE LCD_RSPIN PORTd.3
DECLARE LCD_DTPIN PORTd.4
DECLARE LCD_LINES 4
ALL_DIGITAL TRUE ;Alle ingangen digitaal
; I2C pinnen
DECLARE SCL_PIN = PORTC.3 ;geef aan welke pin SCL is
DECLARE SDA_PIN = PORTC.4 ;geef aan welke pin SDA is
DECLARE SLOW_BUS ON
dim bcd_to_bin_byte 'variable used in BCD and Binary conversion routines
dim low_bits 'low byte for Binary to BCD routine
dim high_bits 'high byte for Binary to BCD routine
dim ds1307_address 'DS1307 internal registers address(0=seconds,1=minutes etc.)
dim second 'seconds variable
dim minute 'minutes variable
dim hour 'hours variable
dim day 'days variable
dim date 'dates variable
dim month 'months variable
dim year 'years variable
dim buttonvalue 'variable used for "up" and "down" buttons values
dim maxvalue 'variable holding highest allowable current value in button routine
dim minvalue 'variable holding lowest current allowable value in button routine
dim leap 'variable used to set "maxvalue" in leapyear
dim monthlength 'variable used to set maximum number of days in the selected month
dim index 'variable used to select display options in "setup"
dim character 'variable holding data selected from eeprom or ldata tables to be printed on display
dim printposition 'start position on the LCD of first "character" in print routine
dim printend 'last "character" position to be printed on LCD
dim printaddress 'start position of data in eeprom or ldata table
dim row 'the line number you wish to display the info on the LCD
dim read_data 'tells the print routine,"print_eeprom" to use eeprom(0) or ldata(1) for its data reading
symbol Op=porta.2 'use porta.2 for the "up" button
symbol Neer=porta.1 'use porta.1 for the "down" button
symbol OK=porta.3 'use porta.3 for the "setup" buton
symbol clockout=%11010000 'set the 1307 to receive data
symbol clockin=%11010001 'set the 1307 to transmit data
trisa=%00110 'set porta to use 2 bits as input
trisa.3=1 'set portb to input for "setup" button
year=0
month=1
date=1
day=0
hour=0
minute=0
start:
cls
if OK=0 then goto start 'debounce setup button on return from setting time
display_time:
gosub get_time 'read time from DS1307
gosub print_time 'print the clock display
if OK=0 then goto time_set 'if the setup button is pressed then goto the time setting routine
goto display_time
start_clock:
busout clockout,0,[0]
return
set_time:
bcd_to_bin_byte=minute:gosub convert_to_bcd:minute=bcd_to_bin_byte
bcd_to_bin_byte=hour:gosub convert_to_bcd:hour=bcd_to_bin_byte
bcd_to_bin_byte=Day:gosub convert_to_bcd:Day=bcd_to_bin_byte
bcd_to_bin_byte=Date:gosub convert_to_bcd:Date=bcd_to_bin_byte
bcd_to_bin_byte=Month:gosub convert_to_bcd:Month=bcd_to_bin_byte
bcd_to_bin_byte=Year:gosub convert_to_bcd:Year=bcd_to_bin_byte
busout clockout,1,[minute,hour,day,date,month,year]
return
get_time:
ds1307_address=0:gosub read_time 'read seconds data from 1307
second=bcd_to_bin_byte 'convert to binary
ds1307_address=1:gosub read_time 'read minute data from 1307
minute=bcd_to_bin_byte 'convert to binary
ds1307_address=2:gosub read_time 'read hour data from 1307
hour=bcd_to_bin_byte 'convert to binary
ds1307_address=3:gosub read_time 'read day data from 1307
day=bcd_to_bin_byte 'convert to binary
ds1307_address=4:gosub read_time 'read date data from 1307
date=bcd_to_bin_byte 'convert to binary
ds1307_address=5:gosub read_time 'read month data from 1307
month=bcd_to_bin_byte 'convert to binary
ds1307_address=6:gosub read_time 'read year data from 1307
year=bcd_to_bin_byte 'convert to binary
return
read_time:
busin clockin,ds1307_address,[bcd_to_bin_byte]
gosub convert_to_bin
return
convert_to_bin:
select bcd_to_bin_byte
case 16 to 25
bcd_to_bin_byte=bcd_to_bin_byte-6
case 32 to 41
bcd_to_bin_byte=bcd_to_bin_byte-12
case 48 to 57
bcd_to_bin_byte=bcd_to_bin_byte-18
case 64 to 73
bcd_to_bin_byte=bcd_to_bin_byte-24
case 80 to 89
bcd_to_bin_byte=bcd_to_bin_byte-30
case 96 to 105
bcd_to_bin_byte=bcd_to_bin_byte-36
case 112 to 121
bcd_to_bin_byte=bcd_to_bin_byte-42
case 128 to 137
bcd_to_bin_byte=bcd_to_bin_byte-48
case 144 to 153
bcd_to_bin_byte=bcd_to_bin_byte-54
endselect
return
convert_to_bcd:
low_bits=bcd_to_bin_byte//10 'get lsb,same for Bin and BCD
high_bits=bcd_to_bin_byte/10 'get msb
bcd_to_bin_byte=high_bits*16 'covert msb to BCD
bcd_to_bin_byte=bcd_to_bin_byte+low_bits 'add BCD msb and lsb together
return
print_eeprom:
repeat
if read_data=0 then character=eread printaddress 'use eeprom for data retrieval
if read_data=1 then character=lread menu_list+printaddress 'use ldata for data retrieval
print at row,printposition,character 'print to the LCD
inc printposition 'move print position one to the right
inc printaddress 'point to the next position in the table
until printposition=printend+1 'end loop when all characters have been printed
return
print_time:
print at 1,11,dec2 second
print_min:
print at 1,8,dec2 minute,":"
print_hour:
print at 1,5,dec2 hour,":"
print_day:
read_data=0 'use eeprom for data reading
row=2 'print on line 2 of LCD
printposition=1 'initial position of "day" on LCD
printend=3 'position of last letter to be printed
printaddress=day*3 'work out address of data in the eeprom(minimum is 3)
gosub print_eeprom 'goto LCD print routine
print_date:
print at 2,5,dec2 date
print_month:
read_data=0 'use eeprom for data reading
row=2 'print on line 2 of LCD
printposition=8 'initial position of "month" on LCD
printend=10 'position of last letter to be printed
printaddress=month*3 'work out address of data in the eeprom(minimum is 3)
printaddress=printaddress+21 'first month data is at address 24 (21+3)in eeprom
gosub print_eeprom 'goto LCD print routine
print_year:
print at 2,12,"20",dec2 Year
return
time_set:
if OK=0 then goto time_set 'debounce button if still pressed from previous routine
cls
second=0 'clears second display at end of setup
print at 1,1,"Set"
buttonvalue=year:index=0:maxvalue=99:minvalue=0:gosub buttons:year=buttonvalue 'work on the YEAR first
buttonvalue=month:index=1:maxvalue=12:minvalue=1:gosub buttons:month=buttonvalue 'next to the MONTH
monthlength=31 'if the months are JAN,MAR,MAY,JUL,AUG,OCT and DEC then "monthlength" will be 31
select month
case 4,6,9,11 'if the months are APR,JUN,SEP or NOV then "monthlength" will be 30
monthlength=30
case 2 'if it is FEB then set "monthlength" to 28
monthlength=28
leap=year//4 'tests for a leap year
if leap=0 then monthlength=29 'if it is a leap year then set FEB "monthlength" to 29
endselect
buttonvalue=date:index=2:maxvalue=monthlength:minvalue=1:gosub buttons:date=buttonvalue 'change the DATE
buttonvalue=day:index=3:maxvalue=7:minvalue=1:gosub buttons:day=buttonvalue 'change the DAY
buttonvalue=hour:index=4:maxvalue=23:minvalue=0:gosub buttons:hour=buttonvalue 'change the HOUR
buttonvalue=minute:index=5:maxvalue=59:minvalue=0:gosub buttons:minute=buttonvalue 'change the MINUTE
print at 1,11,"00 " ,at 2,1,"'Setup' to Start"
restart:
if OK=0 then cls:gosub set_time:gosub start_clock:goto start
goto restart
buttons:
if Op=0 then inc buttonvalue:delayms 200 'if up button is pressed then increase "buttonvalue" by one
if buttonvalue>maxvalue then buttonvalue=minvalue 'if "buttonvalue" goes above its maximum then make it equal to "minvalue"
if Neer=0 then dec buttonvalue:delayms 200 'if down button is pressed then decrease "buttonvalue" by one
if buttonvalue<minvalue or buttonvalue=255 then buttonvalue=maxvalue 'if "buttonvalue" goes below its minimum, or 255 if the minimum is 0, then make it equal to "maxvalue"
'print the time setting menu
read_data=1 'use ldata table for print routine
row=1 'print on line 1 of LCD
printposition=13 'start position on LCD for the menu
printend=16 'last character position on LCD for the menu
printaddress=index*4 'work out start position of data in ldata table
gosub print_eeprom 'jump to LCD print routine
'choose what is to be displayed on the LCD and which variable is to be changed by this "buttons" routine.
'using if-then rather than case as it uses less memory in this routine
if index=0 then year=buttonvalue:gosub print_year
if index=1 then month=buttonvalue:gosub print_month
if index=2 then date=buttonvalue:gosub print_date
if index=3 then day=buttonvalue:gosub print_day
if index=4 then hour=buttonvalue:gosub print_hour
if index=5 then minute=buttonvalue:gosub print_min
if OK=0 then delayms 200:return
goto buttons
edata 0,0,0,"MonTueWedThuFriSatSunJanFebMarAprMayJunJulAugSepOctNovDec"
menu_list:ldata "YearMnthDateDay HourMin "
end
[/code]
en deze
[code]
'****************************************************************
'* Name : UNTITLED.BAS *
'* Author : R Hendriks *
'* Notice : Copyright (c) 2008 www.rhendriks.nl *
'* : All Rights Reserved *
'* Date : 16-7-2008 *
'* Version : 1.0 *
'* Notes : *
'* : *
'****************************************************************
DEVICE 16F877A ;We gebruiken een 16F877A type
CONFIG WDT_OFF, PWRTE_ON, LVP_OFF, HS_OSC
ALL_DIGITAL TRUE ;Alle ingangen digitaal
XTAL 20
DECLARE LCD_TYPE ALPHA
DECLARE LCD_INTERFACE 4
DECLARE LCD_ENPIN PORTd.2
DECLARE LCD_RSPIN PORTd.3
DECLARE LCD_DTPIN PORTd.4
DECLARE LCD_LINES 4
ALL_DIGITAL TRUE ;Alle ingangen digitaal
; I2C pinnen
DECLARE SCL_PIN = PORTC.3 ;geef aan welke pin SCL is
DECLARE SDA_PIN = PORTC.4 ;geef aan welke pin SDA is
DECLARE SLOW_BUS ON
Dim T As Word
Dim dummy As Byte
BusOut %10011110,172,[1] ;oneshot mode in t-sensor.
BusOut %10011110,[238] ;start conversie
;-----------------------------
main:
BusIn %10010001,172,[dummy] ;lees config register van T-sensor
If dummy.7=1 Then ;1=conversion ready, 0=busy
BusIn %10010001,170,[T] ;lees T-sensor
BusOut %10010000,[238] ;start nieuwe temperatuur conversie
End If
DelayMS 100
If T.HighByte<128 Then ;positieve temperatuur
Print At 1,1," "
Print DEC2 T.HighByte
If T.LowByte=0 Then
Print ".0"
Else
Print ".5"
End If
Else ;negatieve T
Print At 1,1,"-"
If T.LowByte=0 Then
Print DEC2 256-T.HighByte
Print ".0"
Else
Print DEC2 255-T.HighByte
Print ".5"
End If
End If
Print 223,"C"
GoTo main
[/code]
deze wilde ik samen voegen
-
- Berichten: 121
- Lid geworden op: 30 september 2008, 00:00
- Stynus
- Site Admin
- Berichten: 2878
- Lid geworden op: 12 september 2008, 13:54
- Locatie: Overpelt (België)
- Contacteer:
Ok dus het samen voegen van de code's was het probleem?
Ik denk dat het zo wel moet werken:
[code]
Device 16F877A ;We gebruiken een 16F877A type
Config WDT_OFF, PWRTE_ON, LVP_OFF, HS_OSC
ALL_DIGITAL TRUE ;Alle ingangen digitaal
XTAL 20
Declare LCD_TYPE ALPHA
Declare LCD_INTERFACE 4
Declare LCD_ENPIN PORTD.2
Declare LCD_RSPIN PORTD.3
Declare LCD_DTPIN PORTD.4
Declare LCD_LINES 4
ALL_DIGITAL TRUE ;Alle ingangen digitaal
; I2C pinnen
Declare SCL_PIN = PORTC.3 ;geef aan welke pin SCL is
Declare SDA_PIN = PORTC.4 ;geef aan welke pin SDA is
Declare SLOW_BUS On
Dim bcd_to_bin_byte 'variable used in BCD and Binary conversion routines
Dim low_bits 'low byte for Binary to BCD routine
Dim high_bits 'high byte for Binary to BCD routine
Dim ds1307_address 'DS1307 internal registers address(0=seconds,1=minutes etc.)
Dim second 'seconds variable
Dim minute 'minutes variable
Dim hour 'hours variable
Dim day 'days variable
Dim date 'dates variable
Dim month 'months variable
Dim year 'years variable
Dim buttonvalue 'variable used for "up" and "down" buttons values
Dim maxvalue 'variable holding highest allowable current value in button routine
Dim minvalue 'variable holding lowest current allowable value in button routine
Dim leap 'variable used to set "maxvalue" in leapyear
Dim monthlength 'variable used to set maximum number of days in the selected month
Dim index 'variable used to select display options in "setup"
Dim character 'variable holding data selected from eeprom or ldata tables to be printed on display
Dim printposition 'start position on the LCD of first "character" in print routine
Dim printend 'last "character" position to be printed on LCD
Dim printaddress 'start position of data in eeprom or ldata table
Dim row 'the line number you wish to display the info on the LCD
Dim read_data 'tells the print routine,"print_eeprom" to use eeprom(0) or ldata(1) for its data reading
Dim T As Word
Dim dummy As Byte
Symbol Op=PORTA.2 'use porta.2 for the "up" button
Symbol Neer=PORTA.1 'use porta.1 for the "down" button
Symbol OK=PORTA.3 'use porta.3 for the "setup" buton
Symbol clockout=%11010000 'set the 1307 to receive data
Symbol clockin=%11010001 'set the 1307 to transmit data
TRISA=%00110 'set porta to use 2 bits as input
TRISA.3=1 'set portb to input for "setup" button
year=0
month=1
date=1
day=0
hour=0
minute=0
start:
Cls
If OK=0 Then GoTo start 'debounce setup button on return from setting time
display_time:
GoSub get_time 'read time from DS1307
GoSub print_time 'print the clock display
GoSub print_temp
If OK=0 Then GoTo time_set 'if the setup button is pressed then goto the time setting routine
GoTo display_time
start_clock:
BusOut clockout,0,[0]
Return
set_time:
bcd_to_bin_byte=minute:GoSub convert_to_bcd:minute=bcd_to_bin_byte
bcd_to_bin_byte=hour:GoSub convert_to_bcd:hour=bcd_to_bin_byte
bcd_to_bin_byte=day:GoSub convert_to_bcd:day=bcd_to_bin_byte
bcd_to_bin_byte=date:GoSub convert_to_bcd:date=bcd_to_bin_byte
bcd_to_bin_byte=month:GoSub convert_to_bcd:month=bcd_to_bin_byte
bcd_to_bin_byte=year:GoSub convert_to_bcd:year=bcd_to_bin_byte
BusOut clockout,1,[minute,hour,day,date,month,year]
Return
get_time:
ds1307_address=0:GoSub read_time 'read seconds data from 1307
second=bcd_to_bin_byte 'convert to binary
ds1307_address=1:GoSub read_time 'read minute data from 1307
minute=bcd_to_bin_byte 'convert to binary
ds1307_address=2:GoSub read_time 'read hour data from 1307
hour=bcd_to_bin_byte 'convert to binary
ds1307_address=3:GoSub read_time 'read day data from 1307
day=bcd_to_bin_byte 'convert to binary
ds1307_address=4:GoSub read_time 'read date data from 1307
date=bcd_to_bin_byte 'convert to binary
ds1307_address=5:GoSub read_time 'read month data from 1307
month=bcd_to_bin_byte 'convert to binary
ds1307_address=6:GoSub read_time 'read year data from 1307
year=bcd_to_bin_byte 'convert to binary
Return
read_time:
BusIn clockin,ds1307_address,[bcd_to_bin_byte]
GoSub convert_to_bin
Return
convert_to_bin:
Select bcd_to_bin_byte
Case 16 To 25
bcd_to_bin_byte=bcd_to_bin_byte-6
Case 32 To 41
bcd_to_bin_byte=bcd_to_bin_byte-12
Case 48 To 57
bcd_to_bin_byte=bcd_to_bin_byte-18
Case 64 To 73
bcd_to_bin_byte=bcd_to_bin_byte-24
Case 80 To 89
bcd_to_bin_byte=bcd_to_bin_byte-30
Case 96 To 105
bcd_to_bin_byte=bcd_to_bin_byte-36
Case 112 To 121
bcd_to_bin_byte=bcd_to_bin_byte-42
Case 128 To 137
bcd_to_bin_byte=bcd_to_bin_byte-48
Case 144 To 153
bcd_to_bin_byte=bcd_to_bin_byte-54
EndSelect
Return
convert_to_bcd:
low_bits=bcd_to_bin_byte//10 'get lsb,same for Bin and BCD
high_bits=bcd_to_bin_byte/10 'get msb
bcd_to_bin_byte=high_bits*16 'covert msb to BCD
bcd_to_bin_byte=bcd_to_bin_byte+low_bits 'add BCD msb and lsb together
Return
print_eeprom:
Repeat
If read_data=0 Then character=ERead printaddress 'use eeprom for data retrieval
If read_data=1 Then character=LRead menu_list+printaddress 'use ldata for data retrieval
Print At row,printposition,character 'print to the LCD
Inc printposition 'move print position one to the right
Inc printaddress 'point to the next position in the table
Until printposition=printend+1 'end loop when all characters have been printed
Return
print_time:
Print At 1,11,DEC2 second
print_min:
Print At 1,8,DEC2 minute,":"
print_hour:
Print At 1,5,DEC2 hour,":"
print_day:
read_data=0 'use eeprom for data reading
row=2 'print on line 2 of LCD
printposition=1 'initial position of "day" on LCD
printend=3 'position of last letter to be printed
printaddress=day*3 'work out address of data in the eeprom(minimum is 3)
GoSub print_eeprom 'goto LCD print routine
print_date:
Print At 2,5,DEC2 date
print_month:
read_data=0 'use eeprom for data reading
row=2 'print on line 2 of LCD
printposition=8 'initial position of "month" on LCD
printend=10 'position of last letter to be printed
printaddress=month*3 'work out address of data in the eeprom(minimum is 3)
printaddress=printaddress+21 'first month data is at address 24 (21+3)in eeprom
GoSub print_eeprom 'goto LCD print routine
print_year:
Print At 2,12,"20",DEC2 year
Return
time_set:
If OK=0 Then GoTo time_set 'debounce button if still pressed from previous routine
Cls
second=0 'clears second display at end of setup
Print At 1,1,"Set"
buttonvalue=year:index=0:maxvalue=99:minvalue=0:GoSub buttons:year=buttonvalue 'work on the YEAR first
buttonvalue=month:index=1:maxvalue=12:minvalue=1:GoSub buttons:month=buttonvalue 'next to the MONTH
monthlength=31 'if the months are JAN,MAR,MAY,JUL,AUG,OCT and DEC then "monthlength" will be 31
Select month
Case 4,6,9,11 'if the months are APR,JUN,SEP or NOV then "monthlength" will be 30
monthlength=30
Case 2 'if it is FEB then set "monthlength" to 28
monthlength=28
leap=year//4 'tests for a leap year
If leap=0 Then monthlength=29 'if it is a leap year then set FEB "monthlength" to 29
EndSelect
buttonvalue=date:index=2:maxvalue=monthlength:minvalue=1:GoSub buttons:date=buttonvalue 'change the DATE
buttonvalue=day:index=3:maxvalue=7:minvalue=1:GoSub buttons:day=buttonvalue 'change the DAY
buttonvalue=hour:index=4:maxvalue=23:minvalue=0:GoSub buttons:hour=buttonvalue 'change the HOUR
buttonvalue=minute:index=5:maxvalue=59:minvalue=0:GoSub buttons:minute=buttonvalue 'change the MINUTE
Print At 1,11,"00 " ,At 2,1,"'Setup' to Start"
restart:
If OK=0 Then Cls:GoSub set_time:GoSub start_clock:GoTo start
GoTo restart
buttons:
If Op=0 Then Inc buttonvalue:DelayMS 200 'if up button is pressed then increase "buttonvalue" by one
If buttonvalue>maxvalue Then buttonvalue=minvalue 'if "buttonvalue" goes above its maximum then make it equal to "minvalue"
If Neer=0 Then Dec buttonvalue:DelayMS 200 'if down button is pressed then decrease "buttonvalue" by one
If buttonvalue<minvalue Or buttonvalue=255 Then buttonvalue=maxvalue 'if "buttonvalue" goes below its minimum, or 255 if the minimum is 0, then make it equal to "maxvalue"
'print the time setting menu
read_data=1 'use ldata table for print routine
row=1 'print on line 1 of LCD
printposition=13 'start position on LCD for the menu
printend=16 'last character position on LCD for the menu
printaddress=index*4 'work out start position of data in ldata table
GoSub print_eeprom 'jump to LCD print routine
'choose what is to be displayed on the LCD and which variable is to be changed by this "buttons" routine.
'using if-then rather than case as it uses less memory in this routine
If index=0 Then year=buttonvalue:GoSub print_year
If index=1 Then month=buttonvalue:GoSub print_month
If index=2 Then date=buttonvalue:GoSub print_date
If index=3 Then day=buttonvalue:GoSub print_day
If index=4 Then hour=buttonvalue:GoSub print_hour
If index=5 Then minute=buttonvalue:GoSub print_min
If OK=0 Then DelayMS 200:Return
GoTo buttons
EData 0,0,0,"MonTueWedThuFriSatSunJanFebMarAprMayJunJulAugSepOctNovDec"
menu_list:LData "YearMnthDateDay HourMin "
print_temp:
BusIn %10010001,172,[dummy] ;lees config register van T-sensor
If dummy.7=1 Then ;1=conversion ready, 0=busy
BusIn %10010001,170,[T] ;lees T-sensor
BusOut %10010000,[238] ;start nieuwe temperatuur conversie
End If
DelayMS 100
If T.HighByte<128 Then ;positieve temperatuur
Print At 3,1," "
Print DEC2 T.HighByte
If T.LowByte=0 Then
Print ".0"
Else
Print ".5"
End If
Else ;negatieve T
Print At 3,1,"-"
If T.LowByte=0 Then
Print DEC2 256-T.HighByte
Print ".0"
Else
Print DEC2 255-T.HighByte
Print ".5"
End If
End If
Print 223,"C"
Return
End[/code]
-
- Berichten: 121
- Lid geworden op: 30 september 2008, 00:00