From b866ada6b3a87537bdd17c7b1e1e341e7fce1d23 Mon Sep 17 00:00:00 2001 From: Nidan Date: Fri, 13 Jan 2012 22:17:39 +0100 Subject: [PATCH 1/8] feeding read bytes from mmc into ringbuffer --- Makefile | 2 +- gg.c | 28 ++++++--------- mmc.c | 107 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ mmc.h | 10 ++++++ 4 files changed, 128 insertions(+), 19 deletions(-) diff --git a/Makefile b/Makefile index b874eeb..ac49a35 100644 --- a/Makefile +++ b/Makefile @@ -1,7 +1,7 @@ CFLAGS += -Wall -Os -I. -mmcu=attiny26 -std=c99 DEFINES += -DF_CPU=16e6 -OBJECTS = gg.o mmc.o +OBJECTS = gg.o mmc.o ringbuf_small.o # further optimization: # this removes dead code and does global linker optimization diff --git a/gg.c b/gg.c index db968ba..64a095f 100644 --- a/gg.c +++ b/gg.c @@ -2,7 +2,7 @@ #include #include -#include "ringbuf_small.c" +#include "ringbuf_small.h" #include "mmc.h" #include "gg.h" @@ -33,13 +33,8 @@ uint8_t overflows = 1;/* remaining counter overflows until next sample */ #define SAMPLE_BITS 11 #define BUFFER_SIZE 64 -#define REFRESH_AMOUNT 32 -#define REFRESH_SIZE (BUFFER_SIZE - REFRESH_AMOUNT) uint8_t buffer[BUFFER_SIZE];/* buffer for mmc data */ -uint8_t pos = 0;/* current playing position */ -uint8_t refresh_buffer = 0;/* position to start buffer refreshing */ - ISR(TIMER1_OVF1_vect, ISR_NAKED) { __asm__("in r2, 0x3f"); // save sreg @@ -52,7 +47,7 @@ ISR(TIMER1_OVF1_vect, ISR_NAKED) cnt_to_next = 8; needs_new_data_flag = 1; } - __asm__("out 0x2f,r2"); // restore sreg + __asm__("out 0x3f,r2"); // restore sreg reti(); } @@ -61,21 +56,18 @@ ISR(TIMER1_OVF1_vect, ISR_NAKED) int main(void) __attribute__((noreturn)); int main(void) { - uint8_t ref; // TODO: what does this do? + ringbuf_t rb; + ringbuf_init(&rb, buffer, BUFFER_SIZE); if(mmc_init() != 0) {/* mmc fail */;} + + mmc_read_part(0, length, 4); + timer_init_tiny26(); sei(); - for(;;) + unsigned long block = 0; + for(;; block++) { - if(pos == refresh_buffer)/* refresh buffer if running low */ - { - /* TODO: this won't do. we don't have enough time to read unnecessary bytes -> read data with the same speed as we consume it, keeping a small buffer. */ - /* expect 2 bit read from mmc between 2 timer interrupts --> 2 byte read per sample played */ - - mmc_read_sector(mmc_position, buffer + (ref + REFRESH_SIZE) % BUFFER_SIZE);//, BUFFER_SIZE - ref), buffer, REFRESH_AMOUNT - (BUFFER_SIZE - ref)); - /* pos on sd , first buffer address , 1st buffer size ,2nd buf, 2nd buffer size */ - refresh_buffer = (refresh_buffer + REFRESH_AMOUNT) % BUFFER_SIZE; - } + mmc_read_to_ringbuffer(block, &rb); } } diff --git a/mmc.c b/mmc.c index d31b202..2065452 100644 --- a/mmc.c +++ b/mmc.c @@ -187,6 +187,7 @@ void mmc_write_byte (unsigned char Byte) #endif } +#if 0 //############################################################################ //Routine zum schreiben eines Blocks(512Byte) auf die MMC/SD-Karte unsigned char mmc_write_sector (unsigned long addr,unsigned char *Buffer) @@ -243,6 +244,7 @@ unsigned char mmc_write_sector (unsigned long addr,unsigned char *Buffer) return(0); } +#endif //############################################################################ //Routine zum lesen des CID Registers von der MMC/SD-Karte (16Bytes) @@ -297,6 +299,111 @@ unsigned char mmc_read_sector (unsigned long addr,unsigned char *Buffer) return(0); } +//############################################################################ +//Routine zum lesen des CID Registers von der MMC/SD-Karte (16Bytes) +void mmc_read_block_part(unsigned char *cmd,unsigned char *Buffer,uint16_t count, unsigned int Bytes) +//############################################################################ +{ + //Sendet Commando cmd an MMC/SD-Karte + if (mmc_write_command (cmd) != 0) + { + return; + } + + //Wartet auf Start Byte von der MMC/SD-Karte (FEh/Start Byte) + + while (mmc_read_byte() != 0xfe){}; + + //Lesen des Bolcks (normal 512Bytes) von MMC/SD-Karte + for (unsigned int a=0;a>24 ); + cmd[2] = ((addr & 0x00FF0000) >>16 ); + cmd[3] = ((addr & 0x0000FF00) >>8 ); + + mmc_read_block_part(cmd, Buffer, count, 512); +} + +//############################################################################ +//Routine zum lesen des CID Registers von der MMC/SD-Karte (16Bytes) +void mmc_read_block_to_ringbuffer(unsigned char *cmd, ringbuf_t *buffer, unsigned int Bytes) +//############################################################################ +{ + //Sendet Commando cmd an MMC/SD-Karte + if (mmc_write_command (cmd) != 0) + { + return; + } + //Wartet auf Start Byte von der MMC/SD-Karte (FEh/Start Byte) + while(mmc_read_byte() != 0xfe) {;} + + uint16_t data = 0; + + //Lesen des Bolcks (normal 512Bytes) von MMC/SD-Karte + for (unsigned int a=0;a>24 ); + cmd[2] = ((addr & 0x00FF0000) >>16 ); + cmd[3] = ((addr & 0x0000FF00) >>8 ); + + mmc_read_block_to_ringbuffer(cmd, buffer, 512); +} + //############################################################################ //Routine zum lesen des CID Registers von der MMC/SD-Karte (16Bytes) unsigned char mmc_read_cid (unsigned char *Buffer) diff --git a/mmc.h b/mmc.h index 11dd25a..908cd47 100644 --- a/mmc.h +++ b/mmc.h @@ -9,6 +9,8 @@ Copyright (C) 2004 Ulrich Radig #include +#include "ringbuf_small.h" + //#define SPI_Mode 1 //1 = Hardware SPI | 0 = Software SPI #define SPI_Mode 0 @@ -60,6 +62,14 @@ extern unsigned char mmc_init(void); extern unsigned char mmc_read_sector (unsigned long,unsigned char *); +extern void mmc_read_block_part(unsigned char *, unsigned char *, uint16_t, unsigned int); + +extern void mmc_read_part(unsigned long, unsigned char *, uint16_t); + +extern void mmc_read_block_to_ringbuffer(unsigned char *, ringbuf_t *, unsigned int); + +extern void mmc_read_to_ringbuffer(unsigned long, ringbuf_t *); + extern unsigned char mmc_write_sector (unsigned long,unsigned char *); extern unsigned char mmc_write_command (unsigned char *); From 1e15396abf51c580e290bd997c38e1fd6cba7d20 Mon Sep 17 00:00:00 2001 From: Nidan Date: Fri, 13 Jan 2012 22:29:18 +0100 Subject: [PATCH 2/8] adapting to new ringbuffer --- gg.c | 6 ++---- ringbuf_small.c | 17 +++++------------ ringbuf_small.h | 10 +++++----- 3 files changed, 12 insertions(+), 21 deletions(-) diff --git a/gg.c b/gg.c index 64a095f..4788a54 100644 --- a/gg.c +++ b/gg.c @@ -32,8 +32,8 @@ uint32_t mmc_position;/* current reading position on mmc */ uint8_t overflows = 1;/* remaining counter overflows until next sample */ #define SAMPLE_BITS 11 -#define BUFFER_SIZE 64 -uint8_t buffer[BUFFER_SIZE];/* buffer for mmc data */ +#define BUFFER_SIZE 32 +uint16_t buffer[BUFFER_SIZE];/* buffer for mmc data */ ISR(TIMER1_OVF1_vect, ISR_NAKED) { @@ -51,8 +51,6 @@ ISR(TIMER1_OVF1_vect, ISR_NAKED) reti(); } - - int main(void) __attribute__((noreturn)); int main(void) { diff --git a/ringbuf_small.c b/ringbuf_small.c index 360d3e9..9be5caa 100644 --- a/ringbuf_small.c +++ b/ringbuf_small.c @@ -10,11 +10,11 @@ * - reading of pointers allowed at any time * - volatile not really necessary, unless you really need the functions to * react to freespace/newdata while they're running - * + * * PUT AND GET ARE NOT REENTRANT! */ -void ringbuf_init(ringbuf_t *rb, uint16_t * buf, uint8_t size){ +void ringbuf_init(ringbuf_t *rb, uint16_t *buf, uint8_t size){ rb->startptr = buf; rb->size = size; rb->readpos = 0; @@ -41,9 +41,9 @@ uint8_t ringbuf_put(ringbuf_t *rb, uint16_t value){ } /* gets a value from the ringbuffer - * returns 0 on success, -1 on buffer empty + * returns 0 on success, 1 on buffer empty */ -int8_t ringbuf_get(ringbuf_t *rb, uint16_t* data){ +uint8_t ringbuf_get(ringbuf_t *rb, uint16_t *data){ //uint16_t value; uint8_t next; // calculate next ptr pos @@ -53,16 +53,9 @@ int8_t ringbuf_get(ringbuf_t *rb, uint16_t* data){ } //check for empty if(rb->readpos == rb->writepos){ - return(-1); + return(1); } *data = *(rb->startptr + rb->readpos); rb->readpos = next; return(0); } - - - - - - - diff --git a/ringbuf_small.h b/ringbuf_small.h index 220c43f..9c3bf5e 100644 --- a/ringbuf_small.h +++ b/ringbuf_small.h @@ -5,14 +5,14 @@ typedef struct { uint16_t* startptr; - uint16_t size; - uint16_t readpos; - uint16_t writepos; + uint8_t size; + uint8_t readpos; + uint8_t writepos; } ringbuf_t; -void ringbuf_init(ringbuf_t* rb, uint16_t* buf, uint8_t size); +void ringbuf_init(ringbuf_t* rb, uint16_t *buf, uint8_t size); uint8_t ringbuf_put(ringbuf_t *rb, uint16_t value); -int8_t ringbuf_get(ringbuf_t *rb, uint16_t* data); +uint8_t ringbuf_get(ringbuf_t *rb, uint16_t *data); #endif From 5d407a1cd0066eb516f926be43f7268e2402bb36 Mon Sep 17 00:00:00 2001 From: Paul Goeser Date: Fri, 13 Jan 2012 22:31:30 +0100 Subject: [PATCH 3/8] new ISR, WIP --- TODO | 1 - gg.c | 25 +++++++++++++++++++++++-- gg.h | 1 + 3 files changed, 24 insertions(+), 3 deletions(-) diff --git a/TODO b/TODO index 17b9e30..fbc2948 100644 --- a/TODO +++ b/TODO @@ -9,7 +9,6 @@ - 11bit-pwm-emu - drumherum - data-streaming von sdcard - - ringbuf (ryx) - testen - sdcard diff --git a/gg.c b/gg.c index db968ba..44f9628 100644 --- a/gg.c +++ b/gg.c @@ -40,7 +40,7 @@ uint8_t buffer[BUFFER_SIZE];/* buffer for mmc data */ uint8_t pos = 0;/* current playing position */ uint8_t refresh_buffer = 0;/* position to start buffer refreshing */ -ISR(TIMER1_OVF1_vect, ISR_NAKED) +/*ISR(TIMER1_OVF1_vect, ISR_NAKED) { __asm__("in r2, 0x3f"); // save sreg if(--cnt_to_ocr_incr){ @@ -48,13 +48,34 @@ ISR(TIMER1_OVF1_vect, ISR_NAKED) } if(--cnt_to_next){ cur_ocr = next_ocr; + OCR1A = cur_ocr; cnt_to_ocr_incr = next_cnt_to_incr; cnt_to_next = 8; needs_new_data_flag = 1; } __asm__("out 0x2f,r2"); // restore sreg reti(); -} +}//*/ + +ISR(TIMER1_OVF1_vect){ + // next try: simplistic, not too much looking at speed + uint16_t next; + + if(--cnt_to_next){ + next = ringbuf_get(ringbuf); + OCR1A = next>>8; + cnt_to_ocr_incr = 7-(next & 0x7); + cnt_to_next = 7; + } else { + if(--cnt_to_ocr_incr){ + OCR1A += 1; + } + } +} + + + + diff --git a/gg.h b/gg.h index a2f2eb1..0873956 100644 --- a/gg.h +++ b/gg.h @@ -11,3 +11,4 @@ register uint8_t next_cnt_to_incr __asm__("r6"); register uint8_t cnt_to_next __asm__("r7"); register uint8_t needs_new_data_flag __asm__("r8"); +ringbuf_t ringbuf; From 29128acbff6b9e40672c7025df30bb66f1360ac0 Mon Sep 17 00:00:00 2001 From: Nidan Date: Fri, 13 Jan 2012 23:12:31 +0100 Subject: [PATCH 4/8] added adjustable delay to mmc read/write --- mmc.c | 52 ++++++++++++++++++++++++++-------------------------- mmc.h | 4 ++-- 2 files changed, 28 insertions(+), 28 deletions(-) diff --git a/mmc.c b/mmc.c index 2065452..7df9a42 100644 --- a/mmc.c +++ b/mmc.c @@ -135,24 +135,25 @@ unsigned char mmc_read_byte (void) unsigned char Byte = 0; #if SPI_Mode //Routine für Hardware SPI SPDR = 0xff; - while(!(SPSR & (1<0; a--) //das Byte wird Bitweise nacheinander Empangen MSB First - { + for(unsigned char a=8; a>0; a--) //das Byte wird Bitweise nacheinander Empangen MSB First + { MMC_Write &=~(1< 0) //Lesen des Pegels von MMC_DI - { - Byte |= (1<<(a-1)); - } - else - { - Byte &=~(1<<(a-1)); - } - MMC_Write |=(1< 0) //Lesen des Pegels von MMC_DI + { + Byte |= (1<<(a-1)); + } + else + { + Byte &=~(1<<(a-1)); } + MMC_Write |=(1<0; a--) //das Byte wird Bitweise nacheinander Gesendet MSB First + for(unsigned char a=8; a>0; a--) //das Byte wird Bitweise nacheinander Gesendet MSB First { - if (bit_is_set(Byte,(a-1))>0) //Ist Bit a in Byte gesetzt - { - MMC_Write |= (1<0) //Ist Bit a in Byte gesetzt + { + MMC_Write |= (1< +#include #include "ringbuf_small.h" //#define SPI_Mode 1 //1 = Hardware SPI | 0 = Software SPI #define SPI_Mode 0 +#define WAIT_HALF_CLOCK _delay_us(1) #define MMC_Write PORTB //Port an der die MMC/SD-Karte angeschlossen ist also des SPI #define MMC_Read PINB @@ -87,5 +89,3 @@ extern unsigned char mmc_read_cid (unsigned char *); #define nop() __asm__ __volatile__ ("nop" ::) #endif //_MMC_H_ - - From f2f385c6d0b939d1afeb2f7fb62951044df0b095 Mon Sep 17 00:00:00 2001 From: Paul Goeser Date: Fri, 13 Jan 2012 23:32:38 +0100 Subject: [PATCH 5/8] small fixes --- Makefile | 2 +- gg.c | 5 +++-- gg.h | 2 +- ringbuf_small.h | 2 +- 4 files changed, 6 insertions(+), 5 deletions(-) diff --git a/Makefile b/Makefile index ac49a35..d736360 100644 --- a/Makefile +++ b/Makefile @@ -1,7 +1,7 @@ CFLAGS += -Wall -Os -I. -mmcu=attiny26 -std=c99 DEFINES += -DF_CPU=16e6 -OBJECTS = gg.o mmc.o ringbuf_small.o +OBJECTS = gg.o mmc.o #ringbuf_small.o # further optimization: # this removes dead code and does global linker optimization diff --git a/gg.c b/gg.c index 140133b..c0f0ac3 100644 --- a/gg.c +++ b/gg.c @@ -3,6 +3,7 @@ #include #include "ringbuf_small.h" +#include "ringbuf_small.c" #include "mmc.h" #include "gg.h" @@ -31,6 +32,7 @@ uint32_t mmc_position;/* current reading position on mmc */ uint8_t overflows = 1;/* remaining counter overflows until next sample */ #define SAMPLE_BITS 11 +ringbuf_t rb; #define BUFFER_SIZE 32 uint16_t buffer[BUFFER_SIZE];/* buffer for mmc data */ @@ -57,7 +59,7 @@ ISR(TIMER1_OVF1_vect){ uint16_t next; if(--cnt_to_next){ - next = ringbuf_get(ringbuf); + ringbuf_get(&rb, &next); OCR1A = next>>8; cnt_to_ocr_incr = 7-(next & 0x7); cnt_to_next = 7; @@ -75,7 +77,6 @@ ISR(TIMER1_OVF1_vect){ int main(void) __attribute__((noreturn)); int main(void) { - ringbuf_t rb; ringbuf_init(&rb, buffer, BUFFER_SIZE); if(mmc_init() != 0) {/* mmc fail */;} diff --git a/gg.h b/gg.h index 0873956..e97ca8c 100644 --- a/gg.h +++ b/gg.h @@ -11,4 +11,4 @@ register uint8_t next_cnt_to_incr __asm__("r6"); register uint8_t cnt_to_next __asm__("r7"); register uint8_t needs_new_data_flag __asm__("r8"); -ringbuf_t ringbuf; +extern ringbuf_t rb; diff --git a/ringbuf_small.h b/ringbuf_small.h index 9c3bf5e..8683c2c 100644 --- a/ringbuf_small.h +++ b/ringbuf_small.h @@ -13,6 +13,6 @@ typedef struct { void ringbuf_init(ringbuf_t* rb, uint16_t *buf, uint8_t size); uint8_t ringbuf_put(ringbuf_t *rb, uint16_t value); -uint8_t ringbuf_get(ringbuf_t *rb, uint16_t *data); +uint8_t ringbuf_get(ringbuf_t *rb, uint16_t *data) __attribute__((always_inline)); #endif From 50a3d3a7c8d25c09048ceec99b82911b335eaaac Mon Sep 17 00:00:00 2001 From: Nidan Date: Sat, 14 Jan 2012 01:19:02 +0100 Subject: [PATCH 6/8] removed some unnecessary bitshifts in mmc code, a bit of cleaning in gg.c, documented pin usage in README --- README | 15 +++++++++++++++ gg.c | 28 +++++++++++++--------------- mmc.c | 11 ++++------- 3 files changed, 32 insertions(+), 22 deletions(-) diff --git a/README b/README index ef50282..7f0a740 100644 --- a/README +++ b/README @@ -1,3 +1,18 @@ +pin belegung: + +PA0 - MMC - +PA1 - MMC - +PA2 - MMC - +PA3 - MMC - + +PA7 - "Ausschalter" + +PB0 - Progger - MOSI +PB1 - Progger - MISO +PB2 - Progger - SCK + +PB7 - Reset + mmc-code from http://www.ulrichradig.de/home/index.php/avr/mmc-sd diff --git a/gg.c b/gg.c index c0f0ac3..3f78dd8 100644 --- a/gg.c +++ b/gg.c @@ -26,16 +26,10 @@ void timer_init_tiny26(void) TIMSK = 1 << TOIE1;/* interrupt on overflow */ } - -uint8_t length[4];/* remaining samples */ -uint32_t mmc_position;/* current reading position on mmc */ - -uint8_t overflows = 1;/* remaining counter overflows until next sample */ -#define SAMPLE_BITS 11 -ringbuf_t rb; - #define BUFFER_SIZE 32 uint16_t buffer[BUFFER_SIZE];/* buffer for mmc data */ +ringbuf_t rb; +uint32_t length;/* remaining samples */ /*ISR(TIMER1_OVF1_vect, ISR_NAKED) { @@ -52,7 +46,7 @@ uint16_t buffer[BUFFER_SIZE];/* buffer for mmc data */ } __asm__("out 0x3f,r2"); // restore sreg reti(); -}//*/ +}// */ ISR(TIMER1_OVF1_vect){ // next try: simplistic, not too much looking at speed @@ -61,26 +55,30 @@ ISR(TIMER1_OVF1_vect){ if(--cnt_to_next){ ringbuf_get(&rb, &next); OCR1A = next>>8; - cnt_to_ocr_incr = 7-(next & 0x7); + cnt_to_ocr_incr = 7-(next & 0x7); cnt_to_next = 7; + if(!--length) + { + /* shut down */ + PORTA &= ~(1 << PA7); + } } else { if(--cnt_to_ocr_incr){ OCR1A += 1; } } -} - - - +} int main(void) __attribute__((noreturn)); int main(void) { + DDRA |= (1 << PA7); + PORTA |= (1 << PA7); ringbuf_init(&rb, buffer, BUFFER_SIZE); if(mmc_init() != 0) {/* mmc fail */;} - mmc_read_part(0, length, 4); + mmc_read_part(0, (unsigned char *) &length, 4); timer_init_tiny26(); sei(); diff --git a/mmc.c b/mmc.c index 7df9a42..11e6808 100644 --- a/mmc.c +++ b/mmc.c @@ -145,12 +145,9 @@ unsigned char mmc_read_byte (void) if(bit_is_set(MMC_Read,SPI_DI) > 0) //Lesen des Pegels von MMC_DI { - Byte |= (1<<(a-1)); - } - else - { - Byte &=~(1<<(a-1)); + Byte |= 1; } + Byte <<= 1; MMC_Write |=(1<0; a--) //das Byte wird Bitweise nacheinander Gesendet MSB First + for(uint8_t current_bit = 1 << 7; current_bit; current_bit >>= 1) //das Byte wird Bitweise nacheinander Gesendet MSB First { - if(bit_is_set(Byte,(a-1))>0) //Ist Bit a in Byte gesetzt + if(Byte & current_bit) //Ist Bit a in Byte gesetzt { MMC_Write |= (1< Date: Sat, 14 Jan 2012 01:22:46 +0100 Subject: [PATCH 7/8] moved pwm output to PB3 / OC1B --- README | 1 + gg.c | 4 ++-- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/README b/README index 7f0a740..abab41e 100644 --- a/README +++ b/README @@ -11,6 +11,7 @@ PA7 - "Ausschalter" PB0 - Progger - MOSI PB1 - Progger - MISO PB2 - Progger - SCK +PB3 - Speaker PB7 - Reset diff --git a/gg.c b/gg.c index 3f78dd8..607cae9 100644 --- a/gg.c +++ b/gg.c @@ -19,9 +19,9 @@ void timer_init_tiny26(void) /* output pin: OC1A = PB1 = 1 or OC1B = PB3 = 2 */ OCR1A = 0; - TCCR1A = (1 << COM1A1) | (1 << PWM1A);/* pwm enable, not inverted */ + TCCR1A = (1 << COM1B1) | (1 << PWM1B);/* pwm enable, not inverted */ TCCR1B = (1 << CS10);/* fast pwm, no prescaler */ - DDRB |= (1 << PB1); + DDRB |= (1 << PB3); TIMSK = 1 << TOIE1;/* interrupt on overflow */ } From b9d345ffb8e5c5efdbbc290c6c8f0794af3abb4c Mon Sep 17 00:00:00 2001 From: Nidan Date: Sat, 14 Jan 2012 02:14:11 +0100 Subject: [PATCH 8/8] OC1A -> OC1B, and some fixes in the interrupt --- gg.c | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/gg.c b/gg.c index 607cae9..da422e5 100644 --- a/gg.c +++ b/gg.c @@ -18,9 +18,11 @@ void timer_init_tiny26(void) OCR1C = 0xff;/* unused */ /* output pin: OC1A = PB1 = 1 or OC1B = PB3 = 2 */ - OCR1A = 0; + OCR1B = 0; + cnt_to_next = 1;/* overflows to next sample */ + TCCR1A = (1 << COM1B1) | (1 << PWM1B);/* pwm enable, not inverted */ - TCCR1B = (1 << CS10);/* fast pwm, no prescaler */ + TCCR1B = (1 << CS11) | (1 << CS10);/* fast pwm, prescaler / 2 */ DDRB |= (1 << PB3); TIMSK = 1 << TOIE1;/* interrupt on overflow */ @@ -50,21 +52,21 @@ uint32_t length;/* remaining samples */ ISR(TIMER1_OVF1_vect){ // next try: simplistic, not too much looking at speed - uint16_t next; + uint16_t next = 0; - if(--cnt_to_next){ + if(!--cnt_to_next){ ringbuf_get(&rb, &next); - OCR1A = next>>8; - cnt_to_ocr_incr = 7-(next & 0x7); - cnt_to_next = 7; + OCR1B = next>>8; + cnt_to_ocr_incr = 8 - (next & 0x7); + cnt_to_next = 8; if(!--length) { /* shut down */ PORTA &= ~(1 << PA7); } } else { - if(--cnt_to_ocr_incr){ - OCR1A += 1; + if(!--cnt_to_ocr_incr){ + OCR1B += 1; } } }