Compare commits
10 Commits
Author | SHA1 | Date | |
---|---|---|---|
51bcae6458 | |||
e22c380faa | |||
4956ae0a24 | |||
2739418e23 | |||
cac4258352 | |||
cb281d378e | |||
f83f5bfa9f | |||
6ee102f904 | |||
3e0315a422 | |||
993d23b928 |
42
Circular buffer/сircular_buf.c
Normal file
42
Circular buffer/сircular_buf.c
Normal file
@ -0,0 +1,42 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
#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;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
36
UART/circular_buf.c
Normal file
36
UART/circular_buf.c
Normal file
@ -0,0 +1,36 @@
|
|||||||
|
#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;//записывает значение value в элемент массива buffer в хвост
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
18
UART/circular_buf.h
Normal file
18
UART/circular_buf.h
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
#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 */
|
22
UART/main.atsln
Normal file
22
UART/main.atsln
Normal file
@ -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
|
123
UART/main.c
Normal file
123
UART/main.c
Normal file
@ -0,0 +1,123 @@
|
|||||||
|
#include <avr/io.h>
|
||||||
|
#include <avr/interrupt.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include "timer.h"
|
||||||
|
#include "circular_buf.h"
|
||||||
|
|
||||||
|
#define F_CPU 16000000
|
||||||
|
|
||||||
|
struct circular_buffer usartTxBuffer;
|
||||||
|
struct circular_buffer usartRxBuffer;
|
||||||
|
|
||||||
|
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); // Включаем прерывание по опустошению буфера передачи
|
||||||
|
}
|
||||||
|
//положить символ в буфер
|
||||||
|
void USART_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 get_char(unsigned char* sym) {
|
||||||
|
if (!buffer_empty(&usartTxBuffer)) {
|
||||||
|
*sym = read_buffer(&usartTxBuffer);
|
||||||
|
return 1; // Успешно прочитан символ из буфера передачи
|
||||||
|
} else {
|
||||||
|
return 0; // Буфер передачи пуст, возникла ошибка чтения
|
||||||
|
}
|
||||||
|
}
|
||||||
|
//функция загрузки строк
|
||||||
|
void USART_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)
|
||||||
|
{
|
||||||
|
|
||||||
|
while (!buffer_empty(&usartRxBuffer)) { //пока буффер не пустой
|
||||||
|
int reader = read_buffer(&usartRxBuffer);//принимаем символ в переменную
|
||||||
|
if(byteCount<=length){
|
||||||
|
data[byteCount] = reader; //записываем в массив с индексом byteCount
|
||||||
|
}
|
||||||
|
else{
|
||||||
|
overflow=1;
|
||||||
|
}
|
||||||
|
byteCount++; //+1 к счетчику
|
||||||
|
|
||||||
|
start_time = millis();
|
||||||
|
}
|
||||||
|
if ((millis() - start_time) > timeout_ms) { //если привыслии время в 4 ms
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
return overflow?-1:byteCount;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
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);// записываем принятый символ в буфер приема
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
//перезаписать чтоб освободитть байт, из udr0
|
86
UART/main.componentinfo.xml
Normal file
86
UART/main.componentinfo.xml
Normal file
@ -0,0 +1,86 @@
|
|||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<Store xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="AtmelPackComponentManagement">
|
||||||
|
<ProjectComponents>
|
||||||
|
<ProjectComponent z:Id="i1" xmlns:z="http://schemas.microsoft.com/2003/10/Serialization/">
|
||||||
|
<CApiVersion></CApiVersion>
|
||||||
|
<CBundle></CBundle>
|
||||||
|
<CClass>Device</CClass>
|
||||||
|
<CGroup>Startup</CGroup>
|
||||||
|
<CSub></CSub>
|
||||||
|
<CVariant></CVariant>
|
||||||
|
<CVendor>Atmel</CVendor>
|
||||||
|
<CVersion>1.7.0</CVersion>
|
||||||
|
<DefaultRepoPath>D:/atmel studio\7.0\Packs</DefaultRepoPath>
|
||||||
|
<DependentComponents xmlns:d4p1="http://schemas.microsoft.com/2003/10/Serialization/Arrays" />
|
||||||
|
<Description></Description>
|
||||||
|
<Files xmlns:d4p1="http://schemas.microsoft.com/2003/10/Serialization/Arrays">
|
||||||
|
<d4p1:anyType i:type="FileInfo">
|
||||||
|
<AbsolutePath>D:/atmel studio\7.0\Packs\atmel\ATmega_DFP\1.7.374\include\</AbsolutePath>
|
||||||
|
<Attribute></Attribute>
|
||||||
|
<Category>include</Category>
|
||||||
|
<Condition>C</Condition>
|
||||||
|
<FileContentHash i:nil="true" />
|
||||||
|
<FileVersion></FileVersion>
|
||||||
|
<Name>include/</Name>
|
||||||
|
<SelectString></SelectString>
|
||||||
|
<SourcePath></SourcePath>
|
||||||
|
</d4p1:anyType>
|
||||||
|
<d4p1:anyType i:type="FileInfo">
|
||||||
|
<AbsolutePath>D:/atmel studio\7.0\Packs\atmel\ATmega_DFP\1.7.374\include\avr\iom328p.h</AbsolutePath>
|
||||||
|
<Attribute></Attribute>
|
||||||
|
<Category>header</Category>
|
||||||
|
<Condition>C</Condition>
|
||||||
|
<FileContentHash>4leX2H78R90/kvebBjYSOw==</FileContentHash>
|
||||||
|
<FileVersion></FileVersion>
|
||||||
|
<Name>include/avr/iom328p.h</Name>
|
||||||
|
<SelectString></SelectString>
|
||||||
|
<SourcePath></SourcePath>
|
||||||
|
</d4p1:anyType>
|
||||||
|
<d4p1:anyType i:type="FileInfo">
|
||||||
|
<AbsolutePath>D:/atmel studio\7.0\Packs\atmel\ATmega_DFP\1.7.374\templates\main.c</AbsolutePath>
|
||||||
|
<Attribute>template</Attribute>
|
||||||
|
<Category>source</Category>
|
||||||
|
<Condition>C Exe</Condition>
|
||||||
|
<FileContentHash>WuscJm7SWE9tRkxyEb0ntg==</FileContentHash>
|
||||||
|
<FileVersion></FileVersion>
|
||||||
|
<Name>templates/main.c</Name>
|
||||||
|
<SelectString>Main file (.c)</SelectString>
|
||||||
|
<SourcePath></SourcePath>
|
||||||
|
</d4p1:anyType>
|
||||||
|
<d4p1:anyType i:type="FileInfo">
|
||||||
|
<AbsolutePath>D:/atmel studio\7.0\Packs\atmel\ATmega_DFP\1.7.374\templates\main.cpp</AbsolutePath>
|
||||||
|
<Attribute>template</Attribute>
|
||||||
|
<Category>source</Category>
|
||||||
|
<Condition>C Exe</Condition>
|
||||||
|
<FileContentHash>mkKaE95TOoATsuBGv6jmxg==</FileContentHash>
|
||||||
|
<FileVersion></FileVersion>
|
||||||
|
<Name>templates/main.cpp</Name>
|
||||||
|
<SelectString>Main file (.cpp)</SelectString>
|
||||||
|
<SourcePath></SourcePath>
|
||||||
|
</d4p1:anyType>
|
||||||
|
<d4p1:anyType i:type="FileInfo">
|
||||||
|
<AbsolutePath>D:/atmel studio\7.0\Packs\atmel\ATmega_DFP\1.7.374\gcc\dev\atmega328p</AbsolutePath>
|
||||||
|
<Attribute></Attribute>
|
||||||
|
<Category>libraryPrefix</Category>
|
||||||
|
<Condition>GCC</Condition>
|
||||||
|
<FileContentHash i:nil="true" />
|
||||||
|
<FileVersion></FileVersion>
|
||||||
|
<Name>gcc/dev/atmega328p</Name>
|
||||||
|
<SelectString></SelectString>
|
||||||
|
<SourcePath></SourcePath>
|
||||||
|
</d4p1:anyType>
|
||||||
|
</Files>
|
||||||
|
<PackName>ATmega_DFP</PackName>
|
||||||
|
<PackPath>D:/atmel studio/7.0/Packs/atmel/ATmega_DFP/1.7.374/Atmel.ATmega_DFP.pdsc</PackPath>
|
||||||
|
<PackVersion>1.7.374</PackVersion>
|
||||||
|
<PresentInProject>true</PresentInProject>
|
||||||
|
<ReferenceConditionId>ATmega328P</ReferenceConditionId>
|
||||||
|
<RteComponents xmlns:d4p1="http://schemas.microsoft.com/2003/10/Serialization/Arrays">
|
||||||
|
<d4p1:string></d4p1:string>
|
||||||
|
</RteComponents>
|
||||||
|
<Status>Resolved</Status>
|
||||||
|
<VersionMode>Fixed</VersionMode>
|
||||||
|
<IsComponentInAtProject>true</IsComponentInAtProject>
|
||||||
|
</ProjectComponent>
|
||||||
|
</ProjectComponents>
|
||||||
|
</Store>
|
23
UART/timer.c
Normal file
23
UART/timer.c
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
#include <avr/io.h>
|
||||||
|
#include <avr/interrupt.h>
|
||||||
|
|
||||||
|
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++;
|
||||||
|
}
|
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
|
120
uart/uart.c
Normal file
120
uart/uart.c
Normal file
@ -0,0 +1,120 @@
|
|||||||
|
#include <avr/io.h>
|
||||||
|
#include <util/delay.h>
|
||||||
|
#include <avr/interrupt.h>
|
||||||
|
|
||||||
|
#define F_CPU 16000000
|
||||||
|
#define SIZE_BUF 8
|
||||||
|
|
||||||
|
//кольцевой (циклический) буфер
|
||||||
|
volatile unsigned char usartTxBuf[SIZE_BUF];
|
||||||
|
unsigned char txBufTail = 0; //"указатель" хвоста буфера
|
||||||
|
unsigned char txBufHead = 0; //"указатель" головы буфера
|
||||||
|
volatile unsigned char txCount = 0; //счетчик символов
|
||||||
|
|
||||||
|
uint8_t receive = 0;
|
||||||
|
uint8_t rx_data = 0;
|
||||||
|
volatile uint8_t rx_flag = 0;
|
||||||
|
|
||||||
|
void UARTInit(void) {
|
||||||
|
UCSR1B=0; UCSR1C=0; // Обнулим, на всякий случай регистры статуса и контроля.
|
||||||
|
UBRR0H = 0; // Старшие биты регистра скорости.
|
||||||
|
UBRR0L = 103; //скорость передачи данных 9600
|
||||||
|
UCSR0B = (1<<RXEN0)|(1<<TXEN0)|(1<<RXCIE0)|(1<<TXCIE0)|(0<<UDRIE0);//Включаем (подключаем ножки контроллера) приём и передачу по UART.
|
||||||
|
UCSR0C = (1<<UCSZ01)|(1<<UCSZ00); //8 bit
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
void UARTSend(uint8_t data) {
|
||||||
|
while (!(UCSR0A & (1<<UDRE0))); // Ждём опустошения буфера передачи (читаем флаг).
|
||||||
|
UDR0 = data; // Записываем в регистр значение переменной "data" для передачи.
|
||||||
|
}
|
||||||
|
|
||||||
|
//"очищает" буфер
|
||||||
|
void USART_FlushTxBuf(void)
|
||||||
|
{
|
||||||
|
txBufTail = 0;
|
||||||
|
txCount = 0;
|
||||||
|
txBufHead = 0;
|
||||||
|
|
||||||
|
//положить символ в буфер
|
||||||
|
void USART_PutChar(unsigned char sym)
|
||||||
|
{
|
||||||
|
if(((UCSRA0 & (1<<UDRE0)) != 0) && (txCount == 0)) //Если модуль USART не занят и передающий буфер пустой,
|
||||||
|
UDR0 = sym; //символ записывается в регистр UDR
|
||||||
|
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 (count > 0){ //если буфер не пустой
|
||||||
|
sym = cycleBuf[head]; //считываем символ из буфера
|
||||||
|
count--; //уменьшаем счетчик символов
|
||||||
|
head++; //инкрементируем индекс головы буфера
|
||||||
|
if (head == SIZE_BUF) head = 0;
|
||||||
|
}
|
||||||
|
return sym;
|
||||||
|
}
|
||||||
|
|
||||||
|
//функция загрузки строк
|
||||||
|
void USART_SendStr(unsigned char * data)
|
||||||
|
{
|
||||||
|
unsigned char sym;
|
||||||
|
while(*data){ // пока не будет считан символ конца строки
|
||||||
|
sym = *data++;
|
||||||
|
USART_PutChar(sym);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// unsigned char UARTGet() {
|
||||||
|
// while(!rx_flag); //пока значение флага не станет равно "1"
|
||||||
|
// rx_flag = 0;
|
||||||
|
// return rx_data; //возвращет состояние переменной rx_data
|
||||||
|
// }
|
||||||
|
|
||||||
|
int main(void) {
|
||||||
|
sei();
|
||||||
|
UARTInit();
|
||||||
|
DDRL = 0b11111111; // Определим порт "L" на выход
|
||||||
|
while(1) {
|
||||||
|
receive = UARTGet();
|
||||||
|
receive++;
|
||||||
|
UARTSend(receive);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//обработчик прерывания по завершению передачи
|
||||||
|
ISR(USART_TXC_vect) {
|
||||||
|
if (txCount > 0){ //если буфер не пустой
|
||||||
|
UDR0 = usartTxBuf[txBufHead]; //записываем в UDR символ из буфера
|
||||||
|
txCount--; //уменьшаем счетчик символов
|
||||||
|
txBufHead++; //инкрементируем индекс головы буфера
|
||||||
|
if (txBufHead == SIZE_BUF)
|
||||||
|
txBufHead = 0;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
UCSR0B &= ~(0<<TXCIE0);
|
||||||
|
}
|
||||||
|
|
||||||
|
// rx_data = UDR0; //В нем данные из приемного буфера UDR переносятся в глобальную переменную rx_data
|
||||||
|
// rx_flag = 1; //выставляется флаг rx_flag, говорящий о том, что прием завершен
|
||||||
|
}
|
||||||
|
//прерывание по завершению приема
|
||||||
|
ISR (USART_RXC_vect)
|
||||||
|
{
|
||||||
|
if (rxCount < SIZE_BUF){ //если в буфере еще есть место
|
||||||
|
usartRxBuf[rxBufTail] = UDR0; //считать символ из UDR в буфер
|
||||||
|
rxBufTail++; //увеличить индекс хвоста приемного буфера
|
||||||
|
if (rxBufTail == SIZE_BUF) rxBufTail = 0;
|
||||||
|
rxCount++; //увеличить счетчик принятых символов
|
||||||
|
}
|
||||||
|
}
|
11
uart/uart.h
Normal file
11
uart/uart.h
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
#ifndef UART_H
|
||||||
|
#define UART_H
|
||||||
|
|
||||||
|
void UARTInit(void);
|
||||||
|
void UARTSend(uint8_t data);
|
||||||
|
void USART_FlushTxBuf(void);
|
||||||
|
void USART_PutChar(unsigned char sym);
|
||||||
|
unsigned char GetChar(void);
|
||||||
|
void USART_SendStr(unsigned char * data);
|
||||||
|
|
||||||
|
#endif /* UART_H */
|
Loading…
Reference in New Issue
Block a user