1 | /*
|
2 | * main.c
|
3 | *
|
4 | * Created on: 25.05.2011
|
5 | * Author: moldov
|
6 | */
|
7 |
|
8 | #include <avr/io.h> // Most basic include files
|
9 | #include <avr/interrupt.h> // Add the necessary ones here
|
10 |
|
11 | //flash char string_1[]="Prog Start";
|
12 | //const char left[] PROGMEM ="no support\0x0ALLno support\0x0D\0x0A";
|
13 |
|
14 |
|
15 | char command_indicator;
|
16 |
|
17 | #define TXB8 0
|
18 | #define RXB8 1
|
19 | #define UPE 2
|
20 | #define OVR 3
|
21 | #define FE 4
|
22 | #define UDRE 5
|
23 | #define RXC 7
|
24 |
|
25 | #define FRAMING_ERROR (1<<FE)
|
26 | #define PARITY_ERROR (1<<UPE)
|
27 | #define DATA_OVERRUN (1<<OVR)
|
28 | #define DATA_REGISTER_EMPTY (1<<UDRE)
|
29 | //#define RX_COMPLETE (1<<RXC)
|
30 |
|
31 |
|
32 | //TCCR1A
|
33 | #define WGM11 1
|
34 | #define WGM10 0
|
35 | #define COM1A1 7
|
36 | #define COM1A0 6
|
37 | #define COM1B1 5
|
38 | #define COM1B0 4
|
39 | //TCCR1B
|
40 | #define WGM13 4
|
41 | #define WGM12 3
|
42 | #define CS0 0
|
43 | #define CS1 1
|
44 | #define CS2 2
|
45 |
|
46 |
|
47 | void USART_Transmit( unsigned char data );
|
48 | void USART_Init( unsigned int baud );
|
49 | unsigned char USART_Receive( void );
|
50 |
|
51 | ISR(USART_RXC_vect) {
|
52 | char status, data;
|
53 | status=UCSRA;
|
54 | data=UDR;
|
55 |
|
56 | if ((status & (FRAMING_ERROR | PARITY_ERROR | DATA_OVERRUN))==0) {
|
57 |
|
58 | if (command_indicator == '$') {
|
59 | switch (data ) {
|
60 | case 'C':
|
61 | PORTB |= 0x03;
|
62 | PORTD |= 0x30;
|
63 | USART_Transmit('C');
|
64 | break;
|
65 |
|
66 | case 'V':
|
67 | PORTB &= ~0x03;
|
68 | PORTD &= ~0x30;
|
69 | USART_Transmit('V');
|
70 | break;
|
71 |
|
72 | case 'Z':
|
73 | PORTB |= (1 << 3);
|
74 | USART_Transmit('Z');
|
75 | break;
|
76 |
|
77 | case 'X':
|
78 | PORTB &= ~(1 << 3);
|
79 | USART_Transmit('X');
|
80 | break;
|
81 |
|
82 | case 'F':
|
83 | PORTB |= (1 << 0);
|
84 | PORTB |= (1 << 1);
|
85 | USART_Transmit('F');
|
86 | USART_Transmit(13);
|
87 | USART_Transmit(10);
|
88 |
|
89 | break;
|
90 |
|
91 | case 'B':
|
92 | PORTB &= ~(1 << 0);
|
93 | PORTB &= ~(1 << 1);
|
94 | USART_Transmit('B');
|
95 | break;
|
96 |
|
97 |
|
98 | case 'L':
|
99 | USART_Transmit('L');
|
100 | break;
|
101 |
|
102 | case 'R':
|
103 | USART_Transmit('R');
|
104 | break;
|
105 |
|
106 | case '0':
|
107 | USART_Transmit('0');
|
108 | OCR1AL = 0x00; // 0x00FF это число 256
|
109 | OCR1BL = 0x00; // 0x00FF это число 256
|
110 | break;
|
111 |
|
112 | case '1':
|
113 | USART_Transmit('1');
|
114 | OCR1AL = 0x0F; // 0x00FF это число 256
|
115 | OCR1BL = 0x0F; // 0x00FF это число 256
|
116 | break;
|
117 |
|
118 | case '2':
|
119 | USART_Transmit('2');
|
120 | OCR1AL = 0x3F; // 0x00FF это число 256
|
121 | OCR1BL = 0x3F; // 0x00FF это число 256
|
122 | break;
|
123 |
|
124 | case '3':
|
125 | USART_Transmit('3');
|
126 | OCR1AL = 0x6F; // 0x00FF это число 256
|
127 | OCR1BL = 0x6F; // 0x00FF это число 256
|
128 | break;
|
129 |
|
130 | case '4':
|
131 | USART_Transmit('4');
|
132 | OCR1AL = 0x9F; // 0x00FF это число 256
|
133 | OCR1BL = 0x9F; // 0x00FF это число 256
|
134 | break;
|
135 |
|
136 | case '5':
|
137 | USART_Transmit('5');
|
138 | OCR1AL = 0xCF; // 0x00FF это число 256
|
139 | OCR1BL = 0xCF; // 0x00FF это число 256
|
140 | break;
|
141 | }
|
142 | command_indicator = 0x00;
|
143 |
|
144 |
|
145 | }
|
146 | if(data =='$') {//If we send command "$" shows that
|
147 | command_indicator = '$';
|
148 | //PORTB &= ~(1 << 3);
|
149 | }
|
150 | }
|
151 |
|
152 | }
|
153 | void USART_Transmit( unsigned char data ) {
|
154 | /* Wait for empty transmit buffer */
|
155 | while ( !( UCSRA & (1<<UDRE)) ); /* Put data into buffer, sends the data */
|
156 | UDR = data;
|
157 | }
|
158 | void USART_Init( unsigned int baud ) {
|
159 | /* Set baud rate */
|
160 | UBRRH = (unsigned char)(baud>>8);
|
161 | UBRRL = (unsigned char)baud;
|
162 |
|
163 | /* Enable receiver and transmitter */
|
164 | UCSRB = (1<<RXEN)|(1<<TXEN);
|
165 | UCSRC = (1<<URSEL)|(0<<USBS)|(3<<UCSZ0); // Set frame format: 8data, 1stop bit
|
166 | UCSRB |= (1 << RXCIE); // Enable the USART Recieve Complete interrupt (USART_RXC)
|
167 | sei();// Enable the Global Interrupt Enable flag so that interrupts can be processed
|
168 | }
|
169 | unsigned char USART_Receive( void ) {
|
170 | /* Wait for data to be received */
|
171 | while ( !(UCSRA & (1<<RXC)) ); /* Get and return received data from buffer */
|
172 | return UDR;
|
173 | }
|
174 | // ***********************************************************
|
175 | // Main program
|
176 | //
|
177 | int main(void) {
|
178 |
|
179 | //DDRD=0xFF;
|
180 | DDRD = 0x30; //4-5-й пин порта D настроим как выход
|
181 | DDRB = 0xFF; //
|
182 | // Настройка TIMER1 для генерации ШИМ
|
183 |
|
184 |
|
185 | TCCR1A = 0x00; //stop Timer
|
186 |
|
187 | TCNT1H = 0xFF; // 11111111
|
188 | TCNT1L = 0x00; // 00000000
|
189 |
|
190 | /* регистр OCR1A состоит из двух 8-ми битных регистров OCR1AH и OCR1AL запись в них нужно проводить в указанной последовательности! */
|
191 | OCR1AH = 0x00;
|
192 | OCR1AL = 0x00;
|
193 | OCR1BH = 0x00;
|
194 | OCR1BL = 0x00;
|
195 | ICR1H = 0x00;
|
196 | ICR1L = 0x00;
|
197 |
|
198 | //Got from OR Project
|
199 | TCCR1A = 0xA1;
|
200 | TCCR1B = 0x0A;
|
201 |
|
202 | //Скорость USART 115200 при кварцевом генераторе 7.3MHz USART_Init (0x03);
|
203 | USART_Init (0x03);
|
204 | USART_Transmit('O');//Передаем при включении
|
205 | USART_Transmit('k');//сообщение "Ok!", что свидетельствует
|
206 | USART_Transmit('!');//о правильно работе программы
|
207 | USART_Transmit(0x0d);//переход в начало строки
|
208 | USART_Transmit(0x0a);//переход на новую строку
|
209 | while(1) {} // Infinite loop; define here the
|
210 |
|
211 | }
|