OpenEdv-开源电子网

 找回密码
 立即注册
正点原子全套STM32/Linux/FPGA开发资料,上千讲STM32视频教程免费下载...
查看: 3361|回复: 0

STM32F030模拟IIC程序无法运行

[复制链接]

349

主题

520

帖子

0

精华

金牌会员

Rank: 6Rank: 6

积分
1625
金钱
1625
注册时间
2014-7-11
在线时间
285 小时
发表于 2016-9-4 13:10:36 | 显示全部楼层 |阅读模式
1金钱
STM32F030模拟IIC程序无法运行
最近在做STM32F030 IIC程序编译能通过,但是却无法运行
#ifndef _IIC_H
#define _IIC_H
#include "stm32f0xx.h"
//SDA=PF7;SCL=PF6

#define SCL_L         GPIOF->BRR = GPIO_Pin_6
#define SCL_H         GPIOF->BSRR = GPIO_Pin_6
#define SDA_L         GPIOF->BRR = GPIO_Pin_7
#define SDA_H         GPIOF->BSRR = GPIO_Pin_7
#define SDA_READ      (GPIOF->IDR&GPIO_Pin_7) >>7

uint8_t g_iic_busy;
//--------------------------------------------------------
//IIC½ó¿ú3õê¼»ˉGPIO
//--------------------------------------------------------
//SDA=PF7;SCL=PF6
void IIC_Configuation(void)
{
       
        GPIO_InitTypeDef GPIO_InitStruct;
        RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOF, ENABLE);
  GPIO_InitStruct.GPIO_Pin = GPIO_Pin_6|GPIO_Pin_7 ;
  GPIO_InitStruct.GPIO_Mode = GPIO_Mode_OUT;
        GPIO_InitStruct.GPIO_OType = GPIO_OType_OD;
  GPIO_InitStruct.GPIO_Speed = GPIO_Speed_Level_3;
  GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_UP;
  //GPIO_Init(GPIOA, &GPIO_InitStruct);
        GPIO_Init(GPIOF, &GPIO_InitStruct);
       
        GPIO_SetBits(GPIOF, GPIO_Pin_6| GPIO_Pin_7 );
}
//--------------------------------------------------------
//éèÖÃêy¾YÏßÎaêä3ö
//--------------------------------------------------------
void SDA_Out(void)
{
       
        GPIO_InitTypeDef GPIO_InitStruct;
        RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOF, ENABLE);
  GPIO_InitStruct.GPIO_Pin = GPIO_Pin_7 ;
  GPIO_InitStruct.GPIO_Mode = GPIO_Mode_OUT;
  //GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;
        GPIO_InitStruct.GPIO_OType = GPIO_OType_OD;
  GPIO_InitStruct.GPIO_Speed =GPIO_Speed_Level_3;
  GPIO_Init(GPIOF, &GPIO_InitStruct);
}
//-------------------------------------------------------
//éèÖÃêy¾YÏßÎaêäèë
//-------------------------------------------------------
void SDA_In(void)
{
       
        GPIO_InitTypeDef GPIO_InitStruct;
        RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOF, ENABLE);
  GPIO_InitStruct.GPIO_Pin = GPIO_Pin_7 ;
  GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IN;
        GPIO_InitStruct.GPIO_OType = GPIO_OType_OD;
  GPIO_InitStruct.GPIO_Speed = GPIO_Speed_Level_2;
  GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_UP;
// GPIO_Init(GPIOA, &GPIO_InitStruct);
        GPIO_Init(GPIOF, &GPIO_InitStruct);

}
//------------------------------------------------------
//Æô¶ˉIIC×üÏß
//------------------------------------------------------
void IIC_Start()
{
  SDA_Out();
        SDA_H;
        SCL_H;
        delay_us(2);
        SDA_L;
        delay_us(2);
        SCL_L;
        delay_us(2);
       
       
}

//------------------------------------------------------
//êí·ÅIIC×üÏß
//------------------------------------------------------
void IIC_Stop()
{
IIC_Start();
        SCL_H;
        SDA_L;
        delay_us(2);
        SDA_H;
        delay_us(2);
}
//-----------------------
uint8_t r_ack(void)
{
         uint8_t RecAck=0;
       

        SDA_In();
       
  SCL_H;

        delay_us(2);
  RecAck = (uint8_t)SDA_READ;
  SCL_L;

        delay_us(2);
       
  return RecAck;
}
void s_ack(uint8_t flag)
{
        //SDA_OUT();
        SDA_Out();
       
  SDA_L;
  SCL_H;

        delay_us(2);
  SCL_L;

        delay_us(2);
}
uint8_t iic_revbyte_io( void )
{
    uint8_t tbyteI2C = 0X00;
    uint8_t i;

                SDA_In();

                delay_us(2);
    for (i = 0; i < 8; i++)
    {

                                SCL_H;
        tbyteI2C <<= 1;

                                delay_us(2);
        if (SDA_READ)
        {
            tbyteI2C++;
        }

                                SCL_L;

                                delay_us(2);
    }
    return tbyteI2C;
}

uint8_t iic_revbyte( uint8_t para )
{
    uint8_t tbyte;
    tbyte = iic_revbyte_io();
    s_ack(para);
    return tbyte;
}

void iic_sendbyte_io(uint8_t byte)
{

               
                unsigned char i;
                        SDA_Out();
                        for(i=0;i<8;i++)
                        {
                                 if(byte&0x80)
                                 {
                                        SDA_H;
                                 }
                                        else
                                        {
                                         SDA_L;
                                        }
                                byte<<=1;
                                SCL_H;
                                delay_us(2);
                                SCL_L;
                                delay_us(2);
                        }
}

void iic_sendbyte(uint8_t byte)
{
    iic_sendbyte_io(byte);
    r_ack();
}





void iic_write(uint8_t chip_id,uint8_t iic_addr,uint8_t *iic_dat,uint8_t n)
{
    g_iic_busy  = 1;

           IIC_Start();
    iic_sendbyte(chip_id);         //???
       
    if (0xff != iic_addr)
    {
        iic_sendbyte(iic_addr);   //???
                       
    for (; n>0; n--)
    {
        iic_sendbyte(*iic_dat++);      //???
                         

                IIC_Stop();
    g_iic_busy = 0;
}

void iic_readn(uint8_t chip_id,uint8_t iic_addr,uint8_t *iic_dat,uint8_t n)
{
    g_iic_busy = 1;

          IIC_Start();
    iic_sendbyte(chip_id);         //???
          
    if (0xff != iic_addr)
    {
        iic_sendbyte(iic_addr);   //???
               
    }
    for (; n>1; n--)
    {
        *iic_dat++ = iic_revbyte(0);      //???
                       
    }
    *iic_dat++ = iic_revbyte(1);
               

                IIC_Stop();
    g_iic_busy = 0;
}

这是程序,应该是程序的问题,请问程序问题在哪里,错在哪里,谢谢!!

正点原子逻辑分析仪DL16劲爆上市
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则



关闭

原子哥极力推荐上一条 /2 下一条

正点原子公众号

QQ|手机版|OpenEdv-开源电子网 ( 粤ICP备12000418号-1 )

GMT+8, 2025-5-26 00:11

Powered by OpenEdv-开源电子网

© 2001-2030 OpenEdv-开源电子网

快速回复 返回顶部 返回列表