Initial commit

This commit is contained in:
2017-09-25 23:35:01 +02:00
commit b757be1314
26 changed files with 2135 additions and 0 deletions

62
libs/led/led.c Normal file
View File

@@ -0,0 +1,62 @@
/*
* led.c
*
* Created on: 26.07.2015
* Author: Flo
*/
#include "led.h"
// init default configuration
volatile uint8_t led_flags = LED_ACTIVE_LOW;
void led_initPorts() {
// define the following pins as OUTPUT
LED_DDR |= ((1<<LED_BLUE)|(1<<LED_RED)|(1<<LED_GREEN));
if (led_flags & LED_ACTIVE_HIGH) {
// set output state to LOW to disable all leds (leds are active high)
LED_PORT &= ~((1<<LED_BLUE)|(1<<LED_RED)|(1<<LED_GREEN));
}
if (led_flags & LED_ACTIVE_LOW) {
// set output state to HIGH to disable all leds (leds are active low)
LED_PORT |= ((1<<LED_BLUE)|(1<<LED_RED)|(1<<LED_GREEN));
}
}
void led_config(uint8_t flags) {
led_flags = flags;
}
void led_init() {
led_initPorts();
}
void led_off(uint8_t pin) {
if (led_flags & LED_ACTIVE_HIGH) cbi(LED_PORT,pin);
if (led_flags & LED_ACTIVE_LOW) sbi(LED_PORT,pin);
}
void led_on(uint8_t pin) {
if (led_flags & LED_ACTIVE_HIGH) sbi(LED_PORT,pin);
if (led_flags & LED_ACTIVE_LOW) cbi(LED_PORT,pin);
}
void led_flash(uint8_t pin) {
led_on(pin);
_delay_ms(10);
led_off(pin);
_delay_ms(140);
}
void disco(){
led_flash(LED_BLUE);
led_flash(LED_RED);
led_flash(LED_GREEN);
}

64
libs/led/led.h Normal file
View File

@@ -0,0 +1,64 @@
/*
* led.h
*
* Created on: 26.07.2015
* Author: Flo
*/
#include <util/delay.h>
#include <avr/io.h>
#ifndef LED_H_
#define LED_H_
#ifndef cbi
#define cbi(sfr, bit) (_SFR_BYTE(sfr) &= ~_BV(bit))
#endif
#ifndef sbi
#define sbi(sfr, bit) (_SFR_BYTE(sfr) |= _BV(bit))
#endif
#define LED_PORT PORTD
#define LED_DDR DDRD
#define LED_ACTIVE_LOW 0x01
#define LED_ACTIVE_HIGH 0x02
#if defined (__AVR_ATmega8__)
#define LED_BLUE PD5
#define LED_RED PD6
#define LED_GREEN PD7
#elif defined (__AVR_ATmega168P__)
#define LED_BLUE PD5
#define LED_RED PD6
#define LED_GREEN PD7
#elif defined (__AVR_ATtiny2313__)
#define LED_BLUE PD3
#define LED_RED PD4
#define LED_GREEN PD5
#else
#if !defined(__COMPILING_AVR_LIBC__)
#warning "device type not defined"
#endif
#endif
extern volatile uint8_t led_flags;
void led_init();
void led_config(uint8_t flags);
void led_initPorts();
void led_off(uint8_t pin);
void led_on(uint8_t pin);
void led_flash(uint8_t pin);
void disco();
#endif /* LED_H_ */

125
libs/uart/uart.c Normal file
View File

@@ -0,0 +1,125 @@
/*
* uart.c
*
*/
#include "uart.h"
char uartReceiveBuffer[UART_RECV_BUFFER_SIZE];
void uart_init() {
#if defined (__AVR_ATmega8__)
// enable receive and transmit
UCSRB |= (1 << RXEN) | (1 << TXEN);
// set frame format
//Set data frame format: asynchronous mode,no parity, 1 stop bit, 8 bit size
UCSRC=(1<<URSEL)|(0<<UMSEL)|(0<<UPM1)|(0<<UPM0)|(0<<USBS)|(0<<UCSZ2)|(1<<UCSZ1)|(1<<UCSZ0);
//UCSRC |= (1<<URSEL)|(3<<UCSZ0); // async 8n1
// set baud rate
UBRRH = UBRR_VAL >> 8;
UBRRL = UBRR_VAL & 0xFF;
#elif defined (__AVR_ATmega168P__)
// enable receive and transmit
UCSR0B = (1<<RXEN0)|(1<<TXEN0);
// set frame format
// UCSR0C = ((1<<UCSZ01)|(1<<UCSZ00))&(0<<USBS0); // 8n1
UCSR0C = (1<<UCSZ01)|(1<<UCSZ00); // 8n2
// set baud rate
UBRR0H = UBRR_VAL >> 8;
UBRR0L = UBRR_VAL;
#else
# if !defined(__COMPILING_AVR_LIBC__)
# warning "device type not defined"
# endif
#endif
}
// ======================= SEND =======================
void uart_sendChar(char c) {
#if defined (__AVR_ATmega8__)
while (!(UCSRA & (1<<UDRE)));
UDR = c;
#elif defined (__AVR_ATmega168P__)
while (!(UCSR0A & (1<<UDRE0))) {}
UDR0 = c;
#else
# if !defined(__COMPILING_AVR_LIBC__)
# warning "device type not defined"
# endif
#endif
}
void uart_sendInt(int c) {
char m[7];
itoa(c,m,10);
uart_sendString(m);
}
void uart_sendBinary(int c) {
char m[7];
itoa(c,m,2);
uart_sendString(m);
}
void uart_sendHex(int c) {
char m[7];
itoa(c,m,16);
uart_sendString(m);
}
void uart_sendString(char *s) {
while (*s != '\0') {
uart_sendChar(*s);
s++;
}
}
void uart_sendLF() {
uart_sendChar('\n');
}
// ======================= RECEIVE =======================
char uart_receiveChar() {
#if defined (__AVR_ATmega8__)
while (!(UCSRA & (1<<RXC)));
return UDR;
#elif defined (__AVR_ATmega168P__)
while ( !(UCSR0A & (1<<RXC0)) );
return UDR0;
#else
# if !defined(__COMPILING_AVR_LIBC__)
# warning "device type not defined"
# endif
#endif
}
char* uart_receiveString() {
char nextChar;
char stringLen = 0;
char *startPtr = uartReceiveBuffer;
char *bufferPtr = uartReceiveBuffer;
nextChar = uart_receiveChar();
while( nextChar != '\n' && nextChar != '\0' && stringLen < UART_RECV_BUFFER_SIZE - 1 ) {
*(bufferPtr++) = nextChar;
stringLen++;
nextChar = uart_receiveChar();
}
*bufferPtr = '\0';
return startPtr;
}

41
libs/uart/uart.h Normal file
View File

@@ -0,0 +1,41 @@
/*
* uart.h
*
*/
#include <avr/io.h>
#include <stdlib.h>
#ifndef UART_H_
#define UART_H_
#define UART_RECV_BUFFER_SIZE 33 // max 32 chars + '\0'
//#define BAUD 19200UL // Baudrate
#define BAUD 9600UL // Baudrate
// Berechnungen
//#define UBRR_VAL F_CPU/16/BAUD-1
#define UBRR_VAL ((F_CPU+BAUD*8UL)/(BAUD*16UL)-1UL) // clever runden
#define BAUD_REAL (F_CPU/(16UL*(UBRR_VAL+1UL))) // Reale Baudrate
#define BAUD_ERROR ((BAUD_REAL*1000)/BAUD) // Fehler in Promille, 1000 = kein Fehler.
#if ((BAUD_ERROR<990) || (BAUD_ERROR>1010))
#error Baud rate error greater than 1% - too high!
#endif
void uart_init();
void uart_sendChar(char c);
void uart_sendString (char *s);
void uart_sendBinary(int c);
void uart_sendInt(int c);
void uart_sendHex(int c);
void uart_sendLF();
char uart_receiveChar();
char* uart_receiveString();
#endif /* UART_H_ */