1 |
// Oscilloscope firmware, Roman Pavelka, 2011 |
1 |
// Oscilloscope firmware, Roman Pavelka, 2011 |
2 |
// written for atmega8 |
2 |
// written for atmega8 |
3 |
|
3 |
|
4 |
#define F_CPU 16000000 |
4 |
#define F_CPU 16000000 |
5 |
#define BAUD 1000000 |
5 |
#define BAUD 1000000 |
6 |
//#define MYUBRR F_CPU/8/BAUD-1 |
6 |
//#define MYUBRR F_CPU/8/BAUD-1 |
7 |
#define MYUBRR 1 |
7 |
#define MYUBRR 1 |
8 |
|
8 |
|
9 |
#include "./libs/usart.h" |
9 |
#include "./libs/usart.h" |
10 |
#include "./libs/spi.h" |
10 |
#include "./libs/spi.h" |
11 |
|
11 |
|
12 |
#include <avr/io.h> |
12 |
#include <avr/io.h> |
13 |
#include <avr/interrupt.h> |
13 |
#include <avr/interrupt.h> |
14 |
#include <util/delay.h> |
14 |
#include <util/delay.h> |
15 |
#include <inttypes.h> |
15 |
#include <inttypes.h> |
16 |
|
16 |
|
17 |
#define LEN 100 |
17 |
#define LEN 100 |
18 |
uint8_t data[2*LEN]; |
18 |
uint8_t data[2*LEN]; |
19 |
|
19 |
|
20 |
void measure(void); |
20 |
void measure(void); |
21 |
void send(void); |
21 |
void send(void); |
22 |
|
22 |
|
23 |
|
23 |
|
24 |
int main (void) { |
24 |
int main (void) { |
25 |
USART_Init(MYUBRR); |
25 |
USART_Init(MYUBRR); |
26 |
|
26 |
|
27 |
DDRB = 0b00100100; //SCK, SS out |
27 |
DDRB = 0b00100100; //SCK, SS out |
28 |
PORTB = 0b00000000; // CONV low |
28 |
PORTB = 0b00000000; // CONV low |
29 |
|
29 |
|
30 |
SPI_MasterInit(); |
30 |
SPI_MasterInit(); |
31 |
|
31 |
|
32 |
sei(); |
32 |
sei(); |
33 |
|
33 |
|
34 |
while(1) {} |
34 |
while(1) {} |
35 |
|
35 |
|
36 |
return 0; |
36 |
return 0; |
37 |
|
37 |
|
38 |
} |
38 |
} |
39 |
|
39 |
|
40 |
|
40 |
|
41 |
void measure(void) { |
41 |
void measure(void) { |
42 |
uint8_t i; |
42 |
uint8_t i; |
43 |
|
43 |
|
44 |
for(i=0;i<LEN;i++) { |
44 |
for(i=0;i<LEN;i++) { |
45 |
PORTB |= 0b00000100; |
45 |
PORTB |= 0b00000100; |
46 |
_delay_us(4); |
46 |
_delay_us(4); |
47 |
PORTB &= 0b11111011; //one conversion |
47 |
PORTB &= 0b11111011; //one conversion |
48 |
|
48 |
|
49 |
data[2*i] = SPI_MasterTransmit(0x0); |
49 |
data[2*i] = SPI_MasterTransmit(0x0); |
50 |
data[2*i+1] = SPI_MasterTransmit(0x0); |
50 |
data[2*i+1] = SPI_MasterTransmit(0x0); |
51 |
} |
51 |
} |
52 |
} |
52 |
} |
53 |
|
53 |
|
54 |
void send(void) { |
54 |
void send(void) { |
55 |
uint8_t i; |
55 |
uint8_t i; |
56 |
|
56 |
|
57 |
for(i=0;i<LEN;i++) { |
57 |
for(i=0;i<LEN;i++) { |
58 |
|
58 |
|
59 |
/* USART_Transmit_uint16(data[i]); //ASCII transfer |
59 |
/* USART_Transmit_uint16(data[i]); //ASCII transfer |
60 |
USART_Transmit('\n');*/ |
60 |
USART_Transmit('\n');*/ |
61 |
|
61 |
|
62 |
USART_Transmit(data[2*i]); //Binary transfer |
62 |
USART_Transmit(data[2*i]); //Binary transfer |
63 |
USART_Transmit(data[2*i+1]); |
63 |
USART_Transmit(data[2*i+1]); |
64 |
// USART_Transmit('\n'); |
64 |
// USART_Transmit('\n'); |
65 |
|
65 |
|
66 |
} |
66 |
} |
67 |
} |
67 |
} |
68 |
|
68 |
|
69 |
|
69 |
|
70 |
ISR(USART_RXC_vect) { |
70 |
ISR(USART_RXC_vect) { |
71 |
char data; |
71 |
char data; |
72 |
|
72 |
|
73 |
data=UDR; //must be read to untrigger interupt |
73 |
data=UDR; //must be read to untrigger interupt |
74 |
|
74 |
|
75 |
if (data=='m') { |
75 |
if (data=='m') { |
76 |
measure(); |
76 |
measure(); |
77 |
send(); |
77 |
send(); |
78 |
} |
78 |
} |
79 |
} |
79 |
} |