Subversion Repositories group.electronics

Rev

Go to most recent revision | Details | 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
 */
65
uint8_t itobcd(uint32_t val, uint8_t *result) {
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