1 | #include <avr/io.h>
|
2 | #include <stdio.h>
|
3 | #include <stdint.h>
|
4 | #include <stdlib.h>
|
5 | #include <util/delay.h>
|
6 |
|
7 |
|
8 | #define DD_MISO 3
|
9 | #define DD_MOSI 2
|
10 | #define DD_SCK 1
|
11 | #define DD_SS 0
|
12 |
|
13 |
|
14 | void uart_init()
|
15 |
|
16 | {
|
17 | uint16_t temp = ((8000000)/(9600.0*16)-1); //Calculation Transmission Rate
|
18 |
|
19 | UBRR0L = temp; //Transmitting Transmission rate
|
20 | UBRR0H = temp>>8;
|
21 |
|
22 | UCSR0B |= (1<<TXEN0)|(1<<RXEN0)|(1<<RXCIE0); //Conect Sending and Receiving
|
23 |
|
24 | UCSR0C |= (1<<UCSZ00)|(1<<UCSZ01); //Assignment format: 8 bit
|
25 |
|
26 | }
|
27 |
|
28 |
|
29 | int sendestring(char *s)
|
30 |
|
31 | {
|
32 | int i=0;
|
33 |
|
34 | for(i=0; s[i] != '\0'; i++)
|
35 | {
|
36 | while bit_is_clear(UCSR0A, UDRE0); //Wait, Until UDRE0 = 1 (Send Register free)
|
37 |
|
38 | UDR0 = s[i]; //Send Data
|
39 |
|
40 | }
|
41 |
|
42 | return 0;
|
43 | }
|
44 |
|
45 |
|
46 | void SPI_MasterInit(void)
|
47 | {
|
48 | //Outputs: MOSI, SCK, SS
|
49 | DDRB = (1<<DD_MOSI)|(1<<DD_SCK)|(1<<DD_SS);
|
50 |
|
51 | //Enable SPI, Master-Mode, set clock rate fck/16
|
52 | SPCR = (1<<SPE)|(1<<MSTR)|(1<<SPR0)|(1<<CPOL);
|
53 | }
|
54 |
|
55 |
|
56 | int SPI_MasterTransmit(uint8_t MOSI)
|
57 | {
|
58 | //Start Transmission
|
59 | SPDR = MOSI;
|
60 |
|
61 | //Wait for transmission complete
|
62 | while(!(SPSR & (1<<SPIF)));
|
63 |
|
64 | //Returned data from slave to master
|
65 | return SPDR;
|
66 | }
|
67 |
|
68 |
|
69 | int main()
|
70 |
|
71 | {
|
72 |
|
73 | char str[50];
|
74 |
|
75 | // Thermo Element to CH0 and CH1
|
76 | uint8_t masterOut1 = 0b10100000;
|
77 |
|
78 | // Intern Temperature measurement:
|
79 | uint8_t masterOut2 = 0b11000000;
|
80 |
|
81 | // Two Dummy Bytes to fill the 32 bits long stream
|
82 | uint8_t masterOut3 = 0b00000000;
|
83 | uint8_t masterOut4 = 0b00000000;
|
84 |
|
85 | uint8_t result1;
|
86 | uint8_t result2;
|
87 | uint8_t result3;
|
88 | uint8_t result4;
|
89 |
|
90 | uint32_t masterIn;
|
91 | uint32_t Temp;
|
92 |
|
93 | uart_init();
|
94 |
|
95 | SPI_MasterInit();
|
96 |
|
97 |
|
98 | for(;;)
|
99 | {
|
100 |
|
101 |
|
102 |
|
103 | //CS Thermochips to low
|
104 | PORTB &= ~(1<<DD_SS);
|
105 |
|
106 | //Byte1
|
107 |
|
108 | result1 = SPI_MasterTransmit(masterOut1);
|
109 | masterIn = result1;
|
110 | masterIn = masterIn | 0b00000000;
|
111 |
|
112 | //For Debugging
|
113 | sendestring(" result1: ");
|
114 | itoa(result1, str, 2); //Representing Binarily
|
115 | sendestring(str);
|
116 |
|
117 | //For Debugging
|
118 | sendestring(" masterIn1: ");
|
119 | itoa(masterIn, str, 2); //Representing Binarily
|
120 | sendestring(str);
|
121 |
|
122 | //Byte2
|
123 | result2 = SPI_MasterTransmit(masterOut2);
|
124 | masterIn = (masterIn<<8) | result2;
|
125 | masterIn = masterIn | 0b0000000000000000;
|
126 |
|
127 | //For Debugging
|
128 | sendestring(" result2: ");
|
129 | itoa(result2, str, 2); //Representing Binarily
|
130 | sendestring(str);
|
131 |
|
132 | //For Debugging
|
133 | sendestring(" masterIn2: ");
|
134 | itoa(masterIn, str, 2); //Representing Binarily
|
135 | sendestring(str);
|
136 |
|
137 | //Byte3
|
138 |
|
139 | result3 = SPI_MasterTransmit(masterOut3);
|
140 | masterIn = (masterIn<<8) | result3;
|
141 | masterIn = masterIn | 0b000000000000000000000000;
|
142 |
|
143 | //For Debugging
|
144 | sendestring(" result3: ");
|
145 | itoa(result3, str, 2); //Representing Binarily
|
146 | sendestring(str);
|
147 |
|
148 | //For Debugging
|
149 | sendestring(" masterIn3: ");
|
150 | itoa(masterIn, str, 2); //Representing Binarily
|
151 | sendestring(str);
|
152 |
|
153 | //Byte4
|
154 |
|
155 | result4 = SPI_MasterTransmit(masterOut4);
|
156 | masterIn = (masterIn<<8) | result4;
|
157 | masterIn = masterIn | 0b00000000000000000000000000000000;
|
158 |
|
159 | //For Debugging
|
160 | sendestring(" result4: ");
|
161 | itoa(result4, str, 2); //Representing Binarily
|
162 | sendestring(str);
|
163 |
|
164 | //For Debugging
|
165 | sendestring(" masterIn4: ");
|
166 | itoa(masterIn, str, 2); //Representing Binarily
|
167 | sendestring(str);
|
168 |
|
169 | //CS des Thermochips auf high
|
170 | PORTB |= (1<<DD_SS);
|
171 |
|
172 |
|
173 | masterIn = masterIn | 0b00000000000000000000000000000000;// The last 5 Bits can be deleted without loss of Resolution.
|
174 |
|
175 | //For Debugging
|
176 | sendestring(" masterInResultBinar: ");
|
177 | itoa(masterIn, str, 2); //Representing Binarily
|
178 | sendestring(str);
|
179 |
|
180 | sendestring(" masterInResult: ");
|
181 | sprintf(str, "%ld", masterIn);
|
182 | sendestring(str);
|
183 |
|
184 | //At an Internal Temperature Measurement: Temperature in ° degrees Celsius calculate according to the data sheet Page21
|
185 | Temp = (masterIn*4/1570)-273;
|
186 |
|
187 | sendestring(" Temp: ");
|
188 | sprintf(str, "%ld", Temp);
|
189 | sendestring(str);
|
190 |
|
191 | _delay_ms(2000);
|
192 | }
|
193 |
|
194 | return 0;
|
195 | }
|