Debian Codegraph @

kpy/mh-timer.c

raw symbols SCIP
1 /*
2 mh-timer.c -- simple functions for handling the Timer/Counter
3 Author : Ajith Kumar, Inter-University Accelerator Centre, New Delhi.
4 License : GNU GPL version 3 or later
5 Date : 23-Oct-2013
6 */
7
8 #include <avr/io.h>
9 #include <avr/interrupt.h>
10
11 void sqwave_tc0(uint8_t csb, uint8_t ocrval)
12 {
13 // Set TCCR0 in the CTC mode
14 TCCR0 = (1 << WGM01) | (1 << COM00) | csb;
15 OCR0 = ocrval;
16 TCNT0 = 0;
17 DDRB |= (1 << PB3);
18 }
19
20
21 void pwm_tc0(uint8_t csb, uint8_t ocrval)
22 {
23 // Set TCCR0 in the Fast PWM mode
24 TCCR0 =(1 << WGM01) | (1 << WGM00) | (1 << COM01) | csb;
25 OCR0 = ocrval;
26 TCNT0 = 0;
27 DDRB |= (1 << PB3);
28 }
29
30 void sqwave_tc1(uint8_t csb, uint16_t ocra)
31 {
32 // This can set very low values of freqency on the output
33 TCCR1A = (1 << COM1A0); // Set TCCR1A in the CTC mode
34 TCCR1B = (1 << WGM12) | csb;
35 OCR1A = ocra; // Output Compare register values
36 TCNT1 = 0;
37 DDRD |= (1 << PD5); // Set pin OC1A as output
38 }
39
40 void pwm10_tc1(uint8_t csb, uint16_t ocra)
41 {
42 TCCR1A = (1 << COM1A1) | (1 << WGM11) |(1 << WGM10); // Set 10bit PWM mode
43 TCCR1B = csb;
44 OCR1A = ocra; // Output Compare register values
45 TCNT1 = 0;
46 DDRD |= (1 << PD5); // Set pin OC1A as output
47 }
48
49 //------------------------- Square wave on TC2 -------------------------
50
51 #define FLIMIT 4000000 // 8 MHz clock /2
52 static uint16_t f[] = {1,8,32,64,128,256,1024};
53
54 uint32_t set_sqr_tc2(uint32_t freq) // freq must be from 15 to 100000 Hz, no checking done
55 {
56 uint32_t tmp;
57 uint8_t ocr, k;
58
59 DDRD |= (1 << PD7); // Make PD7 as output
60 k = 0;
61 while(k < 7)
62 {
63 tmp = FLIMIT / f[k]; // maximum value for the chosen prescaler
64 if (tmp/freq <= 256)
65 {
66 TCCR2 = (1 << WGM21) | (1 << COM20) | (k+1); // CTC mode
67 ocr = tmp/freq;
68 tmp = tmp/ocr; // the value actually set
69 if (ocr)
70 --ocr;
71 OCR2 = ocr;
72 return tmp;
73 }
74 k = k + 1;
75 }
76 return 0;
77 }
78
79
80
81 //------------------- Frequency measurement ----------------------------
82
83 #define MTIME 250 // We count 100 on TC0
84
85 uint32_t measure_freq(void)
86 {
87 volatile uint16_t x, k = 500;
88 DDRB &= ~(1 << PB1); // Timer/Counter1 clock in T1 (PB1) as input
89 TCCR1B = (1 << CS12) | (1 << CS11) | (1 << CS10); // External clock on T1 pin
90 TCNT1 = 0; // Clear TCNT1
91 while(k--) {x=532; while (x--);}
92 TCCR1B = 0; // Stop counter
93 return TCNT1 * 2; // freq = Counts / 500 mS x 2
94 }
95
96 //----------------------- Time interval measurement--------------------------
97
98 volatile uint16_t HIWORD;
99 ISR(TIMER1_COMPA_vect) // TIMER1 Compare Match A Interrupt
100 {
101 TCNT1 = 0;
102 ++HIWORD;
103 }
104
105 void start_timer()
106 {
107 /*
108 When TCNT1 reaches OCR1A, the ISR will run. It will clear TCNT1 and increment HIWORD.
109 The total time elapsed between start_timer and get_timer = HIWORD * 50000 + TCNT1
110 */
111 TCCR1B = (1 << CS11); // Normal mode, with 1MHz clock
112 HIWORD = 0;
113 OCR1A = 50000;
114 OCR1B = 0xffff;
115 TIMSK = (1 << OCIE1A); // Enable compare match interrupt
116 TIFR = (1 << OCF1A);
117 TCNT1 = 0;
118 sei();
119 }
120
121 uint32_t read_timer()
122 {
123 uint32_t x;
124
125 TCCR1B = 0; // stop TC1 clock
126 x = HIWORD * 50000 + TCNT1;
127 cli();
128 return x;
129 }
130
131 uint32_t r2ftime(uint8_t bit)
132 {
133 // measures rising edge to falling edge time on any bit of PORTB. If no pulse input, program may go in infinite loop
134
135 DDRB &= ~(1 << bit); // set the selected bit as input, on PORT B
136 while( (PINB & (1 << bit)) != 0 ) ; // Do nothing until the bit is low
137 while( (PINB & (1 << bit)) == 0 ) ; // Wait for a rising edge
138 start_timer();
139 while( (PINB & (1 << bit)) != 0 ) ; // Wait for a falling edge
140 return read_timer();
141 }
142