你的浏览器版本过低,可能导致网站不能正常访问!
为了你能正常使用网站功能,请使用这些浏览器。

stm8 硬件IIC 双机通信 求解 谢谢

[复制链接]
johnhechang 提问时间:2014-9-9 20:56 /
我想实现stm8  硬件IIC  双机通信  主机已经测试过可以读写24C02,但是改为双机通信时(主机主模式写从机从模式读) ,从机用中断,无法实现。ST-LINK仿真发现主机停在while(!(I2C_SR1&0x02)); 从机没有进入中断 ,请牛人看看是什么问题,非常感谢。
主机程序 
 #include"stm8s103f3p.h"
void CLK_Init(void)  //时钟为16M
{
        CLK_CKDIVR = 0x00;
}
void IIC_Init(void)//IIC初始化设置 100KHZ
{
        I2C_CR1    = 0x00; //禁止I2C外设
        I2C_FREQR = 0x0a;  
        I2C_CCRH = 0x00;  
        I2C_CCRL = 0x32;  
        I2C_TRISER = 0x04;  
        I2C_CR2 |=0x04;  
        I2C_CR1 |=0x01
}
void IW_24C02(unsigned char x)  //IIC主模式写一个字节
{
        unsigned char temp;
        //I2C_CR2 &=~ 0x04;
        while(I2C_SR3 & 0x02); //总线空闲
        I2C_CR2 |= 0x01; //产生起始位
        while(!(I2C_SR1 & 0x01)); //起始位发送完成
        I2C_DR = 0xa0; 
        while(!(I2C_SR1&0x02));
        temp = I2C_SR1;
        temp = I2C_SR3;
        I2C_DR = 0x00;
        while(!(I2C_SR1 & 0x84)); 
        I2C_DR = x;
        while(!(I2C_SR1 & 0x84)); 
        I2C_CR2 |= 0x02;
//        I2C_CR2 |= 0x04;
}
void delay (unsigned int x)//延时
{
        unsigned int i;
        for(i=x;i>0;i--);
}
main()
{
        unsigned char dirflag,k,h,u;
        CLK_Init();
        GPIO_Init();
        IIC_Init();
        while(1)
        {
                IW_24C02(3); 
          delay(200000);//延时很重要
        }
 
}
以上 主机可以读写24C02
从机 程序如下;
#include"stm8s103f3p.h"
void CLK_Init(void)  //时钟为16M
{
        CLK_CKDIVR = 0x00;
}
void IIC_Init(void)//IIC初始化设置 100KHZ
{
        I2C_CR1    = 0x00; //禁止I2C外设
        I2C_FREQR = 0x0a;  
        I2C_CCRL = 0x32; 
        I2C_CCRH = 0x00;        
        I2C_TRISER = 0x04;  
 
        I2C_OARL   = 0xa0;                  //自身地址
        I2C_OARH   = 0x40;
 
        I2C_CR2 |=0x04;  
        I2C_CR1 |=0x01; 
       
        I2C_ITR=0x06;//开IIC中断
       
 
 
 
}
void delay (unsigned int x)
{
        unsigned int i;
        for(i=x;i>0;i--);
}
main()
{
        unsigned char dirflag,k,h,u;
        _asm("sim");
        CLK_Init();
        GPIO_Init();
        IIC_Init();
        _asm("rim");
       
  while(1)
{
 
}
       
}       
@far @interrupt void iic_irq(void)
 
{
                while(!(I2C_SR1 & 0x02)); 
                temp1 = I2C_SR1;
                temp2 = I2C_SR3;
                temp= I2C_DR ;
                while(!(I2C_SR1 & 0x84)); 
                 I2C_CR2 |= 0x02;
                //dispp();
 
}
仿真一直没有进入中断,
下面是stm8_interrupt_vector的修改;
/*        BASIC INTERRUPT VECTOR TABLE FOR STM8 devices
 *        Copyright (c) 2007 STMicroelectronics
 */
 
typedef void @far (*interrupt_handler_t)(void);
 
struct interrupt_vector {
        unsigned char interrupt_instruction;
        interrupt_handler_t interrupt_handler;
};
 
@far @interrupt void NonHandledInterrupt (void)
{
        /* in order to detect unexpected events during development, 
           it is recommended to set a breakpoint on the following instruction
        */
        return;
}
 
extern void _stext();     /* startup routine */
@far @interrupt void iic_irq(void);
struct interrupt_vector const _vectab[] = {
        {0x82, (interrupt_handler_t)_stext}, /* reset */
        {0x82, NonHandledInterrupt}, /* trap  */
        {0x82, NonHandledInterrupt}, /* irq0  */
        {0x82, NonHandledInterrupt}, /* irq1  */
        {0x82, NonHandledInterrupt}, /* irq2  */
        {0x82, NonHandledInterrupt}, /* irq3  */
        {0x82, NonHandledInterrupt}, /* irq4  */
        {0x82, NonHandledInterrupt}, /* irq5  */
        {0x82, NonHandledInterrupt}, /* irq6  */
        {0x82, NonHandledInterrupt}, /* irq7  */
        {0x82, NonHandledInterrupt}, /* irq8  */
        {0x82, NonHandledInterrupt}, /* irq9  */
        {0x82, NonHandledInterrupt}, /* irq10 */
        {0x82, NonHandledInterrupt}, /* irq11 */
        {0x82, NonHandledInterrupt}, /* irq12 */
        {0x82, NonHandledInterrupt}, /* irq13 */
        {0x82, NonHandledInterrupt}, /* irq14 */
        {0x82, NonHandledInterrupt}, /* irq15 */
        {0x82, NonHandledInterrupt}, /* irq16 */
        {0x82, NonHandledInterrupt}, /* irq17 */
        {0x82, NonHandledInterrupt}, /* irq18 */
        {0x82, iic_irq}, /* irq19 */
        {0x82, NonHandledInterrupt}, /* irq20 */
        {0x82, NonHandledInterrupt}, /* irq21 */
        {0x82, NonHandledInterrupt}, /* irq22 */
        {0x82, NonHandledInterrupt}, /* irq23 */
        {0x82, NonHandledInterrupt}, /* irq24 */
        {0x82, NonHandledInterrupt}, /* irq25 */
        {0x82, NonHandledInterrupt}, /* irq26 */
        {0x82, NonHandledInterrupt}, /* irq27 */
        {0x82, NonHandledInterrupt}, /* irq28 */
        {0x82, NonHandledInterrupt}, /* irq29 */
};

不只是哪里有问题 ,请牛人指导,谢谢  本人刚用STM8。
 
 



 
收藏 1 评论10 发布时间:2014-9-9 20:56

举报

10个回答
johnhechang 回答时间:2014-9-9 21:09:40

RE:stm8 硬件IIC 双机通信 求解 谢谢

求助 ,刚刚加的论坛 ,不熟悉 请谅解。
zh384407950 回答时间:2014-9-10 19:37:37

RE:stm8 硬件IIC 双机通信 求解 谢谢

本帖最后由 zh384407950 于 2016-5-2 17:58 编辑

cccccccccccccccccccccc
百年干啤 回答时间:2014-9-13 09:38:06

回复:stm8 硬件IIC 双机通信 求解 谢谢

kkkkkkkkkkkkkkkkkkkkkkkkkkk
aibaibai 回答时间:2016-2-2 09:17:53
楼主你的问题解决了吗
zcl201207 回答时间:2016-2-2 22:18:23
dream_one 回答时间:2016-11-23 11:39:00
是不是系统时钟不同啊?
peter001 回答时间:2016-11-24 00:28:19
关注一下
STM32F051 回答时间:2016-12-14 00:15:16
STM8双机通讯,失败的可能性极大;我几年前不得不改成模拟IIC端口,就是按IIC的时序,不停写IO端口;听说后续的STM32F0系列,已经重新设计了IIC核;新的核怎么样,我还没测试过;心里有些怕。所以,上来看看
ts2000 回答时间:2016-12-17 09:31:40
模拟的比硬件的好用。。。。。。
12下一页

所属标签

相似问题

关于
我们是谁
投资者关系
意法半导体可持续发展举措
创新与技术
意法半导体官网
联系我们
联系ST分支机构
寻找销售人员和分销渠道
社区
媒体中心
活动与培训
隐私策略
隐私策略
Cookies管理
行使您的权利
官方最新发布
STM32N6 AI生态系统
STM32MCU,MPU高性能GUI
ST ACEPACK电源模块
意法半导体生物传感器
STM32Cube扩展软件包
关注我们
微信公众号二维码 微信公众号
手机版二维码 手机版