summaryrefslogtreecommitdiffstats
path: root/source/qFix/qfix.h
blob: 0a5d03951df9e2f636d56655450b81ae1aa76515 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
//------------------------------------------------------------------
// qfix.h
// 
// This file contains general includes and definitions for the
// family of qfix boards.
//
// Copyright 2004-2006 by KTB mechatronics GmbH
// Author: Stefan Enderle
//------------------------------------------------------------------

#ifndef qfix_h
#define qfix_h


#include <inttypes.h>
#include <avr/io.h>
#include <avr/interrupt.h>


/* defines for compatibility */
#ifndef cbi
#define cbi(sfr, bit) (_SFR_BYTE(sfr) &= ~_BV(bit))
#endif
#ifndef sbi
#define sbi(sfr, bit) (_SFR_BYTE(sfr) |= _BV(bit))
#endif
#ifndef BV
#define BV _BV
#endif

   

/** Returns the absolute value of a
*/
inline
int abs(int a) 
{
  if (a>=0) return a;
  else return -a;
}



/** Sleeps for sec seconds. (Note: this is tuned for
*   an Atmel ATmega32 running with 8 MHz.)
*/
inline
void sleep(int sec)
{
  for (int s=0; s<sec; s++) {
    for (long int i=0; i<702839; i++) {
      asm volatile("nop"); 
    }
  }
}


/** Sleeps for msec milliseconds. (Note: this is tuned for
*   an Atmel ATmega32 running with 8 MHz.)
*/
inline
void msleep(int msec)
{
  for (int s=0; s<msec; s++) {
    for (long int i=0; i<703; i++) {
      asm volatile("nop"); 
    }
  }
}




/** Translates the given number into a string which ends
*   with 0.
*/
inline
void num2str(int num, char* str)
{
  bool showZero = false;
  int i=0;
  int n=abs(num);

  if (num<0) str[i++]='-'; 
 
  if (n>=10000 || showZero) { str[i++]=(n/10000)+'0';  n=n % 10000; showZero=true; }
  if (n>=1000  || showZero) { str[i++]=(n/1000) +'0';  n=n % 1000;  showZero=true; }
  if (n>=100   || showZero) { str[i++]=(n/100)  +'0';  n=n % 100;   showZero=true; }
  if (n>=10    || showZero) { str[i++]=(n/10)   +'0';  n=n % 10;    showZero=true; }
  str[i++]=n +'0';     // last digit
  str[i++]=0;          // ending 0
}


#endif