commit ac04dfc73fbc13076131230cf01dfe6c432cb47c Author: Daniel Knüttel Date: Tue Jan 1 16:48:54 2019 +0100 initial diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..f2ff63b --- /dev/null +++ b/Makefile @@ -0,0 +1,15 @@ +CC=avr-gcc +CFLAG= -mmcu=atmega8 -O + +objects= time.o usart.o main.o ringbuffer.o + +all:main.elf + +%.o:%.c + $(CC) $(CFLAG) -c -o $@ $< + + +main.elf: $(objects) + $(CC) $(objects) $(CFLAG) -o main.elf +install: main.elf + avrdude -p m168 -c avrispmkii -U flash:w:main.elf:a diff --git a/conf.h b/conf.h new file mode 100644 index 0000000..d833817 --- /dev/null +++ b/conf.h @@ -0,0 +1,8 @@ +#ifndef _CONF_H_ +#define _CONF_H_ + +#define F_CPU 8000000UL +#define USART_BAUD 9600 +#define USART_BUFFER_SIZE 256 + +#endif diff --git a/main.c b/main.c new file mode 100644 index 0000000..78b510b --- /dev/null +++ b/main.c @@ -0,0 +1,19 @@ +#include "time.h" +#include "usart.h" +#include + +int main(void) +{ + time_init(); + usart_init(); + + while(1) + { + _delay_ms(1000); + unsigned int eigentime = get_eigentime(); + usart_putc((char) eigentime); + usart_putc((char)(eigentime >> 8)); + } + return 0; +} + diff --git a/ringbuffer.c b/ringbuffer.c new file mode 100644 index 0000000..7f373f4 --- /dev/null +++ b/ringbuffer.c @@ -0,0 +1,59 @@ +#include "ringbuffer.h" +#include + +ringbuffer_t +ringbuffer_new(size_t size) +{ + ringbuffer_t buffer = malloc(sizeof(struct ringbuffer_s)); + if(!buffer) + { + return NULL; + } + + buffer->buffer = malloc(sizeof(char) * size); + if(!buffer->buffer) + { + free(buffer); + return NULL; + } + + buffer->length = size; + buffer->head = 0; + buffer->tail = 0; + buffer->ovf = 0; + return buffer; +} + +int +ringbuffer_nonempty(ringbuffer_t buffer) +{ + return buffer->head != buffer->tail; +} + +unsigned char +ringbuffer_get_char(ringbuffer_t buffer) +{ + unsigned char result; + if(buffer->head == buffer->tail) + { + return 0; + } + result = buffer->buffer[buffer->tail]; + buffer->tail = (buffer->tail + 1) % buffer->length; + return result; +} + +int +ringbuffer_put_char(ringbuffer_t buffer + , unsigned char c) +{ + buffer->ovf = 0; + buffer->buffer[buffer->head] = c; + buffer->head = (buffer->head + 1) % buffer->length; + if(buffer->head == buffer->tail) + { + buffer->ovf = 1; + } + return buffer->ovf; +} + diff --git a/ringbuffer.h b/ringbuffer.h new file mode 100644 index 0000000..c1fa67d --- /dev/null +++ b/ringbuffer.h @@ -0,0 +1,23 @@ +#ifndef _RINGBUFFER_H_ +#define _RINGBUFFER_H_ + +#include + +typedef struct ringbuffer_s +{ + size_t length; + size_t head, tail; + unsigned char * buffer; + char ovf; +} * ringbuffer_t; + +ringbuffer_t +ringbuffer_new(size_t size); +int +ringbuffer_nonempty(ringbuffer_t buffer); +unsigned char +ringbuffer_get_char(ringbuffer_t buffer); +int +ringbuffer_put_char(ringbuffer_t buffer + , unsigned char c); +#endif diff --git a/time.c b/time.c new file mode 100644 index 0000000..f9a4295 --- /dev/null +++ b/time.c @@ -0,0 +1,28 @@ +#include "time.h" +#include "conf.h" + +#include +#include + +volatile unsigned int _eigentime_ms; + +void +time_init(void) +{ + _eigentime_ms = 0; + TCCR1B = _BV(CS10); + OCR1A = TIMER1_COMPA_VALUE; + TIMSK |= _BV(OCIE1A); +} + + +unsigned int +get_eigentime(void) +{ + return _eigentime_ms; +} + +ISR(TIMER1_COMPA_vect) +{ + _eigentime_ms++; +} diff --git a/time.h b/time.h new file mode 100644 index 0000000..9f465c2 --- /dev/null +++ b/time.h @@ -0,0 +1,16 @@ +#ifndef _TIME_H_ +#define _TIME_H_ +#include "conf.h" + +#define TIMER1_MAX_VALUE 0xffff +#define TIMER1_COMPA_VALUE (TIMER1_MAX_VALUE / F_CPU) + +extern volatile unsigned int _eigentime_ms; + +unsigned int +get_eigentime(void); + +void +time_init(void); + +#endif diff --git a/usart.c b/usart.c new file mode 100644 index 0000000..c0f0687 --- /dev/null +++ b/usart.c @@ -0,0 +1,35 @@ +#include +#include +#include "usart.h" +#include "conf.h" +#include "ringbuffer.h" + +volatile ringbuffer_t usart_buffer; + + +void +usart_init(void) +{ + usart_buffer = ringbuffer_new(USART_BUFFER_SIZE); + UBRRH = (unsigned char)(UBRR_VALUE >> 8); + UBRRL = (unsigned char)UBRR_VALUE; + UCSRB = _BV(TXEN) | _BV(TXCIE); + UCSRC = _BV(USBS) | _BV(UCSZ0) | _BV(UCSZ1); +} + +int +usart_putc(char c) +{ + return ringbuffer_put_char(usart_buffer, c); +} + + + +ISR(USART_TX_vect) +{ + if(ringbuffer_nonempty(usart_buffer)) + { + while(!(UCSRA & UDRE)); + UDR = ringbuffer_get_char(usart_buffer); + } +} diff --git a/usart.h b/usart.h new file mode 100644 index 0000000..be7099d --- /dev/null +++ b/usart.h @@ -0,0 +1,14 @@ +#ifndef _USART_H_ +#define _USART_H_ + +#include "conf.h" +#define UBRR_VALUE (F_CPU/16/USART_BAUD-1) + +void +usart_init(void); + + +int +usart_putc(char c); + +#endif