Subversion Repositories group.electronics

Rev

Rev 128 | 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
 
128 pfowler 10
uint32_t millis(void) {
11
	return systime;
12
}
13
 
123 pfowler 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--)
128 pfowler 20
    _delay_ms(1);
123 pfowler 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
 
128 pfowler 29
void systemLed(uint8_t state) {
126 pfowler 30
	if (state)
128 pfowler 31
		sbi(PORTA, PA0);
126 pfowler 32
	else
128 pfowler 33
		cbi(PORTA, PA0);
126 pfowler 34
}
35
 
123 pfowler 36
char* tick_addr;
37
 
38
void sysclockInit(void) {
128 pfowler 39
	TIMSK = (1<<TOIE0);             // Eable timer overflow for Timer0
40
	TCCR0B = (1 << CS00) ;          // /1 prescaler
41
	TCNT0 = 0x00;                   // Set Timer0 to 0
123 pfowler 42
}
43
 
126 pfowler 44
void swap_u8(uint8_t* a, uint8_t* b) {
45
	uint8_t t = *b;
46
	*b = *a;
47
	*a = t;
48
}
49
 
50
void swap_u16(uint16_t* a, uint16_t* b) {
51
	uint16_t t = *b;
52
	*b = *a;
53
	*a = t;
54
}
55
 
128 pfowler 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