ST意法半导体
直播中

殷晓婷

7年用户 141经验值
私信 关注
[问答]

STM8S103F3 UART1中断问题

关于STM8S103F3 UART1中断问题!

以上来自于谷歌翻译


以下为原文



About STM8S103F3 UART1 interrupt issue !

回帖(12)

殷晓婷

2019-5-15 06:43:00
我尝试将STM8S103F3 UART1与PC COM1端口一起使用(19200波特,非奇偶校验
 
8位数据,1个停止位),但有一些数据丢失。
 
void UART_Init(void)
{
 // [2] 310 UART1 INITIALIZATION
 // Step1)[2] 346定义字长(UART_CR1中的M位)
 UART1-> CR1& = ~UART1_CR1_M; // M = 0(8个数据位)
 // Step2)[2] 349定义UART_CR3中的停止位数
 UART1-> CR3& = ~UART1_CR3_STOP; // STOP [1:0] = 00(1个停止位)
 //步骤3)[2] 316选择所需的波特率
 // 1)波特率= 19200
 // Tx / Rx波特率= fMASTER / UART_DIV
 // =(16MHz / 8)/ 19200
 // = 2000 / 19.2
 // = 104.16
 //〜= 104 = 068h
 UART1-> BRR2 = 0x08; // 19200波特
 UART1-> BRR1 = 0x06;
 //步骤4)启用发送器模式(UART_CR2中的TEN位)
 UART1-> CR2 | = UART1_CR2_TEN; //传输启用
} / * UART_Init * /
 
u8 bUART_TxCnt;
u8 * pbU​​ART_Tx; //系统发送指针
 
void UART_TxByte(u8 bByte)
{
 pbUART_Tx =& bByte;
 bUART_TxCnt = 1;
 
 UART1-> CR2 | = UART1_CR2_TIEN;
} / * UART_TxByte * /
 
 
@far @interrupt void UART1_TX_IRQHandler(void)
{
 if(bUART_TxCnt)
 {
 //在设置新数据之前是否需要检查TC位?
 UART1-> DR = * pbU​​ART_Tx ++;
 
 bUART_TxCnt--;
 }
 其他
 UART1-> CR2& = ~UART1_CR2_TIEN;
}
 
void main(void)
{
 u8 bData;
 
 UART_Init();
 
 enableInterrupts();
 
 for(;;)
 {
 for(bData ='0'; bData< ='9'; bData ++)
 UART_TxByte(BDATA);
 } / *用于无限循环* /
} / * main * /
 
PC PComm实用程序丢失了一些数据作为附件。
 
我错过了什么吗?
 
Q1)发送数据寄存器空和传输完成中断事件之间有什么区别?
Q2)在将数据写入UART_DR寄存器之前是否需要检查TC位?
Q3)每个传输的数据是否都会产生TXE和TC中断?
Q4)是否可以仅使用TC位中断进行传输?
 
 
 
 
[此消息由以下内容编辑:jeffrey.chang168于18-12-2009 12:57]
 
 
 
 
 
 
 
[此消息编辑:jeffrey.chang168于18-12-2009 12:58]
 
 
[此消息由以下人员编辑:jeffrey.chang168于18-12-2009 17:02发表]
 
 
[此消息由:jeffrey.chang168于20-12-2009 03:29编辑]

以上来自于谷歌翻译


以下为原文




I tried to use STM8S103F3 UART1 with PC COM1 port (19200 baud, Non-parity

8-bit data, 1 stop bit), but there are some data lost.  

void UART_Init (void)  
{  
// [2]310 UART1 INITIALIZATION  
// Step1) [2]346 To define the word length (M bit in UART_CR1)  
UART1->CR1 &= ~UART1_CR1_M; // M=0 (8 data bits)  
// Step2) [2]349 To define the number of stop bits in UART_CR3  
UART1->CR3 &= ~UART1_CR3_STOP; // STOP[1:0]=00 (1 stop bit)  
// Step3) [2]316 To select the desired baud rate  
// 1) Baud rate = 19200  
// Tx/Rx baud rate = fMASTER / UART_DIV  
// = (16MHz / 8) / 19200  
// = 2000 / 19.2  
// = 104.16  
// ~= 104 = 068h  
UART1->BRR2 = 0x08; // 19200 Baud  
UART1->BRR1 = 0x06;  
// Step4) To enable transmitter mode (TEN bit in UART_CR2)  
UART1->CR2 |= UART1_CR2_TEN; // transmit enable  
} /* UART_Init */  

u8 bUART_TxCnt;  
u8 *pbUART_Tx; // system transmit pointer  

void UART_TxByte (u8 bByte)  
{  
pbUART_Tx = &bByte;  
bUART_TxCnt = 1;  

UART1->CR2 |= UART1_CR2_TIEN;  
} /* UART_TxByte */  


@far @interrupt void UART1_TX_IRQHandler (void)  
{  
if (bUART_TxCnt)  
{  
// Is it necessary to check the TC bit before setup new data ?  
UART1->DR = *pbUART_Tx++;  

bUART_TxCnt--;  
}  
else  
UART1->CR2 &= ~UART1_CR2_TIEN;  
}  

void main (void)  
{  
u8 bData;  

UART_Init();  

enableInterrupts();  

for (;;)  
{  
for (bData = '0'; bData <= '9'; bData++)  
UART_TxByte(bData);  
} /* for endless loop */  
} /* main */  

PC PComm utility lost some data as attachment.  

Did I miss anything ?  

Q1) What's the difference between the transmit data register empty and tramsmission complete interrupt events ?  
Q2) Is it necessary to check the TC bit before writing data to the UART_DR register ?  
Q3) Does every transmitted data generate TXE and TC interrupts ?  
Q4) Is it possible to use TC bit interrupt ONLY for transmission ?  




[ This message was edited by: jeffrey.chang168 on 18-12-2009 12:57 ]  

  





[ This message was edited by: jeffrey.chang168 on 18-12-2009 12:58 ]  


[ This message was edited by: jeffrey.chang168 on 18-12-2009 17:02 ]  


[ This message was edited by: jeffrey.chang168 on 20-12-2009 03:29 ]
举报

李正阳

2019-5-15 06:57:55
嗨jeffrey.chang168,
 
 
对于波特率19200(fMASTER = 16MHz)应该是:
 
UART1-&gt; BRR2 = 0x01; // 19200波特
UART1-&gt; BRR1 = 0x34;
 
问候。
短剑的一种

以上来自于谷歌翻译


以下为原文




Hi jeffrey.chang168,


For the baud rate 19200 (fMASTER=16MHz) should be:  

UART1->BRR2 = 0x01; // 19200 Baud  
UART1->BRR1 = 0x34;  

Regards.  
Kris
举报

殷晓婷

2019-5-15 07:14:45
我使用默认的HSI时钟源(fMASTER = 16MHz / 8)作为UART1时钟源
 
并参考STM8S优化示例中的UART3 57600波特示例。
 
1)波特= 57600
Tx / Rx波特率= fMASTER / UART_DIV
=(16MHz / 8)/ 57600
= 2000 / 57.6
= 34.72
〜= 35 = 0x23
UART1-> BRR2 = 0x03; // 576200波特
UART1-&gt; BRR1 = 0x02;
 
所以它也是丢失的数据。
 
2)Baud = 19200,我试着用你的公式如下。
 
Tx / Rx波特率= fMASTER / UART_DIV
=(16MHz)/ 19200
= 16000 / 19.2
= 833.33
〜= 833 = 0x341
UART1-&gt; BRR2 = 0x01; // 19200波特
UART1-&gt; BRR1 = 0x34;
 
如果BRR1 / BRR2寄存器更改为0x341,PC将接收垃圾数据!
 
 
 
 
[此消息由:jeffrey.chang168于19-12-2009 01:38编辑]
 
 
[此消息由以下人员编辑:jeffrey.chang168于19-12-2009 01:40发表]

以上来自于谷歌翻译


以下为原文




I used default HSI clock source (fMASTER =16MHz/8) as UART1 clock source

and refer to the UART3 57600 baud example in STM8S optimized examples.  

1) Baud = 57600  
Tx/Rx baud rate = fMASTER / UART_DIV  
= (16MHz / 8) / 57600  
= 2000 / 57.6  
= 34.72  
~= 35 = 0x23  
UART1->BRR2 = 0x03; // 576200 Baud  
UART1->BRR1 = 0x02;  

So it is also lost data.  

2) Baud = 19200 and I tried to use your formula as below.  

Tx/Rx baud rate = fMASTER / UART_DIV  
= (16MHz) / 19200  
= 16000 / 19.2  
= 833.33  
~= 833 = 0x341  
UART1->BRR2 = 0x01; // 19200 Baud  
UART1->BRR1 = 0x34;  

PC will receive garbage data if BRR1/BRR2 register are changed to 0x341 !  




[ This message was edited by: jeffrey.chang168 on 19-12-2009 01:38 ]  


[ This message was edited by: jeffrey.chang168 on 19-12-2009 01:40 ]
举报

殷晓婷

2019-5-15 07:26:01
我试图实现UART1单字节传输,但无法完全理解RM0016文档的图110和第311页。
 
 
Q5)软件支持USART:
为什么它会自动生成Tx中断(TXE = 1)?

以上来自于谷歌翻译


以下为原文




I tried to implment UART1 Single byte transmission but can NOT fully understand the Figure 110 and page 311 of the RM0016 document.


Q5) Software enables tne USART:  
Why does it generate an Tx Interrupt (TXE=1) automatically ?
举报

更多回帖

发帖
×
20
完善资料,
赚取积分