打印
[STM8]

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

[复制链接]
281|5
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
两只袜子|  楼主 | 2021-1-5 14:10 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
我想实现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。

使用特权

评论回复
沙发
wowu| | 2021-2-2 12:04 | 只看该作者
单机回环模式可以吗

使用特权

评论回复
板凳
xiaoqizi| | 2021-2-2 12:05 | 只看该作者
确定发送数据了吗

使用特权

评论回复
地板
木木guainv| | 2021-2-2 12:07 | 只看该作者
一直无法进入中断吗

使用特权

评论回复
5
磨砂| | 2021-2-2 12:10 | 只看该作者
加终端电阻了吗

使用特权

评论回复
6
晓伍| | 2021-2-2 12:12 | 只看该作者
这个寄存器意味着什么呢

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

1884

主题

6474

帖子

8

粉丝