]> bicyclesonthemoon.info Git - staf/staf/commitdiff
calculator, hardware variant v1.5
authorb <rowerynaksiezycu@gmail.com>
Fri, 15 Jul 2016 19:34:39 +0000 (21:34 +0200)
committerb <rowerynaksiezycu@gmail.com>
Fri, 15 Jul 2016 19:34:39 +0000 (21:34 +0200)
asm.S
definitions.h
gpl.txt [new file with mode: 0644]
main.c
makefile

diff --git a/asm.S b/asm.S
index e57797085705df19add19faf6bf54e3dc74fc889..c3b5f7da3415918b3adf6073e4f928b6293816ff 100644 (file)
--- a/asm.S
+++ b/asm.S
@@ -1,7 +1,8 @@
-; SilentTimer Action Figure software, v1.4
+; SilentTimer Action Figure software, v1.5
 ; asm.S
 ; things that can't be done in C
 ; 
+; 07.03.2016
 ; Copyright (C) 2014-2016  Balthasar Szczepański
 ; 
 ; This program is free software: you can redistribute it and/or modify
 #define SR R2
 
 .section .text
-.global bcd_add_byte
-.global lpm3
+
 
 ;function to add 2-digit BCD numbers
+.global bcd_add_byte
+.type bcd_add_byte, @function
 bcd_add_byte:
        clrc
        dadd.b R14, R15
        ret
 
 ;function to enter low power mode 3
+.global lpm3
+.type lpm3, @function
 lpm3:
        bis.w #SCG1|SCG0|CPUOFF, SR
        ret
+
+;helper function - shifts R13:R12 two digits left
+.global calc_shl
+.type calc_shl, @function
+.type cshl_1, @function
+calc_shl:
+       mov   #8,      R15
+cshl_1:
+       rla            R12
+       rlc            R13
+       dec            R15
+       jnz            cshl_1
+       ret
+
+;helper function - shifts R13:R12 two digits right, with rounding
+.global calc_shr
+.type calc_shr, @function
+.type cshr_1, @function
+.type cshr_2, @function
+calc_shr:
+       mov   R12,     R15
+       and   #0x00f0, R15
+       cmp   #0x0050, R15
+       jlo            cshr_1
+       clrc
+       dadd  #0x0100, R12
+       dadc           R13
+cshr_1:
+       mov   #8,      R15
+cshr_2:
+       rra            R13
+       rrc            R12
+       dec            R15
+       jnz            cshr_2
+       ret
+
+;helper function - negate bcd number in R13:R12
+.global calc_neg
+.type calc_neg, @function
+calc_neg:
+       mov   #0x9999, R15
+       sub   R12,     R15
+       mov   R15,     R12
+       mov   #0x9999, R15
+       sub   R13,     R15
+       mov   R15,     R13
+       clrc
+       dadd  #0x0001, R12
+       dadc           R13
+       ret
+       
+;the calculator add function
+.global calc_add
+.type calc_add, @function
+.type ca_shl2, @function
+.type ca_neg2, @function
+.type ca_shl1, @function
+.type ca_neg1, @function
+.type ca_negr, @function
+.type ca_shr1r, @function
+.type ca_err, @function
+.type ca_end, @function
+calc_add:
+       push  R11
+       push  R10
+       
+       mov   R15,     R14
+       
+       mov   2(R14),  R12
+       clr            R13
+       bit.b #0x08,   6(R14)
+       jnz            ca_shl2
+       call           #calc_shl
+ca_shl2:
+       bit.b #0x04,   6(R14)
+       jz             ca_neg2
+       call           #calc_neg
+ca_neg2:
+       mov   R12,     R10
+       mov   R13,     R11
+       
+       mov   4(R14),  R12
+       clr            R13
+       bit.b #0x20,   6(R14)
+       jnz            ca_shl1
+       call           #calc_shl
+ca_shl1:
+       bit.b #0x10,   6(R14)
+       jz             ca_neg1
+       call           #calc_neg
+ca_neg1:
+;ok, prepared, now add
+       clrc
+       dadd  R10,     R12
+       dadd  R11,     R13
+;and process result
+       mov   #0xff00, R10
+       mov   #0x0000, R11
+       bic.b #0x03,   6(R14)
+       mov   R13,     R15
+       and   #0xff00, R15
+       cmp   #0x9900, R15
+       jne            ca_negr
+       mov   #0xfff0, R10
+       mov   #0xf000, R11
+       bis.b #0x01,   6(R14)
+       call           #calc_neg
+ca_negr:
+       bit   R10,     R13
+       jnz            ca_err
+       bit   #0xffff, R13
+       jnz            ca_shr1r         
+       bit   R11,     R12
+       jnz            ca_shr1r
+       bit   #0x00ff, R12
+       jz             ca_shr1r
+       bis.b #0x02,   6(R14)
+       jmp            ca_end
+ca_shr1r:
+       call           #calc_shr
+       bit   #0xffff, R13
+       jnz            ca_err
+       bit   R11,     R12
+       jnz            ca_err
+       jmp            ca_end
+ca_err:
+       mov.b #0xff,   7(R14)
+ca_end:
+       mov   R12,     0(R14)
+       pop            R10
+       pop            R11
+       ret
+
+;the calculator multiply function
+.global calc_mul
+.type calc_mul, @function
+.type cm_ml, @function
+.type cm_slo, @function
+.type cm_sla, @function
+.type cm_adm1, @function
+.type cm_adm2, @function
+.type cm_nr1, @function
+.type cm_nr2, @function
+.type cm_srl1, @function
+.type cm_srl2, @function
+.type cm_srl3, @function
+.type cm_srl4, @function
+.type cm_srrl1, @function
+.type cm_srrl2, @function
+.type cm_tbg, @function
+.type cm_srrb, @function
+.type cm_zap, @function
+.type cm_err, @function
+.type cm_end, @function
+calc_mul:
+       push  R11
+       push  R10
+       push  R9
+       push  R8
+       push  R7
+       push  R6
+       mov   2(R15),  R7
+       mov   4(R15),  R8
+       clr            R9
+       clr            R12
+       clr            R13
+       mov   #4,      R14
+cm_ml:
+       clr            R10
+       clr            R11
+       mov   #4,      R6
+cm_slo:
+       rla            R8
+       rlc            R9
+       dec            R6
+       jnz            cm_slo
+       mov   #4,      R6
+cm_sla:
+       rla            R12
+       rlc            R13
+       dec            R6
+       jnz            cm_sla
+       bit   #0xffff, R9
+       jz             cm_adm2
+cm_adm1:
+       clrc
+       dadd  R7,      R10
+       dadc           R11
+       dec            R9
+       jnz            cm_adm1
+cm_adm2:
+       clrc
+       dadd  R10,     R12
+       dadd  R11,     R13
+       dec            R14
+       jnz            cm_ml
+;R8, R9, R14 = 0 here
+       mov   #0xff00, R7
+       bic.b #0x03,   6(R15)
+       bit.b #0x10,   6(R15)
+       jz             cm_nr1
+       xor.b #0x01,   6(R15)
+       xor   #0xf000, R8
+       xor   #0x00f0, R7
+cm_nr1:
+       bit.b #0x04,   6(R15)
+       jz             cm_nr2
+       xor.b #0x01,   6(R15)
+       xor   #0xf000, R8
+       xor   #0x00f0, R7
+cm_nr2:
+       bit.b #0x20,   6(R15)
+       jnz            cm_srl1
+       add   #8,      R9
+cm_srl1:
+       bit.b #0x08,   6(R15)
+       jnz            cm_srl2
+       add   #8,      R9
+cm_srl2:
+       cmp   #0x0000, R9
+       jeq            cm_srl4
+cm_srl3:
+       rla            R12
+       rlc            R13
+       rlc            R14
+       dec            R9
+       jnz            cm_srl3
+cm_srl4:
+       bit   #0xffff, R14
+       jnz            cm_err
+       bit   R8,      R13
+       jnz            cm_err
+       bit   R7,      R13
+       jnz            cm_srrl2
+       mov   #8,      R6
+       bis.b #0x02,   6(R15)
+cm_srrl1:
+       rla            R12
+       rlc            R13
+       dec            R6
+       jnz            cm_srrl1
+cm_srrl2:
+       and   #0xf000, R12
+       cmp   #0x5000, R12
+       jlo            cm_zap
+       clrc
+       dadd  #0x0001, R13
+       dadc           R14
+       jnz            cm_tbg
+       bit   R8,      R13
+       jnz            cm_tbg
+       jmp            cm_zap
+cm_tbg:
+       bit.b #0x02,   6(R15)
+       jz             cm_err
+       bic.b #0x02,   6(R15)
+       mov   #8,      R6
+cm_srrb:
+       rra            R13
+       dec            R6
+       jnz            cm_srrb
+cm_zap:
+       bit.b #0x02,   6(R15)
+       jz             cm_end
+       bit   #0x00ff, R13
+       jnz            cm_end
+       swpb           R13
+       bic.b #0x02,   6(R15)
+       bit   #0xffff, R13
+       jnz            cm_end
+       bic.b #0x01,   6(R15)
+       jmp            cm_end
+cm_err:
+       mov.b #0xff,   7(R15)
+cm_end:
+       mov   R13,     0(R15)
+       pop            R6
+       pop            R7
+       pop            R8
+       pop            R9
+       pop            R10
+       pop            R11
+       ret
+
+;the calculator divide function
+.global calc_div
+.type calc_div, @function
+.type cd_slo1, @function
+.type cd_slo2, @function
+.type cd_slo3, @function
+.type cd_slo4, @function
+.type cd_ml, @function
+.type cd_slaa, @function
+.type cd_sbdv1, @function
+.type cd_sbdv2, @function
+.type cd_sbdv3, @function
+.type cd_nr1, @function
+.type cd_nr2, @function
+.type cd_srrl1, @function
+.type cd_srrl2, @function
+.type cd_tbg, @function
+.type cd_srrb, @function
+.type cd_zap, @function
+.type cd_err, @function
+.type cd_end, @function
+calc_div:
+       push  R11
+       push  R10
+       push  R9
+       push  R8
+       push  R7
+       push  R6
+       push  R5
+       
+       mov   2(R15),  R7
+       bit   #0xffff, R7
+       jz             cd_err
+       mov   #0x9999, R6
+       sub   R7,      R6
+       clrc
+       dadd  #0x0001, R6
+       
+       mov   4(R15),  R8
+       clr            R9
+       clr            R10
+       clr            R11
+       clr            R12
+       clr            R13
+       clr            R5
+       
+       bit.b #0x20,   6(R15)
+       jnz            cd_slo1
+       add   #8,      R5     
+cd_slo1:
+       bit.b #0x08,   6(R15)
+       jz             cd_slo2
+       add   #8,      R5
+cd_slo2:
+       bit   #0xffff, R5
+       jz             cd_slo4
+cd_slo3:
+       rla            R8
+       rlc            R9
+       dec            R5
+       jnz            cd_slo3
+cd_slo4:
+       
+       mov   #9,      R14
+cd_ml:
+       mov   #4,      R5
+cd_slaa:
+       rla            R8
+       rlc            R9
+       rlc            R10
+       rlc            R11
+       rla            R12
+       rlc            R13
+       jc             cd_err
+       dec            R5
+       jnz            cd_slaa
+cd_sbdv1:
+       bit   #0xffff, R11
+       jnz            cd_sbdv2
+       cmp   R7,      R10
+       jlo            cd_sbdv3
+cd_sbdv2:
+       clrc
+       dadd  R6,      R10
+       dadd  #0x9999, R11
+       clrc
+       dadd  #0x0001, R12
+       jmp            cd_sbdv1
+cd_sbdv3:
+       dec            R14
+       jnz            cd_ml
+       
+       bic.b #0x03,   6(R15)
+       clr   R14
+       mov   #0x0000, R9
+       mov   #0xf000, R10
+       mov   #0xfff0, R11
+       bit.b #0x10,   6(R15)
+       jz             cd_nr1
+       xor.b #0x01,   6(R15)
+       xor   #0xf000, R9
+       xor   #0x0f00, R10
+       xor   #0x000f, R11
+cd_nr1:
+       bit.b #0x04,   6(R15)
+       jz             cd_nr2
+       xor.b #0x01,   6(R15)
+       xor   #0xf000, R9
+       xor   #0x0f00, R10
+       xor   #0x000f, R11
+cd_nr2:
+       bit   R10,     R13
+       jnz            cd_err
+       bit   R11,     R13
+       jnz            cd_srrl1
+       mov   #12,     R5
+       bis.b #0x02,   6(R15)
+       jmp            cd_srrl2
+cd_srrl1:
+       mov  #4,       R5
+cd_srrl2:
+       rla            R12
+       rlc            R13
+       rlc            R14
+       dec            R5
+       jnz            cd_srrl2
+       and   #0xf000, R12
+       cmp   #0x5000, R12
+       jlo            cd_zap
+       clrc
+       dadd  #0x0001, R13
+       dadc           R14
+       jnz            cd_tbg
+       bit   R9,      R13
+       jnz            cd_tbg
+       jmp            cd_zap
+cd_tbg:
+       bit.b #0x02,   6(R15)
+       jz             cd_err
+       bic.b #0x02,   6(R15)
+       mov   #8,      R5
+cd_srrb:
+       rra            R13
+       dec            R5
+       jnz            cd_srrb
+cd_zap:
+       bit.b #0x02,   6(R15)
+       jz             cd_end
+       bit   #0x00ff, R13
+       jnz            cd_end
+       swpb           R13
+       bic.b #0x02,   6(R15)
+       bit   #0xffff, R13
+       jnz            cd_end
+       bic.b #0x01,   6(R15)
+       jmp            cd_end
+cd_err:
+       mov.b #0xff,   7(R15)
+cd_end:
+       mov   R13,     0(R15)
+       pop            R5
+       pop            R6
+       pop            R7
+       pop            R8
+       pop            R9
+       pop            R10
+       pop            R11
+       ret
+       
+       
+       
\ No newline at end of file
index 94217cf6ea7f51155b22d1e4e3f3ac45f80725a7..d67139774d1926b864be219c09a7f5cdbd6a8e6c 100644 (file)
@@ -1,7 +1,8 @@
-// SilentTimer Action Figure software, v1.4\r
+// SilentTimer Action Figure software, v1.5\r
 // definitions.h\r
 // Definitions used by the code.\r
 // \r
+// 21.03.2016\r
 // Copyright (C) 2014-2016  Balthasar Szczepański\r
 //\r
 // This program is free software: you can redistribute it and/or modify\r
@@ -38,6 +39,8 @@
 \r
 //7 segment display\r
 \r
+#if defined OLD_HARDWARE\r
+\r
 #define SEG_A 0xdf\r
 #define SEG_B 0xef //    aaaa\r
 #define SEG_C 0xbf //   f    b\r
 #define SEG_G 0xf7 //   e    c\r
 #define SEG_H 0xfe //    dddd \r
 \r
+#else\r
+\r
+#define SEG_A 0xfd\r
+#define SEG_B 0xef //    aaaa\r
+#define SEG_C 0xfb //   f    b\r
+#define SEG_D 0xfe //   f    b\r
+#define SEG_E 0xdf //hh  gggg\r
+#define SEG_F 0x7f //   e    c\r
+#define SEG_G 0xbf //   e    c\r
+#define SEG_H 0xf7 //    dddd \r
+\r
+#endif\r
+\r
 #define SYMB_0   (SEG_A & SEG_B & SEG_C & SEG_D & SEG_E & SEG_F        )\r
 #define SYMB_1   (        SEG_B & SEG_C                                )\r
 #define SYMB_2   (SEG_A & SEG_B         & SEG_D & SEG_E         & SEG_G)\r
 #define SYMB_o   (SEG_A & SEG_B                         & SEG_F & SEG_G)\r
 #define SYMB_n   (SEG_A & SEG_B                         & SEG_F        )\r
 #define SYMB_g   (SEG_A & SEG_B & SEG_C & SEG_D         & SEG_F & SEG_G)\r
+#define SYMB_U   (                SEG_C & SEG_D & SEG_E                )\r
+#define SYMB_L   (                        SEG_D & SEG_E & SEG_F        )\r
+#define SYMB_M1  (SEG_A & SEG_B                 & SEG_E & SEG_F        )\r
+#define SYMB_M2  (SEG_A & SEG_B & SEG_C                 & SEG_F        )\r
+#define SYMB_I   (                                SEG_E                )  \r
 #define SYMB_MIN (                                                SEG_G)\r
 #define SYMB_DOT (SEG_H)\r
 \r
 //operation modes\r
-#define MODE_INIT 0\r
-#define MODE_TIME 1\r
-#define MODE_SETTIME 2\r
-#define MODE_SETALARM 3\r
-#define MODE_ALARM 4\r
-#define MODE_DATE 5\r
-#define MODE_SETDATE 6\r
-#define MODE_SETYEAR 7\r
-#define MODE_COUNTUP 8\r
+#define MODE_INIT         0\r
+#define MODE_TIME         1\r
+#define MODE_SETTIME      2\r
+#define MODE_SETALARM     3\r
+#define MODE_ALARM        4\r
+#define MODE_DATE         5\r
+#define MODE_SETDATE      6\r
+#define MODE_SETYEAR      7\r
+#define MODE_COUNTUP      8\r
 #define MODE_SETCOUNTDOWN 9\r
-#define MODE_COUNTDOWN 10\r
+#define MODE_COUNTDOWN   10\r
+\r
+#define MODE_CALIBRATE   11\r
+#define MODE_DEBUG       12\r
+#define MODE_DEBUGADDR   13\r
+#define MODE_DEBUGVAL    14\r
 \r
-#define MODE_CALIBRATE 11\r
-#define MODE_DEBUG 12\r
-#define MODE_DEBUGADDR 13\r
-#define MODE_DEBUGVAL 14\r
+#define MODE_CALC        15\r
+#define MODE_CALCSETNUM  16\r
+#define MODE_CALCSETOPER 17\r
 \r
 #define ALARMVALUE 0xa5\r
 \r
 unsigned char bcd_add_byte (unsigned char, unsigned char);\r
 void          lpm3         ();\r
-\r
+void          calc_add     (unsigned char *);\r
+void          calc_mul     (unsigned char *);\r
+void          calc_div     (unsigned char *);\r
diff --git a/gpl.txt b/gpl.txt
new file mode 100644 (file)
index 0000000..94a9ed0
--- /dev/null
+++ b/gpl.txt
@@ -0,0 +1,674 @@
+                    GNU GENERAL PUBLIC LICENSE
+                       Version 3, 29 June 2007
+
+ Copyright (C) 2007 Free Software Foundation, Inc. <http://fsf.org/>
+ Everyone is permitted to copy and distribute verbatim copies
+ of this license document, but changing it is not allowed.
+
+                            Preamble
+
+  The GNU General Public License is a free, copyleft license for
+software and other kinds of works.
+
+  The licenses for most software and other practical works are designed
+to take away your freedom to share and change the works.  By contrast,
+the GNU General Public License is intended to guarantee your freedom to
+share and change all versions of a program--to make sure it remains free
+software for all its users.  We, the Free Software Foundation, use the
+GNU General Public License for most of our software; it applies also to
+any other work released this way by its authors.  You can apply it to
+your programs, too.
+
+  When we speak of free software, we are referring to freedom, not
+price.  Our General Public Licenses are designed to make sure that you
+have the freedom to distribute copies of free software (and charge for
+them if you wish), that you receive source code or can get it if you
+want it, that you can change the software or use pieces of it in new
+free programs, and that you know you can do these things.
+
+  To protect your rights, we need to prevent others from denying you
+these rights or asking you to surrender the rights.  Therefore, you have
+certain responsibilities if you distribute copies of the software, or if
+you modify it: responsibilities to respect the freedom of others.
+
+  For example, if you distribute copies of such a program, whether
+gratis or for a fee, you must pass on to the recipients the same
+freedoms that you received.  You must make sure that they, too, receive
+or can get the source code.  And you must show them these terms so they
+know their rights.
+
+  Developers that use the GNU GPL protect your rights with two steps:
+(1) assert copyright on the software, and (2) offer you this License
+giving you legal permission to copy, distribute and/or modify it.
+
+  For the developers' and authors' protection, the GPL clearly explains
+that there is no warranty for this free software.  For both users' and
+authors' sake, the GPL requires that modified versions be marked as
+changed, so that their problems will not be attributed erroneously to
+authors of previous versions.
+
+  Some devices are designed to deny users access to install or run
+modified versions of the software inside them, although the manufacturer
+can do so.  This is fundamentally incompatible with the aim of
+protecting users' freedom to change the software.  The systematic
+pattern of such abuse occurs in the area of products for individuals to
+use, which is precisely where it is most unacceptable.  Therefore, we
+have designed this version of the GPL to prohibit the practice for those
+products.  If such problems arise substantially in other domains, we
+stand ready to extend this provision to those domains in future versions
+of the GPL, as needed to protect the freedom of users.
+
+  Finally, every program is threatened constantly by software patents.
+States should not allow patents to restrict development and use of
+software on general-purpose computers, but in those that do, we wish to
+avoid the special danger that patents applied to a free program could
+make it effectively proprietary.  To prevent this, the GPL assures that
+patents cannot be used to render the program non-free.
+
+  The precise terms and conditions for copying, distribution and
+modification follow.
+
+                       TERMS AND CONDITIONS
+
+  0. Definitions.
+
+  "This License" refers to version 3 of the GNU General Public License.
+
+  "Copyright" also means copyright-like laws that apply to other kinds of
+works, such as semiconductor masks.
+
+  "The Program" refers to any copyrightable work licensed under this
+License.  Each licensee is addressed as "you".  "Licensees" and
+"recipients" may be individuals or organizations.
+
+  To "modify" a work means to copy from or adapt all or part of the work
+in a fashion requiring copyright permission, other than the making of an
+exact copy.  The resulting work is called a "modified version" of the
+earlier work or a work "based on" the earlier work.
+
+  A "covered work" means either the unmodified Program or a work based
+on the Program.
+
+  To "propagate" a work means to do anything with it that, without
+permission, would make you directly or secondarily liable for
+infringement under applicable copyright law, except executing it on a
+computer or modifying a private copy.  Propagation includes copying,
+distribution (with or without modification), making available to the
+public, and in some countries other activities as well.
+
+  To "convey" a work means any kind of propagation that enables other
+parties to make or receive copies.  Mere interaction with a user through
+a computer network, with no transfer of a copy, is not conveying.
+
+  An interactive user interface displays "Appropriate Legal Notices"
+to the extent that it includes a convenient and prominently visible
+feature that (1) displays an appropriate copyright notice, and (2)
+tells the user that there is no warranty for the work (except to the
+extent that warranties are provided), that licensees may convey the
+work under this License, and how to view a copy of this License.  If
+the interface presents a list of user commands or options, such as a
+menu, a prominent item in the list meets this criterion.
+
+  1. Source Code.
+
+  The "source code" for a work means the preferred form of the work
+for making modifications to it.  "Object code" means any non-source
+form of a work.
+
+  A "Standard Interface" means an interface that either is an official
+standard defined by a recognized standards body, or, in the case of
+interfaces specified for a particular programming language, one that
+is widely used among developers working in that language.
+
+  The "System Libraries" of an executable work include anything, other
+than the work as a whole, that (a) is included in the normal form of
+packaging a Major Component, but which is not part of that Major
+Component, and (b) serves only to enable use of the work with that
+Major Component, or to implement a Standard Interface for which an
+implementation is available to the public in source code form.  A
+"Major Component", in this context, means a major essential component
+(kernel, window system, and so on) of the specific operating system
+(if any) on which the executable work runs, or a compiler used to
+produce the work, or an object code interpreter used to run it.
+
+  The "Corresponding Source" for a work in object code form means all
+the source code needed to generate, install, and (for an executable
+work) run the object code and to modify the work, including scripts to
+control those activities.  However, it does not include the work's
+System Libraries, or general-purpose tools or generally available free
+programs which are used unmodified in performing those activities but
+which are not part of the work.  For example, Corresponding Source
+includes interface definition files associated with source files for
+the work, and the source code for shared libraries and dynamically
+linked subprograms that the work is specifically designed to require,
+such as by intimate data communication or control flow between those
+subprograms and other parts of the work.
+
+  The Corresponding Source need not include anything that users
+can regenerate automatically from other parts of the Corresponding
+Source.
+
+  The Corresponding Source for a work in source code form is that
+same work.
+
+  2. Basic Permissions.
+
+  All rights granted under this License are granted for the term of
+copyright on the Program, and are irrevocable provided the stated
+conditions are met.  This License explicitly affirms your unlimited
+permission to run the unmodified Program.  The output from running a
+covered work is covered by this License only if the output, given its
+content, constitutes a covered work.  This License acknowledges your
+rights of fair use or other equivalent, as provided by copyright law.
+
+  You may make, run and propagate covered works that you do not
+convey, without conditions so long as your license otherwise remains
+in force.  You may convey covered works to others for the sole purpose
+of having them make modifications exclusively for you, or provide you
+with facilities for running those works, provided that you comply with
+the terms of this License in conveying all material for which you do
+not control copyright.  Those thus making or running the covered works
+for you must do so exclusively on your behalf, under your direction
+and control, on terms that prohibit them from making any copies of
+your copyrighted material outside their relationship with you.
+
+  Conveying under any other circumstances is permitted solely under
+the conditions stated below.  Sublicensing is not allowed; section 10
+makes it unnecessary.
+
+  3. Protecting Users' Legal Rights From Anti-Circumvention Law.
+
+  No covered work shall be deemed part of an effective technological
+measure under any applicable law fulfilling obligations under article
+11 of the WIPO copyright treaty adopted on 20 December 1996, or
+similar laws prohibiting or restricting circumvention of such
+measures.
+
+  When you convey a covered work, you waive any legal power to forbid
+circumvention of technological measures to the extent such circumvention
+is effected by exercising rights under this License with respect to
+the covered work, and you disclaim any intention to limit operation or
+modification of the work as a means of enforcing, against the work's
+users, your or third parties' legal rights to forbid circumvention of
+technological measures.
+
+  4. Conveying Verbatim Copies.
+
+  You may convey verbatim copies of the Program's source code as you
+receive it, in any medium, provided that you conspicuously and
+appropriately publish on each copy an appropriate copyright notice;
+keep intact all notices stating that this License and any
+non-permissive terms added in accord with section 7 apply to the code;
+keep intact all notices of the absence of any warranty; and give all
+recipients a copy of this License along with the Program.
+
+  You may charge any price or no price for each copy that you convey,
+and you may offer support or warranty protection for a fee.
+
+  5. Conveying Modified Source Versions.
+
+  You may convey a work based on the Program, or the modifications to
+produce it from the Program, in the form of source code under the
+terms of section 4, provided that you also meet all of these conditions:
+
+    a) The work must carry prominent notices stating that you modified
+    it, and giving a relevant date.
+
+    b) The work must carry prominent notices stating that it is
+    released under this License and any conditions added under section
+    7.  This requirement modifies the requirement in section 4 to
+    "keep intact all notices".
+
+    c) You must license the entire work, as a whole, under this
+    License to anyone who comes into possession of a copy.  This
+    License will therefore apply, along with any applicable section 7
+    additional terms, to the whole of the work, and all its parts,
+    regardless of how they are packaged.  This License gives no
+    permission to license the work in any other way, but it does not
+    invalidate such permission if you have separately received it.
+
+    d) If the work has interactive user interfaces, each must display
+    Appropriate Legal Notices; however, if the Program has interactive
+    interfaces that do not display Appropriate Legal Notices, your
+    work need not make them do so.
+
+  A compilation of a covered work with other separate and independent
+works, which are not by their nature extensions of the covered work,
+and which are not combined with it such as to form a larger program,
+in or on a volume of a storage or distribution medium, is called an
+"aggregate" if the compilation and its resulting copyright are not
+used to limit the access or legal rights of the compilation's users
+beyond what the individual works permit.  Inclusion of a covered work
+in an aggregate does not cause this License to apply to the other
+parts of the aggregate.
+
+  6. Conveying Non-Source Forms.
+
+  You may convey a covered work in object code form under the terms
+of sections 4 and 5, provided that you also convey the
+machine-readable Corresponding Source under the terms of this License,
+in one of these ways:
+
+    a) Convey the object code in, or embodied in, a physical product
+    (including a physical distribution medium), accompanied by the
+    Corresponding Source fixed on a durable physical medium
+    customarily used for software interchange.
+
+    b) Convey the object code in, or embodied in, a physical product
+    (including a physical distribution medium), accompanied by a
+    written offer, valid for at least three years and valid for as
+    long as you offer spare parts or customer support for that product
+    model, to give anyone who possesses the object code either (1) a
+    copy of the Corresponding Source for all the software in the
+    product that is covered by this License, on a durable physical
+    medium customarily used for software interchange, for a price no
+    more than your reasonable cost of physically performing this
+    conveying of source, or (2) access to copy the
+    Corresponding Source from a network server at no charge.
+
+    c) Convey individual copies of the object code with a copy of the
+    written offer to provide the Corresponding Source.  This
+    alternative is allowed only occasionally and noncommercially, and
+    only if you received the object code with such an offer, in accord
+    with subsection 6b.
+
+    d) Convey the object code by offering access from a designated
+    place (gratis or for a charge), and offer equivalent access to the
+    Corresponding Source in the same way through the same place at no
+    further charge.  You need not require recipients to copy the
+    Corresponding Source along with the object code.  If the place to
+    copy the object code is a network server, the Corresponding Source
+    may be on a different server (operated by you or a third party)
+    that supports equivalent copying facilities, provided you maintain
+    clear directions next to the object code saying where to find the
+    Corresponding Source.  Regardless of what server hosts the
+    Corresponding Source, you remain obligated to ensure that it is
+    available for as long as needed to satisfy these requirements.
+
+    e) Convey the object code using peer-to-peer transmission, provided
+    you inform other peers where the object code and Corresponding
+    Source of the work are being offered to the general public at no
+    charge under subsection 6d.
+
+  A separable portion of the object code, whose source code is excluded
+from the Corresponding Source as a System Library, need not be
+included in conveying the object code work.
+
+  A "User Product" is either (1) a "consumer product", which means any
+tangible personal property which is normally used for personal, family,
+or household purposes, or (2) anything designed or sold for incorporation
+into a dwelling.  In determining whether a product is a consumer product,
+doubtful cases shall be resolved in favor of coverage.  For a particular
+product received by a particular user, "normally used" refers to a
+typical or common use of that class of product, regardless of the status
+of the particular user or of the way in which the particular user
+actually uses, or expects or is expected to use, the product.  A product
+is a consumer product regardless of whether the product has substantial
+commercial, industrial or non-consumer uses, unless such uses represent
+the only significant mode of use of the product.
+
+  "Installation Information" for a User Product means any methods,
+procedures, authorization keys, or other information required to install
+and execute modified versions of a covered work in that User Product from
+a modified version of its Corresponding Source.  The information must
+suffice to ensure that the continued functioning of the modified object
+code is in no case prevented or interfered with solely because
+modification has been made.
+
+  If you convey an object code work under this section in, or with, or
+specifically for use in, a User Product, and the conveying occurs as
+part of a transaction in which the right of possession and use of the
+User Product is transferred to the recipient in perpetuity or for a
+fixed term (regardless of how the transaction is characterized), the
+Corresponding Source conveyed under this section must be accompanied
+by the Installation Information.  But this requirement does not apply
+if neither you nor any third party retains the ability to install
+modified object code on the User Product (for example, the work has
+been installed in ROM).
+
+  The requirement to provide Installation Information does not include a
+requirement to continue to provide support service, warranty, or updates
+for a work that has been modified or installed by the recipient, or for
+the User Product in which it has been modified or installed.  Access to a
+network may be denied when the modification itself materially and
+adversely affects the operation of the network or violates the rules and
+protocols for communication across the network.
+
+  Corresponding Source conveyed, and Installation Information provided,
+in accord with this section must be in a format that is publicly
+documented (and with an implementation available to the public in
+source code form), and must require no special password or key for
+unpacking, reading or copying.
+
+  7. Additional Terms.
+
+  "Additional permissions" are terms that supplement the terms of this
+License by making exceptions from one or more of its conditions.
+Additional permissions that are applicable to the entire Program shall
+be treated as though they were included in this License, to the extent
+that they are valid under applicable law.  If additional permissions
+apply only to part of the Program, that part may be used separately
+under those permissions, but the entire Program remains governed by
+this License without regard to the additional permissions.
+
+  When you convey a copy of a covered work, you may at your option
+remove any additional permissions from that copy, or from any part of
+it.  (Additional permissions may be written to require their own
+removal in certain cases when you modify the work.)  You may place
+additional permissions on material, added by you to a covered work,
+for which you have or can give appropriate copyright permission.
+
+  Notwithstanding any other provision of this License, for material you
+add to a covered work, you may (if authorized by the copyright holders of
+that material) supplement the terms of this License with terms:
+
+    a) Disclaiming warranty or limiting liability differently from the
+    terms of sections 15 and 16 of this License; or
+
+    b) Requiring preservation of specified reasonable legal notices or
+    author attributions in that material or in the Appropriate Legal
+    Notices displayed by works containing it; or
+
+    c) Prohibiting misrepresentation of the origin of that material, or
+    requiring that modified versions of such material be marked in
+    reasonable ways as different from the original version; or
+
+    d) Limiting the use for publicity purposes of names of licensors or
+    authors of the material; or
+
+    e) Declining to grant rights under trademark law for use of some
+    trade names, trademarks, or service marks; or
+
+    f) Requiring indemnification of licensors and authors of that
+    material by anyone who conveys the material (or modified versions of
+    it) with contractual assumptions of liability to the recipient, for
+    any liability that these contractual assumptions directly impose on
+    those licensors and authors.
+
+  All other non-permissive additional terms are considered "further
+restrictions" within the meaning of section 10.  If the Program as you
+received it, or any part of it, contains a notice stating that it is
+governed by this License along with a term that is a further
+restriction, you may remove that term.  If a license document contains
+a further restriction but permits relicensing or conveying under this
+License, you may add to a covered work material governed by the terms
+of that license document, provided that the further restriction does
+not survive such relicensing or conveying.
+
+  If you add terms to a covered work in accord with this section, you
+must place, in the relevant source files, a statement of the
+additional terms that apply to those files, or a notice indicating
+where to find the applicable terms.
+
+  Additional terms, permissive or non-permissive, may be stated in the
+form of a separately written license, or stated as exceptions;
+the above requirements apply either way.
+
+  8. Termination.
+
+  You may not propagate or modify a covered work except as expressly
+provided under this License.  Any attempt otherwise to propagate or
+modify it is void, and will automatically terminate your rights under
+this License (including any patent licenses granted under the third
+paragraph of section 11).
+
+  However, if you cease all violation of this License, then your
+license from a particular copyright holder is reinstated (a)
+provisionally, unless and until the copyright holder explicitly and
+finally terminates your license, and (b) permanently, if the copyright
+holder fails to notify you of the violation by some reasonable means
+prior to 60 days after the cessation.
+
+  Moreover, your license from a particular copyright holder is
+reinstated permanently if the copyright holder notifies you of the
+violation by some reasonable means, this is the first time you have
+received notice of violation of this License (for any work) from that
+copyright holder, and you cure the violation prior to 30 days after
+your receipt of the notice.
+
+  Termination of your rights under this section does not terminate the
+licenses of parties who have received copies or rights from you under
+this License.  If your rights have been terminated and not permanently
+reinstated, you do not qualify to receive new licenses for the same
+material under section 10.
+
+  9. Acceptance Not Required for Having Copies.
+
+  You are not required to accept this License in order to receive or
+run a copy of the Program.  Ancillary propagation of a covered work
+occurring solely as a consequence of using peer-to-peer transmission
+to receive a copy likewise does not require acceptance.  However,
+nothing other than this License grants you permission to propagate or
+modify any covered work.  These actions infringe copyright if you do
+not accept this License.  Therefore, by modifying or propagating a
+covered work, you indicate your acceptance of this License to do so.
+
+  10. Automatic Licensing of Downstream Recipients.
+
+  Each time you convey a covered work, the recipient automatically
+receives a license from the original licensors, to run, modify and
+propagate that work, subject to this License.  You are not responsible
+for enforcing compliance by third parties with this License.
+
+  An "entity transaction" is a transaction transferring control of an
+organization, or substantially all assets of one, or subdividing an
+organization, or merging organizations.  If propagation of a covered
+work results from an entity transaction, each party to that
+transaction who receives a copy of the work also receives whatever
+licenses to the work the party's predecessor in interest had or could
+give under the previous paragraph, plus a right to possession of the
+Corresponding Source of the work from the predecessor in interest, if
+the predecessor has it or can get it with reasonable efforts.
+
+  You may not impose any further restrictions on the exercise of the
+rights granted or affirmed under this License.  For example, you may
+not impose a license fee, royalty, or other charge for exercise of
+rights granted under this License, and you may not initiate litigation
+(including a cross-claim or counterclaim in a lawsuit) alleging that
+any patent claim is infringed by making, using, selling, offering for
+sale, or importing the Program or any portion of it.
+
+  11. Patents.
+
+  A "contributor" is a copyright holder who authorizes use under this
+License of the Program or a work on which the Program is based.  The
+work thus licensed is called the contributor's "contributor version".
+
+  A contributor's "essential patent claims" are all patent claims
+owned or controlled by the contributor, whether already acquired or
+hereafter acquired, that would be infringed by some manner, permitted
+by this License, of making, using, or selling its contributor version,
+but do not include claims that would be infringed only as a
+consequence of further modification of the contributor version.  For
+purposes of this definition, "control" includes the right to grant
+patent sublicenses in a manner consistent with the requirements of
+this License.
+
+  Each contributor grants you a non-exclusive, worldwide, royalty-free
+patent license under the contributor's essential patent claims, to
+make, use, sell, offer for sale, import and otherwise run, modify and
+propagate the contents of its contributor version.
+
+  In the following three paragraphs, a "patent license" is any express
+agreement or commitment, however denominated, not to enforce a patent
+(such as an express permission to practice a patent or covenant not to
+sue for patent infringement).  To "grant" such a patent license to a
+party means to make such an agreement or commitment not to enforce a
+patent against the party.
+
+  If you convey a covered work, knowingly relying on a patent license,
+and the Corresponding Source of the work is not available for anyone
+to copy, free of charge and under the terms of this License, through a
+publicly available network server or other readily accessible means,
+then you must either (1) cause the Corresponding Source to be so
+available, or (2) arrange to deprive yourself of the benefit of the
+patent license for this particular work, or (3) arrange, in a manner
+consistent with the requirements of this License, to extend the patent
+license to downstream recipients.  "Knowingly relying" means you have
+actual knowledge that, but for the patent license, your conveying the
+covered work in a country, or your recipient's use of the covered work
+in a country, would infringe one or more identifiable patents in that
+country that you have reason to believe are valid.
+
+  If, pursuant to or in connection with a single transaction or
+arrangement, you convey, or propagate by procuring conveyance of, a
+covered work, and grant a patent license to some of the parties
+receiving the covered work authorizing them to use, propagate, modify
+or convey a specific copy of the covered work, then the patent license
+you grant is automatically extended to all recipients of the covered
+work and works based on it.
+
+  A patent license is "discriminatory" if it does not include within
+the scope of its coverage, prohibits the exercise of, or is
+conditioned on the non-exercise of one or more of the rights that are
+specifically granted under this License.  You may not convey a covered
+work if you are a party to an arrangement with a third party that is
+in the business of distributing software, under which you make payment
+to the third party based on the extent of your activity of conveying
+the work, and under which the third party grants, to any of the
+parties who would receive the covered work from you, a discriminatory
+patent license (a) in connection with copies of the covered work
+conveyed by you (or copies made from those copies), or (b) primarily
+for and in connection with specific products or compilations that
+contain the covered work, unless you entered into that arrangement,
+or that patent license was granted, prior to 28 March 2007.
+
+  Nothing in this License shall be construed as excluding or limiting
+any implied license or other defenses to infringement that may
+otherwise be available to you under applicable patent law.
+
+  12. No Surrender of Others' Freedom.
+
+  If conditions are imposed on you (whether by court order, agreement or
+otherwise) that contradict the conditions of this License, they do not
+excuse you from the conditions of this License.  If you cannot convey a
+covered work so as to satisfy simultaneously your obligations under this
+License and any other pertinent obligations, then as a consequence you may
+not convey it at all.  For example, if you agree to terms that obligate you
+to collect a royalty for further conveying from those to whom you convey
+the Program, the only way you could satisfy both those terms and this
+License would be to refrain entirely from conveying the Program.
+
+  13. Use with the GNU Affero General Public License.
+
+  Notwithstanding any other provision of this License, you have
+permission to link or combine any covered work with a work licensed
+under version 3 of the GNU Affero General Public License into a single
+combined work, and to convey the resulting work.  The terms of this
+License will continue to apply to the part which is the covered work,
+but the special requirements of the GNU Affero General Public License,
+section 13, concerning interaction through a network will apply to the
+combination as such.
+
+  14. Revised Versions of this License.
+
+  The Free Software Foundation may publish revised and/or new versions of
+the GNU General Public License from time to time.  Such new versions will
+be similar in spirit to the present version, but may differ in detail to
+address new problems or concerns.
+
+  Each version is given a distinguishing version number.  If the
+Program specifies that a certain numbered version of the GNU General
+Public License "or any later version" applies to it, you have the
+option of following the terms and conditions either of that numbered
+version or of any later version published by the Free Software
+Foundation.  If the Program does not specify a version number of the
+GNU General Public License, you may choose any version ever published
+by the Free Software Foundation.
+
+  If the Program specifies that a proxy can decide which future
+versions of the GNU General Public License can be used, that proxy's
+public statement of acceptance of a version permanently authorizes you
+to choose that version for the Program.
+
+  Later license versions may give you additional or different
+permissions.  However, no additional obligations are imposed on any
+author or copyright holder as a result of your choosing to follow a
+later version.
+
+  15. Disclaimer of Warranty.
+
+  THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY
+APPLICABLE LAW.  EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT
+HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY
+OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO,
+THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+PURPOSE.  THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM
+IS WITH YOU.  SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF
+ALL NECESSARY SERVICING, REPAIR OR CORRECTION.
+
+  16. Limitation of Liability.
+
+  IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
+WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS
+THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY
+GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE
+USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF
+DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD
+PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS),
+EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF
+SUCH DAMAGES.
+
+  17. Interpretation of Sections 15 and 16.
+
+  If the disclaimer of warranty and limitation of liability provided
+above cannot be given local legal effect according to their terms,
+reviewing courts shall apply local law that most closely approximates
+an absolute waiver of all civil liability in connection with the
+Program, unless a warranty or assumption of liability accompanies a
+copy of the Program in return for a fee.
+
+                     END OF TERMS AND CONDITIONS
+
+            How to Apply These Terms to Your New Programs
+
+  If you develop a new program, and you want it to be of the greatest
+possible use to the public, the best way to achieve this is to make it
+free software which everyone can redistribute and change under these terms.
+
+  To do so, attach the following notices to the program.  It is safest
+to attach them to the start of each source file to most effectively
+state the exclusion of warranty; and each file should have at least
+the "copyright" line and a pointer to where the full notice is found.
+
+    <one line to give the program's name and a brief idea of what it does.>
+    Copyright (C) <year>  <name of author>
+
+    This program is free software: you can redistribute it and/or modify
+    it under the terms of the GNU General Public License as published by
+    the Free Software Foundation, either version 3 of the License, or
+    (at your option) any later version.
+
+    This program is distributed in the hope that it will be useful,
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+    GNU General Public License for more details.
+
+    You should have received a copy of the GNU General Public License
+    along with this program.  If not, see <http://www.gnu.org/licenses/>.
+
+Also add information on how to contact you by electronic and paper mail.
+
+  If the program does terminal interaction, make it output a short
+notice like this when it starts in an interactive mode:
+
+    <program>  Copyright (C) <year>  <name of author>
+    This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
+    This is free software, and you are welcome to redistribute it
+    under certain conditions; type `show c' for details.
+
+The hypothetical commands `show w' and `show c' should show the appropriate
+parts of the General Public License.  Of course, your program's commands
+might be different; for a GUI interface, you would use an "about box".
+
+  You should also get your employer (if you work as a programmer) or school,
+if any, to sign a "copyright disclaimer" for the program, if necessary.
+For more information on this, and how to apply and follow the GNU GPL, see
+<http://www.gnu.org/licenses/>.
+
+  The GNU General Public License does not permit incorporating your program
+into proprietary programs.  If your program is a subroutine library, you
+may consider it more useful to permit linking proprietary applications with
+the library.  If this is what you want to do, use the GNU Lesser General
+Public License instead of this License.  But first, please read
+<http://www.gnu.org/philosophy/why-not-lgpl.html>.
diff --git a/main.c b/main.c
index 62b27d1a469784f0cb2fb344cdbe4ba451c64af2..fd0828d77be179f5e0f43f65a0b81d6416ae6074 100644 (file)
--- a/main.c
+++ b/main.c
-// SilentTimer Action Figure software, v1.4\r
-// main.c\r
-// The main c file.\r
-// \r
-// Copyright (C) 2014-2016  Balthasar Szczepański\r
-// \r
-// This program is free software: you can redistribute it and/or modify\r
-// it under the terms of the GNU General Public License as published by\r
-// the Free Software Foundation, either version 3 of the License, or\r
-// (at your option) any later version.\r
-// \r
-// This program is distributed in the hope that it will be useful,\r
-// but WITHOUT ANY WARRANTY; without even the implied warranty of\r
-// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
-// GNU General Public License for more details.\r
-// \r
-// You should have received a copy of the GNU General Public License\r
-// along with this program.  If not, see <http://www.gnu.org/licenses/>.\r
-\r
-       #include "msp430.h"\r
-       #include "definitions.h"\r
-\r
-       unsigned char inbuf[8];\r
-       unsigned char outbuf[8];\r
-       unsigned char dispvalue[4];\r
-       unsigned char time[8];\r
-       unsigned char leap;\r
-       unsigned char settime[3];\r
-       unsigned char mode;\r
-       unsigned char alarm[3];\r
-       unsigned char lastcdn[2];\r
-       unsigned char lastyear[5];\r
-       unsigned char digit;\r
-       unsigned char changed;\r
-       unsigned char run;\r
-       unsigned char symbol[16] ={\r
-               SYMB_0,\r
-               SYMB_1,\r
-               SYMB_2,\r
-               SYMB_3,\r
-               SYMB_4,\r
-               SYMB_5,\r
-               SYMB_6,\r
-               SYMB_7,\r
-               SYMB_8,\r
-               SYMB_9,\r
-               SYMB_A,\r
-               SYMB_B,\r
-               SYMB_C,\r
-               SYMB_D,\r
-               SYMB_E,\r
-               SYMB_F};\r
-\r
-       __interrupt void P2_ISR();\r
-       void display(\r
-               unsigned char *data);\r
-       void spi(\r
-               unsigned char command,\r
-               unsigned char addr,\r
-               unsigned char *inbuf,\r
-               unsigned char *outbuf,\r
-               unsigned short n);\r
-       int main( void );\r
-\r
-       void display(unsigned char *data)\r
-       {\r
-               //lsd\r
-               P1OUT =data[0];\r
-               P3OUT |=0x80; //74hc373 latch enable\r
-               P3OUT &=0x0f;\r
-               \r
-               P1OUT =data[1];\r
-               P3OUT |=0x40;\r
-               P3OUT &=0x0f;\r
-               \r
-               P1OUT =data[2];\r
-               P3OUT |=0x20;\r
-               P3OUT &=0x0f;\r
-               \r
-               //msd\r
-               P1OUT =data[3];\r
-               P3OUT |=0x10;\r
-               P3OUT &=0x0f;\r
-       }\r
-\r
-       void spi(\r
-               unsigned char command,\r
-               unsigned char addr,\r
-               unsigned char *inbuf,\r
-               unsigned char *outbuf,\r
-               unsigned short n)\r
-       {\r
-               unsigned short i;\r
-               unsigned char nul;\r
-               unsigned char flags;\r
-               \r
-               switch (command)\r
-               {\r
-               case RTC_EEREAD:\r
-               case RTC_IDREAD:\r
-               case RTC_READ:\r
-                       flags= ADDRFLAG|READFLAG;\r
-                       break;\r
-                       \r
-               case RTC_EEWRITE:\r
-               case RTC_IDWRITE:\r
-                       n=(n>8)?8:n;\r
-               case RTC_WRITE:\r
-                       flags= ADDRFLAG|WRITEFLAG;\r
-                       break;\r
-                       \r
-               case RTC_EEWREN:\r
-               case RTC_EEWRDI:\r
-                       flags=0;\r
-                       break;\r
-                       \r
-               case RTC_SRREAD:\r
-                       n=(n>1)?1:n;\r
-                       flags=READFLAG;\r
-                       break;\r
-               \r
-               case RTC_SRWRITE: //1\r
-               case RTC_CLRRAM: //data ignored but has to be\r
-               case RTC_UNLOCK: //1\r
-                        n=(n>1)?1:n;\r
-                       flags=WRITEFLAG;\r
-                       break;\r
-               default:\r
-                       return;\r
-               }\r
-               \r
-               P3OUT&=0xfe; //chip select down\r
-                \r
-               while((IFG2&UTXIFG0)==0); // wait until can be sent\r
-               U0TXBUF=command; // send command\r
-               while((IFG2&UTXIFG0)==0); // wait until received\r
-               nul=U0RXBUF; //pretend to read "data"\r
-               \r
-               if(flags&ADDRFLAG)\r
-               {\r
-                       while((IFG2&UTXIFG0)==0); // wait until can be sent\r
-                       U0TXBUF=addr; // send address\r
-                       while((IFG2&UTXIFG0)==0); // wait until received\r
-                       nul=U0RXBUF; //pretend to read "data"\r
-               }\r
-               if(flags&(READFLAG|WRITEFLAG))\r
-               {\r
-                       for(i=0;i<n;++i)\r
-                       {\r
-                               while((IFG2&UTXIFG0)==0); // wait until can be sent\r
-                               U0TXBUF=(flags&WRITEFLAG)?outbuf[i]:0xff; // send data\r
-                               while((IFG2&UTXIFG0)==0); // wait until received\r
-                               if(flags&READFLAG)\r
-                                       inbuf[i]=U0RXBUF; //read data\r
-                               else\r
-                                       nul=U0RXBUF;\r
-                       }\r
-               }\r
-               P3OUT|=0x01; //chip select up\r
-               for(i=0;i<255;++i); //looks like the RTC might not like another SPI transmission exactly before the previous one, sometimes.\r
-       }\r
-\r
-       #pragma vector=PORT2_VECTOR\r
-       __interrupt void P2_ISR()\r
-       {\r
-               if(P2IFG&0x08) // RTC\r
-               {\r
-                       P2IFG &= ~0x08;\r
-                       spi(RTC_READ,0x00,inbuf,outbuf,8);\r
-               \r
-                       time[0]=inbuf[0]; //1/100 s\r
-                       time[1]=inbuf[1]&0x7f; //seconds\r
-                       time[2]=inbuf[2]&0x7f; //minutes\r
-                       time[3]=inbuf[3]&0x3f; //hours\r
-                       time[4]=inbuf[4]&0x07; //dow\r
-                       time[5]=inbuf[5]&0x3f; //dom\r
-                       time[6]=inbuf[6]&0x1f; //mon\r
-                       time[7]=inbuf[7];//year\r
-                       leap=inbuf[6]&0x20;\r
-                       \r
-                       spi(RTC_READ,0x0f,inbuf,outbuf,1); // <- because of RTC bug (probably not needed anymore?)\r
-                       if(!((time[2]^alarm[1])|(time[3]^alarm[2])) && alarm[0])\r
-                       {\r
-                               mode=MODE_ALARM;\r
-                               alarm[0]=0; //alarm removed after activated\r
-                               spi(RTC_WRITE,0x20,inbuf,alarm,1);\r
-                       }\r
-                       \r
-                       if(mode==MODE_COUNTUP && run)\r
-                       {\r
-                               if(settime[1]<0x59)\r
-                                       settime[1]=bcd_add_byte(settime[1],0x01);\r
-                               else\r
-                               {\r
-                                       settime[1]=0x00;\r
-                                       settime[2]=bcd_add_byte(settime[2],0x01);\r
-                               }\r
-                       }\r
-                       else if(mode==MODE_COUNTDOWN && run && (settime[1]|settime[2]))\r
-                       {\r
-                               if(settime[1])\r
-                                       settime[1]=bcd_add_byte(settime[1],0x99);\r
-                               else\r
-                               {\r
-                                       settime[1]=0x59;\r
-                                       settime[2]=bcd_add_byte(settime[2],0x99);\r
-                               }\r
-                       }\r
-                       \r
-                       if (mode==MODE_ALARM || (mode==MODE_COUNTDOWN && !(settime[1]|settime[2])))\r
-                       {\r
-                               TACTL^=MC_1;\r
-                               //P2DIR^=0x40;\r
-                               //P2SEL^=0x40;\r
-                       }\r
-                       else if(TACTL & MC_3)\r
-                       {\r
-                               TACTL&= ~MC_3;\r
-                               //P2DIR&= ~0x40;\r
-                               //P2SEL&= ~0x40;\r
-                       }\r
-                       \r
-                       lastyear[4]=(time[7]==0x00 && lastyear[0]==0x99)?bcd_add_byte(lastyear[1],0x01):lastyear[1];\r
-                       if (time[7]!=lastyear[0])\r
-                       {\r
-                               if(time[7]==lastyear[2])\r
-                                       ++(lastyear[3]);\r
-                               else\r
-                                       lastyear[3]=0;\r
-                               \r
-                               lastyear[2]=time[7];\r
-                               \r
-                               if(lastyear[3]>55)\r
-                               {\r
-                                       lastyear[3]=0;\r
-                                       \r
-                                       if(time[7]==0x00 && lastyear[0]==0x99)\r
-                                               lastyear[1]=bcd_add_byte(lastyear[1],0x01);\r
-                                       \r
-                                       lastyear[0]=time[7];\r
-                                       \r
-                                       outbuf[0]=ALARMVALUE;\r
-                                       outbuf[1]=lastyear[0];\r
-                                       outbuf[2]=lastyear[1];\r
-                                       spi(RTC_WRITE,0x26,inbuf,outbuf,3);\r
-                               }      \r
-                       }\r
-                       \r
-                       \r
-                       if(mode==MODE_DEBUG && changed)\r
-                               spi(RTC_READ,settime[1],settime,outbuf,1);\r
-               }\r
-               else if(P2IFG&0x02) // button A\r
-               {\r
-                       P2IFG &= ~0x02;\r
-                       switch(mode)\r
-                       {\r
-                       case MODE_TIME:\r
-                       case MODE_SETDATE:\r
-                       case MODE_SETYEAR:\r
-                               mode=MODE_DATE;\r
-                               break;\r
-                       case MODE_DATE:\r
-                               mode=MODE_COUNTUP;\r
-                               settime[1]=0x00;\r
-                               settime[2]=0x00;\r
-                               run=0;\r
-                               break;\r
-                       case MODE_COUNTUP:\r
-                               if(run)\r
-                               {\r
-                                       settime[1]=0x00;\r
-                                       settime[2]=0x00;\r
-                                       run=0;\r
-                               }\r
-                               else\r
-                               {\r
-                                       mode=MODE_SETCOUNTDOWN;\r
-                                       settime[1]=lastcdn[0];\r
-                                       settime[2]=lastcdn[1];\r
-                                       changed=0;\r
-                                       digit=3;\r
-                               }\r
-                               break;\r
-                       case MODE_DEBUGADDR:\r
-                       case MODE_DEBUGVAL:\r
-                               mode=MODE_DEBUG;\r
-                               break;\r
-                       case MODE_SETCOUNTDOWN:\r
-                       case MODE_COUNTDOWN:\r
-                               if(changed || mode==MODE_COUNTDOWN)\r
-                               {\r
-                                       mode=MODE_SETCOUNTDOWN;\r
-                                       settime[1]=lastcdn[0];\r
-                                       settime[2]=lastcdn[1];\r
-                                       changed=0;\r
-                                       digit=3;\r
-                                       break;\r
-                               }\r
-                       //case MODE_SETTIME:\r
-                       //case MODE_SETALARM:\r
-                       //case MODE_ALARM:\r
-                       //case MODE_CALIBRATE:\r
-                       //case MODE_DEBUG:\r
-                       default:\r
-                               mode=MODE_TIME;\r
-                               break;\r
-                       }\r
-               }\r
-               else if(P2IFG&0x01) // button B\r
-               {\r
-                       P2IFG &= ~0x01;\r
-                       \r
-                       switch(mode)\r
-                       {\r
-                       case MODE_TIME:\r
-                               mode=MODE_SETTIME;\r
-                               digit=3;\r
-                               changed=0;\r
-                               settime[0]=0;\r
-                               settime[1]=time[2];\r
-                               settime[2]=time[3];\r
-                               break;\r
-                       case MODE_DATE:\r
-                               mode=MODE_SETDATE;\r
-                               digit=2;\r
-                               changed=0;\r
-                               settime[0]=time[5];\r
-                               settime[1]=time[6];\r
-                               break;\r
-                       case MODE_COUNTUP:\r
-                       case MODE_COUNTDOWN:\r
-                               run=!run;\r
-                               break;\r
-                       case MODE_SETCOUNTDOWN:\r
-                               if(digit)\r
-                               {\r
-                                       --digit;\r
-                                       changed=1;\r
-                               }\r
-                               else\r
-                               {\r
-                                       lastcdn[0]=settime[1];\r
-                                       lastcdn[1]=settime[2];\r
-                                       \r
-                                       outbuf[0]=ALARMVALUE;\r
-                                       outbuf[1]=settime[1];\r
-                                       outbuf[2]=settime[2];\r
-                                       spi(RTC_WRITE,0x23,inbuf,outbuf,3);\r
-\r
-                                       mode=MODE_COUNTDOWN;\r
-                                       run=0;\r
-                               }\r
-                               break;\r
-                       case MODE_CALIBRATE:\r
-                               if(digit)\r
-                                       --digit;\r
-                               else\r
-                               {\r
-                                       spi(RTC_WRITE,0x09,inbuf,settime,1);\r
-                                       outbuf[0]=time[3]|settime[1];\r
-                                       spi(RTC_WRITE,0x03,inbuf,outbuf,1);\r
-                                       digit=3;\r
-                               }\r
-                               break;\r
-                       case MODE_DEBUGADDR:\r
-                               if(digit)\r
-                                       --digit;\r
-                               else\r
-                               {\r
-                                       spi(RTC_READ,settime[1],settime,outbuf,1);\r
-                                       mode=MODE_DEBUG;\r
-                                       changed=1;\r
-                               }\r
-                               break;\r
-                       case MODE_DEBUGVAL:\r
-                               if(digit)\r
-                                       --digit;\r
-                               else\r
-                               {\r
-                                       spi(RTC_WRITE,settime[1],inbuf,settime,1);\r
-                                       mode=MODE_DEBUG;\r
-                               }\r
-                               break;\r
-                       case MODE_SETTIME:\r
-                               if(digit)\r
-                               {\r
-                                       if(digit==3&&settime[2]>0x23)\r
-                                                settime[2]&=0xf0;\r
-                                       --digit;\r
-                               }\r
-                               else\r
-                               {\r
-                                       if(changed)\r
-                                       {\r
-                                               outbuf[0]=0x00;\r
-                                               outbuf[1]=0x00;\r
-                                               outbuf[2]=settime[1]&0x7f;\r
-                                               outbuf[3]=settime[2]&0x3f;\r
-                                               \r
-                                               spi(RTC_WRITE,0x01,inbuf,outbuf,1);//stop osc.\r
-                                               spi(RTC_READ,0x03,inbuf,outbuf,1);\r
-                                               outbuf[3]|=(inbuf[0]&0x80);//keep the calsgn bit.\r
-                                       \r
-                                               spi(RTC_WRITE,0x00,inbuf,outbuf,4);//write new time\r
-                                       \r
-                                               outbuf[0]=0x80;\r
-                                               spi(RTC_WRITE,0x01,inbuf,outbuf,1);//start osc;\r
-                                               \r
-                                               time[1]=0;\r
-                                               time[2]=settime[1];\r
-                                               time[3]=settime[2];\r
-                                       }\r
-                                       mode=MODE_TIME;\r
-                               }\r
-                               break;\r
-                       case MODE_SETYEAR:\r
-                               if(digit)\r
-                                       --digit;\r
-                               else\r
-                               {\r
-                                       if(changed)\r
-                                       {\r
-                                               time[7]=settime[1];\r
-                                               spi(RTC_WRITE,0x07,inbuf,settime+1,1);\r
-                                               \r
-                                               lastyear[0]=settime[1];\r
-                                               lastyear[1]=settime[2];\r
-                                               settime[0]=ALARMVALUE;\r
-                                               \r
-                                               spi(RTC_WRITE,0x26,inbuf,settime,3);\r
-                                       }\r
-                                       mode=MODE_DATE; \r
-                               }\r
-                               break;\r
-                       case MODE_SETDATE:\r
-                               if(digit)\r
-                               {\r
-                                       --digit;\r
-                                       if(settime[0]>((settime[1]==0x02)?(leap?0x29:0x28):(((settime[1]&0x18) && !(settime[1]&0x01) || !(settime[1]&0x18) && (settime[1]&0x01))?0x31:0x30)))\r
-                                               settime[0]&=(digit?0x00:0xf0);\r
-                                       if(settime[0]==0x00)\r
-                                               settime[0]=0x01;\r
-                               }\r
-                               else\r
-                               {\r
-                                       if (changed)\r
-                                       {\r
-                                               time[5]=settime[0];\r
-                                               time[6]=settime[1];\r
-                                               settime[2]=time[7];\r
-                                               spi(RTC_WRITE,0x06,inbuf,settime+1,2);\r
-                                               spi(RTC_WRITE,0x05,inbuf,settime,1);\r
-                                       }\r
-                                       mode=MODE_DATE;\r
-                               }\r
-                               \r
-                               break;\r
-                       case MODE_SETALARM:\r
-                               if(digit)\r
-                               {\r
-                                       if(digit==3&&settime[2]>0x23)\r
-                                                settime[2]&=0xf0;\r
-                                       --digit;\r
-                               }\r
-                               else\r
-                               {\r
-                                       if(changed|| !alarm[0])\r
-                                       {\r
-                                               alarm[0]=ALARMVALUE;\r
-                                               alarm[1]=settime[1];\r
-                                               alarm[2]=settime[2];\r
-                                               \r
-                                               spi(RTC_WRITE,0x20,inbuf,alarm,3);\r
-                                       }\r
-                                       else\r
-                                       {\r
-                                               alarm[0]=0;\r
-                                               spi(RTC_WRITE,0x20,inbuf,outbuf,1);\r
-                                       }\r
-                                       mode=MODE_TIME;\r
-                               }\r
-                               break;\r
-                       case MODE_ALARM:\r
-                               alarm[0]=0;\r
-                               spi(RTC_WRITE,0x20,inbuf,outbuf,1);\r
-                               mode=MODE_TIME;\r
-                               break;\r
-                       case MODE_DEBUG:\r
-                               //changed=1;\r
-                               digit=1;\r
-                               mode=MODE_DEBUGADDR;\r
-                               break;  \r
-                       default:\r
-                               break;\r
-                       }\r
-               }\r
-               else if(P2IFG&0x20) // button C\r
-               {\r
-                       P2IFG &= ~0x20;\r
-                       \r
-                       switch(mode)\r
-                       {\r
-                       case MODE_COUNTUP:\r
-                               settime[1]=0x00;\r
-                               settime[2]=0x00;\r
-                               break;\r
-                       case MODE_COUNTDOWN:\r
-                               settime[1]=lastcdn[0];\r
-                               settime[2]=lastcdn[1];\r
-                               changed=0;\r
-                               mode=MODE_SETCOUNTDOWN;\r
-                               digit=3;\r
-                               break;\r
-                       case MODE_CALIBRATE:\r
-                               if(digit==2)\r
-                                       settime[1]^=0x80;\r
-                               if(digit>=2)\r
-                                       break;\r
-                       case MODE_DEBUGADDR:\r
-                       case MODE_DEBUGVAL:\r
-                               settime[(mode==MODE_DEBUGADDR)?1:0]+=(digit==1?0x10:(((settime[(mode==MODE_DEBUGADDR)?1:0]&0x0f)==0x0f)?0xf1:0x01));\r
-                               break;\r
-                       case MODE_SETTIME:\r
-                       case MODE_SETALARM:\r
-                       case MODE_SETYEAR:\r
-                       case MODE_SETCOUNTDOWN:\r
-                               changed=1;\r
-                               //these 4 lines replaced a larger switch structure\r
-                               if((settime[digit&0x02?2:1]&((digit&0x01)?0xf0:0x0f))<(digit==1?((mode==MODE_SETYEAR)?0x90:0x50):(digit==3?((mode==MODE_SETCOUNTDOWN||mode==MODE_SETYEAR)?0x90:0x20):((mode!=MODE_SETCOUNTDOWN&&mode!=MODE_SETYEAR&&digit==2&&settime[2]>0x19)?0x03:0x09))))\r
-                                       settime[digit&0x02?2:1]+=((digit&0x01)?0x10:0x01);\r
-                               else\r
-                                       settime[digit&0x02?2:1]&=((digit&0x01)?0x0f:0xf0);\r
-                               break;\r
-                       case MODE_SETDATE:\r
-                               changed=1;\r
-                               switch (digit)\r
-                               {\r
-                               case 2:\r
-                                       if(settime[1]<0x12)\r
-                                               settime[1]=bcd_add_byte(settime[1],0x01);\r
-                                       else\r
-                                               settime[1]=0x01;\r
-                                       break;\r
-                               case 1:\r
-                                       if((settime[0]&0xf0)<((settime[1]==0x02)?0x20:0x30))\r
-                                               settime[0]+=0x10;\r
-                                       else\r
-                                               settime[0]&=0x0f;\r
-                                       break;\r
-                                       \r
-                               case 0:\r
-                                       if((settime[0]<((settime[1]==0x02)?(leap?0x29:0x28):(((settime[1]&0x18) && !(settime[1]&0x01) || !(settime[1]&0x18) && (settime[1]&0x01))?0x31:0x30))) && (settime[0]&0x0f)<0x09)\r
-                                               ++settime[0];\r
-                                       else\r
-                                               settime[0]&=0xf0;\r
-                                       if(settime[0]==0x00)\r
-                                               settime[0]=0x01;\r
-                                       break;\r
-                               }\r
-                               break;\r
-                       case MODE_DATE:\r
-                               mode=MODE_SETYEAR;\r
-                               digit=3;\r
-                               changed=0;\r
-                               \r
-                               settime[1]=time[7];\r
-                               settime[2]=lastyear[1];\r
-                               break;\r
-                       case MODE_TIME:\r
-                               mode=MODE_SETALARM;\r
-                               digit=3;\r
-                               changed=0;\r
-                               \r
-                               settime[0]=0;\r
-                               settime[1]=alarm[1]; //get last alarm time\r
-                               if(settime[1]>0x59 || (settime[1]&0x0f)>0x09)\r
-                                       settime[1]=0x00;\r
-                               settime[2]=alarm[2];\r
-                               if(settime[2]>0x23 || (settime[2]&0x0f)>0x09)\r
-                                       settime[2]=0x00;\r
-                               break;\r
-                       case MODE_ALARM:\r
-                               alarm[0]=0;\r
-                               spi(RTC_WRITE,0x20,inbuf,outbuf,1);\r
-                               mode=MODE_TIME;\r
-                               break;\r
-                       case MODE_DEBUG:\r
-                               if(changed)\r
-                               {\r
-                                       digit=1;\r
-                                       mode=MODE_DEBUGVAL;\r
-                               }\r
-                               break;\r
-                       default:\r
-                               break;\r
-                       }\r
-               }\r
-               else\r
-                       P2IFG=0;\r
-               \r
-               switch(mode)\r
-               {\r
-               case MODE_SETTIME:\r
-               case MODE_SETALARM:\r
-               case MODE_SETCOUNTDOWN:\r
-               case MODE_SETYEAR:\r
-                       dispvalue[0]=(symbol[settime[1]&0x0f]      | ((digit!=0 && (time[1]&0x01))?SYMB_NUL:SYMB_8)) & ((((((time[1]+0x01)&0x02)^((time[1]&0x10)>>3))&&mode==MODE_SETCOUNTDOWN)||mode==MODE_SETYEAR)?SYMB_NUL:SYMB_DOT);\r
-                       dispvalue[1]=(symbol[(settime[1]&0xf0)>>4] | ((digit!=1 && (time[1]&0x01))?SYMB_NUL:SYMB_8)) & (((((time[1]&0x02)^((time[1]&0x10)>>3))&&mode==MODE_SETCOUNTDOWN)||mode==MODE_SETYEAR)?SYMB_NUL:SYMB_DOT);\r
-                       dispvalue[2]=(symbol[settime[2]&0x0f]      | ((digit!=2 && (time[1]&0x01))?SYMB_NUL:SYMB_8)) ;\r
-                       dispvalue[3]=(symbol[(settime[2]&0xf0)>>4] | ((digit!=3 && (time[1]&0x01))?SYMB_NUL:SYMB_8)) & ((mode!=MODE_SETALARM && alarm[0] || mode==MODE_SETALARM && !(time[1]&0x01))?SYMB_DOT:SYMB_NUL);\r
-                       break;\r
-               case MODE_DEBUGADDR:\r
-               case MODE_DEBUGVAL:\r
-                       dispvalue[0]=(symbol[settime[(mode==MODE_DEBUGADDR)?1:0]&0x0f]      | ((digit!=0 && (time[1]&0x01))?SYMB_NUL:SYMB_8)) &SYMB_DOT;\r
-                       dispvalue[1]=(symbol[(settime[(mode==MODE_DEBUGADDR)?1:0]&0xf0)>>4] | ((digit!=1 && (time[1]&0x01))?SYMB_NUL:SYMB_8)) &SYMB_DOT;\r
-                       dispvalue[2]=(mode==MODE_DEBUGADDR)?SYMB_A:SYMB_NUL;   \r
-                       dispvalue[3]=alarm[0]?SYMB_DOT:SYMB_NUL;\r
-                       break;\r
-               case MODE_SETDATE:\r
-                       dispvalue[0]=(symbol[settime[1]&0x0f]|((digit!=2 && (time[1]&0x01))?SYMB_NUL:SYMB_8));\r
-                       dispvalue[1]=(symbol[(settime[1]&0xf0)>>4]|((digit!=2 && (time[1]&0x01))?SYMB_NUL:SYMB_8))&SYMB_DOT;\r
-                       dispvalue[2]=(symbol[settime[0]&0x0f]|((digit!=0 && (time[1]&0x01))?SYMB_NUL:SYMB_8));\r
-                       dispvalue[3]=(symbol[(settime[0]&0xf0)>>4]|((digit!=1 && (time[1]&0x01))?SYMB_NUL:SYMB_8))&(alarm[0]?SYMB_DOT:SYMB_NUL);\r
-                       break;\r
-               case MODE_CALIBRATE:\r
-                       dispvalue[0]=symbol[settime[0]&0x0f]|((digit!=0 && digit!=3 && (time[1]&0x01))?SYMB_NUL:SYMB_8);\r
-                       dispvalue[1]=symbol[(settime[0]&0xf0)>>4]|((digit!=1 && digit!=3 && (time[1]&0x01))?SYMB_NUL:SYMB_8);\r
-                       dispvalue[2]=(settime[1]?SYMB_MIN:SYMB_NUL)|((digit!=2 && digit!=3 && (time[1]&0x01))?SYMB_NUL:SYMB_8);\r
-                       dispvalue[3]=alarm[0]?SYMB_DOT:SYMB_NUL;\r
-                       break;\r
-               case MODE_DATE:\r
-                       if((time[1]&0x03)==((time[1]&0x10)?0x01:0x03))\r
-                       {\r
-                               dispvalue[0]=symbol[time[7]&0x0f];\r
-                               dispvalue[1]=symbol[(time[7]&0xf0)>>4];\r
-                               dispvalue[2]=symbol[lastyear[4]&0x0f];\r
-                               dispvalue[3]=symbol[(lastyear[4]&0xf0)>>4]&(alarm[0]?SYMB_DOT:SYMB_NUL);\r
-                       }\r
-                       else\r
-                       {\r
-                               dispvalue[0]=symbol[time[6]&0x0f];\r
-                               dispvalue[1]=symbol[(time[6]&0xf0)>>4]&SYMB_DOT;\r
-                               dispvalue[2]=symbol[time[5]&0x0f];\r
-                               dispvalue[3]=((time[5]&0xf0)?symbol[(time[5]&0xf0)>>4]:SYMB_NUL)&(alarm[0]?SYMB_DOT:SYMB_NUL);\r
-                       }\r
-                       break;\r
-               case MODE_COUNTDOWN:\r
-                       if(!(settime[1]|settime[2]))\r
-                       {\r
-                               if(!run)\r
-                               {\r
-                                       dispvalue[0]=SYMB_g&SYMB_DOT;\r
-                                       dispvalue[1]=SYMB_n&SYMB_DOT;\r
-                                       dispvalue[2]=SYMB_o;\r
-                                       dispvalue[3]=alarm[0]?SYMB_DOT:SYMB_NUL;\r
-                                       break;\r
-                               }\r
-                               run=0;\r
-                       }\r
-               case MODE_COUNTUP:\r
-                       dispvalue[0]=symbol[settime[1]&0x0f]&SYMB_DOT;\r
-                       dispvalue[1]=symbol[(settime[1]&0xf0)>>4]&SYMB_DOT;\r
-                       dispvalue[2]=symbol[settime[2]&0x0f];\r
-                       dispvalue[3]=((settime[2]&0xf0)?symbol[(settime[2]&0xf0)>>4]:SYMB_NUL)&(alarm[0]?SYMB_DOT:SYMB_NUL);\r
-                       break;\r
-               case MODE_DEBUG:\r
-                       if(changed)\r
-                       {\r
-                               dispvalue[0]=symbol[settime[0]&0x0f]&SYMB_DOT;\r
-                               dispvalue[1]=symbol[(settime[0]&0xf0)>>4]&SYMB_DOT;\r
-                               dispvalue[2]=symbol[settime[1]&0x0f];\r
-                               dispvalue[3]=symbol[(settime[1]&0xf0)>>4]&(alarm[0]?SYMB_DOT:SYMB_NUL);\r
-                       }\r
-                       else\r
-                       {\r
-                               dispvalue[0]=SYMB_NUL;\r
-                               dispvalue[1]=SYMB_4&SYMB_DOT;\r
-                               dispvalue[2]=SYMB_1;\r
-                               dispvalue[3]=SYMB_NUL&(alarm[0]?SYMB_DOT:SYMB_NUL);\r
-                       }\r
-                       break;\r
-               case MODE_ALARM:\r
-               case MODE_TIME:\r
-                       if(((time[1]&0x01) && mode==MODE_ALARM) || (time[3]==0x04 && time[2]==0x33 && time[1]==0x00))\r
-                       {\r
-                               dispvalue[0]=SYMB_g;\r
-                               dispvalue[1]=SYMB_n;\r
-                               dispvalue[2]=SYMB_o;\r
-                               dispvalue[3]=SYMB_DOT;\r
-                               break;\r
-                       }\r
-               default:\r
-                       dispvalue[0]=symbol[time[2]&0x0f]&((time[1]&0x01)?SYMB_NUL:SYMB_DOT);\r
-                       dispvalue[1]=symbol[(time[2]&0xf0)>>4]&((time[1]&0x01)?SYMB_NUL:SYMB_DOT);\r
-                       dispvalue[2]=symbol[time[3]&0x0f];\r
-                       dispvalue[3]=((time[3]&0xf0)?symbol[(time[3]&0xf0)>>4]:SYMB_NUL)&((alarm[0] || mode==MODE_ALARM)?SYMB_DOT:SYMB_NUL);\r
-                       break;\r
-               }\r
-               \r
-               display(dispvalue);\r
-               \r
-               \r
-               return;\r
-       }\r
-\r
-       int main( void )\r
-       {\r
-               unsigned char delay;\r
-               \r
-               //watchdog:\r
-               WDTCTL = WDTPW + WDTHOLD;\r
-               \r
-               mode=MODE_INIT;\r
-               \r
-               //init 7 segment display\r
-               \r
-               P1OUT =0xff; //display data on p1\r
-               P1DIR =0xff;\r
-               \r
-               P3OUT &=0x0f; //display control on p3.4-7\r
-               P3DIR |=0xf0; \r
-               \r
-               dispvalue[0]=SYMB_NUL;\r
-               dispvalue[1]=SYMB_o;\r
-               dispvalue[2]=SYMB_o;\r
-               dispvalue[3]=SYMB_NUL;\r
-               \r
-               display(dispvalue);  \r
-               \r
-               //clocking:\r
-                       \r
-               BCSCTL2 |= DIVM_3; //errata sheet recommends this here \r
-               \r
-               DCOCTL =0x00;\r
-               BCSCTL1 =\r
-                       RSEL2| //in the\r
-                       XTS| //hi freq\r
-                       DIVA_1; //ACLK = 6/2 MHz\r
-               \r
-               do\r
-               {\r
-                       IFG1 &= ~OFIFG;\r
-                       for (delay = 0xFF; delay > 0; --delay);\r
-               }\r
-               while ((IFG1 & OFIFG)); \r
-               \r
-               BCSCTL2 =\r
-                       SELM_2| //MCLK from XT2\r
-                       DIVM_1| //MCLK = 6/2 MHz\r
-                       SELS| //SMCLK from XT2\r
-                       DIVS_0; //SMCLK = 6/1 MHz\r
-                       \r
-               //init SPI\r
-               \r
-               P3SEL =0x0e; // SPI on p3.1, 3.2, 3.3\r
-               P3OUT |=0x01;\r
-               P3DIR |=0x01; //RTC CS on p3.0\r
-               \r
-               U0CTL =\r
-                       SWRST| //reset\r
-                       MM| //master\r
-                       SYNC| //SPI mode\r
-                       CHAR; //8 bit data\r
-               U0TCTL=\r
-                       CKPH| //polarity and phase\r
-                       SSEL0|SSEL1| //SMCLK\r
-                       STC; //3 pin mode\r
-               U0BR1=0x00;\r
-               U0BR0=0x02; //baudrate = SMCLK/2 = 3MHz\r
-               U0MCTL=0x00;\r
-               ME2|=USPIE0;\r
-               U0CTL &= ~SWRST;\r
-               \r
-               \r
-               //init RTC\r
-               \r
-               //Looks like sometimes the first SPI reads rom the RTC may be incorrect.\r
-               //To be sure, let's do a few redundant reads before starting the real work\r
-               \r
-               for (delay = 0xFF; delay > 0; --delay)\r
-                        spi(RTC_READ,0x00,inbuf,outbuf,1);\r
-               \r
-               //set 24 hour mode\r
-               spi(RTC_READ,0x03,inbuf,outbuf,1);\r
-               if(inbuf[0]&0x40)\r
-               {\r
-                       outbuf[0]=inbuf[0]&(~0x40);\r
-                       spi(RTC_WRITE,0x03,inbuf,outbuf,1);\r
-               }\r
-                       \r
-               //enable battery\r
-               spi(RTC_READ,0x04,inbuf,outbuf,1);\r
-               if(!(inbuf[0]&0x08))\r
-               {\r
-                       outbuf[0]=inbuf[0]|0x08; //enable battery\r
-                       spi(RTC_WRITE,0x04,inbuf,outbuf,1);\r
-               }\r
-               \r
-               //set control register\r
-               //spi(RTC_READ,0x08,inbuf,outbuf,1);\r
-               outbuf[0]=\r
-                       0x40| //squarewave enable\r
-                       0x04; //1Hz\r
-               //no hardware alarms :(\r
-               spi(RTC_WRITE,0x08,inbuf,outbuf,1);\r
-               \r
-               //get (soft) alarm from RTC SRAM\r
-               spi(RTC_READ,0x20,inbuf,outbuf,3);\r
-               \r
-               alarm[0]=(inbuf[0]==ALARMVALUE)?ALARMVALUE:0;\r
-               alarm[1]=inbuf[1];\r
-               alarm[2]=inbuf[2];\r
-               \r
-               spi(RTC_READ,0x2f,inbuf,outbuf,1); // <- because of RTC bug (?)\r
-               \r
-               //get last countdown value from RTC SRAM\r
-               spi(RTC_READ,0x23,inbuf,outbuf,3);\r
-               if(inbuf[0]==ALARMVALUE)\r
-               {\r
-                       lastcdn[0]=inbuf[1];\r
-                       lastcdn[1]=inbuf[2];\r
-               }\r
-               else\r
-               {\r
-                       lastcdn[0]=0;\r
-                       lastcdn[1]=0;\r
-               }\r
-               \r
-               spi(RTC_READ,0x2f,inbuf,outbuf,1); // <- because of RTC bug (?)\r
-               \r
-               //get last year value from RTC SRAM\r
-               spi(RTC_READ,0x26,inbuf,outbuf,3);\r
-               if(inbuf[0]==ALARMVALUE)\r
-               {\r
-                       lastyear[0]=inbuf[1];\r
-                       lastyear[1]=inbuf[2];\r
-               }\r
-               else\r
-               {\r
-                       lastyear[0]=0x00;\r
-                       lastyear[1]=0x20;\r
-               }\r
-               lastyear[3]=0;\r
-               \r
-               //start oscillator\r
-               spi(RTC_READ,0x01,inbuf,outbuf,1);\r
-               if(!(inbuf[0]&0x80))\r
-               {\r
-                       outbuf[0]=inbuf[0]|0x80;\r
-                       spi(RTC_WRITE,0x01,inbuf,outbuf,1);\r
-               }\r
-               \r
-               //init timer:\r
-               TACCR0 = 375-1; //3MHz/375/2=4kHz\r
-               TACCTL0=   //also, compare, not capture\r
-                       OUTMOD_4; //toggle\r
-               TACTL=\r
-                       TASSEL_1| //ACLK, 3MHz\r
-                       ID_0| //3MHz/1\r
-                       //MC_1| //up mode\r
-                       TACLR; //reset\r
-               P2SEL|=0x04; //P2.2 timer output.\r
-               P2DIR|=0x04;\r
-               \r
-               // Init interrupts\r
-               P2IE=\r
-                       0x08| //p2.3 - int. from RTC\r
-                       0x02| //p2.1 - int. from button A\r
-                       0x01| //p2.0 - int. from button B\r
-                       0x20; //p2.5 - int. from button C\r
-               P2IES|=0x01|0x02|0x20; //button int. on falling edge\r
-               P2IES&= ~0x08; //RTC int. on rising edge\r
-               P2IFG=0x00;\r
-               \r
-               if(!(P2IN&0x02)) //button A pressed at start?\r
-               {\r
-                       mode=MODE_CALIBRATE;\r
-                       spi(RTC_READ,0x03,inbuf,outbuf,1);\r
-                       settime[1]=inbuf[0]&0x80;\r
-                       spi(RTC_READ,0x09,inbuf,outbuf,1);\r
-                       settime[0]=inbuf[0];\r
-                       digit=3;\r
-               }\r
-               else if(!(P2IN&0x20)) //button C pressed at start?\r
-               {\r
-                       mode=MODE_DEBUG;\r
-                       settime[1]=0x00;\r
-                       changed=0;\r
-               }\r
-               else\r
-                       mode=MODE_TIME;\r
-               \r
-               __enable_interrupt();\r
-               \r
-               \r
-               while(1)\r
-               {\r
-                       lpm3();\r
-               }\r
-       }\r
+// SilentTimer Action Figure software, v1.5
+// main.c
+// The main c file.
+// 
+// 15.07.2016
+// Copyright (C) 2014-2016  Balthasar Szczepański
+// 
+// This program is free software: you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+// 
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+// 
+// You should have received a copy of the GNU General Public License
+// along with this program.  If not, see <http://www.gnu.org/licenses/>.
+       
+       // #define OLD_HARDWARE
+       // #define DEBAG
+       #include "msp430.h"
+       #include "definitions.h"
+
+       unsigned char inbuf[8];
+       unsigned char outbuf[8] __attribute__ ((aligned (2))); 
+       unsigned char dispvalue[4];
+       unsigned char time[8];
+       unsigned char leap;
+       unsigned char settime[3];
+       unsigned char mode;
+       unsigned char alarm[3];
+       unsigned char lastcdn[2];
+       unsigned char lastyear[5];
+       unsigned char digit;
+       unsigned char changed;
+       unsigned char run;
+       unsigned char symbol[16] ={
+               SYMB_0,
+               SYMB_1,
+               SYMB_2,
+               SYMB_3,
+               SYMB_4,
+               SYMB_5,
+               SYMB_6,
+               SYMB_7,
+               SYMB_8,
+               SYMB_9,
+               SYMB_A,
+               SYMB_B,
+               SYMB_C,
+               SYMB_D,
+               SYMB_E,
+               SYMB_F};
+
+       __interrupt void P2_ISR();
+       void display(
+               unsigned char *data);
+       void spi(
+               unsigned char command,
+               unsigned char addr,
+               unsigned char *inbuf,
+               unsigned char *outbuf,
+               unsigned short n);
+       int main( void );
+
+       void display(unsigned char *data)
+       {
+               //lsd
+               P1OUT =data[0];
+               P3OUT |=0x80; //74hc373 latch enable
+               P3OUT &=0x0f;
+               
+               P1OUT =data[1];
+               P3OUT |=0x40;
+               P3OUT &=0x0f;
+               
+               P1OUT =data[2];
+               P3OUT |=0x20;
+               P3OUT &=0x0f;
+               
+               //msd
+               P1OUT =data[3];
+               P3OUT |=0x10;
+               P3OUT &=0x0f;
+       }
+
+       void spi(
+               unsigned char command,
+               unsigned char addr,
+               unsigned char *inbuf,
+               unsigned char *outbuf,
+               unsigned short n)
+       {
+               unsigned short i;
+               unsigned char nul;
+               unsigned char flags;
+               
+               switch (command)
+               {
+               case RTC_EEREAD:
+               case RTC_IDREAD:
+               case RTC_READ:
+                       flags= ADDRFLAG|READFLAG;
+                       break;
+                       
+               case RTC_EEWRITE:
+               case RTC_IDWRITE:
+                       n=(n>8)?8:n;
+               case RTC_WRITE:
+                       flags= ADDRFLAG|WRITEFLAG;
+                       break;
+                       
+               case RTC_EEWREN:
+               case RTC_EEWRDI:
+                       flags=0;
+                       break;
+                       
+               case RTC_SRREAD:
+                       n=(n>1)?1:n;
+                       flags=READFLAG;
+                       break;
+               
+               case RTC_SRWRITE: //1
+               case RTC_CLRRAM: //data ignored but has to be
+               case RTC_UNLOCK: //1
+                        n=(n>1)?1:n;
+                       flags=WRITEFLAG;
+                       break;
+               default:
+                       return;
+               }
+               
+               P3OUT&=0xfe; //chip select down
+                
+               #if !(defined DEBAG)
+               while((IFG2&UTXIFG0)==0); // wait until can be sent
+               #endif
+               U0TXBUF=command; // send command
+               #if !(defined DEBAG)
+               while((IFG2&UTXIFG0)==0); // wait until received
+               #endif
+               nul=U0RXBUF; //pretend to read "data"
+               
+               if(flags&ADDRFLAG)
+               {
+                       #if !(defined DEBAG)
+                       while((IFG2&UTXIFG0)==0); // wait until can be sent
+                       #endif
+                       U0TXBUF=addr; // send address
+                       #if !(defined DEBAG)
+                       while((IFG2&UTXIFG0)==0); // wait until received
+                       #endif
+                       nul=U0RXBUF; //pretend to read "data"
+               }
+               if(flags&(READFLAG|WRITEFLAG))
+               {
+                       for(i=0;i<n;++i)
+                       {
+                               #if !(defined DEBAG)
+                               while((IFG2&UTXIFG0)==0); // wait until can be sent
+                               #endif
+                               U0TXBUF=(flags&WRITEFLAG)?outbuf[i]:0xff; // send data
+                               #if !(defined DEBAG)
+                               while((IFG2&UTXIFG0)==0); // wait until received
+                               #endif
+                               if(flags&READFLAG)
+                                       inbuf[i]=U0RXBUF; //read data
+                               else
+                                       nul=U0RXBUF;
+                       }
+               }
+               P3OUT|=0x01; //chip select up
+               for(i=0;i<255;++i); //looks like the RTC might not like another SPI transmission exactly before the previous one, sometimes.
+       }
+
+       #pragma vector=PORT2_VECTOR
+       __interrupt void P2_ISR()
+       {
+               if(P2IFG&0x08) // RTC
+               {
+                       P2IFG &= ~0x08;
+                       spi(RTC_READ,0x00,inbuf,outbuf,8);
+               
+                       time[0]=inbuf[0]; //1/100 s
+                       time[1]=inbuf[1]&0x7f; //seconds
+                       time[2]=inbuf[2]&0x7f; //minutes
+                       time[3]=inbuf[3]&0x3f; //hours
+                       time[4]=inbuf[4]&0x07; //dow
+                       time[5]=inbuf[5]&0x3f; //dom
+                       time[6]=inbuf[6]&0x1f; //mon
+                       time[7]=inbuf[7];//year
+                       leap=inbuf[6]&0x20;
+                       
+                       spi(RTC_READ,0x0f,inbuf,outbuf,1); // <- because of RTC bug (probably not needed anymore?)
+                       if(!((time[2]^alarm[1])|(time[3]^alarm[2])) && alarm[0])
+                       {
+                               mode=MODE_ALARM;
+                               alarm[0]=0; //alarm removed after activated
+                               spi(RTC_WRITE,0x20,inbuf,alarm,1);
+                       }
+                       
+                       if(mode==MODE_COUNTUP && run)
+                       {
+                               if(settime[1]<0x59)
+                                       settime[1]=bcd_add_byte(settime[1],0x01);
+                               else
+                               {
+                                       settime[1]=0x00;
+                                       settime[2]=bcd_add_byte(settime[2],0x01);
+                               }
+                       }
+                       else if(mode==MODE_COUNTDOWN && run && (settime[1]|settime[2]))
+                       {
+                               if(settime[1])
+                                       settime[1]=bcd_add_byte(settime[1],0x99);
+                               else
+                               {
+                                       settime[1]=0x59;
+                                       settime[2]=bcd_add_byte(settime[2],0x99);
+                               }
+                       }
+                       
+                       if (mode==MODE_ALARM || (mode==MODE_COUNTDOWN && !(settime[1]|settime[2])))
+                       {
+                               TACTL^=MC_1;
+                               //P2DIR^=0x40;
+                               //P2SEL^=0x40;
+                       }
+                       else if(TACTL & MC_3)
+                       {
+                               TACTL&= ~MC_3;
+                               //P2DIR&= ~0x40;
+                               //P2SEL&= ~0x40;
+                       }
+                       
+                       lastyear[4]=(time[7]==0x00 && lastyear[0]==0x99)?bcd_add_byte(lastyear[1],0x01):lastyear[1];
+                       if (time[7]!=lastyear[0])
+                       {
+                               if(time[7]==lastyear[2])
+                                       ++(lastyear[3]);
+                               else
+                                       lastyear[3]=0;
+                               
+                               lastyear[2]=time[7];
+                               
+                               if(lastyear[3]>55)
+                               {
+                                       lastyear[3]=0;
+                                       
+                                       if(time[7]==0x00 && lastyear[0]==0x99)
+                                               lastyear[1]=bcd_add_byte(lastyear[1],0x01);
+                                       
+                                       lastyear[0]=time[7];
+                                       
+                                       outbuf[0]=ALARMVALUE;
+                                       outbuf[1]=lastyear[0];
+                                       outbuf[2]=lastyear[1];
+                                       spi(RTC_WRITE,0x26,inbuf,outbuf,3);
+                               }      
+                       }
+                       
+                       
+                       if(mode==MODE_DEBUG && changed)
+                               spi(RTC_READ,settime[1],settime,outbuf,1);
+               }
+               else if(P2IFG&0x02) // button A
+               {
+                       P2IFG &= ~0x02;
+                       switch(mode)
+                       {
+                       case MODE_CALCSETNUM:
+                       case MODE_CALCSETOPER:
+                               mode=MODE_CALC;
+                               break;
+                       case MODE_CALC:
+                               if(outbuf[7] || outbuf[1]!=0 || outbuf[0]!=0)
+                               {
+                                       outbuf[7]=0x00;
+                                       outbuf[6]=0x00;
+                                       outbuf[3]=0x00;
+                                       outbuf[2]=0x00;
+                                       outbuf[1]=0x00;
+                                       outbuf[0]=0x00;
+                               }
+                               else
+                                       mode=MODE_TIME;
+                               break;
+                       case MODE_TIME:
+                       case MODE_SETDATE:
+                       case MODE_SETYEAR:
+                               mode=MODE_DATE;
+                               break;
+                       case MODE_DATE:
+                               mode=MODE_COUNTUP;
+                               settime[1]=0x00;
+                               settime[2]=0x00;
+                               run=0;
+                               break;
+                       case MODE_COUNTUP:
+                               if(run)
+                               {
+                                       settime[1]=0x00;
+                                       settime[2]=0x00;
+                                       run=0;
+                               }
+                               else
+                               {
+                                       mode=MODE_SETCOUNTDOWN;
+                                       settime[1]=lastcdn[0];
+                                       settime[2]=lastcdn[1];
+                                       changed=0;
+                                       digit=3;
+                               }
+                               break;
+                       case MODE_DEBUGADDR:
+                       case MODE_DEBUGVAL:
+                               mode=MODE_DEBUG;
+                               break;
+                       case MODE_SETCOUNTDOWN:
+                       case MODE_COUNTDOWN:
+                               if(changed || mode==MODE_COUNTDOWN)
+                               {
+                                       mode=MODE_SETCOUNTDOWN;
+                                       settime[1]=lastcdn[0];
+                                       settime[2]=lastcdn[1];
+                                       changed=0;
+                                       digit=3;
+                                       break;
+                               }
+                       default:
+                               mode=MODE_TIME;
+                               break;
+                       }
+               }
+               else if(P2IFG&0x01) // button B
+               {
+                       P2IFG &= ~0x01;
+                       
+                       switch(mode)
+                       {
+                       case MODE_CALCSETOPER:
+                               outbuf[6]&=0x3f;
+                               outbuf[6]|=settime[2];
+                               mode=MODE_CALCSETNUM;
+                               digit=4;
+                               if(changed)
+                               {
+                                       settime[0]=outbuf[2];
+                                       settime[1]=outbuf[3];
+                                       settime[2]=(outbuf[6]&0x0c)>>2;
+                               }
+                               else
+                               {
+                                       settime[0]=0x00;
+                                       settime[1]=0x00;
+                                       settime[2]=0x00;
+                               }
+                               changed=1;
+                               break;
+                       case MODE_CALCSETNUM:
+                               if(digit)
+                                       --digit;
+                               else
+                               {
+                                       if(changed)
+                                       {
+                                               outbuf[6]&=0xf3;
+                                               outbuf[6]|=settime[2]<<2;
+                                               outbuf[3]=settime[1];
+                                               outbuf[2]=settime[0];
+                                               
+                                               
+                                               switch(outbuf[6]&0xc0)
+                                               {
+                                               case 0xc0:
+                                                       calc_div(outbuf);
+                                                       break;
+                                               case 0x80:
+                                                       calc_mul(outbuf);
+                                                       break;
+                                               case 0x40:
+                                                       outbuf[6]^=0x04;
+                                                       calc_add(outbuf);
+                                                       outbuf[6]^=0x04;
+                                                       break;
+                                               case 0x00:
+                                                       calc_add(outbuf);
+                                                       break;
+                                               default:
+                                                       outbuf[7]=0xff;
+                                               }
+                                               mode=MODE_CALC;
+                                       }
+                                       else
+                                       {
+                                               outbuf[6]&=0xcf;
+                                               outbuf[6]|=settime[2]<<4;
+                                               outbuf[5]=settime[1];
+                                               outbuf[4]=settime[0];
+                                               mode=MODE_CALCSETOPER;
+                                               settime[2]=0x00;
+                                       }
+                               }
+                               break;
+                       case MODE_CALC:
+                               if(outbuf[7])
+                               {
+                                       outbuf[0]=0x00;
+                                       outbuf[1]=0x00;
+                                       outbuf[2]=0x00;
+                                       outbuf[3]=0x00;
+                                       outbuf[6]=0x00;
+                                       outbuf[7]=0x00;
+                               }
+                               else
+                               {
+                                       outbuf[6]&=0xcf;
+                                       outbuf[6]|=(outbuf[6]&0x03)<<4;
+                                       outbuf[5]=outbuf[1];
+                                       outbuf[4]=outbuf[0];
+                                       mode=MODE_CALCSETOPER;
+                                       settime[2]=outbuf[6]&0xc0;
+                                       changed=1;
+                               }
+                               break;
+                       case MODE_TIME:
+                               mode=MODE_SETTIME;
+                               digit=3;
+                               changed=0;
+                               settime[0]=0;
+                               settime[1]=time[2];
+                               settime[2]=time[3];
+                               break;
+                       case MODE_DATE:
+                               mode=MODE_SETDATE;
+                               digit=2;
+                               changed=0;
+                               settime[0]=time[5];
+                               settime[1]=time[6];
+                               break;
+                       case MODE_COUNTDOWN:
+                               if(settime[1]==0 && settime[2]==0)
+                               {
+                                       settime[1]=lastcdn[0];
+                                       settime[2]=lastcdn[1];
+                                       run=0;
+                                       break;
+                               }
+                       case MODE_COUNTUP:
+                               run=!run;
+                               break;
+                       case MODE_SETCOUNTDOWN:
+                               if(digit)
+                               {
+                                       --digit;
+                                       changed=1;
+                               }
+                               else
+                               {
+                                       lastcdn[0]=settime[1];
+                                       lastcdn[1]=settime[2];
+                                       
+                                       outbuf[0]=ALARMVALUE;
+                                       outbuf[1]=settime[1];
+                                       outbuf[2]=settime[2];
+                                       spi(RTC_WRITE,0x23,inbuf,outbuf,3);
+
+                                       mode=MODE_COUNTDOWN;
+                                       run=0;
+                               }
+                               break;
+                       case MODE_CALIBRATE:
+                               if(digit)
+                                       --digit;
+                               else
+                               {
+                                       spi(RTC_WRITE,0x09,inbuf,settime,1);
+                                       outbuf[0]=time[3]|settime[1];
+                                       spi(RTC_WRITE,0x03,inbuf,outbuf,1);
+                                       digit=3;
+                               }
+                               break;
+                       case MODE_DEBUGADDR:
+                               if(digit)
+                                       --digit;
+                               else
+                               {
+                                       spi(RTC_READ,settime[1],settime,outbuf,1);
+                                       mode=MODE_DEBUG;
+                                       changed=1;
+                               }
+                               break;
+                       case MODE_DEBUGVAL:
+                               if(digit)
+                                       --digit;
+                               else
+                               {
+                                       spi(RTC_WRITE,settime[1],inbuf,settime,1);
+                                       mode=MODE_DEBUG;
+                               }
+                               break;
+                       case MODE_SETTIME:
+                               if(digit)
+                               {
+                                       if(digit==3&&settime[2]>0x23)
+                                                settime[2]&=0xf0;
+                                       --digit;
+                               }
+                               else
+                               {
+                                       if(changed)
+                                       {
+                                               outbuf[0]=0x00;
+                                               outbuf[1]=0x00;
+                                               outbuf[2]=settime[1]&0x7f;
+                                               outbuf[3]=settime[2]&0x3f;
+                                               
+                                               spi(RTC_WRITE,0x01,inbuf,outbuf,1);//stop osc.
+                                               spi(RTC_READ,0x03,inbuf,outbuf,1);
+                                               outbuf[3]|=(inbuf[0]&0x80);//keep the calsgn bit.
+                                       
+                                               spi(RTC_WRITE,0x00,inbuf,outbuf,4);//write new time
+                                       
+                                               outbuf[0]=0x80;
+                                               spi(RTC_WRITE,0x01,inbuf,outbuf,1);//start osc;
+                                               
+                                               time[1]=0;
+                                               time[2]=settime[1];
+                                               time[3]=settime[2];
+                                       }
+                                       mode=MODE_TIME;
+                               }
+                               break;
+                       case MODE_SETYEAR:
+                               if(digit)
+                                       --digit;
+                               else
+                               {
+                                       if(changed)
+                                       {
+                                               time[7]=settime[1];
+                                               spi(RTC_WRITE,0x07,inbuf,settime+1,1);
+                                               
+                                               lastyear[0]=settime[1];
+                                               lastyear[1]=settime[2];
+                                               settime[0]=ALARMVALUE;
+                                               
+                                               spi(RTC_WRITE,0x26,inbuf,settime,3);
+                                       }
+                                       mode=MODE_DATE; 
+                               }
+                               break;
+                       case MODE_SETDATE:
+                               if(digit)
+                               {
+                                       --digit;
+                                       if(settime[0]>((settime[1]==0x02)?(leap?0x29:0x28):(((settime[1]&0x18) && !(settime[1]&0x01) || !(settime[1]&0x18) && (settime[1]&0x01))?0x31:0x30)))
+                                               settime[0]&=(digit?0x00:0xf0);
+                                       if(settime[0]==0x00)
+                                               settime[0]=0x01;
+                               }
+                               else
+                               {
+                                       if (changed)
+                                       {
+                                               time[5]=settime[0];
+                                               time[6]=settime[1];
+                                               settime[2]=time[7];
+                                               spi(RTC_WRITE,0x06,inbuf,settime+1,2);
+                                               spi(RTC_WRITE,0x05,inbuf,settime,1);
+                                       }
+                                       mode=MODE_DATE;
+                               }
+                               
+                               break;
+                       case MODE_SETALARM:
+                               if(digit)
+                               {
+                                       if(digit==3&&settime[2]>0x23)
+                                                settime[2]&=0xf0;
+                                       --digit;
+                               }
+                               else
+                               {
+                                       if(changed|| !alarm[0])
+                                       {
+                                               alarm[0]=ALARMVALUE;
+                                               alarm[1]=settime[1];
+                                               alarm[2]=settime[2];
+                                               
+                                               spi(RTC_WRITE,0x20,inbuf,alarm,3);
+                                       }
+                                       else
+                                       {
+                                               alarm[0]=0;
+                                               spi(RTC_WRITE,0x20,inbuf,outbuf,1);
+                                       }
+                                       mode=MODE_TIME;
+                               }
+                               break;
+                       case MODE_ALARM:
+                               alarm[0]=0;
+                               spi(RTC_WRITE,0x20,inbuf,outbuf,1);
+                               mode=MODE_TIME;
+                               break;
+                       case MODE_DEBUG:
+                               //changed=1;
+                               digit=1;
+                               mode=MODE_DEBUGADDR;
+                               break;  
+                       default:
+                               break;
+                       }
+               }
+               else if(P2IFG&0x20) // button C
+               {
+                       P2IFG &= ~0x20;
+                       
+                       switch(mode)
+                       {
+                       case MODE_CALC:
+                               if(outbuf[7])
+                               {
+                                       outbuf[0]=0x00;
+                                       outbuf[1]=0x00;
+                                       outbuf[2]=0x00;
+                                       outbuf[3]=0x00;
+                                       outbuf[6]=0x00;
+                                       outbuf[7]=0x00;
+                               }
+                               else
+                               {
+                                       mode=MODE_CALCSETNUM;
+                                       changed=0;
+                                       digit=4;
+                                       settime[0]=0x00;
+                                       settime[1]=0x00;
+                                       settime[2]=0x00;
+                               }
+                               break;
+                       case MODE_CALCSETNUM:
+                               if (digit==4)
+                                       settime[2]^=0x02;
+                               else if (digit==3)
+                               {
+                                       if((settime[1]&0xf0)==0x90)
+                                       {
+                                               settime[1]&=0x0f;
+                                               settime[2]|=0x01;
+                                       }
+                                       else if (settime[2]&0x01)
+                                               settime[2]&=0xfe;
+                                       else
+                                               settime[1]+=0x10;
+                               }
+                               else
+                               {
+                                       if ((settime[(digit&0x02)?1:0]&((digit&0x01)?0xf0:0x0f)) == ((digit&0x01)?0x90:0x09))
+                                               settime[(digit&0x02)?1:0]&=((digit&0x01)?0x0f:0xf0);
+                                       else
+                                               settime[(digit&0x02)?1:0]+=((digit&0x01)?0x10:0x01);
+                               }
+                               break;
+                       case MODE_CALCSETOPER:
+                               settime[2]+=0x40;
+                               break;
+                       case MODE_COUNTUP:
+                               settime[1]=0x00;
+                               settime[2]=0x00;
+                               break;
+                       case MODE_COUNTDOWN:
+                               settime[1]=lastcdn[0];
+                               settime[2]=lastcdn[1];
+                               //changed=0;
+                               //mode=MODE_SETCOUNTDOWN;
+                               //digit=3;
+                               break;
+                       case MODE_CALIBRATE:
+                               if(digit==2)
+                                       settime[1]^=0x80;
+                               if(digit>=2)
+                                       break;
+                       case MODE_DEBUGADDR:
+                       case MODE_DEBUGVAL:
+                               settime[(mode==MODE_DEBUGADDR)?1:0]+=(digit==1?0x10:(((settime[(mode==MODE_DEBUGADDR)?1:0]&0x0f)==0x0f)?0xf1:0x01));
+                               break;
+                       case MODE_SETTIME:
+                       case MODE_SETALARM:
+                       case MODE_SETYEAR:
+                       case MODE_SETCOUNTDOWN:
+                               changed=1;
+                               //these 4 lines replaced a larger switch structure
+                               if((settime[digit&0x02?2:1]&((digit&0x01)?0xf0:0x0f))<(digit==1?((mode==MODE_SETYEAR)?0x90:0x50):(digit==3?((mode==MODE_SETCOUNTDOWN||mode==MODE_SETYEAR)?0x90:0x20):((mode!=MODE_SETCOUNTDOWN&&mode!=MODE_SETYEAR&&digit==2&&settime[2]>0x19)?0x03:0x09))))
+                                       settime[digit&0x02?2:1]+=((digit&0x01)?0x10:0x01);
+                               else
+                                       settime[digit&0x02?2:1]&=((digit&0x01)?0x0f:0xf0);
+                               break;
+                       case MODE_SETDATE:
+                               changed=1;
+                               switch (digit)
+                               {
+                               case 2:
+                                       if(settime[1]<0x12)
+                                               settime[1]=bcd_add_byte(settime[1],0x01);
+                                       else
+                                               settime[1]=0x01;
+                                       break;
+                               case 1:
+                                       if((settime[0]&0xf0)<((settime[1]==0x02)?0x20:0x30))
+                                               settime[0]+=0x10;
+                                       else
+                                               settime[0]&=0x0f;
+                                       break;
+                                       
+                               case 0:
+                                       if((settime[0]<((settime[1]==0x02)?(leap?0x29:0x28):(((settime[1]&0x18) && !(settime[1]&0x01) || !(settime[1]&0x18) && (settime[1]&0x01))?0x31:0x30))) && (settime[0]&0x0f)<0x09)
+                                               ++settime[0];
+                                       else
+                                               settime[0]&=0xf0;
+                                       if(settime[0]==0x00)
+                                               settime[0]=0x01;
+                                       break;
+                               }
+                               break;
+                       case MODE_DATE:
+                               mode=MODE_SETYEAR;
+                               digit=3;
+                               changed=0;
+                               
+                               settime[1]=time[7];
+                               settime[2]=lastyear[1];
+                               break;
+                       case MODE_TIME:
+                               mode=MODE_SETALARM;
+                               digit=3;
+                               changed=0;
+                               
+                               settime[0]=0;
+                               settime[1]=alarm[1]; //get last alarm time
+                               if(settime[1]>0x59 || (settime[1]&0x0f)>0x09)
+                                       settime[1]=0x00;
+                               settime[2]=alarm[2];
+                               if(settime[2]>0x23 || (settime[2]&0x0f)>0x09)
+                                       settime[2]=0x00;
+                               break;
+                       case MODE_ALARM:
+                               alarm[0]=0;
+                               spi(RTC_WRITE,0x20,inbuf,outbuf,1);
+                               mode=MODE_TIME;
+                               break;
+                       case MODE_DEBUG:
+                               if(changed)
+                               {
+                                       digit=1;
+                                       mode=MODE_DEBUGVAL;
+                               }
+                               break;
+                       default:
+                               break;
+                       }
+               }
+               else
+                       P2IFG=0;
+               
+               switch(mode)
+               {
+               case MODE_CALC:
+                       if(outbuf[7])
+                       {
+                               dispvalue[0]=SYMB_E & SYMB_DOT;
+                               dispvalue[1]=SYMB_E & SYMB_DOT;
+                               dispvalue[2]=SYMB_E;
+                               dispvalue[3]=SYMB_NUL;
+                       }
+                       else
+                       {
+                               dispvalue[0]=symbol[outbuf[0]&0x0f] | (((outbuf[0]&0x0f)==0x00 && (outbuf[6]&0x02))?SYMB_NUL:SYMB_8);
+                               dispvalue[1]=(symbol[(outbuf[0]&0xf0)>>4] | (((outbuf[0]&0xf0)==0x00 && outbuf[1]==0x00 && !(outbuf[6]&0x02))?SYMB_NUL:SYMB_8)) & ((outbuf[6]&0x02)?SYMB_DOT:SYMB_NUL);
+                               dispvalue[2]=symbol[outbuf[1]&0x0f] | ((outbuf[1]==0x00 && !(outbuf[6]&0x02))?SYMB_NUL:SYMB_8);
+                               dispvalue[3]=(outbuf[6]&0x01)?SYMB_MIN:(symbol[(outbuf[1]&0xf0)>>4] |  (((outbuf[1]&0xf0)==0x00)?SYMB_NUL:SYMB_8));
+                       }
+                       if(alarm[0])
+                               dispvalue[3]&=SYMB_DOT;
+                       break;
+               case MODE_CALCSETNUM:
+                       dispvalue[0]=(symbol[settime[0]&0x0f]      | ((digit!=0 && (time[1]&0x01))?SYMB_NUL:SYMB_8));
+                       dispvalue[1]=(symbol[(settime[0]&0xf0)>>4] | ((digit!=1 && (time[1]&0x01))?SYMB_NUL:SYMB_8)) & ((settime[2]&0x02)?SYMB_DOT:SYMB_NUL);
+                       dispvalue[2]=(symbol[settime[1]&0x0f]      | ((digit!=2 && (time[1]&0x01))?SYMB_NUL:SYMB_8));
+                       dispvalue[3]=(((settime[2]&0x01)?SYMB_MIN:symbol[(settime[1]&0xf0)>>4]) | ((digit!=3 && (time[1]&0x01))?SYMB_NUL:SYMB_8)) & (alarm[0]?SYMB_DOT:SYMB_NUL);
+                       break;
+               case MODE_CALCSETOPER:
+                       switch (settime[2] & 0xc0)
+                       {
+                       case 0xc0:
+                               dispvalue[0]=SYMB_U;
+                               dispvalue[1]=SYMB_I;
+                               dispvalue[2]=SYMB_D;
+                               dispvalue[3]=SYMB_NUL;
+                               break;
+                       case 0x80:
+                               dispvalue[0]=SYMB_L;
+                               dispvalue[1]=SYMB_U;
+                               dispvalue[2]=SYMB_M2;
+                               dispvalue[3]=SYMB_M1;
+                               break;
+                       case 0x40:
+                               dispvalue[0]=SYMB_B;
+                               dispvalue[1]=SYMB_U;
+                               dispvalue[2]=SYMB_5;
+                               dispvalue[3]=SYMB_NUL;
+                               break;
+                       default:
+                               dispvalue[0]=SYMB_D;
+                               dispvalue[1]=SYMB_D;
+                               dispvalue[2]=SYMB_A;
+                               dispvalue[3]=SYMB_NUL;
+                       }
+                       if(alarm[0])
+                               dispvalue[3]&=SYMB_DOT;
+                       break;
+               case MODE_SETTIME:
+               case MODE_SETALARM:
+               case MODE_SETCOUNTDOWN:
+               case MODE_SETYEAR:
+                       dispvalue[0]=(symbol[settime[1]&0x0f]      | ((digit!=0 && (time[1]&0x01))?SYMB_NUL:SYMB_8)) & ((((((time[1]+0x01)&0x02)^((time[1]&0x10)>>3))&&mode==MODE_SETCOUNTDOWN)||mode==MODE_SETYEAR)?SYMB_NUL:SYMB_DOT);
+                       dispvalue[1]=(symbol[(settime[1]&0xf0)>>4] | ((digit!=1 && (time[1]&0x01))?SYMB_NUL:SYMB_8)) & (((((time[1]&0x02)^((time[1]&0x10)>>3))&&mode==MODE_SETCOUNTDOWN)||mode==MODE_SETYEAR)?SYMB_NUL:SYMB_DOT);
+                       dispvalue[2]=(symbol[settime[2]&0x0f]      | ((digit!=2 && (time[1]&0x01))?SYMB_NUL:SYMB_8)) ;
+                       dispvalue[3]=(symbol[(settime[2]&0xf0)>>4] | ((digit!=3 && (time[1]&0x01))?SYMB_NUL:SYMB_8)) & ((mode!=MODE_SETALARM && alarm[0] || mode==MODE_SETALARM && !(time[1]&0x01))?SYMB_DOT:SYMB_NUL);
+                       break;
+               case MODE_DEBUGADDR:
+               case MODE_DEBUGVAL:
+                       dispvalue[0]=(symbol[settime[(mode==MODE_DEBUGADDR)?1:0]&0x0f]      | ((digit!=0 && (time[1]&0x01))?SYMB_NUL:SYMB_8)) &SYMB_DOT;
+                       dispvalue[1]=(symbol[(settime[(mode==MODE_DEBUGADDR)?1:0]&0xf0)>>4] | ((digit!=1 && (time[1]&0x01))?SYMB_NUL:SYMB_8)) &SYMB_DOT;
+                       dispvalue[2]=(mode==MODE_DEBUGADDR)?SYMB_A:SYMB_NUL;   
+                       dispvalue[3]=alarm[0]?SYMB_DOT:SYMB_NUL;
+                       break;
+               case MODE_SETDATE:
+                       dispvalue[0]=(symbol[settime[1]&0x0f]|((digit!=2 && (time[1]&0x01))?SYMB_NUL:SYMB_8));
+                       dispvalue[1]=(symbol[(settime[1]&0xf0)>>4]|((digit!=2 && (time[1]&0x01))?SYMB_NUL:SYMB_8))&SYMB_DOT;
+                       dispvalue[2]=(symbol[settime[0]&0x0f]|((digit!=0 && (time[1]&0x01))?SYMB_NUL:SYMB_8));
+                       dispvalue[3]=(symbol[(settime[0]&0xf0)>>4]|((digit!=1 && (time[1]&0x01))?SYMB_NUL:SYMB_8))&(alarm[0]?SYMB_DOT:SYMB_NUL);
+                       break;
+               case MODE_CALIBRATE:
+                       dispvalue[0]=symbol[settime[0]&0x0f]|((digit!=0 && digit!=3 && (time[1]&0x01))?SYMB_NUL:SYMB_8);
+                       dispvalue[1]=symbol[(settime[0]&0xf0)>>4]|((digit!=1 && digit!=3 && (time[1]&0x01))?SYMB_NUL:SYMB_8);
+                       dispvalue[2]=(settime[1]?SYMB_MIN:SYMB_NUL)|((digit!=2 && digit!=3 && (time[1]&0x01))?SYMB_NUL:SYMB_8);
+                       dispvalue[3]=alarm[0]?SYMB_DOT:SYMB_NUL;
+                       break;
+               case MODE_DATE:
+                       if((time[1]&0x03)==((time[1]&0x10)?0x01:0x03))
+                       {
+                               dispvalue[0]=symbol[time[7]&0x0f];
+                               dispvalue[1]=symbol[(time[7]&0xf0)>>4];
+                               dispvalue[2]=symbol[lastyear[4]&0x0f];
+                               dispvalue[3]=symbol[(lastyear[4]&0xf0)>>4]&(alarm[0]?SYMB_DOT:SYMB_NUL);
+                       }
+                       else
+                       {
+                               dispvalue[0]=symbol[time[6]&0x0f];
+                               dispvalue[1]=symbol[(time[6]&0xf0)>>4]&SYMB_DOT;
+                               dispvalue[2]=symbol[time[5]&0x0f];
+                               dispvalue[3]=((time[5]&0xf0)?symbol[(time[5]&0xf0)>>4]:SYMB_NUL)&(alarm[0]?SYMB_DOT:SYMB_NUL);
+                       }
+                       break;
+               case MODE_COUNTDOWN:
+                       if(!(settime[1]|settime[2]))
+                       {
+                               if(!run)
+                               {
+                                       dispvalue[0]=SYMB_g&SYMB_DOT;
+                                       dispvalue[1]=SYMB_n&SYMB_DOT;
+                                       dispvalue[2]=SYMB_o;
+                                       dispvalue[3]=alarm[0]?SYMB_DOT:SYMB_NUL;
+                                       break;
+                               }
+                               run=0;
+                       }
+               case MODE_COUNTUP:
+                       dispvalue[0]=symbol[settime[1]&0x0f]&SYMB_DOT;
+                       dispvalue[1]=symbol[(settime[1]&0xf0)>>4]&SYMB_DOT;
+                       dispvalue[2]=symbol[settime[2]&0x0f];
+                       dispvalue[3]=((settime[2]&0xf0)?symbol[(settime[2]&0xf0)>>4]:SYMB_NUL)&(alarm[0]?SYMB_DOT:SYMB_NUL);
+                       break;
+               case MODE_DEBUG:
+                       if(changed)
+                       {
+                               dispvalue[0]=symbol[settime[0]&0x0f]&SYMB_DOT;
+                               dispvalue[1]=symbol[(settime[0]&0xf0)>>4]&SYMB_DOT;
+                               dispvalue[2]=symbol[settime[1]&0x0f];
+                               dispvalue[3]=symbol[(settime[1]&0xf0)>>4]&(alarm[0]?SYMB_DOT:SYMB_NUL);
+                       }
+                       else
+                       {
+                               dispvalue[0]=SYMB_NUL;
+                               dispvalue[1]=SYMB_5&SYMB_DOT;
+                               dispvalue[2]=SYMB_1;
+                               dispvalue[3]=SYMB_NUL&(alarm[0]?SYMB_DOT:SYMB_NUL);
+                       }
+                       break;
+               case MODE_ALARM:
+               case MODE_TIME:
+                       if(((time[1]&0x01) && mode==MODE_ALARM) || (time[3]==0x04 && time[2]==0x33 && time[1]==0x00))
+                       {
+                               dispvalue[0]=SYMB_g;
+                               dispvalue[1]=SYMB_n;
+                               dispvalue[2]=SYMB_o;
+                               dispvalue[3]=SYMB_DOT;
+                               break;
+                       }
+               default:
+                       dispvalue[0]=symbol[time[2]&0x0f]&((time[1]&0x01)?SYMB_NUL:SYMB_DOT);
+                       dispvalue[1]=symbol[(time[2]&0xf0)>>4]&((time[1]&0x01)?SYMB_NUL:SYMB_DOT);
+                       dispvalue[2]=symbol[time[3]&0x0f];
+                       dispvalue[3]=((time[3]&0xf0)?symbol[(time[3]&0xf0)>>4]:SYMB_NUL)&((alarm[0] || mode==MODE_ALARM)?SYMB_DOT:SYMB_NUL);
+                       break;
+               }
+               
+               display(dispvalue);
+               
+               
+               return;
+       }
+
+       int main( void )
+       {
+               unsigned char delay;
+               
+               //watchdog:
+               WDTCTL = WDTPW + WDTHOLD;
+               
+               mode=MODE_INIT;
+               
+               //init 7 segment display
+               
+               P1OUT =0xff; //display data on p1
+               P1DIR =0xff;
+               
+               P3OUT &=0x0f; //display control on p3.4-7
+               P3DIR |=0xf0; 
+               
+               dispvalue[0]=SYMB_NUL;
+               dispvalue[1]=SYMB_o;
+               dispvalue[2]=SYMB_o;
+               dispvalue[3]=SYMB_NUL;
+               
+               display(dispvalue);  
+               
+               //clocking:
+                       
+               BCSCTL2 |= DIVM_3; //errata sheet recommends this here 
+               
+               DCOCTL =0x00;
+               BCSCTL1 =
+                       RSEL2| //in the
+                       XTS| //hi freq
+                       DIVA_1; //ACLK = 6/2 MHz
+               
+               #if !(defined DEBAG)
+               do
+               {
+                       IFG1 &= ~OFIFG;
+                       for (delay = 0xFF; delay > 0; --delay);
+               }
+               while ((IFG1 & OFIFG)); 
+               #endif
+               
+               BCSCTL2 =
+                       SELM_2| //MCLK from XT2
+                       DIVM_1| //MCLK = 6/2 MHz
+                       SELS| //SMCLK from XT2
+                       DIVS_0; //SMCLK = 6/1 MHz
+                       
+               //init SPI
+               
+               P3SEL =0x0e; // SPI on p3.1, 3.2, 3.3
+               P3OUT |=0x01;
+               P3DIR |=0x01; //RTC CS on p3.0
+               
+               U0CTL =
+                       SWRST| //reset
+                       MM| //master
+                       SYNC| //SPI mode
+                       CHAR; //8 bit data
+               U0TCTL=
+                       CKPH| //polarity and phase
+                       SSEL0|SSEL1| //SMCLK
+                       STC; //3 pin mode
+               U0BR1=0x00;
+               U0BR0=0x02; //baudrate = SMCLK/2 = 3MHz
+               U0MCTL=0x00;
+               ME2|=USPIE0;
+               U0CTL &= ~SWRST;
+               
+               
+               //init RTC
+               
+               //Looks like sometimes the first SPI reads rom the RTC may be incorrect.
+               //To be sure, let's do a few redundant reads before starting the real work
+               
+               #if !(defined DEBAG)
+               for (delay = 0xFF; delay > 0; --delay)
+                        spi(RTC_READ,0x00,inbuf,outbuf,1);
+               #endif
+               
+               //set 24 hour mode
+               spi(RTC_READ,0x03,inbuf,outbuf,1);
+               if(inbuf[0]&0x40)
+               {
+                       outbuf[0]=inbuf[0]&(~0x40);
+                       spi(RTC_WRITE,0x03,inbuf,outbuf,1);
+               }
+                       
+               //enable battery
+               spi(RTC_READ,0x04,inbuf,outbuf,1);
+               if(!(inbuf[0]&0x08))
+               {
+                       outbuf[0]=inbuf[0]|0x08; //enable battery
+                       spi(RTC_WRITE,0x04,inbuf,outbuf,1);
+               }
+               
+               //set control register
+               //spi(RTC_READ,0x08,inbuf,outbuf,1);
+               outbuf[0]=
+                       0x40| //squarewave enable
+                       0x04; //1Hz
+               //no hardware alarms :(
+               spi(RTC_WRITE,0x08,inbuf,outbuf,1);
+               
+               //get (soft) alarm from RTC SRAM
+               spi(RTC_READ,0x20,inbuf,outbuf,3);
+               
+               alarm[0]=(inbuf[0]==ALARMVALUE)?ALARMVALUE:0;
+               alarm[1]=inbuf[1];
+               alarm[2]=inbuf[2];
+               
+               spi(RTC_READ,0x2f,inbuf,outbuf,1); // <- because of RTC bug (?)
+               
+               //get last countdown value from RTC SRAM
+               spi(RTC_READ,0x23,inbuf,outbuf,3);
+               if(inbuf[0]==ALARMVALUE)
+               {
+                       lastcdn[0]=inbuf[1];
+                       lastcdn[1]=inbuf[2];
+               }
+               else
+               {
+                       lastcdn[0]=0;
+                       lastcdn[1]=0;
+               }
+               
+               spi(RTC_READ,0x2f,inbuf,outbuf,1); // <- because of RTC bug (?)
+               
+               //get last year value from RTC SRAM
+               spi(RTC_READ,0x26,inbuf,outbuf,3);
+               if(inbuf[0]==ALARMVALUE)
+               {
+                       lastyear[0]=inbuf[1];
+                       lastyear[1]=inbuf[2];
+               }
+               else
+               {
+                       lastyear[0]=0x00;
+                       lastyear[1]=0x20;
+                       //also clear calibration
+                       outbuf[0]=0x00;
+                       spi(RTC_WRITE,0x09,inbuf,outbuf,1);
+               }
+               lastyear[3]=0;
+               
+               //start oscillator
+               spi(RTC_READ,0x01,inbuf,outbuf,1);
+               if(!(inbuf[0]&0x80))
+               {
+                       outbuf[0]=inbuf[0]|0x80;
+                       spi(RTC_WRITE,0x01,inbuf,outbuf,1);
+               }
+               
+               //init timer:
+               TACCR0 = 375-1; //3MHz/375/2=4kHz
+               TACCTL0=   //also, compare, not capture
+                       OUTMOD_4; //toggle
+               TACTL=
+                       TASSEL_1| //ACLK, 3MHz
+                       ID_0| //3MHz/1
+                       //MC_1| //up mode
+                       TACLR; //reset
+               P2SEL|=0x04; //P2.2 timer output.
+               P2DIR|=0x04;
+               
+               // Init interrupts
+               P2IE=
+                       0x08| //p2.3 - int. from RTC
+                       0x02| //p2.1 - int. from button A
+                       0x01| //p2.0 - int. from button B
+                       0x20; //p2.5 - int. from button C
+               P2IES|=0x01|0x02|0x20; //button int. on falling edge
+               P2IES&= ~0x08; //RTC int. on rising edge
+               P2IFG=0x00;
+               
+               if(!(P2IN&0x02)) //button A pressed at start?
+               {
+                       mode=MODE_CALIBRATE;
+                       spi(RTC_READ,0x03,inbuf,outbuf,1);
+                       settime[1]=inbuf[0]&0x80;
+                       spi(RTC_READ,0x09,inbuf,outbuf,1);
+                       settime[0]=inbuf[0];
+                       digit=3;
+               }
+               else if(!(P2IN&0x01)) //button B pressed at start?
+               {
+                       mode=MODE_CALC;
+                       outbuf[0]=0x00;
+                       outbuf[1]=0x00;
+                       outbuf[2]=0x00;
+                       outbuf[3]=0x00;
+                       outbuf[6]=0x00;
+                       outbuf[7]=0x00;
+               }
+               else if(!(P2IN&0x20)) //button C pressed at start?
+               {
+                       mode=MODE_DEBUG;
+                       settime[1]=0x00;
+                       changed=0;
+               }
+               else
+                       mode=MODE_TIME;
+               
+               __enable_interrupt();
+               
+               
+               while(1)
+               {
+                       lpm3();
+               }
+       }
index 6c04be10618946d9d558d50a445f4d404ccd6f6d..55c300b2345cf8a759a575500a614d4dd417aafa 100644 (file)
--- a/makefile
+++ b/makefile
@@ -1,6 +1,7 @@
-# SilentTimer Action Figure software, v1.4
+# SilentTimer Action Figure software, v1.5
 # makefile
 # 
+# 29.02.2016
 # Copyright (C) 2014-2016  Balthasar Szczepański
 # 
 # This program is free software: you can redistribute it and/or modify
 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
 CC=msp430-gcc
-CF=-Os -mmcu=msp430f1232
-OC=msp430-objcopy
-OF=-O ihex
+CF=-g -Wa,-g,-L -Os -mmcu=msp430f1232
+OC=msp430-objcopy
+OF=-O ihex
 
-all: st.elf st.hex
+all: st.elf
 
-st.elf: main.c asm.S definitions.h makefile
-       $(CC) $(CF) -o st.elf asm.S main.c
+main.o: main.c definitions.h makefile
+       msp430-gcc -g -c -Os -mmcu=msp430f1232 -o main.o main.c
+
+asmp.s: asm.S makefile
+       msp430-gcc    -E     -mmcu=msp430f1232 -o asmp.s asm.S
+
+asm.o: asmp.s makefile
+       msp430-as  -g        -mmcu=msp430f1232 -o asm.o  asmp.s
+
+st.elf: main.o asm.o makefile
+       msp430-gcc -g        -mmcu=msp430f1232 -o st.elf main.o asm.o
+#st.elf: main.c asm.S definitions.h makefile
+#      $(CC) $(CF) -o st.elf asm.S main.c
        
-st.hex: st.elf makefile
-       $(OC) $(OF) st.elf st.hex
+st.hex: st.elf makefile
+#      $(OC) $(OF) st.elf st.hex