Created
May 17, 2014 21:27
-
-
Save lucansky/72fe74a090953d65add8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <msp430.h> | |
#include <stdbool.h> | |
#include <stdint.h> | |
#include "74HC595.h" | |
// Ticks for second, DO NOT CHANGE | |
#define SEC_CLOCK 512 | |
//#define DATA_PIN BIT5 | |
//#define CLOCK_PIN BIT3 | |
//#define LATCH_PIN BIT4 | |
static const uint8_t digit_segments[] = | |
{ 3, 159, 37, 13, 153, 73, 65, 31, 1, 9 }; | |
//(0..511).map{|i| (i*(100/511.0)).to_i} | |
static const uint8_t msecs[SEC_CLOCK] = { 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 2, 2, | |
2, 2, 2, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 6, 6, 6, 6, 6, 7, | |
7, 7, 7, 7, 8, 8, 8, 8, 8, 9, 9, 9, 9, 9, 9, 10, 10, 10, 10, 10, 11, 11, | |
11, 11, 11, 12, 12, 12, 12, 12, 13, 13, 13, 13, 13, 14, 14, 14, 14, 14, | |
15, 15, 15, 15, 15, 16, 16, 16, 16, 16, 17, 17, 17, 17, 17, 18, 18, 18, | |
18, 18, 18, 19, 19, 19, 19, 19, 20, 20, 20, 20, 20, 21, 21, 21, 21, 21, | |
22, 22, 22, 22, 22, 23, 23, 23, 23, 23, 24, 24, 24, 24, 24, 25, 25, 25, | |
25, 25, 26, 26, 26, 26, 26, 27, 27, 27, 27, 27, 27, 28, 28, 28, 28, 28, | |
29, 29, 29, 29, 29, 30, 30, 30, 30, 30, 31, 31, 31, 31, 31, 32, 32, 32, | |
32, 32, 33, 33, 33, 33, 33, 34, 34, 34, 34, 34, 35, 35, 35, 35, 35, 36, | |
36, 36, 36, 36, 36, 37, 37, 37, 37, 37, 38, 38, 38, 38, 38, 39, 39, 39, | |
39, 39, 40, 40, 40, 40, 40, 41, 41, 41, 41, 41, 42, 42, 42, 42, 42, 43, | |
43, 43, 43, 43, 44, 44, 44, 44, 44, 45, 45, 45, 45, 45, 45, 46, 46, 46, | |
46, 46, 47, 47, 47, 47, 47, 48, 48, 48, 48, 48, 49, 49, 49, 49, 49, 50, | |
50, 50, 50, 50, 51, 51, 51, 51, 51, 52, 52, 52, 52, 52, 53, 53, 53, 53, | |
53, 54, 54, 54, 54, 54, 54, 55, 55, 55, 55, 55, 56, 56, 56, 56, 56, 57, | |
57, 57, 57, 57, 58, 58, 58, 58, 58, 59, 59, 59, 59, 59, 60, 60, 60, 60, | |
60, 61, 61, 61, 61, 61, 62, 62, 62, 62, 62, 63, 63, 63, 63, 63, 63, 64, | |
64, 64, 64, 64, 65, 65, 65, 65, 65, 66, 66, 66, 66, 66, 67, 67, 67, 67, | |
67, 68, 68, 68, 68, 68, 69, 69, 69, 69, 69, 70, 70, 70, 70, 70, 71, 71, | |
71, 71, 71, 72, 72, 72, 72, 72, 72, 73, 73, 73, 73, 73, 74, 74, 74, 74, | |
74, 75, 75, 75, 75, 75, 76, 76, 76, 76, 76, 77, 77, 77, 77, 77, 78, 78, | |
78, 78, 78, 79, 79, 79, 79, 79, 80, 80, 80, 80, 80, 81, 81, 81, 81, 81, | |
81, 82, 82, 82, 82, 82, 83, 83, 83, 83, 83, 84, 84, 84, 84, 84, 85, 85, | |
85, 85, 85, 86, 86, 86, 86, 86, 87, 87, 87, 87, 87, 88, 88, 88, 88, 88, | |
89, 89, 89, 89, 89, 90, 90, 90, 90, 90, 90, 91, 91, 91, 91, 91, 92, 92, | |
92, 92, 92, 93, 93, 93, 93, 93, 94, 94, 94, 94, 94, 95, 95, 95, 95, 95, | |
96, 96, 96, 96, 96, 97, 97, 97, 97, 97, 98, 98, 98, 98, 98, 99, 99, 99, | |
99, 99, 100 }; | |
static const uint8_t tens[100] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, | |
1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, | |
3, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 6, 6, 6, | |
6, 6, 6, 6, 6, 6, 6, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 8, 8, 8, 8, 8, 8, 8, | |
8, 8, 8, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9 }; | |
static const uint8_t ones[100] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, | |
5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, | |
9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, | |
3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, | |
7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; | |
struct State { | |
unsigned msec; | |
unsigned mclock; | |
unsigned sec; | |
unsigned min; | |
unsigned hour; | |
} state; | |
struct Display { | |
uint8_t digits[8]; | |
unsigned current_digit; | |
} display; | |
inline void WDT_Reset(void) { | |
WDTCTL = WDTPW + WDTCNTCL + WDTIS0; | |
} | |
int main(void) { | |
BCSCTL1 = CALBC1_1MHZ; | |
DCOCTL = CALDCO_1MHZ; | |
WDT_Reset(); | |
// P1OUT &= ~OUT_BITS; | |
// P1DIR |= OUT_BITS; | |
shift_register_setup(); | |
CCTL0 = CCIE; // CCR0 interrupt enabled | |
CCR0 = (0x8000 / SEC_CLOCK) - 1; | |
TACTL = TASSEL_1 + MC_1; // ACLK, upmode | |
P2OUT = 0x00; | |
P2DIR = ~(BIT0 + BIT1); | |
P3OUT = 0x00; | |
P3DIR = 0xFF; | |
//P1IE |= IN_BITS; // P1.3 interrupt enabled | |
//P1IES |= IN_BITS; // P1.3 Hi/lo edge | |
//P1IFG &= ~IN_BITS; // P1.3 IFG cleared | |
//P1IES &= ~IN_BITS; | |
P2IE |= (BIT0 + BIT1); // P1.3 interrupt enabled | |
P1IES |= BIT0 + BIT1; // P1.3 Hi/lo edge | |
P2IFG &= ~(BIT0 + BIT1); // P1.3 IFG cleared | |
P2IES &= ~(BIT0 + BIT1); | |
_BIS_SR(LPM3_bits + GIE); | |
// LPM3_bits + | |
} | |
static inline void fill_text(void) { | |
display.digits[0] = tens[state.hour]; | |
display.digits[1] = ones[state.hour]; | |
display.digits[2] = tens[state.min]; | |
display.digits[3] = ones[state.min]; | |
display.digits[4] = tens[state.sec]; | |
display.digits[5] = ones[state.sec]; | |
display.digits[6] = tens[state.msec]; | |
display.digits[7] = ones[state.msec]; | |
} | |
static inline void tick() { | |
state.msec = msecs[state.mclock]; | |
WDT_Reset(); | |
fill_text(); | |
if (++state.mclock >= SEC_CLOCK) { | |
// Sekunda | |
state.mclock = 0; | |
if (++state.sec >= 60) { | |
// Overflow min | |
state.sec = 0; | |
if (++state.min >= 60) { | |
// Overflow hour | |
state.min = 0; | |
if (++state.hour >= 24) { | |
// Overflow day | |
state.hour = 0; | |
} | |
} | |
} | |
} | |
} | |
#pragma vector=TIMER0_A0_VECTOR | |
__interrupt void Timer_A(void) { | |
tick(); | |
if (display.current_digit > 7) { | |
display.current_digit = 0; | |
} | |
unsigned pos_en = display.current_digit; | |
uint8_t segments_lit = digit_segments[display.digits[display.current_digit]]; | |
if (pos_en % 2 != 0) | |
segments_lit &= ~1; | |
shift_out_int((1 << pos_en) * 256 + segments_lit); | |
display.current_digit++; | |
} | |
// Port 2 interrupt service routine | |
#pragma vector=PORT2_VECTOR | |
__interrupt void Port_2(void) { | |
//P2OUT ^= P2IN&BIT3; // P1.0 = toggle | |
if (BIT0 & P2IN) { | |
if (++state.min >= 60) | |
state.min = 0; | |
} | |
if (BIT1 & P2IN) { | |
if (++state.hour >= 24) | |
state.hour = 0; | |
} | |
//P2IES ^= BIT3; | |
P2IFG &= ~(BIT0 + BIT1); // P1.3 IFG cleared | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment