Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

digital clock with alarm (12hours)

Status
Not open for further replies.
It's got to be a homework assignment.

Anyway how much of it have you designed so far?

already designed sec,min,hours..

however,wen im cascading min and hours,it is not functioning well..

for instance,when the time is 4:59......it will become 5:40 or 4:40..:(

checked all the wirings and schematics,all are correct..

in simulation,our schem is working..
 
This project using LCD but if you want to seven segment display you can check at
View attachment 62092


$regfile = "m8535.dat"
$crystal = 12000000

'------------------------ lcd -------------------------------------------------
Config Lcd = 16 * 2
Config Lcdpin = Pin , Rs = Pina.0 , E = Pina.1 , Db4 = Pina.4 , Db5 = Pina.5 , Db6 = Pina.6 , Db7 = Pina.7

'--------------------------- ds 1307 -----------------------------------------
$lib "ds1307clock.lib"
'configure the scl and sda pins
Config Sda = Portc.7
Config Scl = Portc.6
'address of ds1307
Const Ds1307w = &HD0 ' Addresses of Ds1307 clock
Const Ds1307r = &HD1

'------------------------------ key --------------------------------------------
Config Pinc.1 = Input
Config Pinc.2 = Input
Config Pinc.3 = Input
Config Pinc.4 = Input
Config Portd.2 = Output

$eeprom

'-------------------------------------------------------------------------------
Dim Seco As Integer , Mine As Integer , Hour As Integer , , Al1 As Integer , Al2 As Integer

'-------------------------------------------------------------------------------
Cursor Off
'------------------------------------------------------------------------------
Cls
Locate 1 , 1
Lcd " Digital Clock "
Waitms 100
Cls

'-------------------------------------------------------------------------------
Main:
Do
Gosub Ds1307
Gosub 24_12
Gosub Chekkey
Gosub Alarm
Loop

'-------------------------------------------------------------------------------
Ds1307:
I2cstart ' Generate start code
I2cwbyte Ds1307w ' send address
I2cwbyte 0 ' start address in 1307
I2cstart ' Generate start code
I2cwbyte Ds1307r ' send address
I2crbyte Seco , Ack 'sec
I2crbyte Mine , Ack ' MINUTES
I2crbyte Hour , Nack ' Hours
I2cstop

Seco = Makedec(seco) : Mine = Makedec(mine) : Hour = Makedec(hour)

Return

'------------------------------------------------------------------------------
24_12:
If Pinc.4 = 1 Then Gosub Disply_24
If Pinc.4 = 0 Then Gosub Disply_12
Return

'------------------------------------------------------------------------------
Disply_24:

Locate 1 , 1
Lcd "Clock = " ; Hour ; ":" ; Mine ; ":" ; Seco ; " "
Locate 2 , 6
Lcd "(24 Hour)"
Wait 1

Return

'------------------------------------------------------------------------------
Disply_12:


If Hour = 0 Then Hour = 12
If Hour > 12 Then Hour = Hour - 12

Locate 1 , 1
Lcd "Clock = " ; Hour ; ":" ; Mine ; ":" ; Seco ; " "
Locate 2 , 6
Lcd "(12 Hour)"
Return

'-------------------------------------------------------------------------------
' Select the settings
'-------------------------------------------------------------------------------
Chekkey:

If Pinc.1 = 0 Then
Cls
Bitwait Pinc.1 , Set
Locate 1 , 1
Lcd "****Setting****"
Locate 2 , 1
Lcd "Time"
Locate 2 , 12
Lcd "alarm"
Do
Loop Until Pinc.2 = 0 Or Pinc.3 = 0

'-------------------------------------------------------------------------------
' Set clock
'-------------------------------------------------------------------------------
If Pinc.2 = 0 Then
Cls
Bitwait Pinc.2 , Set
Locate 1 , 1
Lcd "**Setting Time**"
Locate 2 , 1
Lcd "Time = " ; Hour ; ":" ; Mine ; " "

Do
If Pinc.2 = 0 Then
Incr Hour
Waitms 10
If Hour > 24 And Pinc.4 = 1 Then Hour = 0
If Hour > 12 And Pinc.4 = 0 Then Hour = 0
End If
If Pinc.3 = 0 Then
Decr Hour
Waitms 10
If Hour < 0 And Pinc.4 = 1 Then Hour = 24
If Hour < 0 And Pinc.4 = 0 Then Hour = 12
End If

Locate 2 , 1
Lcd " " ; Hour ; ":" ; Mine ; " "
Loop Until Pinc.1 = 0

Bitwait Pinc.1 , Set
Gosub Hour
Waitms 10

Do
If Pinc.2 = 0 Then
Incr Mine
Waitms 10
If Mine > 60 Then Mine = 0
End If
If Pinc.3 = 0 Then
Decr Mine
Waitms 10
If Mine < 0 Then Mine = 60
End If

Locate 2 , 1
Lcd " " ; Hour ; ":" ; Mine ; " "
Loop Until Pinc.1 = 0

Bitwait Pinc.1 , Set
Gosub Mine
Waitms 10
Cls
End If

'-------------------------------------------------------------------------------
' set alarm
'-------------------------------------------------------------------------------
If Pinc.3 = 0 Then
Cls
Bitwait Pinc.3 , Set
Waitms 10
Readeeprom Al1 , 2
Waitms 10
Readeeprom Al2 , 4
Waitms 10
Locate 1 , 1
Lcd "*Set alarm*"

Do
If Pinc.2 = 0 Then
Incr Al1
Waitms 10
If Al1 > 24 And Pinc.4 = 1 Then Al1 = 0
If Al1 > 12 And Pinc.4 = 0 Then Al1 = 0
End If
If Pinc.3 = 0 Then
Decr Al1
Waitms 10
If Al1 < 0 Then Al1 = 24
End If

Locate 2 , 1
Lcd " " ; Al1 ; ":" ; Al2 ; " "
Loop Until Pinc.1 = 0

Writeeeprom Al1 , 2
Waitms 10
Bitwait Pinc.1 , Set

Do
If Pinc.2 = 0 Then
Incr Al2
Waitms 10
If Al2 > 60 Then Al2 = 0
End If
If Pinc.3 = 0 Then
Decr Al2
Waitms 10
If Al2 < 0 Then Al2 = 60
End If

Locate 2 , 1
Lcd " " ; Al1 ; ":" ; Al2 ; " "
Loop Until Pinc.1 = 0

Writeeeprom Al2 , 4
Waitms 10
Bitwait Pinc.1 , Set
Cls
End If
End If

'-------------------------------------------------------------------------------
Mine:
Mine = Makebcd(mine)
I2cstart ' Generate start code
I2cwbyte Ds1307w ' send address
I2cwbyte 1 ' starting address in 1307
I2cwbyte Mine
I2cstop
Return
'-------------------------------------------------------------------------------
Hour:
Hour = Makebcd(hour)
I2cstart ' Generate start code
I2cwbyte Ds1307w ' send address
I2cwbyte 1 ' starting address in 1307
I2cwbyte Hour
I2cstop
Return

'-------------------------------------------------------------------------------
Alarm:
Readeeprom Al1 , 2
Waitms 10
Readeeprom Al2 , 4
Waitms 10
If Al1 = Hour And Al2 = Mine Then
Portd.2 = 1
Do
Loop Until Pinc.1 = 0
End If
Reset Portd.2
Return
 
Last edited by a moderator:
Status
Not open for further replies.

New Articles From Microcontroller Tips

Back
Top