Subversion Repositories group.electronics

Rev

Rev 126 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 126 Rev 128
Line 5... Line 5...
5
#include <util/delay.h>
5
#include <util/delay.h>
6
#include <stdlib.h>
6
#include <stdlib.h>
7
 
7
 
8
#include "avrutil.h"
8
#include "avrutil.h"
9
 
9
 
-
 
10
uint32_t millis(void) {
-
 
11
	return systime;
-
 
12
}
-
 
13
 
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) {
14
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;
15
	return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
12
}
16
}
13
 
17
 
14
inline void delay_ms(uint16_t count) {
18
inline void delay_ms(uint16_t count) {
15
  while(count--)
19
  while(count--)
16
    _delay_ms(1);  
20
    _delay_ms(1);
17
}
21
}
18
 
22
 
19
void pulsePin(uint8_t port, uint8_t pin, uint16_t count) {
23
void pulsePin(uint8_t port, uint8_t pin, uint16_t count) {
20
  sbi(port, pin);
24
  sbi(port, pin);
21
  delay_ms(count);
25
  delay_ms(count);
22
  cbi(port, pin);
26
  cbi(port, pin);
23
}
27
}
24
 
28
 
25
void statusLed(uint8_t state) {
29
void systemLed(uint8_t state) {
26
	if (state)
30
	if (state)
27
		sbi(PORTD, PD6);
31
		sbi(PORTA, PA0);
28
	else
32
	else
29
		cbi(PORTD, PD6);
33
		cbi(PORTA, PA0);
30
}
-
 
31
 
-
 
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
}
34
}
42
 
35
 
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;
36
char* tick_addr;
57
 
37
 
58
void sysclockInit(void) {
38
void sysclockInit(void) {
59
	
-
 
60
	TIMSK0 = (1<<TOIE0);		// Eable timer overflow for Timer0
39
	TIMSK = (1<<TOIE0);             // Eable timer overflow for Timer0
61
	TCNT0 = 0x00;				// Set Timer0 to 0
-
 
62
	TCCR0B = (1<< CS00) ;		// /1 prescaler
40
	TCCR0B = (1 << CS00) ;          // /1 prescaler
63
	
-
 
64
	//tick_addr = func;
41
	TCNT0 = 0x00;                   // Set Timer0 to 0
65
}
42
}
66
 
43
 
67
void swap_u8(uint8_t* a, uint8_t* b) {
44
void swap_u8(uint8_t* a, uint8_t* b) {
68
	uint8_t t = *b;
45
	uint8_t t = *b;
69
	*b = *a;
46
	*b = *a;
Line 74... Line 51...
74
	uint16_t t = *b;
51
	uint16_t t = *b;
75
	*b = *a;
52
	*b = *a;
76
	*a = t;
53
	*a = t;
77
}
54
}
78
 
55
 
-
 
56
/*
-
 
57
 * Convert from a uint32 to BCD
-
 
58
 * Use like:
-
 
59
 * 	uint8_t[10] values; itobcd(ui32num, values);
-
 
60
 * 	values will be an array with BCD digits
-
 
61
 * 	returned value will be how many digits used
-
 
62
 */
-
 
63
uint8_t itobcd(uint32_t val, uint8_t *result) {
-
 
64
  char BCD_text[10]; // max 10 digits on uint32
-
 
65
  uint8_t i = 0;
-
 
66
 
-
 
67
  itoa(val, BCD_text, 10);
-
 
68
  while(BCD_text[i]) {
-
 
69
    result[i] = BCD_text[i]-'0';
-
 
70
    i++;
-
 
71
  }
-
 
72
  return i;
-
 
73
}
-
 
74