Subversion Repositories group.electronics

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

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