-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
158 lines (120 loc) · 3.14 KB
/
main.c
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/*
* main.c
* This file is part of RotaryEncoder.
*
* RotaryEncoder is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* your option) any later version.
*
* RotaryEncoder is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with RotaryEncoder. If not, see <http://www.gnu.org/licenses/>.
*
*
* Created: Oct 10, 2015
* Author: Thomas Heßling <[email protected]>
*
*/
/*
* This program is indented for demonstration purposes only.
* All it accomplishes is running a timer, querying the rotary encoder and
* sending the current position via uart.
*
* To verify the position you have to connect the circuit via uart to a
* computer, e.g. with a FT232-type uart-to-USB converter.
*
*/
#include <avr/io.h>
#include <avr/iom168.h>
#include <avr/sleep.h>
#include <avr/interrupt.h>
#include <util/atomic.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>
#include "rotenc.h"
#ifndef F_CPU
#define F_CPU 16000000UL
#endif
// uart baudrate
#define BAUD 250000L
// verify baud rate setting (taken from the http://www.mikrocontroller.net forums/articles)
#define UBRR_VAL ((F_CPU+BAUD*8)/(BAUD*16)-1) // smart round
#define BAUD_REAL (F_CPU/(16*(UBRR_VAL+1))) // real baudrate
#define BAUD_ERROR ((BAUD_REAL*1000)/BAUD) // error in promille; 1000 = no error
#if ((BAUD_ERROR<990) || (BAUD_ERROR>1010))
#error Error. Your baudrate error exceeds 1%!
#endif
#include <util/setbaud.h>
/* UART-Init ATmega16 */
void uart_init(void)
{
UBRR0H = (uint8_t) UBRR_VAL >> 8;
UBRR0L = (uint8_t) UBRR_VAL & 0xFF;
UCSR0B |= (1<<RXEN0)|(1<<TXEN0)|(1<<RXCIE0);
UCSR0C |= (1<<UCSZ00)|(1<<UCSZ01);
}
void uart_send_char (unsigned char data)
{
while (! ( UCSR0A & (1<<UDRE0)));
UDR0 = data;
}
void uart_send_string (char *data) {
while (*data) {
uart_send_char(*data);
data++;
}
}
unsigned char uart_receive (void)
{
while (!(UCSR0A & (1<<RXC0)));
return UDR0;
}
/*
* When the timer fires we simply call the rotary encoder's update function
*/
ISR (TIMER0_COMPA_vect)
{
rotenc_update();
}
void Timer0Start()
{
// configure timer
TCCR0A = 0;
TIMSK0 |= (1<<OCIE0A);
TCCR0B |= (1<<CS01)|(1<<CS00);
}
int
main(void)
{
// initialization code for various things
set_sleep_mode(SLEEP_MODE_IDLE);
sleep_enable();
uart_init();
rotenc_init();
sei();
Timer0Start();
// position and uart-string variables
uint16_t pos, ppos = 0;
char sdata[50];
// loop indefinitely
for(;;)
{
// enter sleep mode, which we will wake up from when the timer triggers
sleep_cpu();
// query the position and send it via uart (if different from current)
pos = rotenc_get_position();
if (pos != ppos)
{
snprintf(sdata, 49, "%d\n", pos);
uart_send_string(sdata);
ppos = pos;
}
}
return 0;
}