root / Version 0.5 / RS232_MUX.X / main.c @ master
Historique | Voir | Annoter | Télécharger (3,705 ko)
1 |
/*
|
---|---|
2 |
* File: main.c
|
3 |
* Author: eniro
|
4 |
*
|
5 |
* Created on March 25, 2024, 3:51 PM
|
6 |
*/
|
7 |
|
8 |
#define F_CPU 24000000UL |
9 |
|
10 |
#include <xc.h> |
11 |
#include <util/delay.h> |
12 |
#include <avr/interrupt.h> |
13 |
|
14 |
|
15 |
|
16 |
//USART setup
|
17 |
#define UART_BAUD_VALUE 10000 // 10000 is equivalent as 9600 bauds according to datasheet |
18 |
#define USART0_REG 0x3 |
19 |
|
20 |
|
21 |
//OSC. setup
|
22 |
#define FREQSEL 0x9 |
23 |
#define _FREQSEL_REG_WR ((FREQSEL) << 2) |
24 |
#define _USART0_REG_WR (USART0_REG & 0x7) |
25 |
|
26 |
|
27 |
|
28 |
//Private vars
|
29 |
uint8_t _rxReicv = 0; // temp byte |
30 |
uint8_t _rxBuf[256]; //back end serial buffer |
31 |
uint16_t bytesAvailable = 0; //show if bytes available... |
32 |
|
33 |
|
34 |
|
35 |
uint8_t rxRead(void); //read 1 byte from buffer |
36 |
|
37 |
|
38 |
|
39 |
|
40 |
int main(void) { |
41 |
|
42 |
|
43 |
|
44 |
|
45 |
uint8_t localBuf[50];
|
46 |
int k = 0; |
47 |
|
48 |
PORTD.DIR = 0x01; // DEBUG LED |
49 |
PORTA.DIR = 0x0D; //Set TX/RX IO and RX/TX debug leds for UART0 |
50 |
PORTB.DIR = 0x0D; //Set TX/RX IO and RX/TX debug leds for UART3 |
51 |
PORTC.DIR = 0x0D; //Set TX/RX IO and RX/TX debug leds for UART1 |
52 |
PORTE.DIR = 0x0D; //Set TX/RX IO and RX/TX debug leds for UART2 |
53 |
PORTF.DIR = 0x0D; //Set TX/RX IO and RX/TX debug leds for UART4 |
54 |
//PORTMUX.USARTROUTEA = _USART0_REG_WR; //set TX and RX on PD4 and PD5 pins
|
55 |
_PROTECTED_WRITE(CLKCTRL.OSCHFCTRLA, _FREQSEL_REG_WR); //switch to 24 MHz
|
56 |
|
57 |
|
58 |
USART0.BAUD = UART_BAUD_VALUE; |
59 |
USART0.CTRLA = 0x80; //enable RX complete interrupt |
60 |
USART0.CTRLB = 0xD0; //Enable TX and RX sending |
61 |
sei(); //never forget to enable global interrupt mask !
|
62 |
|
63 |
|
64 |
//bad guy
|
65 |
PORTA.OUT = 0x04;
|
66 |
_delay_ms(250);
|
67 |
PORTA.OUT = 0x0C;
|
68 |
_delay_ms(250);
|
69 |
|
70 |
PORTA.OUT = 0x00;
|
71 |
PORTB.OUT = 0x04;
|
72 |
_delay_ms(250);
|
73 |
PORTB.OUT = 0x0C;
|
74 |
_delay_ms(250);
|
75 |
|
76 |
PORTB.OUT = 0x00;
|
77 |
PORTC.OUT = 0x04;
|
78 |
_delay_ms(250);
|
79 |
PORTC.OUT = 0x0C;
|
80 |
_delay_ms(250);
|
81 |
|
82 |
PORTC.OUT = 0x00;
|
83 |
PORTE.OUT = 0x04;
|
84 |
_delay_ms(250);
|
85 |
PORTE.OUT = 0x0C;
|
86 |
_delay_ms(250);
|
87 |
|
88 |
PORTE.OUT = 0x00;
|
89 |
PORTF.OUT = 0x04;
|
90 |
_delay_ms(250);
|
91 |
PORTF.OUT = 0x0C;
|
92 |
_delay_ms(250);
|
93 |
PORTF.OUT = 0x00;
|
94 |
while(1) |
95 |
{ |
96 |
|
97 |
|
98 |
USART0.TXDATAL = 0x45;
|
99 |
PORTD.OUT ^= 0x01;
|
100 |
PORTB.OUT ^= 0x01;
|
101 |
PORTC.OUT ^= 0x01;
|
102 |
PORTE.OUT ^= 0x01;
|
103 |
PORTF.OUT ^= 0x01;
|
104 |
_delay_ms(10);
|
105 |
|
106 |
|
107 |
|
108 |
|
109 |
if(bytesAvailable > 0) //check if we have bytes to read |
110 |
{ |
111 |
localBuf[k] = rxRead(); //store byte into buffer
|
112 |
k++; //increment to next byte
|
113 |
} |
114 |
|
115 |
//check if the 2 first bytes match with theses values
|
116 |
if(localBuf[0] == 0x42 && localBuf[1] == 0x64) |
117 |
{ |
118 |
//Clear buffer
|
119 |
localBuf[0] = 0x00; |
120 |
localBuf[1] = 0x00; |
121 |
k = 0;
|
122 |
PORTA.OUT ^= 0x1; //Set flag on led |
123 |
} |
124 |
|
125 |
//Just in case if bytes does not match, clear counter
|
126 |
if(k >= 2) |
127 |
k = 0;
|
128 |
|
129 |
//_delay_ms(200);
|
130 |
} |
131 |
|
132 |
|
133 |
|
134 |
|
135 |
return 0; |
136 |
} |
137 |
|
138 |
|
139 |
|
140 |
|
141 |
///////////////////////////////////////////////////////////
|
142 |
//Functions / macros
|
143 |
|
144 |
|
145 |
uint8_t rxRead(void) //Read one byte from buffer |
146 |
{ |
147 |
uint8_t result = _rxBuf[0]; //get byte in order (FIFO) |
148 |
|
149 |
for(uint8_t j = 0; j < 255; j++) //shift bytes |
150 |
{ |
151 |
_rxBuf[j] = _rxBuf[j+1];
|
152 |
} |
153 |
if(bytesAvailable > 0) |
154 |
bytesAvailable--; //remove 1 byte
|
155 |
|
156 |
return result;
|
157 |
} |
158 |
|
159 |
ISR(USART0_RXC_vect) |
160 |
{ |
161 |
//while((USART0.RXDATAH & 0x80) == 0x80);
|
162 |
_rxReicv = USART0.RXDATAL; //read buffer
|
163 |
_rxBuf[bytesAvailable] = _rxReicv; //Add one byte to the buffer
|
164 |
_rxReicv = 0x00; //clear value |
165 |
if(bytesAvailable < 255) //ensure not to overflow buffer size... |
166 |
bytesAvailable++; //a new byte is available into the buffer
|
167 |
//PORTA.OUT ^= 0x3; //debug led
|
168 |
} |