Compare commits
2
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
2f050f78c5 | ||
|
|
b344a3564a |
@@ -0,0 +1,64 @@
|
||||
#include "device_ring_buffer.h"
|
||||
|
||||
void rb_initialize(struct rb* _rb)
|
||||
{
|
||||
memset(_rb, 0, sizeof(struct rb));
|
||||
}
|
||||
|
||||
int rb_put(struct rb* _rb, char element)
|
||||
{
|
||||
if (_rb->count < sizeof(_rb->buf))
|
||||
{
|
||||
_rb->count++;
|
||||
if (_rb->tail != _rb->head)
|
||||
{
|
||||
_rb->buf[_rb->tail] = element;
|
||||
_rb->tail++;
|
||||
if (_rb->tail == sizeof(_rb->buf))
|
||||
{
|
||||
_rb->tail = 0;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
int rb_get(struct rb* _rb, char *element)
|
||||
{
|
||||
if (_rb->count > 0)
|
||||
{
|
||||
_rb->count--;
|
||||
if (_rb->tail != _rb->head)
|
||||
{
|
||||
*element = _rb->buf[_rb->head];
|
||||
_rb->head++;
|
||||
if (_rb->head == sizeof(_rb->buf))
|
||||
{
|
||||
_rb->head = 0;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
|
||||
}
|
||||
int count_elements(struct rb* _rb)
|
||||
{
|
||||
return _rb->count;
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
#ifndef DEVICE_RING_BUFFER_H
|
||||
#define DEVICE_RING_BUFFER_H
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
struct rb {
|
||||
char buf[32];
|
||||
int head;
|
||||
int tail;
|
||||
int count;
|
||||
};
|
||||
|
||||
void rb_initialize(struct rb* _rb);
|
||||
int rb_put(struct rb* _rb, char element);
|
||||
int rb_get(struct rb* _rb, char* element);
|
||||
int count_elements(struct rb* _rb);
|
||||
|
||||
#endif /*DEVICE_RING_BUFFER_H*/
|
||||
@@ -0,0 +1,39 @@
|
||||
/*
|
||||
* main.c
|
||||
*
|
||||
* Created: 18.06.2023 19:53:46
|
||||
* Author: Lada Yuzhakova
|
||||
*/
|
||||
|
||||
#include "UART.h"
|
||||
#include "modbus.h"
|
||||
#include "timer.h"
|
||||
#include "device_adc.h"
|
||||
#include "device_ring_buffer.h"
|
||||
#include "gpio.h"
|
||||
#include <avr/io.h>
|
||||
|
||||
int main(void)
|
||||
{
|
||||
// Èíèöèàëèçàöèè
|
||||
adc_init();
|
||||
uart_initialize();
|
||||
modbus_init();
|
||||
gpio_init();
|
||||
rb_initialize(get_rb_receive());
|
||||
rb_initialize(get_rb_transmit());
|
||||
// Çàïóñêàåì òàéìåð
|
||||
setup_timer();
|
||||
|
||||
while(1)
|
||||
{
|
||||
// Ôóíêöèÿ ïðèåìà ìîäáàñ-çàïðîñà
|
||||
modbus_rtu();
|
||||
// Ôóíêöèÿ ôîðìèðîâàíèÿ ìîäáàñ-îòâåòà
|
||||
modbus_answer();
|
||||
// Îòïðàâëÿòü îòâåò ïî óàðò
|
||||
UART_Transmit(get_rb_transmit());
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user