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