Rev 123 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed
#include <avr/io.h>
#include <avr/wdt.h>
#include <avr/interrupt.h>
#include <string.h>
#include <util/delay.h>
#include <stdlib.h>
#include "avrutil.h"
inline uint8_t map_8(uint8_t x, uint8_t in_min, uint8_t in_max, uint8_t out_min, uint8_t out_max) {
return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
}
inline void delay_ms(uint16_t count) {
while(count--)
_delay_ms(1);
}
void pulsePin(uint8_t port, uint8_t pin, uint16_t count) {
sbi(port, pin);
delay_ms(count);
cbi(port, pin);
}
void statusLed(uint8_t state) {
if (state)
sbi(PORTD, PD6);
else
cbi(PORTD, PD6);
}
uint8_t analogRead(uint8_t channel) {
ADMUX = (1<<ADLAR) | (1<<REFS0) | (0<<REFS1) | (channel & 0x0f);
ADCSRA |= (1<<ADSC); // Start converting
while (((ADCSRA >> ADSC) & 1)) {} //Wait until conversion finished
uint8_t result = ADCH;
//ADCSRA |= (0<<ADSC); // Stop converting
return result;
}
void analogInit() {
ACSR |= (1<<ACD); // Disable analog comparator
/*
Setup ADC
ADMUX: 8 bit mode, Avcc ref
ADCSRA: Enable, 128 prescale
*/
ADMUX = (1<<ADLAR) | (0<<REFS0) | (1<<REFS1);
ADCSRA = (1<<ADEN) | (1<<ADPS2) | (1<<ADPS1) | (1<<ADPS0) ;
}
char* tick_addr;
void sysclockInit(void) {
TIMSK0 = (1<<TOIE0); // Eable timer overflow for Timer0
TCNT0 = 0x00; // Set Timer0 to 0
TCCR0B = (1<< CS00) ; // /1 prescaler
//tick_addr = func;
}
void swap_u8(uint8_t* a, uint8_t* b) {
uint8_t t = *b;
*b = *a;
*a = t;
}
void swap_u16(uint16_t* a, uint16_t* b) {
uint16_t t = *b;
*b = *a;
*a = t;
}