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.

182 lines
4.5 KiB

#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
float g_adc_bytes[ADC_NUM_CH] = {0.0f};
uint8_t g_adc_len = 0;
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);
}
/**
* : ADC_ReadAndSend_UART
* : ADC
*
* :
* 1) ADC
* - ADC_NUM_CH
* - : {0x02, 0x03, 0x04, 0x05}
*
* 2)
* for (i = 0; i < ADC_NUM_CH; i++)
* a) ADS
* b) R_ADC_Start()
* c) (ADIF == 1 )
* d) ADIF
* e) : R_ADC_Get_Result(&adc_value)
* f) ADC : R_ADC_Stop()
*
* 3) ADC
* - voltage = (adc_value / ADC_RESOLUTION) * VREF
* - : 12bit ADC, VREF = 5V이면 0~4095 0~5V
*
* 4)
* - g_adc_bytes[i] = voltage
* - g_adc_len
*
* :
* - g_adc_bytes[]:
* - g_adc_len:
*/
void ADC_ReadAndSend_UART(void)
{
static const uint8_t ADC_CHANNELS[ADC_NUM_CH] = { 0x02,0x03,0x04,0x05};
uint16_t adc_value;
float voltage;
int i;
g_adc_len = 0;
for (i = 0; i < ADC_NUM_CH; i++) {
ADS = ADC_CHANNELS[i]; // 채널 선택
R_ADC_Start(); // 변환 시작
while (ADIF == 0U); // 변환 완료 대기
ADIF = 0U;
R_ADC_Get_Result(&adc_value);
R_ADC_Stop();
/* V 계산 */
voltage = (adc_value / ADC_RESOLUTION) * VREF;
g_adc_bytes[i] = voltage;
g_adc_len++;
}
}