完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
我一直在努力研究如何使STM8S充当I2C从设备,说实话我失败了。我想我已根据参考手册设置了所有内容但由于某种原因我没有得到预期的中断被解雇。
我有一个Netduino设置作为I2C主设备。逻辑分析仪和示波器都显示写入和地址0x02以及在I2C总线上生成NAK。 我期待STM8S生成ACK和中断以请求数据或发送数据。至少我期待产生一个中断。 我知道中断处理程序正在连接,因为当我重新编译Netduino代码并重新部署时,我得到一个错误中断。这时它弄乱了I2C总线。 STM8S应用程序代码是: #include< iostm8s103f3.h> #include< intrinsics.h> // // I2C中断都共享同一个处理程序。 // #pragma vector = I2C_RXNE_vector __interrupt void I2C_IRQHandler() { if(I2C_SR1_RXNE) { unsigned char数据; data = I2C_DR; } if(I2C_SR1_TXE) { I2C_DR = 0xaa; //要发送的一些虚拟数据。 } } // //设置系统时钟。 // void InitialiseSystemClock() { CLK_ICKR = 0; //重置内部时钟寄存器。 CLK_ICKR_HSIEN = 1; //启用HSI。 CLK_ECKR = 0; //禁用外部时钟。 while(CLK_ICKR_HSIRDY == 0); //等待HSI准备好使用。 CLK_CKDIVR = 0; //确保时钟全速运行。 CLK_PCKENR1 = 0xff; //启用所有外设时钟。 CLK_PCKENR2 = 0xff; //同上 CLK_CCOR = 0; //关闭CCO。 CLK_HSITRIMR = 0; //关闭任何HSIU修剪。 CLK_SWIMCCR = 0; //将SWIM设置为以时钟/ 2运行。 CLK_SWR = 0xe1; //使用HSI作为时钟源。 CLK_SWCR = 0; //复位时钟切换控制寄存器。 CLK_SWCR_SWEN = 1; //启用切换。 while(CLK_SWCR_SWBSY!= 0); //时钟开关忙时暂停 } // //设置I2C。 // void InitialiseI2C() { I2C_CR1_PE = 0; //配置开始前的Diable I2C。 // //设置时钟信息。 // I2C_FREQR = 16; //设置内部时钟频率(MHz)。 I2C_CCRH_F_S = 0; // I2C运行是标准模式。 I2C_CCRL = 0xa0; // SCL时钟速度为50 KHz。 I2C_CCRH_CCR = 0x00; // //设置此设备的地址。 // I2C_OARL_ADD = 0x02; //七位地址 I2C_CR1_ENGC = 1; //启用通用呼叫地址。 I2C_OARH_ADDMODE = 0; // 7位地址模式 I2C_OARH_ADDCONF = 1; //文件说这必须始终为1。 // //设置总线特性。 // I2C_CR2_ACK = 1; I2C_TRISER = 17; // //打开中断。 // I2C_ITR_ITBUFEN = 1; //启用缓冲中断。 I2C_ITR_ITEVTEN = 1; //启用事件中断 I2C_ITR_ITERREN = 1; // //配置完成,打开外围设备。 // I2C_CR1_PE = 1; } int main() { __disable_interrupt(); InitialiseSystemClock(); InitialiseI2C(); __enable_interrupt(); 而(1) { __wait_for_interrupt(); } } 谁能看到我哪里出错了? 问候, Markl 以上来自于谷歌翻译 以下为原文 I've been trying to work out how to make the STM8S act as an I2C slave and to be honest I'm failing. I think I've set everything up according to the reference manual but for some reason I'm not getting the expected interrupts being fired. I have a Netduino setup as an I2C master. The logic analyser and the scope both show a write and an address of 0x02 and a NAK being generated on the I2C bus. I was expecting the STM8S to generate an ACK and an interrupt to request data or to send data. At the very least I was expecting an interrupt to be generated. I know the interrupt handler is being wired up as I get an error interrupt when I recompile the Netduino code and it redeploys. At this time it messes up the I2C bus. The STM8S application code is: #include #include // // I2C interrupts all share the same handler. // #pragma vector = I2C_RXNE_vector __interrupt void I2C_IRQHandler() { if (I2C_SR1_RXNE) { unsigned char data; data = I2C_DR; } if (I2C_SR1_TXE) { I2C_DR = 0xaa; // Some dummy data to send. } } // // Setup the system clock. // void InitialiseSystemClock() { CLK_ICKR = 0; // Reset the Internal Clock Register. CLK_ICKR_HSIEN = 1; // Enable the HSI. CLK_ECKR = 0; // Disable the external clock. while (CLK_ICKR_HSIRDY == 0); // Wait for the HSI to be ready for use. CLK_CKDIVR = 0; // Ensure the clocks are running at full speed. CLK_PCKENR1 = 0xff; // Enable all peripheral clocks. CLK_PCKENR2 = 0xff; // Ditto. CLK_CCOR = 0; // Turn off CCO. CLK_HSITRIMR = 0; // Turn off any HSIU trimming. CLK_SWIMCCR = 0; // Set SWIM to run at clock / 2. CLK_SWR = 0xe1; // Use HSI as the clock source. CLK_SWCR = 0; // Reset the clock switch control register. CLK_SWCR_SWEN = 1; // Enable switching. while (CLK_SWCR_SWBSY != 0); // Pause while the clock switch is busy. } // // Setup I2C. // void InitialiseI2C() { I2C_CR1_PE = 0; // Diable I2C before configuration starts. // // Setup the clock information. // I2C_FREQR = 16; // Set the internal clock frequency (MHz). I2C_CCRH_F_S = 0; // I2C running is standard mode. I2C_CCRL = 0xa0; // SCL clock speed is 50 KHz. I2C_CCRH_CCR = 0x00; // // Set the address of this device. // I2C_OARL_ADD = 0x02; // Seven bit address. I2C_CR1_ENGC = 1; // Enable the general call address. I2C_OARH_ADDMODE = 0; // 7 bit address mode. I2C_OARH_ADDCONF = 1; // Docs say this must always be 1. // // Setup the bus characteristics. // I2C_CR2_ACK = 1; I2C_TRISER = 17; // // Turn on the interrupts. // I2C_ITR_ITBUFEN = 1; // Buffer interrupt enabled. I2C_ITR_ITEVTEN = 1; // Event interrupt enabled. I2C_ITR_ITERREN = 1; // // Configuration complete so turn the peripheral on. // I2C_CR1_PE = 1; } int main() { __disable_interrupt(); InitialiseSystemClock(); InitialiseI2C(); __enable_interrupt(); while (1) { __wait_for_interrupt(); } } Can anyone see where I'm going wrong? Regards, Markl |
|
相关推荐
3个回答
|
|
我快速浏览了一下数据表,看来当PE关闭时,ACK位被强制为低,所以你可能需要提高它。
除此之外,我建议您查看应用程序库中的参考实现和示例代码。 以上来自于谷歌翻译 以下为原文 I took a quick peek at the datasheet and it seems the ACK bit is forced low while PE is off, so you may need to raise it late. Beyond that I'd suggest taking a peek at the reference implementation and example code in the application libraries. |
|
|
|
感谢您抽出宝贵时间查看代码。我会放手一搏。
我确定我的时钟设置正常,因为我已经将I2C置于主模式,我可以通过运行在50KHz的时钟从系统中获取一个地址。 我把手册看作“在启用外围设备之前设置所有内容”,然后看看我是如何让主人工作的,我认为你会做对的。我需要从总线控制中拆分外围设备。 问候, 标记 以上来自于谷歌翻译 以下为原文 Thanks for taking the time to look at the code. I'll give that a go. I'm sure I've got the clock setup OK as I've put the I2C into master mode and I can get an address out of the system with a clock running at 50KHz. I read the manual as ''set everything up before enabling the peripheral'' and looking at how I got the master working I think you are going to be right. I need to split the set up of the peripheral from the bus control. Regards, Mark |
|
|
|
约翰当场了。我将语句I2C_CR2_ACK = 1移动到外设使能的位置(即在I2C_CR1_PE = 1之后),我现在有一个工作的从接收器。
感谢Johan的帮助 - 非常感谢。 问候, 标记 以上来自于谷歌翻译 以下为原文 Johan was spot on. I moved the statement I2C_CR2_ACK = 1 to a point where the peripheral was enabled (i.e. after I2C_CR1_PE = 1) and I now have a working slave receiver. Thanks for the help Johan - greatly appreciated. Regards, Mark |
|
|
|
只有小组成员才能发言,加入小组>>
请教:在使用UDE STK时,单片机使用SPC560D30L1,在配置文件怎么设置或选择?里面只有SPC560D40的选项
2617 浏览 1 评论
3203 浏览 1 评论
请问是否有通过UART连接的两个微处理器之间实现双向值交换的方法?
1776 浏览 1 评论
3602 浏览 6 评论
5981 浏览 21 评论
931浏览 4评论
1308浏览 4评论
在Linux上安装Atollic TRUEStudio的步骤有哪些呢?
576浏览 3评论
使用DMA激活某些外设会以导致外设无法工作的方式生成代码是怎么回事
1296浏览 3评论
1350浏览 3评论
小黑屋| 手机版| Archiver| 电子发烧友 ( 湘ICP备2023018690号 )
GMT+8, 2024-11-19 11:41 , Processed in 1.145879 second(s), Total 80, Slave 64 queries .
Powered by 电子发烧友网
© 2015 bbs.elecfans.com
关注我们的微信
下载发烧友APP
电子发烧友观察
版权所有 © 湖南华秋数字科技有限公司
电子发烧友 (电路图) 湘公网安备 43011202000918 号 电信与信息服务业务经营许可证:合字B2-20210191 工商网监 湘ICP备2023018690号