2116 |
paro |
1 |
// Template - Roman Pavelka, 2011 |
|
|
2 |
// written for atmega8 |
|
|
3 |
|
|
|
4 |
#define F_CPU 16000000 |
|
|
5 |
#define BAUD 1000000 |
|
|
6 |
//#define MYUBRR F_CPU/8/BAUD-1 |
|
|
7 |
#define MYUBRR 1 |
|
|
8 |
|
|
|
9 |
#include <avr/io.h> |
|
|
10 |
#include <avr/interrupt.h> |
|
|
11 |
#include <util/delay.h> |
|
|
12 |
|
|
|
13 |
#include "./libs/usart.h" |
|
|
14 |
#include "./libs/spi.h" |
|
|
15 |
//#include "./libs/mcp4922.h" |
|
|
16 |
//#include "./libs/adc.h" |
|
|
17 |
//#include "./libs/timer.h" |
|
|
18 |
|
|
|
19 |
|
|
|
20 |
#define LEN 128 |
|
|
21 |
|
|
|
22 |
void act(void); |
|
|
23 |
void drive(unsigned char t); |
|
|
24 |
|
|
|
25 |
int main (void) { |
|
|
26 |
USART_Init(MYUBRR); |
|
|
27 |
PORTC = 0b00000000; // drive port |
|
|
28 |
DDRC = 0b00000011; // drive port out |
|
|
29 |
|
|
|
30 |
DDRB = 0b00101110; //MOSI out CONV out, DAC CS out |
|
|
31 |
PORTB = 0b00101010; // SCK, MOSI, CONV, DAC high |
|
|
32 |
SPI_MasterInit(); |
|
|
33 |
|
|
|
34 |
sei(); |
|
|
35 |
|
|
|
36 |
while (1) ; |
|
|
37 |
return 0; |
|
|
38 |
} |
|
|
39 |
|
|
|
40 |
|
|
|
41 |
void act(void) { |
|
|
42 |
|
|
|
43 |
unsigned char data[2*LEN]; |
|
|
44 |
unsigned char i,j; |
|
|
45 |
|
|
|
46 |
for(j=0;j<40;j++) { |
|
|
47 |
for(i=0;i<LEN;i++) { |
|
|
48 |
drive(i); |
|
|
49 |
_delay_us(5); |
|
|
50 |
} |
|
|
51 |
} |
|
|
52 |
|
|
|
53 |
for(i=0;i<LEN;i++) { |
|
|
54 |
drive(i); |
|
|
55 |
|
|
|
56 |
PORTB |= 0b00000100; |
|
|
57 |
_delay_us(4); // converting |
|
|
58 |
PORTB &= 0b11111011; |
|
|
59 |
|
|
|
60 |
data[2*i]=SPI_MasterTransmit(0x0); |
|
|
61 |
data[2*i+1]=SPI_MasterTransmit(0x0); |
|
|
62 |
} |
|
|
63 |
|
|
|
64 |
PORTC=0; |
|
|
65 |
|
|
|
66 |
for(i=0;i<LEN;i++) { |
|
|
67 |
USART_Transmit(data[2*i]); |
|
|
68 |
// USART_Transmit(data[2*i+1]); |
|
|
69 |
} |
|
|
70 |
|
|
|
71 |
} |
|
|
72 |
|
|
|
73 |
|
|
|
74 |
void value(void) { |
|
|
75 |
|
|
|
76 |
unsigned char i,j; |
|
|
77 |
long resultH,resultL; |
|
|
78 |
|
|
|
79 |
resultH=0; |
|
|
80 |
resultL=0; |
|
|
81 |
|
|
|
82 |
for(j=0;j<32;j++) { |
|
|
83 |
for(i=0;i<LEN;i++) { |
|
|
84 |
drive(i); |
|
|
85 |
_delay_us(5); |
|
|
86 |
} |
|
|
87 |
} |
|
|
88 |
|
|
|
89 |
for(j=0;j<4;j++) { |
|
|
90 |
for(i=0;i<LEN/2;i++) { |
|
|
91 |
drive(i); |
|
|
92 |
|
|
|
93 |
PORTB |= 0b00000100; |
|
|
94 |
_delay_us(4); // converting |
|
|
95 |
PORTB &= 0b11111011; |
|
|
96 |
|
|
|
97 |
resultH+=(unsigned char)SPI_MasterTransmit(0x0); |
|
|
98 |
resultL+=(unsigned char)SPI_MasterTransmit(0x0); |
|
|
99 |
} |
|
|
100 |
for(i=LEN/2;i<(LEN/4)*3;i++) { //dela podivnou kvantitatizaci vysledku |
|
|
101 |
drive(i); |
|
|
102 |
|
|
|
103 |
PORTB |= 0b00000100; |
|
|
104 |
_delay_us(4); // converting |
|
|
105 |
PORTB &= 0b11111011; |
|
|
106 |
|
|
|
107 |
resultH-=(unsigned char)SPI_MasterTransmit(0x0); |
|
|
108 |
resultL-=(unsigned char)SPI_MasterTransmit(0x0); |
|
|
109 |
} |
|
|
110 |
} |
|
|
111 |
|
|
|
112 |
PORTC=0; // drive off |
|
|
113 |
|
|
|
114 |
// resultH *= 256; |
|
|
115 |
// USART_Transmit_longnum(resultH+resultL); |
|
|
116 |
|
|
|
117 |
USART_Transmit_longnum(resultH); |
|
|
118 |
|
|
|
119 |
USART_Transmit('\n'); |
|
|
120 |
} |
|
|
121 |
|
|
|
122 |
|
|
|
123 |
ISR(USART_RXC_vect) { |
|
|
124 |
cli(); |
|
|
125 |
char c; |
|
|
126 |
|
|
|
127 |
c=UDR; //must be read to untrigger interupt |
|
|
128 |
|
|
|
129 |
if (c=='a') |
|
|
130 |
act(); |
|
|
131 |
else if (c=='v') |
|
|
132 |
value(); |
|
|
133 |
|
|
|
134 |
sei(); |
|
|
135 |
} |
|
|
136 |
|
|
|
137 |
|
|
|
138 |
void drive(unsigned char t) { |
|
|
139 |
if (t<LEN/2) |
|
|
140 |
PORTC = 0b00000001; |
|
|
141 |
else |
|
|
142 |
PORTC = 0b00000010; |
|
|
143 |
} |
|
|
144 |
|
|
|
145 |
|