commit
						0ccb490096
					
				
							
								
								
									
										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
 | 
					 | 
				
			||||||
							
								
								
									
										57
									
								
								UART/uart.c
									
									
									
									
									
								
							
							
						
						
									
										57
									
								
								UART/uart.c
									
									
									
									
									
								
							@ -2,12 +2,14 @@
 | 
				
			|||||||
#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 << UDRIE0); // прерывание по приему и опустошению буфера передачи
 | 
						UCSR0B = (1 << RXEN0) | (1 << TXEN0) | (1 << RXCIE0) | (1<<TXCIE0) | (1 << UDRIE0); // прерывание по приему и передаче
 | 
				
			||||||
	UCSR0C = (1 << UCSZ01) | (1 << UCSZ00);
 | 
						UCSR0C = (1 << UCSZ01) | (1 << UCSZ00);
 | 
				
			||||||
	UBRR0H = 0;
 | 
						UBRR0H = 0;
 | 
				
			||||||
	UBRR0L = 103;
 | 
						UBRR0L = 103;
 | 
				
			||||||
@ -15,50 +17,45 @@ 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(&usartTxBuffer)) {
 | 
							if (!buffer_full(&uartTxBuffer)) {
 | 
				
			||||||
			write_buffer(&usartTxBuffer, data[i]);
 | 
								write_buffer(&uartTxBuffer, data[i]);
 | 
				
			||||||
			} else {
 | 
								} else {
 | 
				
			||||||
			break; // если буфер передачи заполнен, то отправка прерывается
 | 
								break; // если буфер передачи заполнен, то отправка прерывается
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	UCSR0B |= (1 << UDRIE0); // Включаем прерывание по опустошению буфера
 | 
						UCSR0B |= (1 << TXCIE0); // включаем прерывание по завершении передачи
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// Получение данных из буфера
 | 
					// Получение данных из буфера
 | 
				
			||||||
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; // Таймаут в миллисекундах для общего времени приема данных
 | 
					    // Пока буфер приема не пуст и не превышен лимит длины массива,
 | 
				
			||||||
	uint32_t start_time = millis();
 | 
					 | 
				
			||||||
	//Цикл приема данных с таймаутом
 | 
					 | 
				
			||||||
	while(1)
 | 
					 | 
				
			||||||
	{
 | 
					 | 
				
			||||||
		// Пока буфер приема не пуст и не истек таймаут общего времени,  
 | 
					 | 
				
			||||||
    // функция продолжает читать байты из буфера и сохранять их в массив data.
 | 
					    // функция продолжает читать байты из буфера и сохранять их в массив data.
 | 
				
			||||||
		while (!buffer_empty(&usartRxBuffer)) {
 | 
					    while (!buffer_empty(&uartRxBuffer) && byteCount < length) {
 | 
				
			||||||
			int  reader = read_buffer(&usartRxBuffer);//прием и запись символа в переменную
 | 
					        int reader = read_buffer(&uartRxBuffer);  // Прием и запись символа в переменную
 | 
				
			||||||
			if(byteCount<=length){
 | 
					        data[byteCount] = reader;  // Запись в массив с индексом byteCount
 | 
				
			||||||
				data[byteCount] = reader; // запись в массив с индексом byteCount
 | 
					 | 
				
			||||||
			}
 | 
					 | 
				
			||||||
			else{
 | 
					 | 
				
			||||||
				overflow=1;
 | 
					 | 
				
			||||||
			}
 | 
					 | 
				
			||||||
        byteCount++;
 | 
					        byteCount++;
 | 
				
			||||||
 | 
					 | 
				
			||||||
			start_time = millis();
 | 
					 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
		if ((millis() - start_time) > timeout_ms) { // если превышение времени в 4 ms
 | 
					    // Проверка переполнения
 | 
				
			||||||
			break;
 | 
					    if (byteCount > length) {
 | 
				
			||||||
 | 
					        overflow = 1;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
		
 | 
					    return overflow ? -1 : byteCount;  // Возвращает количество успешно принятых байт или -1 в случае переполнения
 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
	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(&usartRxBuffer)) {
 | 
						if (!buffer_full(&uartRxBuffer)) {
 | 
				
			||||||
		write_buffer(&usartRxBuffer, data);// записываем символ в буфер приема
 | 
							write_buffer(&uartRxBuffer, data);// записываем символ в буфер приема
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					ISR(USART_TX_vect) {
 | 
				
			||||||
 | 
						if (!buffer_empty(&uartTxBuffer)) {
 | 
				
			||||||
 | 
							UDR0 = read_buffer(&uartTxBuffer);
 | 
				
			||||||
 | 
						} else {
 | 
				
			||||||
 | 
							UCSR0B &= ~(1 << TXCIE0); // отключаем прерывание, когда все данные отправлены
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
@ -2,14 +2,9 @@
 | 
				
			|||||||
#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