Compare commits
No commits in common. "158df4a788f26d3fec20e1624c06f187c1040afd" and "659ce6c581f738a4c8e97b0e99d5acdc73e32cd1" have entirely different histories.
158df4a788
...
659ce6c581
@ -1,36 +0,0 @@
|
|||||||
#include <stdio.h>
|
|
||||||
#include <stdlib.h>
|
|
||||||
#include "circular_buf.h"
|
|
||||||
|
|
||||||
|
|
||||||
void initialize_buffer(struct circular_buffer* cb) {
|
|
||||||
cb->buf_head = 0;
|
|
||||||
cb->buf_tail = 0;
|
|
||||||
}
|
|
||||||
// Проверка, пустой ли буфер
|
|
||||||
int buffer_empty(const struct circular_buffer* cb) {
|
|
||||||
return cb->buf_head == cb->buf_tail;
|
|
||||||
}
|
|
||||||
// Проверка, заполнен ли буфер
|
|
||||||
int buffer_full(const struct circular_buffer* cb) {
|
|
||||||
return (cb->buf_tail + 1) % BUFFER_SIZE == cb->buf_head; //проверяем следующее число, если оно будет совпадать с индексом головы то будет false, при совпадении вывод true
|
|
||||||
}
|
|
||||||
// Запись в буфер
|
|
||||||
void write_buffer(struct circular_buffer* cb, int value) {
|
|
||||||
if (buffer_full(cb)) { // проверяем, заполнен ли буфер
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
cb->buffer[cb->buf_tail] = value;// записываем значение в элемент массива в хвост
|
|
||||||
cb->buf_tail = (cb->buf_tail + 1) % BUFFER_SIZE;// присваивается cb->buf_tail, обновляется его значение на следующий индекс в буфере
|
|
||||||
}
|
|
||||||
// Чтение элемента
|
|
||||||
int read_buffer(struct circular_buffer* cb) {
|
|
||||||
if (buffer_empty(cb)) { // проверка на пустоту
|
|
||||||
return -1;// -1 как индикатор в случае ошибки
|
|
||||||
}
|
|
||||||
int value = cb->buffer[cb->buf_head]; // чтение по индексу головы
|
|
||||||
cb->buf_head = (cb->buf_head + 1) % BUFFER_SIZE; // увеличиваем индекс на 1
|
|
||||||
return value;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
@ -1,18 +0,0 @@
|
|||||||
#ifndef CIRCULAR_BUFFER_H
|
|
||||||
#define CIRCULAR_BUFFER_H
|
|
||||||
|
|
||||||
#define BUFFER_SIZE 32
|
|
||||||
|
|
||||||
struct circular_buffer{
|
|
||||||
unsigned char buffer[BUFFER_SIZE];
|
|
||||||
unsigned char buf_head;
|
|
||||||
unsigned char buf_tail;
|
|
||||||
};
|
|
||||||
|
|
||||||
void initialize_buffer(struct circular_buffer* cb);
|
|
||||||
int buffer_empty(const struct circular_buffer* cb);
|
|
||||||
int buffer_full(const struct circular_buffer* cb);
|
|
||||||
void write_buffer(struct circular_buffer* cb, int value);
|
|
||||||
int read_buffer(struct circular_buffer* cb);
|
|
||||||
|
|
||||||
#endif /* CIRCULAR_BUFFER_H */
|
|
21
UART/timer.c
21
UART/timer.c
@ -1,21 +0,0 @@
|
|||||||
#include <avr/io.h>
|
|
||||||
#include <avr/interrupt.h>
|
|
||||||
|
|
||||||
static volatile unsigned long timerMillis = 0;
|
|
||||||
|
|
||||||
void timerInit() {
|
|
||||||
TCCR1B |= (1 << WGM12) | (1 << CS11) | (1 << CS10);
|
|
||||||
OCR1A = 250;
|
|
||||||
TIMSK1 |= (1 << OCIE1A);
|
|
||||||
sei();
|
|
||||||
}
|
|
||||||
|
|
||||||
unsigned long millis() {
|
|
||||||
unsigned long ms;
|
|
||||||
ms = timerMillis;
|
|
||||||
return ms;
|
|
||||||
}
|
|
||||||
|
|
||||||
ISR(TIMER1_COMPA_vect) {
|
|
||||||
timerMillis++;
|
|
||||||
}
|
|
@ -1,7 +0,0 @@
|
|||||||
#ifndef TIMER_H
|
|
||||||
#define TIMER_H
|
|
||||||
|
|
||||||
void timerInit();
|
|
||||||
unsigned long millis();
|
|
||||||
|
|
||||||
#endif // TIMER_H
|
|
64
UART/uart.c
64
UART/uart.c
@ -1,64 +0,0 @@
|
|||||||
#include <avr/io.h>
|
|
||||||
#include <avr/interrupt.h>
|
|
||||||
#include <stdint.h>
|
|
||||||
#include <string.h>
|
|
||||||
#include "timer.h"
|
|
||||||
#include "circular_buf.h"
|
|
||||||
#include "uart.h"
|
|
||||||
|
|
||||||
void UART_init(void) {
|
|
||||||
UCSR0B = (1 << RXEN0) | (1 << TXEN0) | (1 << RXCIE0) | (1 << UDRIE0); // прерывание по приему и опустошению буфера передачи
|
|
||||||
UCSR0C = (1 << UCSZ01) | (1 << UCSZ00);
|
|
||||||
UBRR0H = 0;
|
|
||||||
UBRR0L = 103;
|
|
||||||
}
|
|
||||||
|
|
||||||
void UART_send(uint8_t* data, size_t length) {
|
|
||||||
for (size_t i = 0; i < length; i++) {
|
|
||||||
if (!buffer_full(&usartTxBuffer)) {
|
|
||||||
write_buffer(&usartTxBuffer, data[i]);
|
|
||||||
} else {
|
|
||||||
break; // если буфер передачи заполнен, то отправка прерывается
|
|
||||||
}
|
|
||||||
}
|
|
||||||
UCSR0B |= (1 << UDRIE0); // Включаем прерывание по опустошению буфера
|
|
||||||
}
|
|
||||||
|
|
||||||
// Получение данных из буфера
|
|
||||||
int UART_receive(uint8_t* data, size_t length) {
|
|
||||||
char overflow=0; // Флаг переполнения, который устанавливается, если превышен размер массива
|
|
||||||
uint32_t byteCount=0; // Счетчик байтов, принятых из буфера приема
|
|
||||||
uint32_t timeout_ms = 4; // Таймаут в миллисекундах для общего времени приема данных
|
|
||||||
uint32_t start_time = millis();
|
|
||||||
//Цикл приема данных с таймаутом
|
|
||||||
while(1)
|
|
||||||
{
|
|
||||||
// Пока буфер приема не пуст и не истек таймаут общего времени,
|
|
||||||
// функция продолжает читать байты из буфера и сохранять их в массив data.
|
|
||||||
while (!buffer_empty(&usartRxBuffer)) {
|
|
||||||
int reader = read_buffer(&usartRxBuffer);//прием и запись символа в переменную
|
|
||||||
if(byteCount<=length){
|
|
||||||
data[byteCount] = reader; // запись в массив с индексом byteCount
|
|
||||||
}
|
|
||||||
else{
|
|
||||||
overflow=1;
|
|
||||||
}
|
|
||||||
byteCount++;
|
|
||||||
|
|
||||||
start_time = millis();
|
|
||||||
}
|
|
||||||
if ((millis() - start_time) > timeout_ms) { // если превышение времени в 4 ms
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
return overflow?-1:byteCount; // возвращает количество успешно принятых байт или -1
|
|
||||||
}
|
|
||||||
|
|
||||||
// прерывание по завершению приема
|
|
||||||
ISR(USART_RX_vect) {
|
|
||||||
uint8_t data = UDR0; // читаем из регистра UDR0
|
|
||||||
if (!buffer_full(&usartRxBuffer)) {
|
|
||||||
write_buffer(&usartRxBuffer, data);// записываем символ в буфер приема
|
|
||||||
}
|
|
||||||
}
|
|
18
UART/uart.h
18
UART/uart.h
@ -1,18 +0,0 @@
|
|||||||
#ifndef UART_H
|
|
||||||
#define UART_H
|
|
||||||
#include <stdint.h>
|
|
||||||
#include <stddef.h>
|
|
||||||
#include "timer.h"
|
|
||||||
#include "circular_buf.h"
|
|
||||||
|
|
||||||
#define F_CPU 16000000
|
|
||||||
|
|
||||||
struct circular_buffer usartRxBuffer;
|
|
||||||
struct circular_buffer usartTxBuffer;
|
|
||||||
|
|
||||||
void UART_init(void);
|
|
||||||
void UART_send(uint8_t* data, size_t length);
|
|
||||||
int UART_receive(uint8_t* data, size_t length);
|
|
||||||
|
|
||||||
|
|
||||||
#endif /* UART_H */
|
|
Loading…
Reference in New Issue
Block a user