1 |
|
2 | /**
|
3 | * \file
|
4 | *
|
5 | * \brief Empty user application template
|
6 | *
|
7 | */
|
8 |
|
9 | /**
|
10 | * \mainpage User Application template doxygen documentation
|
11 | *
|
12 | * \par Empty user application template
|
13 | *
|
14 | * Bare minimum empty user application template
|
15 | *
|
16 | * \par Content
|
17 | *
|
18 | * -# Include the ASF header files (through asf.h)
|
19 | * -# "Insert system clock initialization code here" comment
|
20 | * -# Minimal main function that starts with a call to board_init()
|
21 | * -# "Insert application code here" comment
|
22 | *
|
23 | */
|
24 |
|
25 | /*
|
26 | * Include header files for all drivers that have been imported from
|
27 | * Atmel Software Framework (ASF).
|
28 | */
|
29 |
|
30 | #define uchar unsigned char
|
31 | #define uint unsigned int
|
32 | #define bit uchar
|
33 | #define idata
|
34 | #define code
|
35 |
|
36 | #define W1_PIN PIN2_bm
|
37 | #define W1_IN PORTD_IN
|
38 | #define W1_OUT PORTD_OUT
|
39 | #define W1_DDR PORTD_DIR
|
40 |
|
41 | #include "1wire.h"
|
42 | #include <avr/io.h>
|
43 | #include <avr/interrupt.h>
|
44 | #include <avr/signal.h>
|
45 | #include <stdlib.h>
|
46 | #include <stdio.h>
|
47 | #include "delay.h"
|
48 | #include <string.h>
|
49 |
|
50 | void UART_init(void);
|
51 | void Clock_init(void);
|
52 | void Int_init(void);
|
53 | void TimerC0_init(void);
|
54 |
|
55 | void Send_UART_string(char data[]);
|
56 | void Send_UART_integer(unsigned int data);
|
57 |
|
58 | void start_meas( void );
|
59 | void read_meas( void );
|
60 |
|
61 | uint16_t TCTW=0x85ED;
|
62 | uint8_t lenght, Counter;
|
63 |
|
64 |
|
65 | int main (void)
|
66 | {
|
67 | // Insert system clock initialization code here (sysclk_init()).
|
68 |
|
69 | Clock_init();
|
70 | Int_init();
|
71 | TimerC0_init();
|
72 | UART_init();
|
73 |
|
74 | TCC0.PER = TCTW;
|
75 |
|
76 | start_meas();
|
77 |
|
78 | while (1)
|
79 | {
|
80 | }
|
81 |
|
82 | // Insert application code here, after the board has been initialized.
|
83 | }
|
84 |
|
85 |
|
86 | //=================Initialisierungen================
|
87 |
|
88 | void Clock_init(void)
|
89 | {
|
90 | OSC.CTRL |= OSC_RC32MEN_bm; // Oszillator auf 32Mhz stellen
|
91 | while(!(OSC.STATUS & OSC_RC32MEN_bm)); // Warten bis der Oszillator bereit ist
|
92 | CCP = CCP_IOREG_gc;
|
93 | CLK.CTRL = CLK_SCLKSEL_RC32M_gc; // Clock auf 32MHz stellen
|
94 | }
|
95 |
|
96 | void UART_init(void)
|
97 | {
|
98 | PORTC.DIR = 0xEF;
|
99 |
|
100 | USARTC0.BAUDCTRLB = 0; // BSCALE = 0
|
101 | USARTC0.BAUDCTRLA = 0x67; // Baudrate 19200 @ 41MHz
|
102 | USARTC0.CTRLB = USART_TXEN_bm | USART_RXEN_bm; // RX+TX Enable CLK
|
103 | USARTC0.CTRLC = 0x03; // Async, no parity, 8 bit data, 1 stop bit
|
104 | USARTC0.CTRLA = 0;
|
105 | }
|
106 |
|
107 | void Int_init(void)
|
108 | {
|
109 | PMIC.CTRL |= PMIC_LOLVLEN_bm | PMIC_MEDLVLEN_bm | PMIC_HILVLEN_bm; // Interrupts (Highlevel, Mediumlevel und Lowlevel freigeben)
|
110 | sei(); // Globale Interruptfreigabe
|
111 | }
|
112 |
|
113 | void TimerC0_init()
|
114 | {
|
115 | TCC0.CTRLA = TC_CLKSEL_DIV1024_gc; // Vorteiler einstellen
|
116 | TCC0.CTRLB = 0x00; // Timer in Normalmodus stellen
|
117 | TCC0.INTCTRLA = 0x03; // Interrupt konfigurieren
|
118 | }
|
119 |
|
120 |
|
121 | //==================ISR===============
|
122 |
|
123 |
|
124 | ISR(TCC0_OVF_vect)
|
125 | {
|
126 | // Send_UART_integer(0x22);
|
127 | read_meas();
|
128 |
|
129 | TCC0.PER = TCTW;
|
130 | }
|
131 |
|
132 |
|
133 | //==============Sonstige Funktionen=============
|
134 |
|
135 |
|
136 | void Send_UART_string(char data[])
|
137 | {
|
138 | lenght = strlen(data);
|
139 |
|
140 | while(Counter < lenght)
|
141 | {
|
142 | while (!(USARTC0.STATUS & USART_DREIF_bm));
|
143 | USARTC0.DATA = data[Counter];
|
144 | Counter++;
|
145 | }
|
146 |
|
147 | Counter = 0x00;
|
148 | while (!( USARTC0.STATUS & USART_DREIF_bm));
|
149 | USARTC0.DATA = 0x0A;
|
150 | while (!( USARTC0.STATUS & USART_DREIF_bm));
|
151 | USARTC0.DATA = 0x0D;
|
152 | }
|
153 |
|
154 | void Send_UART_integer(unsigned int data)
|
155 | {
|
156 | while (!(USARTC0.STATUS & USART_DREIF_bm));
|
157 | USARTC0.DATA = data;
|
158 | }
|
159 |
|
160 |
|
161 | //=================Temperaturmessen================
|
162 |
|
163 |
|
164 | void start_meas( void ){
|
165 | if( W1_IN & 1<< W1_PIN ){
|
166 | w1_command( CONVERT_T, NULL );
|
167 | PORTD_OUT |= 1<< W1_PIN;
|
168 | W1_DDR |= 1<< W1_PIN; // parasite power on
|
169 |
|
170 | }else{
|
171 | Send_UART_string( "Short Circuit !" );
|
172 | }
|
173 | }
|
174 |
|
175 | void read_meas( void )
|
176 | {
|
177 | uchar id[8], diff;
|
178 | char s[30];
|
179 | uchar i;
|
180 | uint temp;
|
181 |
|
182 | for( diff = SEARCH_FIRST; diff != LAST_DEVICE; ){
|
183 | diff = w1_rom_search( diff, id );
|
184 |
|
185 | if( diff == PRESENCE_ERR ){
|
186 | Send_UART_string( "No Sensor found" );
|
187 | break;
|
188 | }
|
189 | if( diff == DATA_ERR ){
|
190 | Send_UART_string( "Bus Error" );
|
191 | break;
|
192 | }
|
193 | if( id[0] == 0x28 || id[0] == 0x10 ){ // temperature sensor
|
194 | Send_UART_string( "ID: " );
|
195 | for( i = 0; i < 8; i++ ){
|
196 | sprintf( s, "%02X ", id[i] );
|
197 | Send_UART_string( s );
|
198 | }
|
199 | w1_byte_wr( READ ); // read command
|
200 | temp = w1_byte_rd(); // low byte
|
201 | temp |= (uint)w1_byte_rd() << 8; // high byte
|
202 | if( id[0] == 0x10 ) // 9 -> 12 bit
|
203 | temp <<= 3;
|
204 | sprintf( s, " T: %04X = ", temp ); // hex value
|
205 | Send_UART_string( s );
|
206 | sprintf( s, "%4d.%01døC", temp >> 4, (temp << 12) / 6553 ); // 0.1øC
|
207 | Send_UART_string( s );
|
208 | }
|
209 | }
|
210 | Send_UART_string( "" );
|
211 | }
|