Subversion Repositories group.electronics

Rev

Rev 126 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
123 pfowler 1
#include <avr/io.h>
126 pfowler 2
#include <avr/wdt.h>
3
#include <avr/interrupt.h>
4
#include <string.h>
5
#include <util/delay.h>
6
#include <stdlib.h>
123 pfowler 7
 
8
#include "avrutil.h"
9
 
10
inline uint8_t map_8(uint8_t x, uint8_t in_min, uint8_t in_max, uint8_t out_min, uint8_t out_max) {
11
	return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
12
}
13
 
14
inline void delay_ms(uint16_t count) {
15
  while(count--)
16
    _delay_ms(1);  
17
}
18
 
19
void pulsePin(uint8_t port, uint8_t pin, uint16_t count) {
20
  sbi(port, pin);
21
  delay_ms(count);
22
  cbi(port, pin);
23
}
24
 
126 pfowler 25
void statusLed(uint8_t state) {
26
	if (state)
27
		sbi(PORTD, PD6);
28
	else
29
		cbi(PORTD, PD6);
30
}
31
 
123 pfowler 32
uint8_t analogRead(uint8_t channel) {
33
        ADMUX = (1<<ADLAR) | (1<<REFS0) | (0<<REFS1) | (channel & 0x0f);
34
        ADCSRA |= (1<<ADSC);            // Start converting
35
 
36
        while (((ADCSRA >> ADSC) & 1)) {}       //Wait until conversion finished
37
        uint8_t result = ADCH;
38
        //ADCSRA |= (0<<ADSC);          // Stop converting
39
 
40
        return result;
41
}
42
 
43
void analogInit() {
44
	ACSR |= (1<<ACD); // Disable analog comparator
45
 
46
	/*
47
        Setup ADC
48
        ADMUX: 8 bit mode, Avcc ref
49
        ADCSRA: Enable, 128 prescale
50
	*/
51
	ADMUX = (1<<ADLAR) | (0<<REFS0) | (1<<REFS1);
52
	ADCSRA = (1<<ADEN) | (1<<ADPS2) | (1<<ADPS1) | (1<<ADPS0) ;
53
}
54
 
55
 
56
char* tick_addr;
57
 
58
void sysclockInit(void) {
59
 
60
	TIMSK0 = (1<<TOIE0);		// Eable timer overflow for Timer0
61
	TCNT0 = 0x00;				// Set Timer0 to 0
62
	TCCR0B = (1<< CS00) ;		// /1 prescaler
63
 
64
	//tick_addr = func;
65
}
66
 
126 pfowler 67
void swap_u8(uint8_t* a, uint8_t* b) {
68
	uint8_t t = *b;
69
	*b = *a;
70
	*a = t;
71
}
72
 
73
void swap_u16(uint16_t* a, uint16_t* b) {
74
	uint16_t t = *b;
75
	*b = *a;
76
	*a = t;
77
}
78