完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
我使用STM8L051F3芯片作为从机,STM32L476作为主机,但STM8L拒绝响应,它一直报告SR2-> BERR即忙碌错误。
我知道STM32代码没问题,因为我有两个代码在没有任何问题的情况下工作在I2C链接上,但是当我使用STM8作为从属时它失败了。 时钟和数据线在示波器上看起来不错,漂亮和正方形。我比较了32-32和32-8的信号,在32-8中'ACK'脉冲保持高电平而不是拉低。 STM8L代码粘贴在下面 --- I2C设置:--- void Init_I2C(void) { CLK-> CKDIVR = 0; // sys clock / 1 CLK-&gt; PCKENR1 | = 0x08; //启用I2C外设clk //初始化GPIO以供I2C使用 GPIOC-> DDR | = 0x03; //输出模式 GPIOC-> CR1&amp; = 0xFC; //带开漏的输出 GPIOC-> CR2 | = 0x03; //快// --- PE之前设定= 1 --- I2C1-&gt; TRISER = 2; //设置时钟控制寄存器(CCR) I2C1-> CCRL = 0x50; //提供100kHz的输出时钟...... I2C1-> CCRH =(u8)0; // ...占空比为50% //如果I2C1参数未激活,则无法更改它! I2C1-> CR1 = 0x01; //启用I2C外设 I2C1-> CR2 = 0x04; //启用I2C确认 I2C1-> FREQR = 16; //设置I2C频率值(16MHz) I2C1-> OARL =(SLAVE_ADDRESS&lt;&lt; 1); //设置从站地址(将寄存器的0xA2加到7位地址) I2C1-> OARH = 0x40; //设置7bit地址模式 I2C1-> ITR = 0x07; //所有I2C中断使能 } - - 打断 - - @far @interrupt void I2C_Slave_check_event(void) { 静态u8 sr1; 静态u8 sr2; 静态u8 sr3; //保存I2C寄存器配置 sr1 = I2C1-&gt; SR1; sr2 = I2C1-&gt; SR2; sr3 = I2C1-&gt; SR3; // 通信故障? if(sr2&amp; I2C_SR2_WUFH) { I2C1-&gt; CR2 | = I2C_CR2_STOP; //停止沟通 - 释放线条 I2C1-> SR2 = 0; //清除所有错误标志 } 否则如果(sr2&amp; I2C_SR2_OVR) { I2C1-&gt; CR2 | = I2C_CR2_STOP; //停止沟通 - 释放线条 I2C1-> SR2 = 0; //清除所有错误标志 } 否则如果(sr2&amp; I2C_SR2_BERR) {// ----总是得到这个标志---- I2C1-&gt; CR2 | = I2C_CR2_STOP; //停止沟通 - 释放线条 I2C1-> SR2 = 0; //清除所有错误标志 } //收到更多字节? if((sr1&amp;(I2C_SR1_RXNE | I2C_SR1_BTF))==(I2C_SR1_RXNE | I2C_SR1_BTF)) { I2C_byte_received(I2C1-&GT; DR); } //字节收到? if(sr1&amp; I2C_SR1_RXNE) { I2C_byte_received(I2C1-&GT; DR); } // NAK? (=从发送通信结束) if(sr2&amp; I2C_SR2_AF) { I2C1-&gt; SR2&amp; = ~I2C_SR2_AF; //清除自动对焦 I2C_transaction_end(); } //从Master停止位(=从端接收comm的结束) if(sr1&amp; I2C_SR1_STOPF) { I2C1-&gt; CR2 | = I2C_CR2_ACK; // CR2写入清除STOPF I2C_transaction_end(); } //从站地址匹配(=启动Comm) if(sr1&amp; I2C_SR1_ADDR) { I2C_transaction_begin(); } //要传输更多字节? if((sr1&amp;(I2C_SR1_TXE | I2C_SR1_BTF))==(I2C_SR1_TXE | I2C_SR1_BTF)) { I2C1-&gt; DR = I2C_byte_write(); } //字节传输? if(sr1&amp; I2C_SR1_TXE) { I2C1-&gt; DR = I2C_byte_write(); } } #stm8l051f3#i2c-busy 以上来自于谷歌翻译 以下为原文 I'm using an STM8L051F3 chip as slave and a STM32L476 as master, but the STM8L refuses to respond, it keeps reporting a SR2->BERR aka busy error. I know the STM32 code is ok as I had two of them working on an I2C link without any problems, but when I use an STM8 as a slave it fails. The clock and data lines look ok on a scope, nice and square. I have compared the signals from 32-32 and 32-8, in the 32-8 the 'ACK' pulse remains high instead of being pulled low. STM8L code pasted below --- I2C setup : --- void Init_I2C(void) { CLK->CKDIVR = 0; // sys clock /1 CLK->PCKENR1 |= 0x08; // enable I2C peripheral clk// Init GPIO for I2C use GPIOC->DDR |= 0x03; // Output mode GPIOC->CR1 &= 0xFC; // Output with Open drain GPIOC->CR2 |= 0x03; // Fast// --- SET BEFORE PE = 1 --- I2C1->TRISER = 2; // Set the clock control register (CCR) I2C1->CCRL = 0x50; // Provides an output clock of 100kHz... I2C1->CCRH = (u8)0; // ...at 50% duty cycle // Cannot change I2C1 parameters if it is not active ! I2C1->CR1 = 0x01; // Enable I2C peripheral I2C1->CR2 = 0x04; // Enable I2C acknowledgement I2C1->FREQR = 16; // Set I2C Freq value (16MHz) I2C1->OARL = (SLAVE_ADDRESS << 1) ; // set slave address (put 0xA2 for the register dues to7bit address) I2C1->OARH = 0x40; // Set 7bit address mode I2C1->ITR = 0x07; // all I2C interrupt enable }--- Interrupt --- @far @interrupt void I2C_Slave_check_event(void) { static u8 sr1; static u8 sr2; static u8 sr3; // save the I2C registers configuration sr1 = I2C1->SR1; sr2 = I2C1->SR2; sr3 = I2C1->SR3; // Communication error? if (sr2 & I2C_SR2_WUFH) { I2C1->CR2 |= I2C_CR2_STOP; // stop communication - release the lines I2C1->SR2 = 0; // clear all error flags } else if (sr2 & I2C_SR2_OVR) { I2C1->CR2 |= I2C_CR2_STOP; // stop communication - release the lines I2C1->SR2 = 0; // clear all error flags } else if (sr2 & I2C_SR2_BERR) {// ---- ALWAYS GET THIS FLAG SET ---- I2C1->CR2 |= I2C_CR2_STOP; // stop communication - release the lines I2C1->SR2 = 0; // clear all error flags } // More bytes received ? if ((sr1 & (I2C_SR1_RXNE | I2C_SR1_BTF)) == (I2C_SR1_RXNE | I2C_SR1_BTF)) { I2C_byte_received(I2C1->DR); } // Byte received ? if (sr1 & I2C_SR1_RXNE) { I2C_byte_received(I2C1->DR); } // NAK? (=end of slave transmit comm) if (sr2 & I2C_SR2_AF) { I2C1->SR2 &= ~I2C_SR2_AF; // clear AF I2C_transaction_end(); } // Stop bit from Master (= end of slave receive comm) if (sr1 & I2C_SR1_STOPF) { I2C1->CR2 |= I2C_CR2_ACK; // CR2 write to clear STOPF I2C_transaction_end(); } // Slave address matched (= Start Comm) if (sr1 & I2C_SR1_ADDR) { I2C_transaction_begin(); } // More bytes to transmit ? if ((sr1 & (I2C_SR1_TXE | I2C_SR1_BTF)) == (I2C_SR1_TXE | I2C_SR1_BTF)) { I2C1->DR = I2C_byte_write(); } // Byte to transmit ? if (sr1 & I2C_SR1_TXE) { I2C1->DR = I2C_byte_write(); } } #stm8l051f3 #i2c-busy |
|
相关推荐
1个回答
|
|
对不起,从我的Feed中撞出旧的僵尸未答复的线程
以上来自于谷歌翻译 以下为原文 Sorry, bumping old zombie unanswered threads off my feed |
|
|
|
只有小组成员才能发言,加入小组>>
请教:在使用UDE STK时,单片机使用SPC560D30L1,在配置文件怎么设置或选择?里面只有SPC560D40的选项
2798 浏览 1 评论
3258 浏览 1 评论
请问是否有通过UART连接的两个微处理器之间实现双向值交换的方法?
1832 浏览 1 评论
3682 浏览 6 评论
6079 浏览 21 评论
对H747I-DISCO写程序时将CN2的st-link复用为usart1,再次烧录时无法检测到stlink怎么解决?
384浏览 2评论
STM32G474RE芯片只是串口发个数据就发烫严重是怎么回事?
473浏览 2评论
STM32处理增量式编码器Z信号如何判断中断是正转的还是反向转的?
303浏览 2评论
使用STM32F407VET6的USB2.0功能,发现ctl后芯片无数据返回,是什么原因可能导致的呢?
222浏览 2评论
用STM32G431开发工程,1.50版本的Package包文件无法输出项目工程
328浏览 2评论
小黑屋| 手机版| Archiver| 电子发烧友 ( 湘ICP备2023018690号 )
GMT+8, 2025-1-24 01:42 , Processed in 1.062360 second(s), Total 75, Slave 59 queries .
Powered by 电子发烧友网
© 2015 bbs.elecfans.com
关注我们的微信
下载发烧友APP
电子发烧友观察
版权所有 © 湖南华秋数字科技有限公司
电子发烧友 (电路图) 湘公网安备 43011202000918 号 电信与信息服务业务经营许可证:合字B2-20210191 工商网监 湘ICP备2023018690号