我想实现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。 |
RE:stm8 硬件IIC 双机通信 求解 谢谢
RE:stm8 硬件IIC 双机通信 求解 谢谢
cccccccccccccccccccccc
回复:stm8 硬件IIC 双机通信 求解 谢谢