not yet working

This commit is contained in:
hg 2016-01-26 22:18:25 +01:00
parent b96045e0c9
commit f4df422832
8 changed files with 155 additions and 278 deletions

36
src/debug.cpp Normal file
View File

@ -0,0 +1,36 @@
/*
* debug.cpp
*
* Created on: 24.05.2014
* Author: wn
*/
#include "debug.h"
#include <msp430g2553.h>
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
}

27
src/debug.h Normal file
View File

@ -0,0 +1,27 @@
/*
* debug.h
*
* Created on: 24.05.2014
* Author: wn
*/
#ifndef DEBUG_H_
#define DEBUG_H_
#include <stdint.h>
#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_ */

View File

@ -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;
}

View File

@ -11,7 +11,6 @@
void engineInit();
uint32_t ticks();
uint16_t engineMaxPwmValue();
void engineSetPwmValue(uint16_t val);

View File

@ -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);
}
}

View File

@ -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();
}
}

View File

@ -1,128 +0,0 @@
#include <msp430g2553.h>
#include <stdio.h>
#include <isr_compat.h>
#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;
}

View File

@ -1,22 +0,0 @@
#ifndef UART_H_
#define UART_H_
#include <stdint.h>
// #include <stdio.h>
#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_ */