Subversion Repositories group.NITPanels

Rev

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

Rev Author Line No. Line
13 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
 
29
char* tick_addr;
30
 
31
void sysclockInit(void) {
24 pfowler 32
 
33
#if defined (__AVR_ATtiny24__)
13 pfowler 34
	TIMSK0 = (1<<TOIE0);             // Eable timer overflow for Timer0
24 pfowler 35
#elif defined (__AVR_ATtiny4313__)
36
	TIMSK = (1<<TOIE0);             // Eable timer overflow for Timer0
37
#endif
13 pfowler 38
	TCCR0B = (1 << CS00) ;          // /1 prescaler
39
	TCNT0 = 0x00;                   // Set Timer0 to 0
40
}
41
 
42
void swap_u8(uint8_t* a, uint8_t* b) {
43
	uint8_t t = *b;
44
	*b = *a;
45
	*a = t;
46
}
47
 
48
void swap_u16(uint16_t* a, uint16_t* b) {
49
	uint16_t t = *b;
50
	*b = *a;
51
	*a = t;
52
}
53
 
54
/*
55
 * Convert from a uint32 to BCD
56
 * Use like:
57
 * 	uint8_t[10] values; itobcd(ui32num, values);
58
 * 	values will be an array with BCD digits
59
 * 	returned value will be how many digits used
60
 */
61
uint8_t itobcd(uint32_t val, uint8_t *result) {
62
  char BCD_text[10]; // max 10 digits on uint32
63
  uint8_t i = 0;
64
 
65
  itoa(val, BCD_text, 10);
66
  while(BCD_text[i]) {
67
    result[i] = BCD_text[i]-'0';
68
    i++;
69
  }
70
  return i;
71
}
72