From 577488e08517172c1e926b6f3fb15a4ca9f6633f Mon Sep 17 00:00:00 2001 From: Dimitri Sokolyuk Date: Sun, 13 Mar 2011 23:50:49 +0000 Subject: admux --- kernel/Makefile | 4 +--- kernel/adc.c | 23 ++++++++++++++--------- kernel/kernel.h | 20 -------------------- kernel/lcd3.c | 11 ++++++----- kernel/main.c | 25 ++++++++++--------------- kernel/rgb.c | 20 ++++++-------------- kernel/tasks.h | 34 +++++++++++++++++++++++++++++----- 7 files changed, 66 insertions(+), 71 deletions(-) (limited to 'kernel') diff --git a/kernel/Makefile b/kernel/Makefile index 3ef5bc3..0807a73 100644 --- a/kernel/Makefile +++ b/kernel/Makefile @@ -10,7 +10,6 @@ STACK = 64 TASKS = 8 SEMAPHORES = 8 BAUD = 9600 -ADCPRESCALE = 16 # You should not have to change anything below here. @@ -21,8 +20,7 @@ SIZE = avr-size OBJS = ${SRCS:.c=.o} CFLAGS = -Wall -Os -mmcu=${MCU_TARGET} \ -DF_CPU=${F_CPU} -DPRESCALE=${PRESCALE} -DBAUD=${BAUD} \ - -DSTACK=${STACK} -DTASKS=${TASKS} -DSEMAPHORES=${SEMAPHORES} \ - -DADCPRESCALE=${ADCPRESCALE} + -DSTACK=${STACK} -DTASKS=${TASKS} -DSEMAPHORES=${SEMAPHORES} LDFLAGS = -Wl,-Map,${PROG}.map .SUFFIXES: .elf .lst .hex .bin .srec .ehex .ebin .esrec diff --git a/kernel/adc.c b/kernel/adc.c index 8f2164d..0037756 100644 --- a/kernel/adc.c +++ b/kernel/adc.c @@ -20,13 +20,15 @@ #include "kernel.h" #include "tasks.h" +#define MUXMASK 0x07 + uint16_t rdadc(uint8_t ch) { - ADMUX &= ~0x07; - ADMUX |= (ch & 0x07); + ADMUX &= ~MUXMASK; + ADMUX |= (ch & MUXMASK); ADCSRA |= _BV(ADSC); - loop_until_bit_is_set(ADCSRA, ADSC); + loop_until_bit_is_clear(ADCSRA, ADSC); return ADCW; } @@ -35,14 +37,17 @@ void adc(void *arg) { struct adcarg *a = (struct adcarg *)arg; - a->r = release(); - a->d = deadline(); + uint32_t r = release(); + uint32_t d = deadline(); + uint8_t i; - ADCSRA |= (_BV(ADEN) | _BV(ADFR) | ADC_FLAGS); + ADCSRA |= (_BV(ADEN) | ADC_FLAGS); + /* ADMUX |= _BV(REFS0); */ for (;;) { - *a->value = rdadc(a->channel); - a->r = a->d += MSEC(20); - update(a->r, a->d); + for (i = 0; i < ADCCHANNELS; i++) + a->value[i] = rdadc(i); + r = d += MSEC(1); + update(r, d); } } diff --git a/kernel/kernel.h b/kernel/kernel.h index 866ed5c..feab5b0 100644 --- a/kernel/kernel.h +++ b/kernel/kernel.h @@ -57,26 +57,6 @@ #warning "invalid PRESCALE value" #endif -#if (ADCPRESCALE == 1) -#define ADC_FLAGS 0 -#elif (ADCPRESCALE == 2) -#define ADC_FLAGS _BV(ADPS0) -#elif (ADCPRESCALE == 4) -#define ADC_FLAGS _BV(ADPS1) -#elif (ADCPRESCALE == 8) -#define ADC_FLAGS (_BV(ADPS1) | _BV(ADPS0)) -#elif (ADCPRESCALE == 16) -#define ADC_FLAGS _BV(ADPS2) -#elif (ADCPRESCALE == 32) -#define ADC_FLAGS (_BV(ADPS2) | _BV(ADPS0)) -#elif (ADCPRESCALE == 64) -#define ADC_FLAGS (_BV(ADPS2) | _BV(ADPS1)) -#elif (ADCPRESCALE == 128) -#define ADC_FLAGS (_BV(ADPS2) | _BV(ADPS1) | _BV(ADPS0)) -#else -#warning "invalid ADCPRESCALE value" -#endif - #define SEC(T) ((uint32_t)((T) * (F_CPU / PRESCALE))) #define MSEC(T) ((uint32_t)(T) * ((F_CPU / 1000) / PRESCALE)) #define USEC(T) ((uint32_t)(T) * ((F_CPU / 1000000) / PRESCALE)) diff --git a/kernel/lcd3.c b/kernel/lcd3.c index 2259da8..b4125b1 100644 --- a/kernel/lcd3.c +++ b/kernel/lcd3.c @@ -145,6 +145,7 @@ itohex(uint32_t x) void lcd(void *arg) { + struct lcdarg *a = (struct lcdarg *)arg; uint8_t i, t; PORTDIR |= (_BV(DATA) | _BV(CLOCK) | _BV(E)); @@ -167,21 +168,21 @@ lcd(void *arg) /* entry mode */ write_cmd(ENTRY_MODE_SET | INC_DDRAM, 39); - snooze(100); + home(); mvputs(0, 0, "ADC0"); - mvputs(1, 0, "Time"); + mvputs(1, 0, "ADC1"); for (;;) { - extern uint16_t adcval; /* t = previous() - 1; // 0 is idle for (i = 0; i < TASKS; i++) mvputch(0, 5 + i, t == i ? '1' + t : '-'); - */ - mvputs(0, 5, itohex(adcval)); mvputs(1, 5, itohex(now())); + */ + mvputs(0, 5, itohex(a->adc[0])); + mvputs(1, 5, itohex(a->adc[1])); lcdargs.r = lcdargs.d + MSEC(100); lcdargs.d += MSEC(50); diff --git a/kernel/main.c b/kernel/main.c index bbcae3c..1588254 100644 --- a/kernel/main.c +++ b/kernel/main.c @@ -21,21 +21,16 @@ #include "tasks.h" uint8_t red, green, blue; -uint16_t adcval; - -struct rgbarg rgbargs[] = { - { &red, &green, &blue }, -}; +uint16_t adcval[ADCCHANNELS]; +struct rgbarg rgbargs = { &red, &green, &blue }; struct pwmarg pwmargs[] = { - { &red, PB2, 0, 0 }, - { &green, PB3, 0, 0 }, - { &blue, PB4, 0, 0 }, -}; - -struct adcarg adcarg[] = { - { 0, 0, 0, &adcval }, + { &red, PB2 }, + { &green, PB3 }, + { &blue, PB4 } }; +struct adcarg adcarg = { adcval }; +struct lcdarg lcdarg = { 0, 0, adcval }; int main() @@ -45,12 +40,12 @@ main() init_uart(); task(heartbeat, STACK, SEC(0), MSEC(10), 0); - task(rgb, STACK, SEC(0), MSEC(10), &rgbargs[0]); + task(rgb, STACK, SEC(0), MSEC(10), &rgbargs); task(cpwm, STACK, SEC(0), MSEC(10), &pwmargs[0]); task(cpwm, STACK, SEC(0), MSEC(10), &pwmargs[1]); task(cpwm, STACK, SEC(0), MSEC(10), &pwmargs[2]); - task(lcd, STACK, MSEC(40), SEC(1), 0); - task(adc, STACK, MSEC(0), MSEC(20), &adcarg[0]); + task(lcd, STACK, MSEC(40), SEC(1), &lcdarg); + task(adc, STACK, MSEC(0), MSEC(20), &adcarg); for (;;); diff --git a/kernel/rgb.c b/kernel/rgb.c index 23e3b9f..06bd694 100644 --- a/kernel/rgb.c +++ b/kernel/rgb.c @@ -22,14 +22,6 @@ void hsv(uint8_t *, uint8_t *, uint8_t *, uint16_t, uint8_t, uint8_t); -/* -struct pwmarg pwmargs[] = { - { &red, PB2, 0, 0 }, - { &green, PB3, 0, 0 }, - { &blue, PB4, 0, 0 }, -}; - */ - void rgb(void *arg) { @@ -57,8 +49,8 @@ cpwm(void *arg) { #define SCALE 4 struct pwmarg *a = (struct pwmarg *)arg; - a->d = deadline(); - a->r = release(); + uint32_t d = deadline(); + uint32_t r = release(); DDRB |= _BV(a->pin); PORTB &= ~_BV(a->pin); @@ -66,14 +58,14 @@ cpwm(void *arg) for (;;) { if (*a->value > 0) { PORTB |= _BV(a->pin); - a->r = a->d += USEC(*a->value << SCALE); - update(a->r, a->d); + r = d += USEC(*a->value << SCALE); + update(r, d); } if (*a->value < 255) { PORTB &= ~_BV(a->pin); - a->r = a->d += USEC((255 - *a->value) << SCALE); - update(a->r, a->d); + r = d += USEC((255 - *a->value) << SCALE); + update(r, d); } } } diff --git a/kernel/tasks.h b/kernel/tasks.h index 4137402..cf386c7 100644 --- a/kernel/tasks.h +++ b/kernel/tasks.h @@ -18,6 +18,29 @@ #ifndef __TASKS_H #define __TASKS_H +#define ADCCHANNELS 6 +#define ADCPRESCALE 32 + +#if (ADCPRESCALE == 1) +#define ADC_FLAGS 0 +#elif (ADCPRESCALE == 2) +#define ADC_FLAGS _BV(ADPS0) +#elif (ADCPRESCALE == 4) +#define ADC_FLAGS _BV(ADPS1) +#elif (ADCPRESCALE == 8) +#define ADC_FLAGS (_BV(ADPS1) | _BV(ADPS0)) +#elif (ADCPRESCALE == 16) +#define ADC_FLAGS _BV(ADPS2) +#elif (ADCPRESCALE == 32) +#define ADC_FLAGS (_BV(ADPS2) | _BV(ADPS0)) +#elif (ADCPRESCALE == 64) +#define ADC_FLAGS (_BV(ADPS2) | _BV(ADPS1)) +#elif (ADCPRESCALE == 128) +#define ADC_FLAGS (_BV(ADPS2) | _BV(ADPS1) | _BV(ADPS0)) +#else +#warning "invalid ADCPRESCALE value" +#endif + struct rgbarg { uint8_t *r, *g, *b; }; @@ -25,17 +48,18 @@ struct rgbarg { struct pwmarg { uint8_t *value; uint8_t pin; - uint32_t d; - uint32_t r; }; struct adcarg { - uint32_t d; - uint32_t r; - uint8_t channel; uint16_t *value; }; +struct lcdarg { + char *first; + char *second; + uint16_t *adc; +}; + void init_uart(void); void heartbeat(void *); -- cgit v1.2.3