diff --git a/main/new_main.c b/main/new_main.c new file mode 100644 index 0000000..d6f0e65 --- /dev/null +++ b/main/new_main.c @@ -0,0 +1,252 @@ +#include <avr/io.h> +#include <util/delay.h> +#include <avr/interrupt.h> +#include <stdint.h> +#include <string.h> +#include <stdbool.h> +#include "UARTTT.h" +#include "modbus.h" + +#define F_CPU 16000000 + +//���������� ������� Slave +#define SLAVE_ADDR 0x32 + +// ���������� ������� Discrete Inputs (��������� 1,2 - �������; 3-4 - ����������) +#define DISCRETE_INPUTS_1 0x0000 +#define DISCRETE_INPUTS_2 0x0001 +#define DISCRETE_INPUTS_3 0x0002 +#define DISCRETE_INPUTS_4 0x0003 + +// ���������� ������� Coil (�������������� 1,2 - ��������; 3-4 - ��������) +#define COIL_1 0x0000 +#define COIL_2 0x0001 +#define COIL_3 0x0002 +#define COIL_4 0x0003 + +// ���������� ������� Input Registers +#define INPUT_REGISTERS 0x0000 + +// ��������� ��� ����������� ���������� �������� ������������ +#define LIGHT_DAY_TO_NIGHT 0x01F4 //500 +#define LIGHT_NIGHT_TO_DAY 0x0258 //600 + + +// ������� ������ �������� � ������� ������������ +int read_light_sensor(uint16_t* result) { + uint8_t buf_light[16]; + uint8_t buf_ans_light[16]; + size_t size; + size_t size_ans; + size = read_input_register(SLAVE_ADDR, INPUT_REGISTERS, buf_light, sizeof(buf_light)); + UART_send(buf_light, size); + size_ans = UART_receive(buf_ans_light, sizeof(buf_ans_light)); + if (size_ans > 0) { + return read_input_register_parse(buf_ans_light, size_ans, result); + } + else return size_ans; +} + +// ������� ������ �������� ���������� +int read_inter_lock_swith(uint8_t* result, uint8_t discreteInputs) { + uint8_t buf_ILS[16]; + uint8_t buf_ans_ILS[16]; + size_t size; + size_t size_ans; + size = read_input_status(SLAVE_ADDR, discreteInputs, 0x01, buf_ILS, sizeof(buf_ILS)); + UART_send(buf_ILS, size); + size_ans = UART_receive(buf_ans_ILS, sizeof(buf_ans_ILS)); + if (size_ans > 0) { + return read_input_status_parse(buf_ans_ILS, size_ans, result); + } + else return size_ans; +} + +typedef enum { + OPEN_SHUTTERS_COIL_1, + OPEN_SHUTTERS_COIL_2, + STOP_COIL_1, + STOP_COIL_2, + CLOSE_SHUTTERS_COIL_3, + CLOSE_SHUTTERS_COIL_4, + STOP_COIL_3, + STOP_COIL_4 +} CommandType; + +// ������� ��������� � ���������� ��� �������� ��� �������� ���� +int process_command(CommandType command) { + uint8_t buf[10]; + uint8_t buf_ans[10]; + size_t size; + size_t size_ans; + uint8_t buf_2[10]; + uint8_t buf_ans_2[10]; + size_t size_2; + size_t size_ans_2; + if (command == OPEN_SHUTTERS_COIL_1) { + // ������� �������� ���� (��������� coil_1) + size = forse_single_coil(SLAVE_ADDR, COIL_1, 0xFF, buf, sizeof(buf)); + UART_send(buf, size); + size_ans = UART_receive(buf_ans, size_ans); + if (size_ans > 0) { + return forse_single_coil_parse(buf, sizeof(buf_ans)); + } + else return size_ans; + } + else if (command == OPEN_SHUTTERS_COIL_2) { + // ������� �������� ���� (��������� coil_2) + size = forse_single_coil(SLAVE_ADDR, COIL_2, 0xFF, buf_2, sizeof(buf_2)); + UART_send(buf_2, size_2); + size_ans_2 = UART_receive(buf_ans_2, sizeof(buf_ans_2)); + if (size_ans_2 > 0) { + return forse_single_coil_parse(buf_2, size_ans_2); + } + else return size_ans_2; + + } + else if (command == STOP_COIL_1) { + // ������� ��������� coil_1 + size = forse_single_coil(SLAVE_ADDR, COIL_1, 0x00, buf, sizeof(buf)); + UART_send(buf, size); + size_ans = UART_receive(buf_ans, sizeof(buf_ans)); + if (size_ans > 0) { + return forse_single_coil_parse(buf, size_ans); + } + else return size_ans; + + } + else if (command == STOP_COIL_2) { + // ������� ��������� coil_2 + size = forse_single_coil(SLAVE_ADDR, COIL_2, 0x00, buf, sizeof(buf)); + UART_send(buf_2, size_2); + size_ans_2 = UART_receive(buf_ans_2, sizeof(buf_ans_2)); + if (size_ans_2 > 0) { + return forse_single_coil_parse(buf_2, size_ans_2); + } + else return size_ans_2; + + } + else if (command == CLOSE_SHUTTERS_COIL_3) { + // ������� �������� ����(��������� coil_3) + size = forse_single_coil(SLAVE_ADDR, COIL_3, 0xFF, buf, sizeof(buf)); + UART_send(buf, size); + size_ans = UART_receive(buf_ans, sizeof(buf_ans)); + if (size_ans > 0) { + return forse_single_coil_parse(buf, size_ans); + } + else return size_ans; + + } + else if (command == CLOSE_SHUTTERS_COIL_3) { + // ������� �������� ����(��������� coil_4) + size = forse_single_coil(SLAVE_ADDR, COIL_4, 0xFF, buf, sizeof(buf)); + UART_send(buf_2, size_2); + size_ans_2 = UART_receive(buf_ans_2, sizeof(buf_ans_2)); + if (size_ans_2 > 0) { + return forse_single_coil_parse(buf_2, size_ans_2); + } + else return size_ans_2; + + } + else if (command == STOP_COIL_3) { + // ������� ��������� coil_3 + size = forse_single_coil(SLAVE_ADDR, COIL_3, 0x00, buf, sizeof(buf)); + UART_send(buf, size); + size_ans = UART_receive(buf_ans, sizeof(buf_ans)); + if (size_ans > 0) { + return forse_single_coil_parse(buf, size_ans); + } + else return size_ans; + + } + else if (command == STOP_COIL_4) { + // ������� ��������� coil_4 + size = forse_single_coil(SLAVE_ADDR, COIL_4, 0x00, buf, sizeof(buf)); + UART_send(buf_2, size_2); + size_ans_2 = UART_receive(buf_ans_2, sizeof(buf_ans_2)); + if (size_ans_2 > 0) { + return forse_single_coil_parse(buf_2, size_ans_2); + } + else return size_ans_2; + } +} + +int main(void) { + UART_init(); + uint8_t buf[16]; + uint8_t buf_ans[16]; + sei(); + bool day_right_now = true; // true - ���� false - ���� + while (1) { + uint16_t light_sensor; // ���������� ��� ������ �������� ������� ��������� + int read_result_sensor = read_light_sensor(&light_sensor); // ������� ������ �������� ������� ��������� � ������ � ��������������� ���������� + if (read_result_sensor < 1) { // �������� �� ���������� + continue; + } + + uint8_t read_inter_lock_swith_1; // ���������� ��� ������ �������� ��������� 1 + uint8_t read_inter_lock_swith_2; // ���������� ��� ������ �������� ��������� 2 + uint8_t read_inter_lock_swith_3; // ���������� ��� ������ �������� ��������� 3 + uint8_t read_inter_lock_swith_4; // ���������� ��� ������ �������� ��������� 4 + int read_result_inter_1 = read_inter_lock_swith(&read_inter_lock_swith_1, DISCRETE_INPUTS_1); // ������� ������ ��������� 1 �������� � ������ � ��������������� ���������� + int read_result_inter_2 = read_inter_lock_swith(&read_inter_lock_swith_2, DISCRETE_INPUTS_2); // ������� ������ ��������� 2 �������� � ������ � ��������������� ���������� + int read_result_inter_3 = read_inter_lock_swith(&read_inter_lock_swith_3, DISCRETE_INPUTS_3); // ������� ������ ��������� 3 �������� � ������ � ��������������� ���������� + int read_result_inter_4 = read_inter_lock_swith(&read_inter_lock_swith_4, DISCRETE_INPUTS_4); // ������� ������ ��������� 4 �������� � ������ � ��������������� ���������� + if (read_result_inter_1 < 1 || read_result_inter_2 < 1 || read_result_inter_3 < 1 || read_result_inter_4 < 1) { // �������� �� ���������� + continue; + } + + + if (day_right_now == true) { // ���� � ��� ������ ����, ��... + if (light_sensor < LIGHT_DAY_TO_NIGHT) { // ���� �������� � ������� ������ ������ �������� �� ��� � ����, ��... + day_right_now = false; // ������������� �������� false, �� ���� ����. + } + } + else { // ���� � ��� ������ ����, ��... + if (light_sensor > LIGHT_NIGHT_TO_DAY) { // ���� �������� � ������� ������ ������ �������� �� ���� � ����, ��... + day_right_now = true; // ������������� �������� true, �� ���� ����. + } + } + + // ������ ������ ���� + if (day_right_now == false) { // ���� � ��� ����, ��... + if (read_inter_lock_swith_3 == 0x00) { // ���� ���������� ��������(3) = 0. + int process_comand_1 = process_command(CLOSE_SHUTTERS_COIL_3); // ������ 1 ������� (�������� ����). + continue; + } + else { // ���� ���������� �������� (3) = 1. + int process_comand_3 = process_command(STOP_COIL_3); // ��������� ������ 1 �������. + continue; + } + + if (read_inter_lock_swith_4 == 0x00) { // ���� ���������� ��������(4) = 0. + int process_comand_2 = process_command(CLOSE_SHUTTERS_COIL_4); // ������ 2 ������� (�������� ����). + continue; + } + else { // ���� ���������� �������� (4) = 1. + int process_comand_4 = process_command(STOP_COIL_4); // ��������� ������ 2 ������� + continue; + } + } + else { // ���� � ��� ����, ��... + if (read_inter_lock_swith_1 == 0x00) { // ���� ������� �������� (1) = 0. + int process_command_5 = process_command(OPEN_SHUTTERS_COIL_1); // ������ 3 ������� (�������� ����). + continue; + } + else { // ���� ������� ��������(1) = 1. + int process_command_7 = process_command(STOP_COIL_1); // ��������� ������ 3 �������. + continue; + } + + if (read_inter_lock_swith_2 == 0x00) { //���� ������� �������� (2) = 0. + int process_command_6 = process_command(OPEN_SHUTTERS_COIL_2); // ������ 4 ������� (�������� ����). + continue; + } + else { // ���� ������� ��������(2) = 1. + int process_command_8 = process_command(STOP_COIL_2); // ��������� ������ 4 �������. + continue; + } + } + } + return 0; +}