initial
This commit is contained in:
commit
ac04dfc73f
15
Makefile
Normal file
15
Makefile
Normal file
|
@ -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
|
8
conf.h
Normal file
8
conf.h
Normal file
|
@ -0,0 +1,8 @@
|
|||
#ifndef _CONF_H_
|
||||
#define _CONF_H_
|
||||
|
||||
#define F_CPU 8000000UL
|
||||
#define USART_BAUD 9600
|
||||
#define USART_BUFFER_SIZE 256
|
||||
|
||||
#endif
|
19
main.c
Normal file
19
main.c
Normal file
|
@ -0,0 +1,19 @@
|
|||
#include "time.h"
|
||||
#include "usart.h"
|
||||
#include <util/delay.h>
|
||||
|
||||
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;
|
||||
}
|
||||
|
59
ringbuffer.c
Normal file
59
ringbuffer.c
Normal file
|
@ -0,0 +1,59 @@
|
|||
#include "ringbuffer.h"
|
||||
#include <stdlib.h>
|
||||
|
||||
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;
|
||||
}
|
||||
|
23
ringbuffer.h
Normal file
23
ringbuffer.h
Normal file
|
@ -0,0 +1,23 @@
|
|||
#ifndef _RINGBUFFER_H_
|
||||
#define _RINGBUFFER_H_
|
||||
|
||||
#include <stdlib.h>
|
||||
|
||||
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
|
28
time.c
Normal file
28
time.c
Normal file
|
@ -0,0 +1,28 @@
|
|||
#include "time.h"
|
||||
#include "conf.h"
|
||||
|
||||
#include <avr/interrupt.h>
|
||||
#include <avr/io.h>
|
||||
|
||||
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++;
|
||||
}
|
16
time.h
Normal file
16
time.h
Normal file
|
@ -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
|
35
usart.c
Normal file
35
usart.c
Normal file
|
@ -0,0 +1,35 @@
|
|||
#include <avr/io.h>
|
||||
#include <stdlib.h>
|
||||
#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);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user