Subversion Repositories group.electronics

Rev

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

Rev Author Line No. Line
130 pfowler 1
#include <avr/io.h>
2
#include <avr/wdt.h>
3
#include <avr/interrupt.h>
4
#include <string.h>
5
#include <util/delay.h>
6
#include <stdlib.h>
7
 
8
#include "avrutil.h"
9
 
10
uint32_t millis(void) {
11
	return systime;
12
}
13
 
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) {
15
	return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
16
}
17
 
18
inline void delay_ms(uint16_t count) {
19
  while(count--)
20
    _delay_ms(1);
21
}
22
 
23
void pulsePin(uint8_t port, uint8_t pin, uint16_t count) {
24
  sbi(port, pin);
25
  delay_ms(count);
26
  cbi(port, pin);
27
}
28
 
132 pfowler 29
/*
130 pfowler 30
void systemLed(uint8_t state) {
31
	if (state)
32
		sbi(PORTA, PA0);
33
	else
34
		cbi(PORTA, PA0);
35
}
132 pfowler 36
*/
130 pfowler 37
 
38
char* tick_addr;
39
 
40
void sysclockInit(void) {
132 pfowler 41
	TIMSK = (1<<TOIE0);             // Eable timer overflow for Timer0
130 pfowler 42
	TCCR0B = (1 << CS00) ;          // /1 prescaler
43
	TCNT0 = 0x00;                   // Set Timer0 to 0
44
}
45
 
46
void swap_u8(uint8_t* a, uint8_t* b) {
47
	uint8_t t = *b;
48
	*b = *a;
49
	*a = t;
50
}
51
 
52
void swap_u16(uint16_t* a, uint16_t* b) {
53
	uint16_t t = *b;
54
	*b = *a;
55
	*a = t;
56
}
57
 
58
/*
59
 * Convert from a uint32 to BCD
60
 * Use like:
61
 * 	uint8_t[10] values; itobcd(ui32num, values);
62
 * 	values will be an array with BCD digits
63
 * 	returned value will be how many digits used
64
 */
139 pfowler 65
uint8_t itobcd32(uint32_t val, uint8_t *result) {
130 pfowler 66
  char BCD_text[10]; // max 10 digits on uint32
67
  uint8_t i = 0;
68
 
69
  itoa(val, BCD_text, 10);
70
  while(BCD_text[i]) {
71
    result[i] = BCD_text[i]-'0';
72
    i++;
73
  }
74
  return i;
75
}
76
 
139 pfowler 77
/*
78
 * Convert from a uint16 to BCD
79
 * Use like:
80
 * 	uint8_t[5] values; itobcd(ui16num, values);
81
 * 	values will be an array with BCD digits
82
 * 	returned value will be how many digits used
83
 */
84
uint8_t itobcd16(uint16_t val, uint8_t *result) {
85
  char BCD_text[6]; // max 10 digits on uint32
86
  uint8_t i = 0;
87
 
88
  itoa(val, BCD_text, 10);
89
  while(BCD_text[i]) {
90
    result[i] = BCD_text[i]-'0';
91
    i++;
92
  }
93
  return i;
94
}
95
 
96
/*
97
 * Convert from a uint8 to BCD
98
 * Use like:
99
 * 	uint8_t[3] values; itobcd(ui8num, values);
100
 * 	values will be an array with BCD digits
101
 * 	returned value will be how many digits used
102
 */
103
uint8_t itobcd8(uint8_t val, uint8_t *result) {
104
  char BCD_text[3]; // max 10 digits on uint32
105
  uint8_t i = 0;
106
 
107
  itoa(val, BCD_text, 10);
108
  while(BCD_text[i]) {
109
    result[i] = BCD_text[i]-'0';
110
    i++;
111
  }
112
  return i;
113
}
114