howfast.c


1
#include <stdlib.h>
2
#include <stdint.h>
3
#include <math.h>
4
#include <avr/io.h>
5
6
#define LEDPORT      PORTB
7
#define LEDDDR      DDRB
8
#define LEDGRUEN    0   
9
#define LEDTOGGLE(x)  (LEDPORT ^= (x))            
10
11
#define PWMDDR      DDRB 
12
#define PWMPIN      3   
13
#define PWMVALUE     OCR1B
14
15
// Test Parameter
16
17
// Die Anzahl der Funktionsaufrufe
18
#define COUNT_INIT     100  
19
20
// Der Teiler bzw. Shift-Wert
21
#define TEILER      8192
22
#define SHIFT       13  // = log2(TEILER)
23
24
// der zu unteruchende Datentyp
25
#define test_t       int16_t  
26
27
// Die testfunktion (1=Dummy, 2=Division, 3=Shift)
28
#define TESTFUN     2
29
30
31
#if (TESTFUN==1) 
32
test_t scale(test_t x, uint8_t y){ 
33
  
34
  return(x + y);     
35
}
36
#endif
37
38
#if (TESTFUN==2) 
39
test_t scale(test_t x, uint8_t y){ 
40
  
41
  return(x / (test_t) TEILER + y);     
42
}
43
#endif
44
45
#if (TESTFUN==3) 
46
test_t scale(test_t x, uint8_t y){ 
47
  
48
  x = (x >> (test_t) SHIFT);
49
50
  if (x < 0)
51
    return(++x + y);
52
  else
53
    return(x + y);
54
}
55
#endif
56
57
58
 
59
void init(){
60
61
  PWMDDR |= (1 << PWMPIN);          // Ausgangspins einschalten
62
  LEDDDR |= (1 << LEDGRUEN);
63
64
  while (!(PLLCSR & (1 << PLOCK)));       // Warten bis PLL gelockt
65
  PLLCSR |= (1 << PCKE);             // PLL-CK schalten 
66
  TCCR1B |= (1 << CS10);             // PCK ohne Presacler
67
  TCCR1A = ((1 << COM1B1) | (1 << PWM1B));   // PWM1B, OC1B cleared on CM, nOC1B n.c.
68
  OCR1C = 0xfa;                 // Timer immer voll durchlaufen lassen
69
}
70
71
72
int main(void){
73
      
74
  uint8_t counter = COUNT_INIT;
75
76
  // irgendein Long-Wert, erzeugt bei kleineren test-Variablen einen Compiler-Error  
77
  test_t temp   = 0xABCDEF01;        
78
79
  init();
80
81
  // loop forever
82
  for (;;) {                             
83
84
    counter--;
85
    temp = scale( temp , counter );         
86
    if ( counter == 0 ) {
87
      
88
      LEDTOGGLE( 1 << LEDGRUEN ); 
89
        counter    = COUNT_INIT;    
90
      
91
      // Ausgabe verhindert wegoptimieren durch den Compilers...
92
      PWMVALUE   = (uint8_t) temp;    
93
    }
94
  }     //End of loop forever  
95
}    // End of main