;-- ------------------------------------------------------
;--  Project: 	DIGITAL CLOCK WITH DATE AND THERMOMETER
;-- 				WITH PIC16F84(A)/PIC16F628(A) AND DS18S20/DS18B20
;--  Electric diagram & Software: Bogdan Borca/www.bogdi.ro
;-- ------------------------------------------------------
;-- Updates
;-- Wednesday March 02 2011, 2:07 AM
;--			Why?:	->added schematics for big clock (14 segments/led)
;-- Monday February 28 2011, 3:08 AM
;--			Why?:	->added support for battery backup
;-- Wednesday January 26 2011, 11:51 PM
;--			Why?:	->added support for common cathode displays
;-- Friday January 07 2011, 8:10 PM
;--			Why?:	->degrees mark bug: L letter was displayed instead degrees mark for negative temps between -1 and -9
;-- Monday November 22 2010, 10:09 PM
;--			Why?:	->found & fixed a bug in second calibration procedure (mixed Sh=40 & Sl=42 and it should have been Sh=42/Sl=40)
;--					->optimize the code and saved some space
;--  -----------------------------------------------------

	#define FEEDBACK http://www.bogdi.ro/guestbook Thank You! :-)
	
	CBLOCK 0x00			
	;-- Sections	
		Overview		
		Supported PICS	
		Supported temperature sensors
		Supported displays (COMMON ANODE/CATHODE)
		EE memory, default values, HEX file, recompiling
		How it works		
		Settings		
		Clock calibration 1		
		Clock calibration 2		
		Proteus	
	ENDC

	;-------------------------------------------------
	;-- Overview
	;-------------------------------------------------

	This is a digital clock date thermometer I made because I like electronics and 
	programming and also because I wanted to learn PIC micro. I didn't even knew I had
	DS1820 in my electronic components box. It is a nice project I enjoy doing
	and ... it's done. All I learned was from the internet so I think it's nice 
	to give something in return. Enjoy!
	
	;-------------------------------------------------
	;-- Supported PICS
	;-------------------------------------------------	
	
	The project can be compiled with any PIC with support for the pins. In define.inc
	file from the Inc\ folder in project you can find the PROCESSOR:	
		
;	#define _PROCESSOR 16F84			;--  If you want to compile the project using PIC16F628
	#define _PROCESSOR 16F628			;--  Uncomment to use it and comment the line above
										;--  Select proper device for the project 
	if _PROCESSOR == 16F84				;--  for MPLAB: Configure->Select Device
		#define FRA H'10'				;--  FRA is used for different bank address between chips
		list p=16F84					;--  for example PIC16F84 has File Registers starting from
		#include p16F84.inc				;--  H'10' address and PIC16F628 has it starting from H'20'
	__CONFIG _CP_OFF & _WDT_OFF & _PWRTE_ON & _XT_OSC 
	endif

	if _PROCESSOR == 16F628								
		#define FRA H'20'
		list p=16F628					
		#include p16F628.inc			
	__CONFIG _CP_OFF & _WDT_OFF & _PWRTE_ON & _XT_OSC & _BODEN_OFF & _LVP_OFF
	endif

	You can set your own device with your config word. 	
		
	;-------------------------------------------------
	;-- Supported temperature sensors
	;-------------------------------------------------
	
	DS18S20/DS18B20	(DS1631 in the digital/clock/date/thermometer/thermostat project)
	There are defined DS18S20 and DS18B20 in project's Inc\ folder, define.inc file

;	#define DS18S20 "Present"			;--  Compile project using DS18S20
	#define DS18B20 "Present"			;--  Compile project using DS18B20
										;--  If you want to compile the project using ds18B20 then
										;--  comment #define DS18S20.. and uncomment #define DS18B20

	Comment/Uncomment the sensor you want (not to use)/use and recompile, export new hex, use it.
	I made another project very similar to this one using DS1631, the advantage using this sensor
	is that you can have a thermostat function. 
	See more on http://bogdi.ro/electronics/digital%20clock%20with%20date%20and%20thermometer/

	;-- -----------------------------------------------
	;--  Display configuration (COMMON ANODE/COMMON CATHODE)
	;-- -----------------------------------------------

	#define _COMMON_Anode "Present"		;--  Compile project using COMMON ANODE DISPLAYS
;	#define _COMMON_Cathode "Present"	;--  Compile project using COMMON CATHODE DISPLAYS	
										;--  Don't forget to Load EE Table 
										;--  EEPROM_dcdt_COMMON_CATHODE.MCH/EEPROM_dcdt_COMMON_ANODE.MCH
										;--  (Right Click !First! Entry Address 00-00 and select Import Table ...)
										;--  You can change these defauls with your own, read
										;--  [EE memory, default values and HEX file] section in README.txt
										;-- -----------------------------------------------
	
	;-------------------------------------------------
	;-- EE memory, default values, HEX file, recompiling
	;-------------------------------------------------	
	
	Important note: remember to load EE with the EEPROM_dcdt_COMMON_X.MCH when 
	compiling/exporting HEX file for the project. I've used EE to save some code. 
	This file has the configuration to display each character
	(0-9 and some letters A/B/C...) needed by the clock, also holds default	values for
	the settings (h/m/dd/dm/dY/dt/tt/td/te/Sh/Sl)
	You can change these values starting to count 16 
	numbers back from the end of the file, that would be:
	
	0E	-> default hour in hex 0E=14 (decimal)	[0..17 hex] 23 in dec
	2E	-> default minute in hex 2E=46			[0..3B hex] 59 in dec
	19	-> default day in hex 19=25				[0..1F hex (max days/month)] 31 in dec
	02	-> default month 02 = FE				[1..0C hex] 12 in dec
	0B	-> default year 0B = 11 (2011)			[1..63 hex] 99 in dec meaning 2099
	01	-> default date type, 1 means showing letters for month	[1..2]
	08	-> default seconds to show time			[2..3B hex] 59 in dec
	02	-> default seconds to show date			[0..3B]
	03	-> default seconds to show temperature	[0..3B]
	42	-> default Sh second calibration 42		[0..FF]
	40	-> default Sl second calibration 40		[0..FF]
	FF
	FF
	FF
	FF
	FF	-> end of the file (last entry) 
	
	You can see these values in the HEX file I compiled and can change them when
	programming the pic if you want other default values without recompiling the project. 
	See lines that start with :10426 and :10427 (in the hex file):	
	:104260000E002E00190002000B00010008000200E1
	:10427000030042004000FF00FF00FF00FF00FF00BE
	and identify each value, I made the selection for a better association:	
	:10426000 [0E] 00 [2E] 00 [19] 00 [02] 00 [0B] 00 [01] 00 [08] 00 [02] 00E1
	:10427000 [03] 00 [42] 00 [40] 00 [FF] 00 [FF] 00 [FF] 00 [FF] 00 [FF] 00BE	
	
	Override carefully hex numbers with your own. Make sure you override with
	valid numbers, minutes for example between 0-3B (HEX) (0-60 decimal) the day
	to be a valid day for the month and so on.
	If you want to recompile the project, after compilation/building project,
	use Export option from File menu (MPLAB), (don't know other IDE, used one
	on Linux but don't remember the command) to export hex code and hex memory
	in the same hex file. Otherwise EE will not contain any data when loaded 
	to the pic programmer and w/out it nothing will work.
	
	;-------------------------------------------------
	;-- How it works
	;-------------------------------------------------
	
	When you first power the device the clock is mentained for 8 seconds. Then 
	the date for 2 seconds and temperature will be displayed for 3 seconds. The clock has
	four buttons, with one button you enter in set mode, with another button you
	increase values, the third button will be used to decrease values and you can
	put or not, the reset. If you don't want a reset button just connect the pin to +5V/+3V.
	You can change:		
		* ho -> hour, from 0 to 23		
		* nn -> minutes, from 0 to 59 (I could not display m so I used n twice for it)		
		* dn -> date month (JA FE |upperscore|A AP |upperscore|Y JU JL AU SE oc no dE -or- 01 02 03 04		
		05 06 07 08 09 10 11 12, depending on dt setting)		
		* dd -> date day, from 1 to days/month -> calculated based on month and year
		* dy -> date year, from 0 meaning 2000 to 99 meaning 2099 (not displayed 
		in time/date mode but needed to calculate days per month)
		* dt -> date type can be 1 or 2, if date type equals 1 then letters are used 
		for month, else numbers are used  
		* tt -> time in seconds for showing time/clock, from 2 to 59, you cannot set 
		0 or 1 for this setting	
		* td -> time in seconds for showing date, from 0 to 59, if 0 is used then the 
		date is not shown	
		* tE -> time in seconds for showing temperature, from 0 to 59, if 0 is used 
		then the temperature is not shown	
		* Sh -> calibrate the clock by adjusting second high (see below)
		* Sl -> calibrate the clock by adjusting second low (see below)	
		
		When setting up the day for month take in consideration the month and year and days/that 
		month in that year. You cannot set 31 day for February for example. The two middle points 
		are not blinking when date/temperature are shown and also when set mode is on. If both td 
		and tE are set to 0 then the time will be the only one displayed. In the case DS18S/B20 is 
		missing you can set tE to 0.
	
		The clock has a battery backup and will work silently with power supply turned off. 
		I didn't test the oscillator and if the clock loses precision in this situation. 	
		
	;-------------------------------------------------
	;-- Settings 
	;-------------------------------------------------	
	
	Default settings are saved in EE chip internal memory. When you	change hour/minutes/.. 
	(h/m/dd/dm/dY/dt/tt/td/te/Sh/Sl) all of them will be saved. If power is off	you will 
	not have to change them again when powering the device back on. 
	
	;-------------------------------------------------
	;-- Clock calibration 1 (see also Calibration details.doc, maybe helps)
	;-------------------------------------------------	
	
	The clock can be calibrated through software. This is useful if the clock lost or gain 
	seconds during a period of time. This can happen for various reasons (crystal, capacitors, 
	PIC?, temperature). The idea is to make the second last longer or less. One second lasts
	1000000 microseconds. This is a big number for my display where I can write only 4 numbers, 
	but.. if we represent this number in hexadecimal will have this value: 0F4240. 
	Calibrations means adjusting around 1000000 so I dropped 0F, you cannot set that, but you
	can adjust 42(Sh) and 40(Sl) from 00 to FF. This gives you a large possibility to adjust your 
	second. Adjusting examples:	
	-> lost 30 seconds / 24 hours => 30/86400=0.000347
	1000000-(1000000*0.000347)=999653(decimal)=F40E5(hexadecimal) => Set 40 for Sh and E5 for Sl.	
	-> lost 2 seconds / 1 hour => 2/3600=0.000555 
	1000000-(1000000*0.000555)=999445(decimal)=F4015(hexadecimal) => Set 40 for Sh and 15 for Sl.
	-> gain 15 seconds / 60 days => 15/5184000=0.000002 
	1000000+(1000000*0.000555)=1000002(decimal)=F4242(hexadecimal) => Set 42 for Sh and 42 for Sl.
	A longer the period of time being tested will have a better accuracy for the clock. 
	
	;-------------------------------------------------
	;-- Clock calibration 2
	;-------------------------------------------------	
	
	Other way to calibrate the clock is adjusting Sh and mostly Sl like below. First you need to 
	know two values: one that makes the clock run faster and one that makes the clock run slower. 
	Let's say our two values are: 999840(clock runs faster=FAST) and 999884(clock runs slower=SLOW). 
	Repeat changing the second length to (FAST+SLOW)/2 until FAST=SLOW(+/-1). 
	This should make your clock very accurate.
	
	;-------------------------------------------------
	;-- Proteus
	;-------------------------------------------------	
	
	Still in progress. I didn't have time to reinstall it after my old hard-drive died. It needs
	few changes from the old project, to be up to date. I believe you can also do the changes after 
	the schematic and load the hex file. It should work.	
	
	Thanks to Roman Black for the One second event!	(Zero-error 1 second Timer)
	http://www.romanblack.com/one_sec.htm