some important changes

This commit is contained in:
Wolfgang Hottgenroth 2020-08-27 22:06:45 +02:00
parent 242d723fd8
commit 67131408cd
Signed by: wn
GPG Key ID: B586EAFCDF2F65F4

85
main.c
View File

@ -12,14 +12,13 @@
* conversion result from the ADC is saved and held until the
* COMM_SAMPLE_HOLD_BIT goes to low again.
*
* When the COMM_ENABLE_BIT is pulled to high and an earlier ADC value
* is currently held, it is checked whether the current ADC value is
* by SPACE_MARK_THRESHOLD greater then the held value.
* If it is greater, the COMM_RESULT_BIT is set to high, otherwise
* it is set to low.
* While the COMM_SAMPLE_HOLD_BIT is pulled to high it is checked
* whether the current ADC value is by SPACE_MARK_THRESHOLD greater
* then the held value. If it is greater, the COMM_RESULT_BIT is set
* to low, otherwise it is set to high.
*
* If either the COMM_ENABLE_BIT or the COMM_SAMPLE_HOLD_BIT goes to
* low the COMM_RESULT_BIT is set to low.
* If COMM_SAMPLE_HOLD_BIT goes to low the COMM_RESULT_BIT is set to
* high.
*/
@ -36,15 +35,45 @@
#define COMM_PORT_OUT P2OUT
#define COMM_PORT_IN P2IN
#define COMM_PORT_REN P2REN
#define COMM_PORT_DIR P2DIR
#define COMM_RESULT_BIT BIT2 // P2.2
#define COMM_SAMPLE_HOLD_BIT BIT3 // P2.3
#define COMM_ENABLE_BIT BIT4 // P2.4
// derived from the calculation in mbus-converter.ods
// assumes a 25Ohm shunt resistor and a 10mA swing
#define SPACE_MARK_THRESHOLD 100
// SPI debug codes
#define DBG_START 0xffffffff
#define DBG_HOLD 0x01000000
#define DBG_SAMPLE 0x02000000
#define DBG_RX_SLOPE 0x04000000
// 32bit-value to transmit via SPI
volatile uint32_t spiTxBuf = 0;
// ISR for SPI transmitter
__attribute__((interrupt(USCIAB0TX_VECTOR)))
void spiTxIsr(void) {
static uint8_t oc = 0;
oc++;
if (oc < 4) {
UCA0TXBUF = (uint8_t)((spiTxBuf >> (8 * oc)) & 0x00ff);
} else {
IE2 &= ~UCA0TXIE;
oc = 0;
}
}
void writeSpi(uint32_t m) {
spiTxBuf = m;
UCA0TXBUF = (uint8_t)(spiTxBuf & 0x00ff);
IE2 |= UCA0TXIE;
}
// ISR to read and process result from adc
__attribute__((interrupt(ADC10_VECTOR)))
@ -59,19 +88,21 @@ void adcIsr(void)
if (! holdFlag) {
holdValue = currentValue;
holdFlag = true;
}
} else {
holdFlag = false;
}
if ((COMM_PORT_IN & COMM_ENABLE_BIT) && holdFlag) {
if (currentValue > (holdValue + SPACE_MARK_THRESHOLD)) {
COMM_PORT_OUT &= ~COMM_RESULT_BIT;
writeSpi(DBG_HOLD | holdValue);
} else {
COMM_PORT_OUT |= COMM_RESULT_BIT;
if (currentValue > (holdValue + SPACE_MARK_THRESHOLD)) {
COMM_PORT_OUT &= ~COMM_RESULT_BIT;
// writeSpi(DBG_RX_SLOPE | currentValue);
} else {
COMM_PORT_OUT |= COMM_RESULT_BIT;
}
}
} else {
COMM_PORT_OUT |= COMM_RESULT_BIT;
if (holdFlag) {
holdFlag = false;
COMM_PORT_OUT |= COMM_RESULT_BIT;
writeSpi(DBG_SAMPLE);
}
}
#ifdef DEBUG
@ -99,14 +130,26 @@ void setup() {
// communication
COMM_PORT_DIR |= COMM_RESULT_BIT;
COMM_PORT_DIR &= ~(COMM_ENABLE_BIT | COMM_SAMPLE_HOLD_BIT);
COMM_PORT_DIR &= ~COMM_SAMPLE_HOLD_BIT;
COMM_PORT_REN |= COMM_SAMPLE_HOLD_BIT;
COMM_PORT_OUT &= ~COMM_SAMPLE_HOLD_BIT;
COMM_PORT_OUT |= COMM_RESULT_BIT;
// adc
ADC10CTL0 = SREF1 | REFON | ADC10ON | ADC10IE | MSC;
ADC10CTL0 = SREF_1 | REFON | ADC10ON | ADC10IE | MSC;
ADC10CTL1 = INCH_3 | CONSEQ_2;
ADC10AE0 = BIT3;
// spi
UCA0CTL1 = UCSWRST | UCSSEL_2;
UCA0CTL0 = UCMST | UCSYNC;
UCA0BR0 = 8;
UCA0BR1 = 0;
P1SEL |= BIT1 | BIT2 | BIT4;
P1SEL2 |= BIT1 | BIT2 | BIT4;
UCA0CTL1 &= ~UCSWRST;
__enable_interrupt();
// start the adc
@ -124,6 +167,8 @@ void loop() {
int main() {
setup();
writeSpi(DBG_START);
while (1) {
loop();
}