diff --git a/src/debug.cpp b/src/debug.cpp new file mode 100644 index 0000000..ad8bcc1 --- /dev/null +++ b/src/debug.cpp @@ -0,0 +1,36 @@ +/* + * debug.cpp + * + * Created on: 24.05.2014 + * Author: wn + */ + +#include "debug.h" +#include + + +void debugInit() { +#ifdef DEBUG + DEBUG_DIR_REG |= DEBUG_CS | DEBUG_CLK | DEBUG_OUT; + DEBUG_OUT_REG |= DEBUG_CS; + DEBUG_OUT_REG &= ~DEBUG_CLK; +#endif +} + +void debugWrite(uint16_t o) { +#ifdef DEBUG + DEBUG_OUT_REG &= ~DEBUG_CS; + + for (uint16_t i = 0; i < 16; i++) { + if (((o << i) & 0x8000) == 0x8000) { + DEBUG_OUT_REG |= DEBUG_OUT; + } else { + DEBUG_OUT_REG &= ~DEBUG_OUT; + } + DEBUG_OUT_REG |= DEBUG_CLK; + DEBUG_OUT_REG &= ~DEBUG_CLK; + } + + DEBUG_OUT_REG |= DEBUG_CS; +#endif +} diff --git a/src/debug.h b/src/debug.h new file mode 100644 index 0000000..37101ac --- /dev/null +++ b/src/debug.h @@ -0,0 +1,27 @@ +/* + * debug.h + * + * Created on: 24.05.2014 + * Author: wn + */ + +#ifndef DEBUG_H_ +#define DEBUG_H_ + +#include + +#define DEBUG 1 + + +#define DEBUG_CS BIT0 +#define DEBUG_CLK BIT1 +#define DEBUG_OUT BIT2 +#define DEBUG_DIR_REG P1DIR +#define DEBUG_OUT_REG P1OUT + + +void debugInit(); +void debugWrite(uint16_t o); + + +#endif /* DEBUG_H_ */ diff --git a/src/engine.cpp b/src/engine.cpp index 5564e90..88d22d5 100644 --- a/src/engine.cpp +++ b/src/engine.cpp @@ -11,7 +11,6 @@ #include "engine.h" -volatile uint16_t timestamp = 0; volatile uint32_t tickCnt = 0; const uint16_t TICK = 1024; uint16_t pwmVal = 512; @@ -19,79 +18,54 @@ volatile uint16_t onTime = 500; volatile uint16_t offTime = 200; interrupt (TIMER0_A0_VECTOR) timer0a0_isr(void) { - tickCnt++; - - static uint8_t state0 = 0; - static uint8_t state1 = 0; - - switch (state0) { - case 0: - P1OUT |= BIT0; - state0 = 1; - break; - case 1: - P1OUT &= ~BIT0; - state0 = 0; - break; - default: - state0 = 0; - } - - timestamp++; - switch (state1) { - case 0: - if (timestamp >= onTime) { - timestamp = 0; - P1OUT |= BIT1; - P1SEL &= ~BIT6; - P1OUT &= ~BIT6; - state1 = 1; - } - break; - case 1: - if (timestamp >= offTime) { - timestamp = 0; - P1OUT &= ~BIT1; - P1SEL |= BIT6; - state1 = 0; - } - break; - default: - state1 = 0; - } - - +// static uint8_t state = 0; +// static uint16_t timestamp = 0; +// +// timestamp++; +// +// switch (state) { +// case 0: +// if (timestamp >= onTime) { +// timestamp = 0; +// // P1SEL &= ~BIT6; +// // P1OUT &= ~BIT6; +// state = 1; +// } +// break; +// case 1: +// if (timestamp >= offTime) { +// timestamp = 0; +// // P1SEL |= BIT6; +// state = 0; +// } +// break; +// default: +// state = 0; +// } +// +// } void engineInit() { - timestamp = 0; - - P1DIR |= BIT0 | BIT1 | BIT6; - P1OUT = 0; + P1DIR |= BIT6; + P1OUT &= ~BIT6; P1SEL |= BIT6; - +// TACCR0 = TICK; TACCR1 = 0; - +// TACCTL0 = CCIE; TACCTL1 = OUTMOD_7; + //TACTL = MC_1 | ID_0 | TASSEL_2 | TACLR | TAIE; TACTL = MC_1 | ID_0 | TASSEL_2 | TACLR | TAIE; } -uint32_t ticks() { - __disable_interrupt(); - uint32_t t = tickCnt; - __enable_interrupt(); - - return t; -} - uint16_t engineMaxPwmValue() { return TICK; } void engineSetPwmValue(uint16_t val) { - TACCR1 = val; + //TACCR1 = val; } diff --git a/src/engine.h b/src/engine.h index 93495b8..87f94e1 100644 --- a/src/engine.h +++ b/src/engine.h @@ -11,7 +11,6 @@ void engineInit(); -uint32_t ticks(); uint16_t engineMaxPwmValue(); void engineSetPwmValue(uint16_t val); diff --git a/src/hmi.cpp b/src/hmi.cpp index a1ca7fd..be50e9a 100644 --- a/src/hmi.cpp +++ b/src/hmi.cpp @@ -3,47 +3,48 @@ #include "hmi.h" #include "engine.h" -#include "uart.h" - - +#include "debug.h" class SwitchPort { public: - enum e_Value { e_NIL, e_SHORT, e_LONG }; SwitchPort(uint16_t bit, uint16_t inRegister) : m_bit(bit), m_state(0), m_cnt(0), m_inRegister(inRegister) { }; - e_Value get() { - e_Value result = e_NIL; + bool get() { + bool result = false; + bool inValue = false; + switch (m_inRegister) { + case 1: + inValue = P1IN & m_bit; + break; + case 2: + inValue = P2IN & m_bit; + break; + } switch (m_state) { case 0: - //uartWrite('0'); - if (getInValue()) { - m_state = 1; + + if (inValue) { + m_cnt = 0; } else { m_cnt++; } + if (m_cnt > 100) { + m_state = 1; + m_cnt = 0; + } break; case 1: - //uartWrite('1'); - if (m_cnt > LONG_THRESHOLD) { - result = e_LONG; - m_state = 2; - } else if (m_cnt > SHORT_THRESHOLD) { - result = e_SHORT; - } else { - m_state = 0; - } - m_cnt = 0; + result = true; + m_state = 2; break; case 2: - //uartWrite('2'); - if (getInValue()) { + if (inValue) { m_cnt++; } else { m_cnt = 0; } - if (m_cnt > COOLDOWN_THRESHOLD) { + if (m_cnt > 20) { m_state = 0; m_cnt = 0; } @@ -54,25 +55,13 @@ public: } return result; }; -protected: - virtual bool getInValue() { - //uartWrite('x'); - return true; - }; - uint8_t m_inRegister; private: - static const uint16_t SHORT_THRESHOLD; - static const uint16_t LONG_THRESHOLD; - static const uint16_t COOLDOWN_THRESHOLD; uint16_t m_bit; uint8_t m_state; uint16_t m_cnt; + uint8_t m_inRegister; }; -const uint16_t SwitchPort::SHORT_THRESHOLD = 100; -const uint16_t SwitchPort::LONG_THRESHOLD = 300; -const uint16_t SwitchPort::COOLDOWN_THRESHOLD = 20; - class SwitchPort2 : public SwitchPort { public: SwitchPort2(uint16_t bit) : SwitchPort(bit, 2) { @@ -80,12 +69,6 @@ public: P2DIR &= ~bit; P2SEL &= ~bit; }; -protected: - virtual bool getInValue() { - bool r = P2IN & m_inRegister; - if (! r) uartWrite('-'); - return r; - }; }; class SwitchPort1 : public SwitchPort { public: @@ -94,18 +77,13 @@ public: P1DIR &= ~bit; P1SEL &= ~bit; }; -protected: - virtual bool getInValue(uint8_t bit) { - bool r = P1IN & m_inRegister; - if (! r) uartWrite('-'); - return r; - }; }; + SwitchPort intensitySwitch = SwitchPort2(BIT1); -SwitchPort onTimeSwitch = SwitchPort2(BIT2); -SwitchPort offTimeSwitch = SwitchPort2(BIT3); -SwitchPort modeSwitch = SwitchPort2(BIT4); +//SwitchPort onTimeSwitch = SwitchPort2(BIT2); +//SwitchPort offTimeSwitch = SwitchPort2(BIT3); +//SwitchPort modeSwitch = SwitchPort2(BIT4); int16_t intensity = 0; uint16_t maxIntensity = 0; @@ -116,21 +94,20 @@ const uint16_t intensityStepCnt = 8; void hmiInit() { maxIntensity = engineMaxPwmValue(); intensityStep = maxIntensity / intensityStepCnt; + + P1DIR |= BIT4 | BIT5; + P1OUT &= ~(BIT4 | BIT5); } void hmiExec() { - if (SwitchPort::e_SHORT == intensitySwitch.get()) { + engineSetPwmValue(512); + + if (intensitySwitch.get()) { intensity += intensityStep; if (intensity > maxIntensity) { intensity = maxIntensity; } - - engineSetPwmValue(intensity); - } else if (SwitchPort::e_LONG == intensitySwitch.get()) { - intensity -= intensityStep; - if (intensity < 0) { - intensity = 0; - } + debugWrite(intensity); engineSetPwmValue(intensity); } } diff --git a/src/main.cpp b/src/main.cpp index 78677dc..26d5d25 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -12,26 +12,40 @@ #include "engine.h" #include "hmi.h" -#include "uart.h" +#include "debug.h" int main() { - WDTCTL = WDTPW | WDTHOLD; + WDTCTL = WDTPW | WDTHOLD; + + // highest possible system clock +// DCOCTL = DCO0 | DCO1 | DCO2; +// BCSCTL1 = XT2OFF | RSEL0 | RSEL1 | RSEL2 | RSEL3; +// BCSCTL2 = 0; +// BCSCTL3 = 0; - //engineInit(); - hmiInit(); - uartInit(); + P1DIR |= BIT7; + P1OUT &= ~BIT7; + P1OUT |= BIT7; - __enable_interrupt(); + engineInit(); + hmiInit(); + debugInit(); + + __enable_interrupt(); - uartWrite('W'); + debugWrite(0); - while (1) { - hmiExec(); - //uartWrite('.'); - } + + + while (1) { + P1OUT &= ~BIT7; + P1OUT |= BIT7; + + hmiExec(); + } } diff --git a/src/uart.cpp b/src/uart.cpp deleted file mode 100644 index 06c3559..0000000 --- a/src/uart.cpp +++ /dev/null @@ -1,128 +0,0 @@ -#include -#include -#include - -#include "uart.h" - - - -volatile uint8_t txBuffer[UART_TX_BUFFER_SIZE+5]; - -volatile uint8_t txBufferReadIdx = 0; -volatile uint8_t txBufferWriteIdx = 0; - -volatile uint8_t rxBuffer[UART_RX_BUFFER_SIZE+5]; -volatile uint8_t rxBufferReadIdx = 0; -volatile uint8_t rxBufferWriteIdx = 0; - - -static inline void enableDataRegisterEmptyInterrupt() { - IE2 |= UCA0TXIE; -} - -static inline void disableDataRegisterEmptyInterrupt() { - IE2 &= ~UCA0TXIE; -} - -void uartInit() { - UCA0CTL1 |= UCSWRST; - - P1SEL = BIT1 + BIT2; // select secondary function TX, RX - P1SEL2 = BIT1 + BIT2; // dti - - UCA0CTL0 |= UCPEN | UCPAR; // even parity - UCA0CTL1 |= UCSSEL0; // ACLK - - UCA0BR0 = 13; // divider for 2400@32768 - UCA0BR1 = 0; - - UCA0MCTL = UCBRS1 | UCBRS2; // modulator for 2400@32768 - - UCA0CTL1 &= ~UCSWRST; - - IE2 |= UCA0RXIE; -} - - -void uartWrite(uint8_t o) { - if (txBufferWriteIdx == (UART_TX_BUFFER_SIZE - 1)) { - while (txBufferReadIdx == UART_TX_BUFFER_SIZE); - } else { - while (txBufferReadIdx == (txBufferWriteIdx + 1)); - } - - txBuffer[txBufferWriteIdx] = o; - txBufferWriteIdx++; - - if (txBufferWriteIdx > UART_TX_BUFFER_SIZE) { - txBufferWriteIdx = 0; - } - - enableDataRegisterEmptyInterrupt(); -} - - -//int uartPutchar(char c, FILE *stream) { -// if (c == '\n') -// uartPutchar('\r', stream); -// uartWrite((uint8_t) c); -// return 0; -//} - - - -ISR(USCIAB0TX, UART_TX_ISR) { - if ((IFG2 & UCA0TXIE) != 0) { - if (txBufferReadIdx != txBufferWriteIdx) { - UCA0TXBUF = txBuffer[txBufferReadIdx]; - txBufferReadIdx++; - if (txBufferReadIdx > UART_TX_BUFFER_SIZE) { - txBufferReadIdx = 0; - } - } else { - disableDataRegisterEmptyInterrupt(); - } - } -} - -ISR(USCIAB0RX, UART_RX_ISR) { - while ((IFG2 & UCA0RXIE) != 0) { - if (rxBufferWriteIdx == UART_RX_BUFFER_SIZE - 1) { - if (rxBufferReadIdx == UART_RX_BUFFER_SIZE) { - // rx buffer overflow - } - } else { - if (rxBufferReadIdx == rxBufferWriteIdx + 1) { - // rx buffer overflow - } - } - - rxBuffer[rxBufferWriteIdx] = UCA0RXBUF; - rxBufferWriteIdx++; - - if (rxBufferWriteIdx > UART_RX_BUFFER_SIZE) { - rxBufferWriteIdx = 0; - } - } -} - -uint8_t uartHasChar() { - return rxBufferWriteIdx != rxBufferReadIdx; -} - -uint8_t uartGetChar() { - uint8_t c = rxBuffer[rxBufferReadIdx]; - rxBufferReadIdx++; - if (rxBufferReadIdx > UART_RX_BUFFER_SIZE) { - rxBufferReadIdx = 0; - } - return c; -} - -int uartRead() { - int res = -1; - if (uartHasChar()) { - res = uartGetChar(); - } - return res; -} diff --git a/src/uart.h b/src/uart.h deleted file mode 100644 index 4e361d5..0000000 --- a/src/uart.h +++ /dev/null @@ -1,22 +0,0 @@ -#ifndef UART_H_ -#define UART_H_ - -#include -// #include - - - -#define UART_TX_BUFFER_SIZE 32 -#define UART_RX_BUFFER_SIZE 16 - - -void uartInit(); -// int uartPutchar(char c, FILE *stream); -void uartWrite(uint8_t o); - -uint8_t uartHasChar(); -uint8_t uartGetChar(); - -int uartRead(); - -#endif /* UART_H_ */