From 8a8bb762e0638d2083e55b26b6aefde541649d6f Mon Sep 17 00:00:00 2001 From: Jens Ahrensfeld Date: Thu, 28 Feb 2019 19:18:40 +0000 Subject: [PATCH] - adc is event based - changed timer frequency - prepared for SW timer - added PowerSwitch_in git-svn-id: http://moon:8086/svn/projects/HendiControl@30 fda53097-d464-4ada-af97-ba876c37ca34 --- Control/firmware/uart_echo/adc.c | 35 ++++- Control/firmware/uart_echo/adc.h | 8 +- Control/firmware/uart_echo/main.c | 146 ++++++++++----------- Control/firmware/uart_echo/port.h | 3 +- Control/firmware/uart_echo/timer.c | 4 +- Control/firmware/uart_echo/timer.h | 16 ++- Control/firmware/uart_echo/uart_echo.cproj | 3 + 7 files changed, 133 insertions(+), 82 deletions(-) diff --git a/Control/firmware/uart_echo/adc.c b/Control/firmware/uart_echo/adc.c index 365acc5..d051fbe 100644 --- a/Control/firmware/uart_echo/adc.c +++ b/Control/firmware/uart_echo/adc.c @@ -5,19 +5,40 @@ * Author: jens */ +#include #include +#include #include "adc.h" +#include "message.h" -void adc_init() +static int16_t adc_value[16]; +static size_t adc_ch = 0; +static Fifo *g_pFifo = NULL; + +ISR(ADC_vect) { + int16_t value_low = (int16_t)ADCL; + int16_t value_high = (int16_t)ADCH; + ADCSRA |= (1< #include "fifo.h" +#include "message.h" #include "adc.h" #include "port.h" #include "timer.h" @@ -21,7 +22,6 @@ #include "mcp42x6.h" #define ARDUINO_NANO 1 -#define WITH_ADC_EVENTS 0 #define WITH_COMMAND_EVENTS 0 #define PRINT_PROMPT printf("\n:") @@ -32,51 +32,17 @@ #endif #define USART_BAUDRATE 38400UL #define BAUD_PRESCALE (((F_CPU / (USART_BAUDRATE * 16UL))) - 1) -#define TIMER_RELOAD (0xFFFF-(F_CPU/1024/4-1)) +#define TIMER_HW_CLOCK (F_CPU/8) +#define TIMER_IRQ_CLOCK 1000 +#define TIMER_SW_CLOCK 10 +#define TIMER_SW_RELOAD (TIMER_IRQ_CLOCK/TIMER_SW_CLOCK) + +#define TIMER_HW_RELOAD (0xFFFF-(TIMER_HW_CLOCK/TIMER_IRQ_CLOCK)) static uint8_t ledState = 0; -static int16_t adc_value[16]; -static size_t adc_ch = 0; +static int16_t adc_last = 0; +static int pwr_Switch_state_last = 0; static Fifo messageFifo; - -enum MessageCode -{ - NOP=0, - Uart, - Timer, - AdcComplete, - Command, - Error = 0xFF -}; - -typedef struct _sMsg_t -{ - uint8_t code; - uint8_t data; -} Msg_t; - -ISR(ADC_vect) -{ -#if WITH_ADC_EVENTS - int16_t value_low = (int16_t)ADCL; - int16_t value_high = (int16_t)ADCH; - ADCSRA |= (1< 1)) - { - Msg_t msg = {AdcComplete, adc_ch}; - fifo_push(&messageFifo, &msg); - } - - adc_value[adc_ch] = value; - adc_ch++; - if (adc_ch == 16) - { - adc_ch = 0; - } - adc_start_conversion(adc_ch); -#endif -} +static int timer_count = 0; ISR(USART_RX_vect) { @@ -99,15 +65,24 @@ ISR(USART_RX_vect) ISR (TIMER1_OVF_vect) // Timer1 ISR { - Msg_t msg = {Timer, ledState}; - fifo_push(&messageFifo, &msg); + TCNT1 = TIMER_HW_RELOAD; - TCNT1 = TIMER_RELOAD; - ledState++; - if (ledState == 4) + Msg_t msg = {Timer, ledState}; + if (timer_count == 0) { - ledState = 0; - } + timer_count = TIMER_SW_RELOAD; + fifo_push(&messageFifo, &msg); + ledState++; + if (ledState == 4) + { + ledState = 0; + } + } + else + { + timer_count--; + } + } FILE uart_file = FDEV_SETUP_STREAM(uart_putchar, NULL, _FDEV_SETUP_WRITE); @@ -119,8 +94,8 @@ int main(void) /* Replace with your application code */ uart_init(BAUD_PRESCALE); - timer_init(TIMER_RELOAD); - adc_init(); + timer_init(TIMER_HW_RELOAD, TimerClockSel_PS_8); + adc_init(&messageFifo); i2c_init(); port_init(); @@ -132,7 +107,7 @@ int main(void) i2c_send(0x60, I2C_WRITE, dac_cmd, res); sei(); - adc_start_conversion(adc_ch); + adc_start_conversion(0x06); char cmdStr[16]; size_t cmdStrSize = 0; @@ -172,7 +147,7 @@ int main(void) if (toupper(cmdStr[0]) == 'S') { uint16_t arg = atol(cmdStr+1); - portSet(PwrSwitch, arg != 0); + portSet(PwrSwitch_out, arg != 0); } cmdStrSize = 0; PRINT_PROMPT; @@ -202,32 +177,55 @@ int main(void) case AdcComplete: { - size_t ch = msg.data; - if (ch == 6) + if (adc_getChannel() == 6) { - printf("ADC=%d\n", adc_value[ch]); - uint8_t dac_cmd[] = {0x00, 0x00}; // 6.2 Write Volatile Memory (C2:C0 = ‘010’) - size_t res = MCP47x6_write_volatile_dac(adc_value[ch]<<2, dac_cmd, sizeof(dac_cmd)); - i2c_send(0x60, I2C_WRITE, dac_cmd, res); + int16_t adc_curr = adc_getData(); + if (((adc_curr - adc_last) < -1) || ((adc_curr - adc_last) > 1)) + { + printf("ADC=%d\n", adc_curr); + uint8_t dac_cmd[] = {0x00, 0x00}; // 6.2 Write Volatile Memory (C2:C0 = ‘010’) + size_t res = MCP47x6_write_volatile_dac(adc_curr<<2, dac_cmd, sizeof(dac_cmd)); + i2c_send(0x60, I2C_WRITE, dac_cmd, res); + adc_last = adc_curr; + } } } break; case Timer: - switch(msg.data) { - case 0: - PORTB = (PORTB & 0xFC) | 0x00; - break; - case 1: - PORTB = (PORTB & 0xFC) | 0x01; - break; - case 2: - PORTB = (PORTB & 0xFC) | 0x03; - break; - case 3: - PORTB = (PORTB & 0xFC) | 0x02; - break; + switch(msg.data) + { + case 0: + PORTB = (PORTB & 0xFC) | 0x00; + break; + case 1: + PORTB = (PORTB & 0xFC) | 0x01; + break; + case 2: + PORTB = (PORTB & 0xFC) | 0x03; + break; + case 3: + PORTB = (PORTB & 0xFC) | 0x02; + break; + } +#if 0 + int state = portGet(PwrSwitch_in); + + if (state != pwr_Switch_state_last) + { + if (state) + { + printf("Power switch is ON\n"); + } + else + { + printf("Power switch is OFF\n"); + } + } + pwr_Switch_state_last = state; +#endif + adc_start_conversion(0x06); } break; diff --git a/Control/firmware/uart_echo/port.h b/Control/firmware/uart_echo/port.h index a618700..2783a08 100644 --- a/Control/firmware/uart_echo/port.h +++ b/Control/firmware/uart_echo/port.h @@ -14,7 +14,8 @@ enum PortItem Led0 = 0, Led1 = 1, Led2 = 2, - PwrSwitch = 3, + PwrSwitch_out = 3, + PwrSwitch_in = 4, NUM_ITEMS }; diff --git a/Control/firmware/uart_echo/timer.c b/Control/firmware/uart_echo/timer.c index 1aaa832..f66eb68 100644 --- a/Control/firmware/uart_echo/timer.c +++ b/Control/firmware/uart_echo/timer.c @@ -8,13 +8,13 @@ #include #include "timer.h" -void timer_init(uint16_t TIMER_RELOAD) +void timer_init(uint16_t TIMER_RELOAD, TimerClockSel clockSel) { TCNT1 = TIMER_RELOAD; TCCR1A = 0x00; - TCCR1B = (1< compile + + compile + compile