You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

125 lines
2.9 KiB

2 weeks ago
#include "uart.h"
#include "delay.h"
#include "r_cg_adc.h"
#include "r_cg_port.h"
#define RS485_EN_PORT P4
#define RS485_EN_PM PM4
#define RS485_EN_MASK (0x20U) // P4.5
1 week ago
2 weeks ago
void rs485_set_tx(uint8_t on)
{
if (on) RS485_EN_PORT |= RS485_EN_MASK; // EN=1 (TX)
else RS485_EN_PORT &= (uint8_t)~RS485_EN_MASK; // EN=0 (RX)
}
void rs485_init(void)
{
RS485_EN_PM &= (uint8_t)~RS485_EN_MASK; // 출력
rs485_set_tx(0); // 기본 RX 모드
}
/**
* : uart_send_string
* : null UART0로
*
* :
* - str : (C , '\0' )
*
* : (void)
*
* :
* 1)
* - '\0' len
*
* 2) UART
* - R_UART0_Send()
* - (uint8_t *)
*
* :
* - null ('\0')
* - UART0
*/
// UART0(RS485)
void uart_send_string(const char *str)
{
uint16_t len = 0;
while (str[len] != '\0') len++;
rs485_set_tx(1);
R_UART0_Send((uint8_t *)str, len);
}
// UART1(PC)
void uart1_send_string(const char *str)
{
uint16_t len = 0;
while (str[len] != '\0') len++;
R_UART1_Send((uint8_t *)str, len);
}
/**
* : uart_send_hex
* : 8 (uint8_t) 16 UART0로
*
* :
* - val : 8
*
* : (void)
*
* :
* 1) / 4
* - high = val >> 4, 4
* - low = val & 0x0F, 4
*
* 2) 16
* - 0~9 '0'~'9'
* - 10~15 'A'~'F'
* - hex[0] = 4
* - hex[1] = 4
*
* 3) UART
* - R_UART0_Send() 2
*
* 4)
* - delay(10000)
*
* :
* - 1 2 16
* - : val = 0xAF "AF"
*/
void uart_send_hex(uint8_t val)
{
uint8_t hex[2];
uint8_t high = (val >> 4) & 0x0F;
uint8_t low = val & 0x0F;
hex[0] = (high < 10) ? ('0' + high) : ('A' + (high - 10));
hex[1] = (low < 10) ? ('0' + low) : ('A' + (low - 10));
rs485_set_tx(1);
R_UART0_Send(hex, 2);
delay(10000);
}
void uart1_send_hex(uint8_t val)
{
uint8_t hex[2];
uint8_t high = (val >> 4) & 0x0F;
uint8_t low = val & 0x0F;
hex[0] = (high < 10) ? ('0' + high) : ('A' + (high - 10));
hex[1] = (low < 10) ? ('0' + low ) : ('A' + (low - 10));
R_UART1_Send(hex, 2);
delay(10000);
}