1 | #define F_CPU 16000000UL
|
2 |
|
3 | #define UARTaktiv //Auskommentieren, um UART zu deaktivieren
|
4 |
|
5 | #include <avr/io.h>
|
6 | #include <util/delay.h>
|
7 | #include <avr/interrupt.h>
|
8 |
|
9 | void TimerInit ()
|
10 | {
|
11 | TCCR1A=0b10100001;
|
12 | TCCR1B=0b00000011;
|
13 | DDRB=(1<<PINB6) | (1<<PINB5);
|
14 | }
|
15 |
|
16 |
|
17 | void USART_init(uint8_t baud)
|
18 | {
|
19 |
|
20 | /*Setzen der Baudrate (Übertragungsrate)*/
|
21 | UBRR1H = (unsigned char) (baud>>8);
|
22 | UBRR1L = (unsigned char) baud;
|
23 |
|
24 | /*Aktivieren von Empfänger(RX) und Sender(TX)*/
|
25 | UCSR1B = (1<<RXEN1) | (1<<TXEN1);
|
26 |
|
27 | /*Setzen des Formates: 8bit Groß, 2bits zum Stoppen*/
|
28 | UCSR1C = (1<<USBS1) | (3<<UCSZ10);
|
29 |
|
30 | }
|
31 |
|
32 | void USARTTransmit (unsigned char data)
|
33 | {
|
34 | /*Auf leeren Übertragungspuffer warten*/
|
35 | while (!(UCSR1A & (1<<UDRE1)) );
|
36 |
|
37 | /*Packe die Daten in den Puffer und sende diese*/
|
38 | UDR1 = data;
|
39 | }
|
40 |
|
41 | void USART_Recieve()
|
42 | {
|
43 | /*Warte auf Empfangen von Daten*/
|
44 | while (!(UCSR1A &(1<<RXC1)) );
|
45 |
|
46 | /*Bekomme die Daten vom Puffe und gebe sie zurück*/
|
47 | return UDR1;
|
48 | }
|
49 |
|
50 | void SPIinit(void)
|
51 | {
|
52 | DDRB = (1<<PINB1)|(1<<PINB2)|(1<<PINB0);
|
53 | SPCR = 0b01110001;
|
54 | SPSR= 0;
|
55 | }
|
56 | unsigned char SPI_start(unsigned char cData)
|
57 | {
|
58 | SPDR = cData;
|
59 | while(!(SPSR & (1<<SPIF)));
|
60 | return SPDR;
|
61 | }
|
62 |
|
63 | uint8_t SensorArray()
|
64 | {
|
65 | [...]
|
66 | }
|
67 |
|
68 | void PullupSet()
|
69 | {
|
70 | PORTB = (1<<PINB7);
|
71 | PORTC = (1<<PINC6);
|
72 | PORTD = (1<<PIND4);
|
73 | PORTF = (1<<PINF7) | (1<<PINF5) | (1<<PINF4);
|
74 | }
|
75 |
|
76 |
|
77 | int main(void)
|
78 | {
|
79 | uint16_t ObjectBlock;
|
80 | uint16_t Checksum;
|
81 | uint16_t signatureNr;
|
82 | uint16_t xposition;
|
83 | uint16_t yposition;
|
84 | uint16_t width;
|
85 | uint16_t height;
|
86 | uint16_t count;
|
87 | uint8_t buffer;
|
88 |
|
89 | uint8_t sig1;
|
90 | uint8_t sig2;
|
91 |
|
92 | TimerInit();
|
93 | USART_init(103);
|
94 | PullupSet();
|
95 | SPIinit();
|
96 |
|
97 | while(1)
|
98 | {
|
99 | count++;
|
100 | buffer=SPI_start(0xFF);
|
101 |
|
102 | if(buffer==0xAA )
|
103 | {
|
104 | count=0;
|
105 | sig1 =(buffer);
|
106 | }
|
107 | else if (count==1)
|
108 | sig2 =(buffer);
|
109 | else if (count==2)
|
110 | Checksum=buffer;
|
111 | else if (count==3)
|
112 | Checksum=Checksum+buffer<<8;
|
113 | else if (count==4)
|
114 | signatureNr = buffer;
|
115 | else if (count==5)
|
116 | signatureNr+= buffer<<8;
|
117 | else if (count==6)
|
118 | xposition=buffer;
|
119 | else if (count==7)
|
120 | xposition+=buffer<<8;
|
121 | else if (count==8)
|
122 | yposition=buffer;
|
123 | else if (count==9)
|
124 | yposition=yposition+buffer<<8;
|
125 | else if (count==10)
|
126 | width=buffer;
|
127 | else if (count==11)
|
128 | width=width+buffer<<8;
|
129 | else if (count==12)
|
130 | height=buffer;
|
131 | else if (count==13)
|
132 | height=height+buffer<<8;
|
133 |
|
134 | if ((sig1==0xAA) && (sig2==0x55))
|
135 | {
|
136 | USARTTransmit((uint8_t)((xposition & 0xff00)>>8));
|
137 | USARTTransmit((uint8_t)(xposition & 0x00ff));
|
138 | USARTTransmit(sig1);
|
139 | USARTTransmit(sig2);
|
140 | USARTTransmit((uint8_t)((Checksum & 0xff00)>>8));
|
141 | USARTTransmit((uint8_t)(Checksum & 0x00ff));
|
142 | USARTTransmit((uint8_t)((signatureNr & 0xff00)>>8));
|
143 | USARTTransmit((uint8_t)(signatureNr & 0x00ff));
|
144 | USARTTransmit((uint8_t)((xposition & 0xff00)>>8));
|
145 | USARTTransmit((uint8_t)(xposition & 0x00ff));
|
146 | USARTTransmit((uint8_t)((yposition & 0xff00)>>8));
|
147 | USARTTransmit((uint8_t)(yposition & 0x00ff));
|
148 | USARTTransmit((uint8_t)((width & 0xff00)>>8));
|
149 | USARTTransmit((uint8_t)(width & 0x00ff));
|
150 | USARTTransmit((uint8_t)((height & 0xff00)>>8));
|
151 | USARTTransmit((uint8_t)(height & 0x00ff));
|
152 | USARTTransmit(13);
|
153 | }
|
154 | }
|
155 | }
|