1 | /***************************************************************************************************
|
2 |
|
3 | Beispiel um den Inhalt einer Variable mit der "Interrupt controlled UART library"
|
4 | von Peter Fleury an das terminal Hterm zu senden.
|
5 |
|
6 |
|
7 |
|
8 | Um die Libary zu verwenden müssen die Dateien Soloutionexplorer mit eingebunden werden.
|
9 | uart.c
|
10 | uart.h
|
11 |
|
12 |
|
13 | Atmega88P
|
14 | uartlibary
|
15 | ****************************************************************************************************/
|
16 |
|
17 | #define F_CPU 16000000
|
18 |
|
19 | #include <stdlib.h>
|
20 | #include <avr/io.h>
|
21 | #include <avr/interrupt.h>
|
22 | #include <avr/pgmspace.h>
|
23 | #include "uart.h"
|
24 | #include <util/delay.h>
|
25 |
|
26 |
|
27 | /* Define UART buad rate here */
|
28 | #define UART_BAUD_RATE 9600
|
29 |
|
30 |
|
31 |
|
32 | /* Kofiguration Drehgeber */
|
33 | #define XTAL 16e6 // 16MHz
|
34 |
|
35 |
|
36 | #define PHASE_A (PINB & 1<<PB0)
|
37 | #define PHASE_B (PINB & 1<<PB1)
|
38 |
|
39 | #define LEDS_DDR DDRD
|
40 | #define LEDS PORTD // LEDs against VCC
|
41 |
|
42 | volatile int8_t enc_delta; // -128 ... 127
|
43 | static int8_t last;
|
44 |
|
45 | void encode_init( void ) {
|
46 | int8_t new;
|
47 |
|
48 | // init encoder
|
49 | new = 0;
|
50 | if( PHASE_A ) new = 3;
|
51 | if( PHASE_B ) new ^= 1; // convert gray to binary
|
52 | last = new; // power on state
|
53 | enc_delta = 0;
|
54 |
|
55 | // init timer 0
|
56 | TCCR1B = (1<<WGM12) | (1<<CS12) ; // CTC, prescaler 256
|
57 | OCR1A = (uint8_t)(XTAL / 256 * 1e-3 - 0.5); // 1ms
|
58 | TIMSK1 |= (1<<OCIE1A); // interrupt bei Macht aktivieren
|
59 | }
|
60 |
|
61 | ISR( TIMER1_COMPA_vect ) { // 1ms for manual movement / encoder mit Handbetätigung
|
62 | int8_t new, diff;
|
63 |
|
64 | new = 0;
|
65 | if ( PHASE_A ) new = 3;
|
66 | if ( PHASE_B ) new ^= 1; // convert gray to binary
|
67 | diff = last - new; // difference last - new
|
68 | if( diff & 1 ) { // bit 0 = value (1)
|
69 | last = new; // store new as next last
|
70 | enc_delta += (diff & 2) - 1; // bit 1 = direction (+/-)
|
71 | }
|
72 | }
|
73 |
|
74 | // read 1, 2, or 4 step encoders
|
75 | int8_t encode_read1( void )
|
76 | {
|
77 | int8_t val;
|
78 |
|
79 | // atomic access to enc_delta
|
80 | cli();
|
81 | val = enc_delta;
|
82 | enc_delta = 0;
|
83 |
|
84 | sei();
|
85 | return val; // counts since last call
|
86 | }
|
87 |
|
88 |
|
89 | int main(void)
|
90 | {
|
91 | int32_t val = 0;
|
92 |
|
93 | LEDS_DDR = 0xFF;
|
94 | encode_init();
|
95 | sei();
|
96 |
|
97 | //for(;;){
|
98 | // val += encode_read1(); // read a single step encoder
|
99 | // LEDS = val;
|
100 | // }
|
101 |
|
102 |
|
103 | uint16_t x = 1024;
|
104 | char buffer[7];
|
105 |
|
106 |
|
107 | uart_init(UART_BAUD_SELECT(UART_BAUD_RATE,F_CPU) );
|
108 |
|
109 | sei();
|
110 |
|
111 |
|
112 | while (1)
|
113 | {
|
114 |
|
115 | val += encode_read1(); // read a single step encoder
|
116 | LEDS = val;
|
117 |
|
118 | itoa( val, buffer, 10); // convert interger into string (decimal format)
|
119 | uart_puts(buffer); // and transmit string to UART
|
120 | uart_puts("\r\n"); //neue Zeile verwenden CR+LF im Terminal
|
121 | _delay_ms(1);
|
122 |
|
123 | }
|
124 |
|
125 |
|
126 |
|
127 | }
|