[STM8] STM8S105k4 的I2C问题,没反应呀,读不到数据

[复制链接]
1384|9
 楼主| zgyhc2050 发表于 2015-8-21 10:01 | 显示全部楼层 |阅读模式
#include "main.h"



void I2C_Start(void);
void I2C_Stop(void);
void I2C_ACK(u8 ack);
u8 I2C_Send_Byte(u8 send_data);
u8 I2C_Receive_Byte(void);




//16Mhz 的时钟
void Calendar_Init(void)
{
        SCL_OUT();
        SDA_OUT();
}

void Calendar_Write(u8 addr, u8 calendar_data)
{
         I2C_Start();
         I2C_Send_Byte(0xa0);
         I2C_Send_Byte(addr);
         I2C_Send_Byte(calendar_data);
         I2C_Stop();
}



u8 Calendar_Read(u8 addr)
{
         u8 r_data;
        I2C_Start();
        I2C_Send_Byte(0xa0);
        I2C_Send_Byte(addr);
       
        I2C_Start();
        I2C_Send_Byte(0xa1);
        r_data =  I2C_Receive_Byte();
        I2C_ACK(1);
        I2C_Stop();
        return r_data;
}


void I2C_Start(void)
{
        GPIOC->ODR |= 0x40;                //SDA=1
        GPIOD->ODR |= 0x08;                //SCL=1       
        Delay_us(5);
        GPIOC->ODR &= ~0x40;                //SDA=0       
        Delay_us(5);       
        GPIOD->ODR &= ~0x08;                //SCL=0       
}


void I2C_Stop(void)
{
        GPIOC->ODR &= ~0x40;                //SDA=0       
        GPIOD->ODR |= 0x08;                //SCL=1
        Delay_us(5);       
        GPIOC->ODR |= 0x40;                //SDA=1       
        Delay_us(5);               
}


void I2C_ACK(u8 ack)
{
        if(ack)
                GPIOC->ODR |= 0x40;                //SDA=1       
        else
                GPIOC->ODR &= ~0x40;                //SDA=0
       
        GPIOD->ODR |= 0x08;                //SCL=1       
        Delay_us(5);       
        GPIOD->ODR &= ~0x08;                //SCL=0
        Delay_us(5);       
}


u8 I2C_Send_Byte(u8 send_data)
{
        u8 i,ack;
        for(i=0;i<8;i++)
        {
                GPIOD->ODR &= ~0x08;                //SCL=0
                Delay_us(5);       
                if(send_data & 0x80)
                        GPIOC->ODR |= 0x40;                //SDA=1               
                else
                        GPIOC->ODR &= ~0x40;                //SDA=0
                send_data <<=1;
                GPIOD->ODR |= 0x08;                //SCL=1       
                Delay_us(5);       
        }
        GPIOD->ODR &= ~0x08;                //SCL=0
        Delay_us(5);
        SDA_IN();
        GPIOD->ODR |= 0x08;                //SCL=1       
        Delay_us(1);               
        ack = (GPIOC->IDR & 0x40)>>6;
        Delay_us(5);
        GPIOD->ODR &= ~0x08;                //SCL=0
        SDA_OUT();
        Delay_us(5);
        return ack;
}


u8 I2C_Receive_Byte(void)
{
        u8 i,received_data;
        SDA_IN();       
        for(i=0;i<8;i++)
        {
                received_data <<=1;
                GPIOD->ODR |= 0x08;                //SCL=1
                nop();
                nop();
                if(GPIOC->IDR & 0x40)
                        received_data |= 1;       
                Delay_us(5);       
                GPIOD->ODR &= ~0x08;                //SCL=0       
                Delay_us(5);
        }
        SDA_OUT();
        return received_data;
}










mmuuss586 发表于 2015-8-21 12:49 | 显示全部楼层

用逻辑分析仪看下时序呗;
 楼主| zgyhc2050 发表于 2015-8-21 15:22 | 显示全部楼层
mmuuss586 发表于 2015-8-21 12:49
用逻辑分析仪看下时序呗;

已解决,谢谢
mintspring 发表于 2015-8-21 17:13 | 显示全部楼层
是什么问题,修改了哪儿?
 楼主| zgyhc2050 发表于 2015-8-22 14:23 | 显示全部楼层
mintspring 发表于 2015-8-21 17:13
是什么问题,修改了哪儿?

硬件有问题
速度与激情8 发表于 2015-8-22 15:08 | 显示全部楼层
火速占楼
lwsn 发表于 2015-8-23 16:01 | 显示全部楼层
硬件有问题,是最让人郁闷的
 楼主| zgyhc2050 发表于 2015-8-23 16:23 | 显示全部楼层
lwsn 发表于 2015-8-23 16:01
硬件有问题,是最让人郁闷的

是呀,
mintspring 发表于 2015-8-28 17:48 | 显示全部楼层
硬件问题,必须返工了。
米尔豪斯 发表于 2015-8-30 21:17 | 显示全部楼层
硬件什么问题
您需要登录后才可以回帖 登录 | 注册

本版积分规则

1

主题

4

帖子

0

粉丝
快速回复 在线客服 返回列表 返回顶部