Hallo Leute. Ich möchte mit meinem Xplained-A1-Board und einem DS18B20 die Temperatur messen. Dazu hab ich mir bereits schon aus dem Tread Beitrag "DS1820, DS18B20 in C" einige ZIP-Archive heruntergeladen. Leider haben die XMegas einen anderen Befehlssatz als in den Beispielen verwendet wurde, zumal beim kompilieren eh Fehler angezeigt werden, die noch nicht in dem Tread diskutiert wurden. Gibts denn schon Bibliotheken die sich am Befehlssatz der XMega`s orientieren oder andere Möglichkeiten den Code an meinen Xmega128 anzupassen, als alles mühsam von Hand umzuschreiben? Viele Grüße
Ich habe den Code nun abgeändert. Lässt sich kompilieren und scheinbar läuft er auch auf dem Board. Die Ausgabe über UART funktioniert. Ein kleines Problem gibts aber dennoch: Der Sensor macht beim anschließen an VCC und GND Probleme. Als Spannungsversorgung nutze ich die VCC- und GND-Pins des Boards. Wenn ich den Sensor anschließe kommt es scheinbar zu einem Kurzschluss - wodurch die Spannung scheinbar zusammenbricht und die Status-LED des Boards ausgeht. Die Verkabelung müsste eigentlich stimmen. Ich würde jetzt vermuten, dass der Sensor defekt ist, aber kann hier auch ein anderer Fehler zugrunde liegen? Der Sensor wird somit nicht erkannt. Ich weis ebenfalls nicht ob der Port und der Pin von der Syntax her ordnungsgemäß definiert ist. Könnte da mal jemand drüber gucken? Viele Grüße
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 | }
|
Bitte melde dich an um einen Beitrag zu schreiben. Anmeldung ist kostenlos und dauert nur eine Minute.
Bestehender Account
Schon ein Account bei Google/GoogleMail? Keine Anmeldung erforderlich!
Mit Google-Account einloggen
Mit Google-Account einloggen
Noch kein Account? Hier anmelden.