Modbus_master_2/UART/main.c
Никита Солодянкин cac4258352 Изменил(а) на 'UART/main.c'
с изменениями под новый кольцевой буфер
2023-06-16 22:21:42 +00:00

115 lines
5.4 KiB
C

#include <avr/io.h>
#include <avr/interrupt.h>
#include <stdint.h>
#include <string.h>
#include "timer.h"
#include "circular_buf.h"
#define F_CPU 16000000
#define SIZE_BUF 16
CircularBuffer usartTxBuffer;
CircularBuffer usartRxBuffer;
void UARTInit(void) {
UCSR0B = (1 << RXEN0) | (1 << TXEN0) | (1 << RXCIE0) | (1 << UDRIE0); // &#1074;&#1082;&#1083;&#1102;&#1095;&#1072;&#1077;&#1084; &#1087;&#1088;&#1077;&#1088;&#1099;&#1074;&#1072;&#1085;&#1080;&#1077; &#1087;&#1086; &#1087;&#1088;&#1080;&#1077;&#1084;&#1091; &#1080; &#1086;&#1087;&#1091;&#1089;&#1090;&#1086;&#1096;&#1077;&#1085;&#1080;&#1102; &#1073;&#1091;&#1092;&#1077;&#1088;&#1072; &#1087;&#1077;&#1088;&#1077;&#1076;&#1072;&#1095;&#1080;
UCSR0C = (1 << UCSZ01) | (1 << UCSZ00);
UBRR0H = 0;
UBRR0L = 103;
}
void UARTSend(uint8_t* data, size_t length) {
for (size_t i = 0; i < length; i++) {
if (!BufferFull(&usartTxBuffer)) {
writeBuffer(&usartTxBuffer, data[i]);
} else {
break; // &#1073;&#1091;&#1092;&#1077;&#1088; &#1087;&#1077;&#1088;&#1077;&#1076;&#1072;&#1095;&#1080; &#1079;&#1072;&#1087;&#1086;&#1083;&#1085;&#1077;&#1085;, &#1087;&#1088;&#1077;&#1088;&#1099;&#1074;&#1072;&#1077;&#1084; &#1086;&#1090;&#1087;&#1088;&#1072;&#1074;&#1082;&#1091;
}
}
UCSR0B |= (1 << UDRIE0); // &#1042;&#1082;&#1083;&#1102;&#1095;&#1072;&#1077;&#1084; &#1087;&#1088;&#1077;&#1088;&#1099;&#1074;&#1072;&#1085;&#1080;&#1077; &#1087;&#1086; &#1086;&#1087;&#1091;&#1089;&#1090;&#1086;&#1096;&#1077;&#1085;&#1080;&#1102; &#1073;&#1091;&#1092;&#1077;&#1088;&#1072; &#1087;&#1077;&#1088;&#1077;&#1076;&#1072;&#1095;&#1080;
}
//&#1087;&#1086;&#1083;&#1086;&#1078;&#1080;&#1090;&#1100; &#1089;&#1080;&#1084;&#1074;&#1086;&#1083; &#1074; &#1073;&#1091;&#1092;&#1077;&#1088;
void USART_PutChar(unsigned char sym) {
if (((UCSR0A & (1 << UDRE0)) != 0) && BufferEmpty(&usartTxBuffer)) {
UDR0 = sym;
} else {
if (!BufferFull(&usartTxBuffer)) {
writeBuffer(&usartTxBuffer, sym);
} else {
// &#1041;&#1091;&#1092;&#1077;&#1088; &#1087;&#1077;&#1088;&#1077;&#1076;&#1072;&#1095;&#1080; &#1087;&#1086;&#1083;&#1086;&#1085;, &#1086;&#1073;&#1088;&#1072;&#1073;&#1086;&#1090;&#1082;&#1072; &#1086;&#1096;&#1080;&#1073;&#1082;&#1080; &#1080;&#1083;&#1080; &#1076;&#1088;&#1091;&#1075;&#1072;&#1103; &#1083;&#1086;&#1075;&#1080;&#1082;&#1072;
}
}
}
//&#1074;&#1079;&#1103;&#1090;&#1100; &#1089;&#1080;&#1084;&#1074;&#1086;&#1083; &#1080;&#1079; &#1073;&#1091;&#1092;&#1077;&#1088;&#1072;
int GetChar(unsigned char* sym) {
if (!BufferEmpty(&usartTxBuffer)) {
*sym = readBuffer(&usartTxBuffer);
return 1; // &#1059;&#1089;&#1087;&#1077;&#1096;&#1085;&#1086; &#1087;&#1088;&#1086;&#1095;&#1080;&#1090;&#1072;&#1085; &#1089;&#1080;&#1084;&#1074;&#1086;&#1083; &#1080;&#1079; &#1073;&#1091;&#1092;&#1077;&#1088;&#1072; &#1087;&#1077;&#1088;&#1077;&#1076;&#1072;&#1095;&#1080;
} else {
return 0; // &#1041;&#1091;&#1092;&#1077;&#1088; &#1087;&#1077;&#1088;&#1077;&#1076;&#1072;&#1095;&#1080; &#1087;&#1091;&#1089;&#1090;, &#1074;&#1086;&#1079;&#1085;&#1080;&#1082;&#1083;&#1072; &#1086;&#1096;&#1080;&#1073;&#1082;&#1072; &#1095;&#1090;&#1077;&#1085;&#1080;&#1103;
}
}
//&#1092;&#1091;&#1085;&#1082;&#1094;&#1080;&#1103; &#1079;&#1072;&#1075;&#1088;&#1091;&#1079;&#1082;&#1080; &#1089;&#1090;&#1088;&#1086;&#1082;
void USART_SendStr(const unsigned char* data) {
while (*data) {
USART_PutChar(*data++);
}
}
//&#1087;&#1086;&#1083;&#1091;&#1095;&#1072;&#1077;&#1084; &#1076;&#1072;&#1085;&#1085;&#1099;&#1077; &#1080;&#1079; &#1073;&#1091;&#1092;&#1077;&#1088;&#1072;
void UARTReceive(uint8_t* data) {
uint32_t timeout_ms_mb = 100;
uint32_t timeout_ms = 4;
uint32_t start_time = millis();
while (BufferEmpty(&usartRxBuffer)) {
if ((millis() - start_time) > timeout_ms_mb) {
// &#1054;&#1073;&#1088;&#1072;&#1073;&#1086;&#1090;&#1082;&#1072; &#1086;&#1096;&#1080;&#1073;&#1082;&#1080; &#1080;&#1083;&#1080; &#1090;&#1072;&#1081;&#1084;&#1072;&#1091;&#1090;&#1072; &#1087;&#1088;&#1080;&#1077;&#1084;&#1072;
return 0;
}
}
data[0] = readBuffer(&usartRxBuffer);
start_time = millis();
while (!BufferEmpty(&usartRxBuffer)) {
if ((millis() - start_time) > timeout_ms) {
break;
}
}
}
int main(void) {
sei();
UARTInit();
timerInit();
DDRD = 0xFF;
uint8_t buff[] = {0x48, 0x45, 0x4C, 0x4C, 0x4F, 0x21, 0x09};
UARTSend(buff, sizeof(buff));
uint8_t receivedData[SIZE_BUF];
UARTReceive(receivedData);
UARTSend(receivedData, sizeof(receivedData));
while (1) {
//unsigned long currentTime = millis();
}
return 0;
}
ISR(USART_RX_vect) {
if (!BufferFull(&usartRxBuffer)) {
writeBuffer(&usartRxBuffer, UDR0);// &#1047;&#1072;&#1087;&#1080;&#1089;&#1099;&#1074;&#1072;&#1077;&#1084; &#1087;&#1088;&#1080;&#1085;&#1103;&#1090;&#1099;&#1081; &#1089;&#1080;&#1084;&#1074;&#1086;&#1083; &#1074; &#1073;&#1091;&#1092;&#1077;&#1088; &#1087;&#1088;&#1080;&#1077;&#1084;&#1072;
}
if (!BufferEmpty(&usartTxBuffer)) {
UDR0 = readBuffer(&usartTxBuffer);// &#1054;&#1090;&#1087;&#1088;&#1072;&#1074;&#1083;&#1103;&#1077;&#1084; &#1089;&#1080;&#1084;&#1074;&#1086;&#1083; &#1080;&#1079; &#1073;&#1091;&#1092;&#1077;&#1088;&#1072; &#1087;&#1077;&#1088;&#1077;&#1076;&#1072;&#1095;&#1080;
} else {
UCSR0B &= ~(1 << UDRIE0); // &#1054;&#1090;&#1082;&#1083;&#1102;&#1095;&#1072;&#1077;&#1084; &#1087;&#1088;&#1077;&#1088;&#1099;&#1074;&#1072;&#1085;&#1080;&#1077; &#1087;&#1086; &#1086;&#1087;&#1091;&#1089;&#1090;&#1086;&#1096;&#1077;&#1085;&#1080;&#1102; &#1073;&#1091;&#1092;&#1077;&#1088;&#1072; &#1087;&#1077;&#1088;&#1077;&#1076;&#1072;&#1095;&#1080;
}
}