uart #11
@ -5,16 +5,14 @@ static volatile unsigned long timerMillis = 0;
|
||||
|
||||
void timerInit() {
|
||||
TCCR1B |= (1 << WGM12) | (1 << CS11) | (1 << CS10);
|
||||
OCR1A = 249;
|
||||
OCR1A = 250;
|
||||
TIMSK1 |= (1 << OCIE1A);
|
||||
sei();
|
||||
}
|
||||
|
||||
unsigned long millis() {
|
||||
unsigned long ms;
|
||||
cli();
|
||||
ms = timerMillis;
|
||||
sei();
|
||||
return ms;
|
||||
}
|
||||
|
||||
|
63
UART/uart.c
63
UART/uart.c
@ -4,11 +4,7 @@
|
||||
#include <string.h>
|
||||
#include "timer.h"
|
||||
#include "circular_buf.h"
|
||||
|
||||
#define F_CPU 16000000
|
||||
|
||||
struct circular_buffer usartTxBuffer;
|
||||
struct circular_buffer usartRxBuffer;
|
||||
#include "uart.h"
|
||||
|
||||
void UART_init(void) {
|
||||
UCSR0B = (1 << RXEN0) | (1 << TXEN0) | (1 << RXCIE0) | (1 << UDRIE0); // прерывание по приему и опустошению буфера передачи
|
||||
@ -28,49 +24,12 @@ void UART_send(uint8_t* data, size_t length) {
|
||||
UCSR0B |= (1 << UDRIE0); // Включаем прерывание по опустошению буфера
|
||||
}
|
||||
|
||||
void UART_put_char(unsigned char sym) {
|
||||
if (((UCSR0A & (1 << UDRE0)) != 0) && buffer_empty(&usartTxBuffer)) {
|
||||
UDR0 = sym;
|
||||
} else {
|
||||
if (!buffer_full(&usartTxBuffer)) {
|
||||
write_buffer(&usartTxBuffer, sym);
|
||||
} else {
|
||||
// Буфер передачи полон, обработка ошибки или другая логика
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int UART_get_char(unsigned char* sym) {
|
||||
if (!buffer_empty(&usartRxBuffer)) {
|
||||
*sym = read_buffer(&usartRxBuffer);
|
||||
return 1; // Символ успешно прочитан
|
||||
} else {
|
||||
return 0; // Буфер пуст, ошибка чтения
|
||||
}
|
||||
}
|
||||
|
||||
void UART_send_str(const unsigned char* data) {
|
||||
while (*data) {
|
||||
USART_put_char(*data++);
|
||||
}
|
||||
}
|
||||
|
||||
//uint32_t receivedByteCount = 0;
|
||||
|
||||
// Получение данных из буфера
|
||||
int UART_receive(uint8_t* data, size_t length) {
|
||||
char overflow=0; // Флаг переполнения, который устанавливается, если превышен размер массива
|
||||
uint32_t byteCount=0; // Счетчик байтов, принятых из буфера приема
|
||||
uint32_t timeout_ms_mb = 100; // Таймаут в миллисекундах для ожидания первого байта в буфере приема
|
||||
uint32_t timeout_ms = 4; // Таймаут в миллисекундах для общего времени приема данных
|
||||
uint32_t start_time = millis();
|
||||
// Цикл ожидания первого байта в буфере приема с учетом таймаута
|
||||
while (buffer_empty(&usartRxBuffer)) {
|
||||
if ((millis() - start_time) > timeout_ms_mb) {
|
||||
// Обработка таймаута
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
//Цикл приема данных с таймаутом
|
||||
while(1)
|
||||
{
|
||||
@ -96,30 +55,10 @@ int UART_receive(uint8_t* data, size_t length) {
|
||||
return overflow?-1:byteCount; // возвращает количество успешно принятых байт или -1
|
||||
}
|
||||
|
||||
|
||||
|
||||
int main(void) {
|
||||
sei();
|
||||
UART_init();
|
||||
timerInit();
|
||||
DDRD = 0xFF;
|
||||
uint8_t buff[] = {0x48, 0x45, 0x4C, 0x4C, 0x4F, 0x21, 0x09};
|
||||
//UART_send(buff, sizeof(buff));
|
||||
//
|
||||
//uint8_t receivedData[BUFFER_SIZE];
|
||||
//UART_receive(receivedData,sizeof(receivedData));
|
||||
//UART_send(receivedData, sizeof(receivedData));
|
||||
|
||||
while (1) {
|
||||
//unsigned long currentTime = millis();
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
// прерывание по завершению приема
|
||||
ISR(USART_RX_vect) {
|
||||
uint8_t data = UDR0; // читаем из регистра UDR0
|
||||
if (!buffer_full(&usartRxBuffer)) {
|
||||
write_buffer(&usartRxBuffer, data);// записываем символ в буфер приема
|
||||
}
|
||||
|
||||
}
|
||||
|
12
UART/uart.h
12
UART/uart.h
@ -1,11 +1,17 @@
|
||||
#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);
|
||||
void UART_put_char(unsigned char sym);
|
||||
int UART_get_char(unsigned char* sym);
|
||||
void UART_send_str(const unsigned char* data);
|
||||
int UART_receive(uint8_t* data, size_t length);
|
||||
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user