From 6ee102f904ed1c32df414e1648ed8b0728b4389a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9D=D0=B8=D0=BA=D0=B8=D1=82=D0=B0=20=D0=A1=D0=BE=D0=BB?= =?UTF-8?q?=D0=BE=D0=B4=D1=8F=D0=BD=D0=BA=D0=B8=D0=BD?= Date: Fri, 16 Jun 2023 21:32:12 +0000 Subject: [PATCH] UART MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit main.c - не конечный результат, нужно адаптировать под новый кольцевой буфер, загружу позже кроме этого тут circular_buf.c и timer.c --- UART/circular_buf.c | 42 ++++++++++ UART/circular_buf.h | 18 +++++ UART/main.atsln | 22 +++++ UART/main.c | 156 ++++++++++++++++++++++++++++++++++++ UART/main.componentinfo.xml | 86 ++++++++++++++++++++ UART/ring_buf.c | 42 ++++++++++ UART/timer.c | 23 ++++++ UART/timer.h | 7 ++ 8 files changed, 396 insertions(+) create mode 100644 UART/circular_buf.c create mode 100644 UART/circular_buf.h create mode 100644 UART/main.atsln create mode 100644 UART/main.c create mode 100644 UART/main.componentinfo.xml create mode 100644 UART/ring_buf.c create mode 100644 UART/timer.c create mode 100644 UART/timer.h diff --git a/UART/circular_buf.c b/UART/circular_buf.c new file mode 100644 index 0000000..3f09963 --- /dev/null +++ b/UART/circular_buf.c @@ -0,0 +1,42 @@ +#include +#include + +#define BUFFER_SIZE 10 + +typedef struct { + int buffer[BUFFER_SIZE]; + int BufHead; + int BufTail; +} CircularBuffer; + +void initializeBuffer(CircularBuffer* cb) { + cb->BufHead = 0; + cb->BufTail = 0; +} +// , +int BufferEmpty(const CircularBuffer* cb) { + return cb->BufHead == cb->BufTail; +} +// , +int BufferFull(const CircularBuffer* cb) { + return (cb->BufTail + 1) % BUFFER_SIZE == cb->BufHead;// , false, true +} +// +void writeBuffer(CircularBuffer* cb, int value) { + if (BufferFull(cb)) { // + return; + } + cb->buffer[cb->BufTail] = value;// value buffer + cb->BufTail = (cb->BufTail + 1) % BUFFER_SIZE;// cb->BufTail, +} +// +int readBuffer(CircularBuffer* cb) { + if (BufferEmpty(cb)) { // + return -1;// -1 + } + int value = cb->buffer[cb->BufHead]; // + cb->BufHead = (cb->BufHead + 1) % BUFFER_SIZE; // +1 + return value; +} + + diff --git a/UART/circular_buf.h b/UART/circular_buf.h new file mode 100644 index 0000000..33d820c --- /dev/null +++ b/UART/circular_buf.h @@ -0,0 +1,18 @@ +#ifndef CIRCULAR_BUFFER_H +#define CIRCULAR_BUFFER_H + +#define BUFFER_SIZE 10 + +typedef struct { + int buffer[BUFFER_SIZE]; + int BufHead; + int BufTail; +} CircularBuffer; + +void initializeBuffer(CircularBuffer* cb); +int BufferEmpty(const CircularBuffer* cb); +int BufferFull(const CircularBuffer* cb); +void writeBuffer(CircularBuffer* cb, int value); +int readBuffer(CircularBuffer* cb); + +#endif /* CIRCULAR_BUFFER_H */ diff --git a/UART/main.atsln b/UART/main.atsln new file mode 100644 index 0000000..5c0a235 --- /dev/null +++ b/UART/main.atsln @@ -0,0 +1,22 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Atmel Studio Solution File, Format Version 11.00 +VisualStudioVersion = 14.0.23107.0 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{54F91283-7BC4-4236-8FF9-10F437C3AD48}") = "main", "main\main.cproj", "{DCE6C7E3-EE26-4D79-826B-08594B9AD897}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|AVR = Debug|AVR + Release|AVR = Release|AVR + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {DCE6C7E3-EE26-4D79-826B-08594B9AD897}.Debug|AVR.ActiveCfg = Debug|AVR + {DCE6C7E3-EE26-4D79-826B-08594B9AD897}.Debug|AVR.Build.0 = Debug|AVR + {DCE6C7E3-EE26-4D79-826B-08594B9AD897}.Release|AVR.ActiveCfg = Release|AVR + {DCE6C7E3-EE26-4D79-826B-08594B9AD897}.Release|AVR.Build.0 = Release|AVR + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection +EndGlobal diff --git a/UART/main.c b/UART/main.c new file mode 100644 index 0000000..4d943bd --- /dev/null +++ b/UART/main.c @@ -0,0 +1,156 @@ +#include +#include +#include +#include +#include "timer.h" +#include "circular_buf.h" + +#define F_CPU 16000000 +#define SIZE_BUF 16 + + + +volatile unsigned char usartTxBuf[SIZE_BUF]; +volatile unsigned char txBufTail = 0; +volatile unsigned char txBufHead = 0; +volatile unsigned char txCount = 0; + +volatile unsigned char usartRxBuf[SIZE_BUF]; +volatile unsigned char rxBufTail = 0; +volatile unsigned char rxBufHead = 0; +volatile unsigned char rxCount = 0; + + + +void UARTInit(void) { + UCSR0B = (1 << RXEN0) | (1 << TXEN0) | (1 << RXCIE0) | (1 << TXCIE0); + 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++) { // ( ). + while (!(UCSR0A & (1 << UDRE0))); + UDR0 = data[i];// "data" . + } +} +// +void USART_PutChar(unsigned char sym) { + if (((UCSR0A & (1 << UDRE0)) != 0) && (txCount == 0)) { + UDR0 = sym; + } else { + if (txCount < SIZE_BUF) { + usartTxBuf[txBufTail] = sym; + txCount++; + txBufTail++; + if (txBufTail == SIZE_BUF) { + txBufTail = 0; + } + } + } +} +// +unsigned char GetChar(void) { + unsigned char sym = 0; + if (txCount > 0) { + sym = usartTxBuf[txBufHead]; + txCount--; + txBufHead++; + if (txBufHead == SIZE_BUF) { + txBufHead = 0; + } + } + return sym; +} +// +void USART_SendStr(const unsigned char* data) { + while (*data) { + USART_PutChar(*data++); + } +} +// +void UARTReceive(uint8_t* data) { + uint32_t timeout_ms_mb = 100;//timeout 100ms, , 1 , 4-5 , 1-2, + uint32_t timeout_ms = 4; + uint32_t start_time = millis();// + while (rxCount == 0) { // + if ((millis() - start_time) > timeout_ms_mb) { // + return 0; // + } + } + data[0] = usartRxBuf[rxBufHead];// + rxCount--; + rxBufHead++; + if (rxBufHead == SIZE_BUF) { + rxBufHead = 0; + } + start_time = millis();// , + while (rxCount > 0) { // + 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)); + //for (int i = 0; i < 7; i++) { + //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 (rxCount < SIZE_BUF) { + usartRxBuf[rxBufTail] = UDR0; + rxBufTail++; + if (rxBufTail == SIZE_BUF) { + rxBufTail = 0; + } + rxCount++; + } +} + + +//// +//ISR(USART_TXC_vect) { + //if (txCount > 0){ // + //UDR0 = usartTxBuf[txBufHead]; // UDR + //txCount--; // + //txBufHead++; // + //if (txBufHead == SIZE_BUF) + //txBufHead = 0; + //} + //else { + //UCSR0B &= ~(0< + + + + + + Device + Startup + + + Atmel + 1.7.0 + D:/atmel studio\7.0\Packs + + + + + D:/atmel studio\7.0\Packs\atmel\ATmega_DFP\1.7.374\include\ + + include + C + + + include/ + + + + + D:/atmel studio\7.0\Packs\atmel\ATmega_DFP\1.7.374\include\avr\iom328p.h + + header + C + 4leX2H78R90/kvebBjYSOw== + + include/avr/iom328p.h + + + + + D:/atmel studio\7.0\Packs\atmel\ATmega_DFP\1.7.374\templates\main.c + template + source + C Exe + WuscJm7SWE9tRkxyEb0ntg== + + templates/main.c + Main file (.c) + + + + D:/atmel studio\7.0\Packs\atmel\ATmega_DFP\1.7.374\templates\main.cpp + template + source + C Exe + mkKaE95TOoATsuBGv6jmxg== + + templates/main.cpp + Main file (.cpp) + + + + D:/atmel studio\7.0\Packs\atmel\ATmega_DFP\1.7.374\gcc\dev\atmega328p + + libraryPrefix + GCC + + + gcc/dev/atmega328p + + + + + ATmega_DFP + D:/atmel studio/7.0/Packs/atmel/ATmega_DFP/1.7.374/Atmel.ATmega_DFP.pdsc + 1.7.374 + true + ATmega328P + + + + Resolved + Fixed + true + + + \ No newline at end of file diff --git a/UART/ring_buf.c b/UART/ring_buf.c new file mode 100644 index 0000000..3f09963 --- /dev/null +++ b/UART/ring_buf.c @@ -0,0 +1,42 @@ +#include +#include + +#define BUFFER_SIZE 10 + +typedef struct { + int buffer[BUFFER_SIZE]; + int BufHead; + int BufTail; +} CircularBuffer; + +void initializeBuffer(CircularBuffer* cb) { + cb->BufHead = 0; + cb->BufTail = 0; +} +// , +int BufferEmpty(const CircularBuffer* cb) { + return cb->BufHead == cb->BufTail; +} +// , +int BufferFull(const CircularBuffer* cb) { + return (cb->BufTail + 1) % BUFFER_SIZE == cb->BufHead;// , false, true +} +// +void writeBuffer(CircularBuffer* cb, int value) { + if (BufferFull(cb)) { // + return; + } + cb->buffer[cb->BufTail] = value;// value buffer + cb->BufTail = (cb->BufTail + 1) % BUFFER_SIZE;// cb->BufTail, +} +// +int readBuffer(CircularBuffer* cb) { + if (BufferEmpty(cb)) { // + return -1;// -1 + } + int value = cb->buffer[cb->BufHead]; // + cb->BufHead = (cb->BufHead + 1) % BUFFER_SIZE; // +1 + return value; +} + + diff --git a/UART/timer.c b/UART/timer.c new file mode 100644 index 0000000..24cbfd0 --- /dev/null +++ b/UART/timer.c @@ -0,0 +1,23 @@ +#include +#include + +static volatile unsigned long timerMillis = 0; + +void timerInit() { + TCCR1B |= (1 << WGM12) | (1 << CS11) | (1 << CS10); + OCR1A = 249; + TIMSK1 |= (1 << OCIE1A); + sei(); +} + +unsigned long millis() { + unsigned long ms; + cli(); + ms = timerMillis; + sei(); + return ms; +} + +ISR(TIMER1_COMPA_vect) { + timerMillis++; +} \ No newline at end of file diff --git a/UART/timer.h b/UART/timer.h new file mode 100644 index 0000000..cd7f72f --- /dev/null +++ b/UART/timer.h @@ -0,0 +1,7 @@ +#ifndef TIMER_H +#define TIMER_H + +void timerInit(); +unsigned long millis(); + +#endif // TIMER_H \ No newline at end of file