本帖最后由 Xflyan 于 2012-4-6 13:38 编辑
最近在STM8S103上调MODBUS RTU,MODBUS的Serial通信字符帧长度为11bit,即无校验(NONE) + STOP_BIT_2 或 奇校验(ODD) + STOP_BIT_1 或 偶校验(EVEN) + STOP_BIT_1;但一个奇怪的问题就是,只要103的字符帧长度为11bit,前面的3选1,PC端不管用哪种方式发送(ODD/EVEN/MARK/SPACE + 1),103居然都能够正确接收并返回,为此我还特意测了一下我PC端的发送序列,奇校验和偶校验时明显是不一样的,103这个情况似乎太强悍了:funk:,能解释下吗?为什么没有103没有提示校验错误?
UART1配置为 测试波特率为9600:
void halUARTInit(u8 baudrate, u8 parity)
{
switch (parity) {
case UART_PARITY_NONE:
UART1->CR3 = 0x20; // STOP_BIT_2
break;
case UART_PARITY_EVEN:
UART1->CR1 = UART1_CR1_PCEN | UART1_CR1_M;
break;
case UART_PARITY_ODD:
UART1->CR1 = UART1_CR1_PCEN | UART1_CR1_PS | UART1_CR1_M;
break;
default:
UART1->CR1 = UART1_CR1_PCEN | UART1_CR1_M;
break;
}
switch (baudrate) {
case UART_BR_115200:
UART1->BRR2 = 0x0B; // 12MHz 115200:BRR=0x0068;
UART1->BRR1 = 0x08; // 16MHz 115200:BRR=0x008B
break;
case UART_BR_57600:
UART1->BRR2 = 0x06; // 12MHz 57600:BRR=0x00D0;
UART1->BRR1 = 0x11; // 16MHz 57600:BRR=0x0116
break;
case UART_BR_38400:
UART1->BRR2 = 0x01; // 12MHz 38400:BRR=0x0139;
UART1->BRR1 = 0x1A; // 16MHz 38400:BRR=0x01A1
break;
case UART_BR_19200:
UART1->BRR2 = 0x01; // 12MHz 19200:BRR=0x0271;
UART1->BRR1 = 0x34; // 16MHz 19200:BRR=0x0341
break;
case UART_BR_9600:
UART1->BRR2 = 0x03; // 12MHz 9600:BRR=0x04E2;
UART1->BRR1 = 0x68; // 16MHz 9600:BRR=0x0683
break;
}
UART1->CR2 |= UART1_CR2_TEN | UART1_CR2_REN;
UART1->CR2 |= UART1_CR2_RIEN; // RX Interrupt Enable
}
|