1 | /* define CPU frequency in Mhz here if not defined in Makefile */
|
2 | #ifndef F_CPU
|
3 | #define F_CPU 6000000UL // Externer Takt von dem FTDI FT232R Module (6Mz)
|
4 | #endif
|
5 |
|
6 | #include <util/delay.h>
|
7 | #include <stdlib.h>
|
8 | #include <string.h>
|
9 | #include <avr/io.h>
|
10 | #include <inttypes.h>
|
11 | #include <avr/interrupt.h>
|
12 | #include <avr/pgmspace.h>
|
13 |
|
14 | #include "peter_fleury_usart\uart.h"
|
15 | #include "spi.h"
|
16 | #include "adc.h"
|
17 |
|
18 | #define UART_BAUD_RATE 125000
|
19 |
|
20 | #define CS_DDR DDRC // Chip Select fuer TI Temp Sensor
|
21 | #define CS_PORT PORTC
|
22 | #define P_CS PORTC4
|
23 |
|
24 |
|
25 | volatile uint32_t Zehn_Millisekunden = 0;
|
26 |
|
27 | volatile uint32_t Mittelwert_ADC = 0;
|
28 |
|
29 | volatile uint8_t Mittelwert_Zaehler = 0;
|
30 |
|
31 | volatile uint8_t diesen_Mittelwert_ignorieren = 1;
|
32 |
|
33 | volatile uint16_t Messungs_Zaehler = 0;
|
34 |
|
35 | volatile uint16_t Messwerte[16]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
|
36 |
|
37 | volatile char rs232_array[7] = {'_','_','_','_','_','_',0};
|
38 |
|
39 |
|
40 | ISR(TIMER1_COMPB_vect)
|
41 | {
|
42 | Zehn_Millisekunden ++;
|
43 | }
|
44 |
|
45 | ISR(ADC_vect)
|
46 | {
|
47 | uint16_t alter_Wert = Messwerte[Mittelwert_Zaehler];
|
48 | uint16_t neuer_Wert = ADC;
|
49 | Messwerte[Mittelwert_Zaehler] = neuer_Wert;
|
50 | Mittelwert_Zaehler++;
|
51 | Mittelwert_ADC = Mittelwert_ADC + (uint32_t) neuer_Wert - (uint32_t) alter_Wert;
|
52 | if (Mittelwert_Zaehler >= 16)
|
53 | {
|
54 | Mittelwert_Zaehler = 0;
|
55 | if (diesen_Mittelwert_ignorieren) // Der erste Mittelwert wird entsorgt !
|
56 | {
|
57 | diesen_Mittelwert_ignorieren = 0;
|
58 | }
|
59 | else // Zweiter und folgender Mittelwert
|
60 | {
|
61 | rs232_array[0] = (uint8_t) (Mittelwert_ADC>>12); // >> 12 -> Division durch 16 entspricht schieben um 4
|
62 | // schieben um 8 fuer das hoeherwertige Byte
|
63 | rs232_array[1] = (uint8_t) (Mittelwert_ADC>>4);
|
64 | CS_PORT &= ~(1<<P_CS); // Chip Select auf low
|
65 | _delay_us(1);
|
66 | rs232_array[2] = SPI_MasterReceive();
|
67 | _delay_us(1);
|
68 | rs232_array[3] = SPI_MasterReceive();
|
69 | _delay_us(1);
|
70 | CS_PORT |= (1<<P_CS); // Chip Select auf high
|
71 | _delay_ms(1);
|
72 | rs232_array[4] = (uint8_t) (Messungs_Zaehler>>8);
|
73 | rs232_array[5] = (uint8_t) (Messungs_Zaehler++);
|
74 | for (int i=0;i<7;i++)
|
75 | uart_putc(rs232_array[i]);
|
76 | }
|
77 | }
|
78 | }
|
79 |
|
80 | int main ()
|
81 | {
|
82 | CS_DDR |= (1<<P_CS),
|
83 | CS_PORT |= (1<<P_CS);
|
84 |
|
85 | TCCR1B = (1<<WGM12) | (1<<CS11) | (1<<CS10);
|
86 | TIMSK1 = (1<<OCIE1B);//(1<<OCIE1A) | (1<<OCIE1B);
|
87 |
|
88 | OCR1A = 937; // 937 fuer ~10 ms
|
89 | OCR1B = 937; // 937 fuer ~10 ms // OCR1B fuer das Triggern des ADCs
|
90 | TCNT1 = 0x00;
|
91 |
|
92 | uart_init(UART_BAUD_SELECT(UART_BAUD_RATE,F_CPU));
|
93 | init_ADC();
|
94 | SPI_MasterInit();
|
95 |
|
96 | sei();
|
97 |
|
98 | for (;;)
|
99 | {
|
100 | }
|
101 | }
|