PDA

View Full Version : Virtual LED Clock For Windows By Trent Jackson



T.Jackson
- 17th May 2007, 15:31
<table width="100%" border="0" Cellpadding="5">
<tr>
<td width="50px">
<img src="http://www.picbasic.co.uk/forum/attachment.php?attachmentid=1645&stc=1&d=1179405139">
</td>
<td>
Not your ordinary Windows-based desktop clock, this one's PIC-driven! Yes that's right, the Windows software is just a display, it's the PIC that actually keeps the time. The circuit connects to any spare serial port on your PC and feeds the Windows-based software with an accurate time string every second. Setting the time is snack, this is done directly via the Windows software. The time can be set or displayed in either 12 or 24 hour formats.

An interesting aspect of this project is that, in the Windows software, instead of using MSCOMM32.ocx, it uses a relatively much easier-to-use API based approach. First initial tests with this protocol have proven flawless. I haven't confirmed this yet but I think it might actually be much faster too. So for Windows developers out there using VB, I strongly suggest that you take a close look at the protocol in the download.

Anyhow, turning your attention back to the clock for a moment, if you take a look at the schematic below you'll see that the whole thing can be built for less than the cost of your lunch. The circuit relies on no dedicated RTC chip, but instead uses portions of Paul R. Borgmeier's EZCLOCK2 code which makes good use of TMR0 to enable for some relatively accurate time keeping. Arguably almost on par with the DS1302. Last but not least, some of you may be asking what's the buzzer and LED for? Well, as it stands with the software "as is", the buzzer briefly goes off every second making a sort of tick-tock sound.
</td>
</tr>
</table>
<table align="center" width="100px" border="2" >
<tr>
<td>
Schematic
<img src="http://www.picbasic.co.uk/forum/attachment.php?attachmentid=1644&stc=1&d=1179406688">
</td>
</tr>
</table>
<hr/>


'************************************************* *****************************
'* Name : VClock.BAS *
'* Author : Trent Jackson *
'* Notice : No Copyright *
'* : *
'* Date : 17/5/2007 *
'* Version : 1.0 *
'* *
'* Acknowledgements *
'* Portions of Program extracted from: EZCLOCK2 By Paul R. Borgmeier, PhD *
'* Crux analysis & design, LLC www.cruxanalysis.com *
'************************************************* *****************************

Include "modedefs.bas" ' Serial Protocol
Define OSC 4 ' 4MHz crystal used
'
'// Variables
Counter VAR Word ' Track TMRO
Hours VAR Byte ' Total hrs (0-23)
Minutes VAR ByTE ' Mins (0-59)
Seconds VAR Byte ' Secs (0-59)
RX_To_PC VAR PORTA.0 ' RX line
TX_To_PC VAR PORTA.1 ' TX ...
LED_Buzzer VAR PORTB.5 ' LED & Buzzer

'// Init
CMCON = 7 ' All digital
OPTION_REG = %10000111 ' TMRO prescale = 256
TRISA = %00000010 ' All outputs except RA1
TRISB = %00000000 ' All outputs ...
TMR0 = 0 ' Reset TMRO
INTCON.2 = 0 ' Clear TMRO overflow flag
Hours = 0 ' Null
Minutes = 0 ' ^
Seconds = 0 ' ^
Counter = $7A12 ' Load time counter

'=============
Wait_For_TMR0: ' Main loop
'=============
IF INTCON.2 = 0 THEN Wait_For_TMR0 ' Wait for TMRO overflow
INTCON.2 = 0 ' Clear TMRO overflow flag
Counter = Counter - $800 ' Dec counter until 1 sec has elapsed

'// Process hrs, mins secs accordingly ...
IF Counter < $800 THEN
Seconds = Seconds + 1
If Seconds = 60 Then
Seconds = 0
Minutes = Minutes + 1
If Minutes = 60 Then
Minutes = 0
Hours = Hours + 1
If Hours = 24 Then
Hours = 0
EndIf
EndIf
EndIf

'// Add leading zeros to all single digits & output str serially to PC
If Hours < 10 And Minutes > 9 And Seconds > 9 Then
SEROUT RX_To_PC, N2400, ["@","0",#Hours,":",#Minutes,":",#Seconds]
ELSE:IF Hours > 9 and Minutes < 10 and Seconds > 9 THEN
SEROUT RX_To_PC, N2400, ["@",#Hours,":0",#Minutes,":",#Seconds]
ELSE:IF Hours < 10 and Minutes < 10 and Seconds > 9 THEN
SEROUT RX_To_PC, N2400, ["@","0",#Hours,":0",#Minutes,":",#Seconds]
ELSE:IF Hours > 9 and Minutes > 9 and Seconds < 10 THEN
SEROUT RX_To_PC, N2400, ["@",#Hours,":",#Minutes,":0",#Seconds]
ELSE:IF Hours < 10 and Minutes > 9 and Seconds < 10 THEN
SEROUT RX_To_PC, N2400, ["@","0",#Hours,":",#Minutes,":0",#Seconds]
ELSE:IF Hours > 9 and Minutes < 10 and Seconds < 10 THEN
SEROUT RX_To_PC, N2400, ["@",#Hours,":0",#Minutes,":0",#Seconds]
ELSE:IF Hours < 10 and Minutes < 10 and Seconds < 10 THEN
SEROUT RX_To_PC, N2400, ["@","0",#Hours,":0",#Minutes,":0",#Seconds]
Else
SEROUT RX_To_PC, N2400, ["@",#Hours,":",#Minutes,":",#Seconds]
EndIf: EndIf: EndIf: EndIf: EndIf: EndIf: EndIf

Counter = Counter + $7A12 ' Reset counter
Toggle LED_Buzzer '
'//
Else 'Wait here for 50mS to see if the host(PC) is trying to set new time val
'//
SERIN TX_To_PC, N2400, 50, Wait_For_TMR0, ["@"], Hours, Minutes, Seconds
EndIf
GoTo Wait_For_TMR0 ' Loop back
end


If you plan on running the Windows executable you'll firstly need to obtain msvbvm50.dll <a href="http://www.dll-files.com/dllindex/dll-files.shtml?msvbvm50" target="_blank">Click here!</a>.
<br/>
Happy time keeping !
<hr/>
Trent Jackson
<br/>

T.Jackson
- 17th May 2007, 17:28
I think it's possible to squeeze a relatively good calendar into this too. I actually have an algorithm that can workout the dates in which the days in ea month fall on for any given year based on the total num of days since 1900. Anyone here coded a calendar for a PIC before?

Ioannis
- 18th May 2007, 08:06
Nice project Trent!

I would like to see the algo you have for the calendar, if I may.

Thanks, Ioannis

T.Jackson
- 18th May 2007, 08:34
I wrote this calendar in VB early this year ....

<img src="http://www.picbasic.co.uk/forum/attachment.php?attachmentid=1651&stc=1&d=1179469544">
<hr/>

Portion of code for generating the dates in ea month (2000 - 2100)


Public Sub Draw_Calendar()
'-----------------------------------------------------------------------------
'// Build / draw calendar onto field where user can actively select from it
'-----------------------------------------------------------------------------

Dim Print_Y As Integer 'Contains current X-axis for printing
Dim Print_X As Integer 'Y-axis
Dim Date_Num As String 'Holds current day for the selected month
Dim Cal_Col As Integer 'Column in (6x7) matrix
Dim Cal_Row As Integer 'Row
Dim Days_From_01 As Long 'Total days from the year 0001
Dim Start_Day As Long 'First day of the month
Dim i As Long 'General working var
ReDim Grid_Dat(6, 7) 'Grid contents (all the dates in the month)

'// From Jan 0001 to 2000 there's 730127 days (Crucial needed key to this algorithm)
Days_From_01 = 730127

'// Wipe area ready for new print / render
Calendar_Field.Cls

If Draw_3D_Cells.Checked Then
Blit_3D_Cells
End If

Days_In_Months(2) = 28 'Assume no leap
'// Check for leap year with currently selected year
If Is_Leap_Year(Set_Year) Then Days_In_Months(2) = 29

'// Add up total days from yr 2000 until selected yr
For i = 2000 To Set_Year - 1
Days_In_Year = 365 'No leap

'// Check for leap
If Is_Leap_Year(i) Then
Days_In_Year = 366 'Leap
End If

'// Sum it all
Days_From_01 = Days_From_01 + Days_In_Year
Next i

'// Add up total days until a month before the selected month
For i = 1 To (Set_Month - 1)
Days_From_01 = Days_From_01 + Days_In_Months(i)
Next i

'// Swap vars (new meaninful name with what we're about to do next)
Start_Day = Days_From_01

'// Dec week until we find the start day of the month
Do While Start_Day > 7: Start_Day = Start_Day - 7: Loop

'// Reset to 0 if > 7
Start_Day = Start_Day Mod 7

'// Starting print coords
Print_X = (Start_Day * 23) + 5
Print_Y = 5

'// Starting col in sync with the actual first day of the month
Cal_Col = Start_Day

'// Loop through days in month (1 to total) '
For i = 1 To Days_In_Months(Set_Month)

'// Set new row after 7 across (gird is 6x7)
If Print_X > 160 Then '
Print_Y = Print_Y + 23 'Inc
Print_X = 5 'Reset
Cal_Row = Cal_Row + 1 'Inc
Cal_Col = 0 'Reset
End If

'// Copy var to string
Date_Num = Trim(Str(i))

'// Add space for single digit vals (center alignment)
If Len(Date_Num) = 1 Then
Date_Num = Space(1) & Date_Num
End If

'// Print date to feild using the TextOut API (much faster)
TextOut Calendar_Field.hdc, Print_X, Print_Y, Date_Num, Len(Date_Num)

'// Store locations in grid array for later usage (user clicks fields)
Grid_Dat(Cal_Row, Cal_Col) = Date_Num

'// Inc X-axis for printing and col pos
Print_X = Print_X + 23
Cal_Col = Cal_Col + 1
Next

Start_Day = Start_Day + Set_Day 'Set start day in month
Cal_Col = 0 'Reset col & row
Cal_Row = 0 '

'-----------------------------------------------------------------------------
'// Here we need to locate the col & row in the grid that matches first day
'-----------------------------------------------------------------------------
For i = 0 To Start_Day - 1 'Loop through
Cal_Col = Cal_Col + 1 'Inc col
If Cal_Col = 7 Then 'New row?
Cal_Row = Cal_Row + 1 'Inc row
Cal_Col = 0 'Reset col
End If
Next

If Show_3D_Sel.Checked Then
'// Blit selected 3D cell
BitBlt Calendar_Field.hdc, Cal_Col * 23 + 3, Cal_Row * 23 + 3, 20, 20, Selected_3D.hdc, 0, 0, SrcCopy

'// Rectangle around it
Rectangle Calendar_Field.hdc, Cal_Col * 23 + 3, Cal_Row * 23 + 3, 23 + Cal_Col * 23, 23 + Cal_Row * 23

'// Show day inside cell
TextOut Calendar_Field.hdc, Cal_Col * 23 + 5, Cal_Row * 23 + 5, Grid_Dat(Cal_Row, Cal_Col), Len(Grid_Dat(Cal_Row, Cal_Col))
'//
Else '(Show 2D selector)
'//
'// Position selector square to selected day in the month
Selector.Move (Cal_Col * 23) + 5, (Cal_Row * 23) + 5

'// Show date inside square
Selector.Cls
TextOut Selector.hdc, 1, 0, Grid_Dat(Cal_Row, Cal_Col), Len(Grid_Dat(Cal_Row, Cal_Col))
End If

'// Build return string (mm-dd-yy)
Return_Date = Set_Month & "-" & Grid_Dat(Cal_Row, Cal_Col) & "-" & Set_Year
Call Extract_Date_Specifics

'// Update bottom portion of picker with a readout of the selected date
Show_Date.Caption = Format(Return_Date, IIf(Show_Long_Date.Checked, "dddd, mmmm d, yyyy", "dd/mm/yyyy"))

'// Reset (var only used by Set_Picker_Date proc to force a sel)
Set_Day = 0

'// Draw grid? (optional)
If Show_Grid.Checked Then
Draw_Grid
End If

Calendar_Field.Refresh
End Sub


*Definitely not directly applicable to PBP, there's lot of work needed to port it. Be great if PBP supported strings & string manipulation !
<br/>

T.Jackson
- 18th May 2007, 13:00
Nice project Trent!

I would like to see the algo you have for the calendar, if I may.

Thanks, Ioannis

By the way - you have some nice products!

Ioannis
- 18th May 2007, 13:10
Thanks for the post. I thought it was for the PIC itself though!

Thanks also for the comment about my products. Wait to see the new ones!

Regards,
Ioannis

T.Jackson
- 18th May 2007, 14:35
No worries. A calendar project for PBP is kind of swimming around in my mind a bit at the moment, so don't be too surprised to see something a bit further down the track.

Ioannis
- 21st May 2007, 09:59
Thanks! If I 'll find the time, I may do it first! Will see..

Ioannis

jellis00
- 10th November 2009, 09:20
![/B]
<hr/>
Trent Jackson
<br/>

Trent, first of all let me say you did an awesome job on this project.
I am trying to port it over to a PIC 18F4550. My code (shown below) compiles and assembles OK, but when I turn on the PIC with the WINDOWS Clock HID running, set the clock time in the WINDOWS HID, and then turn on the COM2 port, nothing happens. The time display never changes. Nor do I see the Test LED blink.
Can you please look at my code and advise me if you see where I may have gone wrong.



'************************************************* ***************
'* Name : VClock_18F4550.BAS *
'* Author : John R. Ellis *
'* Notice : *
'* : A *
'* Date : 11/9/2009 *
'* Version : 1.0 *
'* Notes : Portions of Program extracted from: *
'* 1) EZCLOCK2 By Paul R. Borgmeier, PhD *
'* Crux analysis & design, LLC www.cruxanalysis.com *
'* 2) VClock by Trent Jackson (see reference below) *
'* Device : Options of 18F2550/4550 *
'* Memory : 1624 bytes of Program Memory required *
'* : *
'************************************************* ***************
'* Name : VClock.BAS *
'* Author : Trent Jackson *
'* Notice : No Copyright *
'* : *
'* Date : 17/5/2007 *
'* Version : 1.0 *
'* *
'* Acknowledgements *
'* Portions of Program extracted from: *
'* EZCLOCK2 By Paul R. Borgmeier, PhD *
'* Crux analysis & design, LLC www.cruxanalysis.com *
'************************************************* **********
;--- if you un-comment these, you must comment the ones in the .inc file ---
ASM ; setup for 18F2550/4550, 8mhz crystal used in EasyPic6..4 MHz PLL input
__CONFIG _CONFIG1L, _PLLDIV_2_1L & _CPUDIV_OSC1_PLL2_1L & _USBDIV_2_1L
__CONFIG _CONFIG1H, _FOSC_HSPLL_HS_1H
__CONFIG _CONFIG2H, _WDT_ON_2H & _WDTPS_512_2H
__CONFIG _CONFIG3H, _PBADEN_OFF_3H
__CONFIG _CONFIG2L, _PWRT_ON_2L & _BOR_OFF_2L & _VREGEN_ON_2L
__CONFIG _CONFIG4L, _LVP_OFF_4L & _XINST_OFF_4L
ENDASM
;ASM ; setup for 18F13K50/14K50...Only 12mhz crystal can be used for USB
; __CONFIG _CONFIG1L, _CPUDIV_NOCLKDIV_1L & _USBDIV_OFF_1L
; __CONFIG _CONFIG1H, _FOSC_HS_1H & _PLLEN_ON_1H & _PCLKEN_ON_1H & _FCMEN_OFF_1H & _IESO_OFF_1H
;ENDASM

Include "modedefs.bas" ' Serial Protocol
Define OSC 4 ' EasyPic6 is 8MHz crystal; 4MHz used
'PIC: 18F4550 pin usage
' Pin 1 (RE3) : (MCLR Internal)
' Pin 25 (TX) : Tx pin for Serial interface
' Pin 26 (RX) : Rx pin for Serial interface
' Pin 13 (OSC1): XTAL 8.0000 Hz (w/tolerance = 30 ppm) w/ cap to GND
' Pin 14 (OSC2): XTAL 8.0000 Hz (w/tolerance = 30 ppm) w/ cap to GND
' Pin 27 (RD4) : Turn Something On (relay, FET, alarm, etc)
' Pin 28 (RD5) : Test Location LED, Buzzer, VOM, nothing? '
'// Variables
Counter VAR Word ' Track TMRO
Hours VAR Byte ' Total hrs (0-23)
Minutes VAR ByTE ' Mins (0-59)
Seconds VAR Byte ' Secs (0-59)
RX_To_PC VAR PORTC.7 ' RX line to Pin 26 (RC7)
TX_To_PC VAR PORTC.5 ' TX line to Pin 25 (RC6)
LED_Buzzer VAR PORTB.5 ' LED & Buzzer

'// Init
CMCON = 7 ' All digital
T0CON = %10000111 ' TMR0 enabled, TMRO prescale = 256
TRISA = %00000000 ' All outputs
TRISB = %00000000 ' All outputs
TRISC = %01000000 ' All outputs except RC7
INTCON.2 = 0 ' Clear TMRO overflow flag
Hours = 0 ' Null
Minutes = 0 ' ^
Seconds = 0 ' ^
Counter = $7A12 ' Load time counter

'=============
Wait_For_TMR0: ' Main loop
'=============
IF INTCON.2 = 0 THEN Wait_For_TMR0 ' Wait for TMRO overflow
INTCON.2 = 0 ' Clear TMRO overflow flag
Counter = Counter - $800 ' Dec counter until 1 sec has elapsed

'// Process hrs, mins secs accordingly ...
IF Counter < $800 THEN
Seconds = Seconds + 1
If Seconds = 60 Then
Seconds = 0
Minutes = Minutes + 1
If Minutes = 60 Then
Minutes = 0
Hours = Hours + 1
If Hours = 24 Then
Hours = 0
EndIf
EndIf
EndIf

'// Add leading zeros to all single digits & output str serially to PC
If Hours < 10 And Minutes > 9 And Seconds > 9 Then
SEROUT RX_To_PC, N2400, ["@","0",#Hours,":",#Minutes,":",#Seconds]
ELSE
IF Hours > 9 and Minutes < 10 and Seconds > 9 THEN
SEROUT RX_To_PC, N2400, ["@",#Hours,":0",#Minutes,":",#Seconds]
ELSE
IF Hours < 10 and Minutes < 10 and Seconds > 9 THEN
SEROUT RX_To_PC, N2400, ["@","0",#Hours,":0",#Minutes,":",#Seconds]
ELSE
IF Hours > 9 and Minutes > 9 and Seconds < 10 THEN
SEROUT RX_To_PC, N2400, ["@",#Hours,":",#Minutes,":0",#Seconds]
ELSE
IF Hours < 10 and Minutes > 9 and Seconds < 10 THEN
SEROUT RX_To_PC, N2400, ["@","0",#Hours,":",#Minutes,":0",#Seconds]
ELSE
IF Hours > 9 and Minutes < 10 and Seconds < 10 THEN
SEROUT RX_To_PC, N2400, ["@",#Hours,":0",#Minutes,":0",#Seconds]
ELSE
IF Hours < 10 and Minutes < 10 and Seconds < 10 THEN
SEROUT RX_To_PC, N2400, ["@","0",#Hours,":0",#Minutes,":0",#Seconds]
Else
SEROUT RX_To_PC, N2400, ["@",#Hours,":",#Minutes,":",#Seconds]
EndIf : EndIf : EndIf : EndIf : EndIf : EndIf : EndIf

Counter = Counter + $7A12 ' Reset counter
Toggle LED_Buzzer '
'//
Else 'Wait here for 50mS to see if the host(PC) is trying to set new time val
'//
SERIN TX_To_PC, N2400, 50, Wait_For_TMR0, ["@"], Hours, Minutes, Seconds
EndIf
GoTo Wait_For_TMR0 ' Loop back
end