UART通信

电路图

室内通信电路 SI1,TxD1,RxD1;室外通信电路 SI2,TxD2,RxD2





生成代码

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。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 204,684评论 6 478
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 87,143评论 2 381
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 151,214评论 0 337
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,788评论 1 277
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,796评论 5 368
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,665评论 1 281
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 38,027评论 3 399
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,679评论 0 258
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 41,346评论 1 299
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,664评论 2 321
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,766评论 1 331
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,412评论 4 321
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 39,015评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,974评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,203评论 1 260
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 45,073评论 2 350
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,501评论 2 343

推荐阅读更多精彩内容