电路图
生成代码
void R_SAU0_Create(void);
void R_UART1_Create(void);
void R_UART1_Start(void);
void R_UART1_Stop(void);
MD_STATUS R_UART1_Send(uint8_t * const tx_buf, uint16_t tx_num);
MD_STATUS R_UART1_Receive(uint8_t * const rx_buf, uint16_t rx_num);
static void r_uart1_callback_receiveend(void);
static void r_uart1_callback_sendend(void);
void R_SAU1_Create(void);
void R_UART2_Create(void);
void R_UART2_Start(void);
void R_UART2_Stop(void);
MD_STATUS R_UART2_Send(uint8_t * const tx_buf, uint16_t tx_num);
MD_STATUS R_UART2_Receive(uint8_t * const rx_buf, uint16_t rx_num);
static void r_uart2_callback_receiveend(void);
static void r_uart2_callback_sendend(void);
/***********************************************************************************************************************
* Function Name: R_UART1_Create
* Description : This function initializes the UART1 module.
* Arguments : None
* Return Value : None
***********************************************************************************************************************/
void R_UART1_Create(void)
{
ST0 |= _0008_SAU_CH3_STOP_TRG_ON | _0004_SAU_CH2_STOP_TRG_ON; /* disable UART1 receive and transmit */
STMK1 = 1U; /* disable INTST1 interrupt */
STIF1 = 0U; /* clear INTST1 interrupt flag */
SRMK1 = 1U; /* disable INTSR1 interrupt */
SRIF1 = 0U; /* clear INTSR1 interrupt flag */
SREMK1 = 1U; /* disable INTSRE1 interrupt */
SREIF1 = 0U; /* clear INTSRE1 interrupt flag */
/* Set INTST1 low priority */
STPR11 = 1U;
STPR01 = 1U;
/* Set INTSR1 low priority */
SRPR11 = 1U;
SRPR01 = 1U;
SMR02 = _0020_SAU_SMRMN_INITIALVALUE | _0000_SAU_CLOCK_SELECT_CK00 | _0000_SAU_TRIGGER_SOFTWARE |
_0002_SAU_MODE_UART | _0000_SAU_TRANSFER_END;
SCR02 = _8000_SAU_TRANSMISSION | _0000_SAU_INTSRE_MASK | _0000_SAU_PARITY_NONE | _0080_SAU_LSB | _0010_SAU_STOP_1 |
_0007_SAU_LENGTH_8;
SDR02 = _CE00_UART1_TRANSMIT_DIVISOR;
NFEN0 |= _04_SAU_RXD1_FILTER_ON;
SIR03 = _0004_SAU_SIRMN_FECTMN | _0002_SAU_SIRMN_PECTMN | _0001_SAU_SIRMN_OVCTMN; /* clear error flag */
SMR03 = _0020_SAU_SMRMN_INITIALVALUE | _0000_SAU_CLOCK_SELECT_CK00 | _0100_SAU_TRIGGER_RXD | _0000_SAU_EDGE_FALL |
_0002_SAU_MODE_UART | _0000_SAU_TRANSFER_END;
SCR03 = _4000_SAU_RECEPTION | _0000_SAU_INTSRE_MASK | _0000_SAU_PARITY_NONE | _0080_SAU_LSB | _0010_SAU_STOP_1 |
_0007_SAU_LENGTH_8;
SDR03 = _CE00_UART1_RECEIVE_DIVISOR;
SO0 |= _0004_SAU_CH2_DATA_OUTPUT_1;
SOL0 |= _0000_SAU_CHANNEL2_NORMAL; /* output level normal */
SOE0 |= _0004_SAU_CH2_OUTPUT_ENABLE; /* enable UART1 output */
/* Set RxD1 pin */
PM0 |= 0x02U;
/* Set TxD1 pin */
P0 |= 0x01U;
PM0 &= 0xFEU;
}
/* Set RxD1 pin */
PM0 |= 0x02U;
/* Set TxD1 pin */
P0 |= 0x01U;
PM0 &= 0xFEU;
/***********************************************************************************************************************
* Function Name: R_UART2_Create
* Description : This function initializes the UART2 module.
* Arguments : None
* Return Value : None
***********************************************************************************************************************/
void R_UART2_Create(void)
{
ST1 |= _0002_SAU_CH1_STOP_TRG_ON | _0001_SAU_CH0_STOP_TRG_ON; /* disable UART2 receive and transmit */
STMK2 = 1U; /* disable INTST2 interrupt */
STIF2 = 0U; /* clear INTST2 interrupt flag */
SRMK2 = 1U; /* disable INTSR2 interrupt */
SRIF2 = 0U; /* clear INTSR2 interrupt flag */
SREMK2 = 1U; /* disable INTSRE2 interrupt */
SREIF2 = 0U; /* clear INTSRE2 interrupt flag */
/* Set INTST2 low priority */
STPR12 = 1U;
STPR02 = 1U;
/* Set INTSR2 low priority */
SRPR12 = 1U;
SRPR02 = 1U;
SMR10 = _0020_SAU_SMRMN_INITIALVALUE | _0000_SAU_CLOCK_SELECT_CK00 | _0000_SAU_TRIGGER_SOFTWARE |
_0002_SAU_MODE_UART | _0000_SAU_TRANSFER_END;
SCR10 = _8000_SAU_TRANSMISSION | _0000_SAU_INTSRE_MASK | _0000_SAU_PARITY_NONE | _0080_SAU_LSB | _0010_SAU_STOP_1 |
_0007_SAU_LENGTH_8;
SDR10 = _CE00_UART2_TRANSMIT_DIVISOR;
NFEN0 |= _10_SAU_RXD2_FILTER_ON;
SIR11 = _0004_SAU_SIRMN_FECTMN | _0002_SAU_SIRMN_PECTMN | _0001_SAU_SIRMN_OVCTMN; /* clear error flag */
SMR11 = _0020_SAU_SMRMN_INITIALVALUE | _0000_SAU_CLOCK_SELECT_CK00 | _0100_SAU_TRIGGER_RXD | _0000_SAU_EDGE_FALL |
_0002_SAU_MODE_UART | _0000_SAU_TRANSFER_END;
SCR11 = _4000_SAU_RECEPTION | _0000_SAU_INTSRE_MASK | _0000_SAU_PARITY_NONE | _0080_SAU_LSB | _0010_SAU_STOP_1 |
_0007_SAU_LENGTH_8;
SDR11 = _CE00_UART2_RECEIVE_DIVISOR;
SO1 |= _0001_SAU_CH0_DATA_OUTPUT_1;
SOL1 |= _0000_SAU_CHANNEL0_NORMAL; /* output level normal */
SOE1 |= _0001_SAU_CH0_OUTPUT_ENABLE; /* enable UART2 output */
/* Set RxD2 pin */
PM1 |= 0x10U;
/* Set TxD2 pin */
P1 |= 0x08U;
PM1 &= 0xF7U;
}
/* Set RxD2 pin */
PM1 |= 0x10U;
/* Set TxD2 pin */
P1 |= 0x08U;
PM1 &= 0xF7U;
波特率
根据《xxxx通信协议1》:
- 波特率:300BPS
- 起始位:1BIT (低电平)
- 数据位:8BIT(通讯数据字节)
- 奇偶校验: 无
- 结束位:1BIT (高电平)
UART
通过串行数据发送(TxD)和串行数据接收(RxD)共2 条线进行异步通信的功能。使用这2 条通信线,按数据帧(由起始位、数据、奇偶校验位和停止位构成)与其他通信方进行异步(使用内部波特率)的数据发送和接收。能通过使用发送专用(偶数通道)和接收专用(奇数通道)共2 个通道来实现全双工UART 通信,而且还能通过组合定时器阵列单元和外部中断(INTP0)来支持LIN-bus。
UART 发送
UART 发送是RL78 微控制器将数据异步发送到其他设备的运行。
UART 使用的2 个通道中的偶数通道用于UART 发送。
UART 接收
UART 接收是RL78 微控制器从其他设备异步接收数据的运行。
UART使用的2 个通道中的奇数通道用于UART 接收。但是,需要设定奇数通道和偶数通道的SMR寄存器。
CSI
One form of serial communications is the Clocked Serial Interface (CSI). CSI communications operate in full duplex mode, using one wire for transmission and one wire for reception. A third wire in CSI communications provides the clock for communication. CSI is therefore synchronous.
UART
Another form of serial communications is the Universal Asynchronous Receiver/Transmitter, or UART. UARTs typically provide both a transmit line and a receive line, allowing full duplex communication. A UART may operate in half duplex mode3 if the receive and transmit lines share a bus. A UART most often transmits data in 8-bit (one byte) characters at a time, but normally can be configured to do more or less. In the digital domain, a transmitter keeps its output line high until the beginning of data communication. Once data communication begins, a start bit4 is first sent, immediately followed by all eight bits of the byte, with the least significant bit first, and then normally one stop bit5. The next byte’s start bit can be sent immediately. Depending on the specific settings, the most significant byte may be sent first, one or two stop bits may be used, and to verify the integrity of sent data, parity may also be added. Because start and stop bits, and sometimes a parity bit, must be added to the communication of data, data speeds in serial communications are normally measured in symbols per second rather than bits per second.A symbol is any bit in communication that may represent start or stop of transmission, as well as any bit that is part of the transmitted data.
The term baud rate6 refers to symbols per second, and in order for UART communication to successfully take place, the baud rates of both devices communicating must be equal. It is therefore important when programming a microcontroller to know the baud rates of peripherals using UART communication so as to be able to configure the UART correctly.
Likewise, it is also important to properly configure how many data bits are sent in each frame (7, 8, or 9), how many stop bits are used (either 1 or 2), whether or not there will be even parity, odd parity, or no parity, etc. Having all of these aspects of UART communication pre-established allows for simple communication to occur over two wires.
I2C
A third popular form of communications is called the Inter-Integrated Circuit (I2C) protocol. Unlike the two former protocols, I2C operates only in half duplex mode, and uses only two wires. For this reason, I2C is also commonly referred to as a two-wire interface. I2C is used most frequently for local communication with peripherals within one embedded system. I2C is synchronous like CSI—a clock signal is sent along with a data signal. Only one device can be a master at a time, as it will generate the clock signal. The second wire in I2C provides data and addressing information, and is used for both reception and transmission (since I2C provides half duplex mode operation).
SNOOZE
以下通道的UART 接收支持SNOOZE 模式。SNOOZE 模式功能是指:如果在STOP 模式的状态下检测到RxD 的输入,就不需要CPU 运行而接收数据。只能对支持接收时的波特率调整功能的以下UART 进行设定。
• 20~64引脚产品:UART0
• 80~128引脚产品:UART0、UART2
UART2 (单元1 的通道0 和通道1)支持LIN-bus (只限于30 ~ 128 引脚的产品)。
LIN-bus 功能
• 唤醒信号的检测
• 间隔场(BF)的检测
• 同步场的测量、波特率的计算
使用外部中断(INTP0)和定时器阵列单元。
外围允许寄存器PER0
void R_SAU0_Create(void)
{
SAU0EN = 1U; /* supply SAU0 clock */
NOP();
NOP();
NOP();
NOP();
SPS0 = _0009_SAU_CK00_FCLK_9 | _0090_SAU_CK01_FCLK_9;
R_UART1_Create();
R_SAU0_Create_UserInit();
}
In order to use a SAU it must be provided with a clock signal by setting the
SAUmEN bit in PER0 to 1.
PER0 寄存器是设定允许或者禁止给各外围硬件提供时钟的寄存器。例如:
SAU0EN = 1U; /* supply SAU0 clock */
SAU1EN = 1U; /* supply SAU1 clock */
串行时钟选择寄存器
SPSm 寄存器是16 位寄存器,选择提供给各通道的2种公共运行时钟(CKm0、CKm1)。通过SPSm 寄存器的bit7 ~ 4 选择CKm1,通过bit3 ~ 0 选择CKm0。