Compare commits
No commits in common. "0ccb490096d9c1cd9667a98fefbe2464f7fb65d5" and "7ba1e3146d65559df840c2e9f3d57af60b49c8c6" have entirely different histories.
0ccb490096
...
7ba1e3146d
21
UART/timer.c
Normal file
21
UART/timer.c
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
#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++;
|
||||||
|
}
|
7
UART/timer.h
Normal file
7
UART/timer.h
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
#ifndef TIMER_H
|
||||||
|
#define TIMER_H
|
||||||
|
|
||||||
|
void timerInit();
|
||||||
|
unsigned long millis();
|
||||||
|
|
||||||
|
#endif // TIMER_H
|
65
UART/uart.c
65
UART/uart.c
@ -2,14 +2,12 @@
|
|||||||
#include <avr/interrupt.h>
|
#include <avr/interrupt.h>
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
#include "timer.h"
|
||||||
#include "circular_buf.h"
|
#include "circular_buf.h"
|
||||||
#include "uart.h"
|
#include "uart.h"
|
||||||
|
|
||||||
struct circular_buffer uartRxBuffer;
|
|
||||||
struct circular_buffer uartTxBuffer;
|
|
||||||
|
|
||||||
void UART_init(void) {
|
void UART_init(void) {
|
||||||
UCSR0B = (1 << RXEN0) | (1 << TXEN0) | (1 << RXCIE0) | (1<<TXCIE0) | (1 << UDRIE0); // прерывание по приему и передаче
|
UCSR0B = (1 << RXEN0) | (1 << TXEN0) | (1 << RXCIE0) | (1 << UDRIE0); // прерывание по приему и опустошению буфера передачи
|
||||||
UCSR0C = (1 << UCSZ01) | (1 << UCSZ00);
|
UCSR0C = (1 << UCSZ01) | (1 << UCSZ00);
|
||||||
UBRR0H = 0;
|
UBRR0H = 0;
|
||||||
UBRR0L = 103;
|
UBRR0L = 103;
|
||||||
@ -17,45 +15,50 @@ void UART_init(void) {
|
|||||||
|
|
||||||
void UART_send(uint8_t* data, size_t length) {
|
void UART_send(uint8_t* data, size_t length) {
|
||||||
for (size_t i = 0; i < length; i++) {
|
for (size_t i = 0; i < length; i++) {
|
||||||
if (!buffer_full(&uartTxBuffer)) {
|
if (!buffer_full(&usartTxBuffer)) {
|
||||||
write_buffer(&uartTxBuffer, data[i]);
|
write_buffer(&usartTxBuffer, data[i]);
|
||||||
} else {
|
} else {
|
||||||
break; // если буфер передачи заполнен, то отправка прерывается
|
break; // если буфер передачи заполнен, то отправка прерывается
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
UCSR0B |= (1 << TXCIE0); // включаем прерывание по завершении передачи
|
UCSR0B |= (1 << UDRIE0); // Включаем прерывание по опустошению буфера
|
||||||
}
|
}
|
||||||
|
|
||||||
// Получение данных из буфера
|
// Получение данных из буфера
|
||||||
int UART_receive(uint8_t* data, size_t length) {
|
int UART_receive(uint8_t* data, size_t length) {
|
||||||
char overflow = 0; // Флаг переполнения, который устанавливается, если превышен размер массива
|
char overflow=0; // Флаг переполнения, который устанавливается, если превышен размер массива
|
||||||
uint32_t byteCount = 0; // Счетчик байтов, принятых из буфера приема
|
uint32_t byteCount=0; // Счетчик байтов, принятых из буфера приема
|
||||||
// Пока буфер приема не пуст и не превышен лимит длины массива,
|
uint32_t timeout_ms = 4; // Таймаут в миллисекундах для общего времени приема данных
|
||||||
// функция продолжает читать байты из буфера и сохранять их в массив data.
|
uint32_t start_time = millis();
|
||||||
while (!buffer_empty(&uartRxBuffer) && byteCount < length) {
|
//Цикл приема данных с таймаутом
|
||||||
int reader = read_buffer(&uartRxBuffer); // Прием и запись символа в переменную
|
while(1)
|
||||||
data[byteCount] = reader; // Запись в массив с индексом byteCount
|
{
|
||||||
byteCount++;
|
// Пока буфер приема не пуст и не истек таймаут общего времени,
|
||||||
}
|
// функция продолжает читать байты из буфера и сохранять их в массив data.
|
||||||
// Проверка переполнения
|
while (!buffer_empty(&usartRxBuffer)) {
|
||||||
if (byteCount > length) {
|
int reader = read_buffer(&usartRxBuffer);//прием и запись символа в переменную
|
||||||
overflow = 1;
|
if(byteCount<=length){
|
||||||
}
|
data[byteCount] = reader; // запись в массив с индексом byteCount
|
||||||
return overflow ? -1 : byteCount; // Возвращает количество успешно принятых байт или -1 в случае переполнения
|
}
|
||||||
|
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) {
|
ISR(USART_RX_vect) {
|
||||||
uint8_t data = UDR0; // читаем из регистра UDR0
|
uint8_t data = UDR0; // читаем из регистра UDR0
|
||||||
if (!buffer_full(&uartRxBuffer)) {
|
if (!buffer_full(&usartRxBuffer)) {
|
||||||
write_buffer(&uartRxBuffer, data);// записываем символ в буфер приема
|
write_buffer(&usartRxBuffer, data);// записываем символ в буфер приема
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
ISR(USART_TX_vect) {
|
|
||||||
if (!buffer_empty(&uartTxBuffer)) {
|
|
||||||
UDR0 = read_buffer(&uartTxBuffer);
|
|
||||||
} else {
|
|
||||||
UCSR0B &= ~(1 << TXCIE0); // отключаем прерывание, когда все данные отправлены
|
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -2,9 +2,14 @@
|
|||||||
#define UART_H
|
#define UART_H
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
#include <stddef.h>
|
#include <stddef.h>
|
||||||
|
#include "timer.h"
|
||||||
|
#include "circular_buf.h"
|
||||||
|
|
||||||
#define F_CPU 16000000
|
#define F_CPU 16000000
|
||||||
|
|
||||||
|
struct circular_buffer usartRxBuffer;
|
||||||
|
struct circular_buffer usartTxBuffer;
|
||||||
|
|
||||||
void UART_init(void);
|
void UART_init(void);
|
||||||
void UART_send(uint8_t* data, size_t length);
|
void UART_send(uint8_t* data, size_t length);
|
||||||
int UART_receive(uint8_t* data, size_t length);
|
int UART_receive(uint8_t* data, size_t length);
|
||||||
|
Loading…
Reference in New Issue
Block a user