打印

那位好心的同志帮我来看看 这个程序哪有问题??(1302)

[复制链接]
1771|4
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
爱学小生|  楼主 | 2009-10-28 15:31 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
我找着pdf资料写的 怎么不对呢??高手帮帮忙呀!!!急。。。。
#include<reg52.h>
#define uint unsigned int
#define uchar unsigned char
sbit     SCLK      = P3^6;  
sbit     IO       = P3^4;  
sbit     RST      = P3^5;
struct
{
uint second;
uint minute;
uint hour;
uint day;
uint month;
uint week;
uint year;
}current;
void write(uint con)
{
  uchar i;
  for(i=8;i>0;i--)
  {
    if(con&0x01)
{
  IO=1;
}
else
{
   IO=0;
}
con>>=1;
SCLK=1;
SCLK=0;
  }
}
uint read()
{
  uint i,readv;
  IO=1;
  for(i=8;i>0;i--)
  {
    readv>>=1;
  if(IO)
  {
   readv|=0x80;
  }
  else
  {
    readv&=0x7f;
  }
  return readv;
  }
}
void write_byte(uint address,uint con)
{
   RST=0;
   SCLK=0;
   RST=1;
   write(address);
   write(con);
   RST=0;
   SCLK=1;
}
uint read_byte(uint address)
{
  uint readv;
  RST=0;
  SCLK=0;
  RST=1;
  write(address);
  readv=read();
  RST=0;
  SCLK=1;
  return readv;
}
void clockinit()
{
  if(read_byte(0xc1)!=0xf0)
  {
   write_byte(0x8e,0x00);
   write_byte(0x80,0x00);
   write_byte(0x82,0x08);
   write_byte(0x84,0x08);
   write_byte(0x86,0x08);
   write_byte(0x88,0x08);
   write_byte(0x8a,0x08);
   write_byte(0x8c,0x08);
   write_byte(0x90,0xa5);
   write_byte(0xc0,0xf0);
   write_byte(0x8e,0x80);
   }
}
void clockup()
{
current.second=read_byte(0x81);
current.minute=read_byte(0x83);
current.hour=read_byte(0x85);
current.day=read_byte(0x87);
current.month=read_byte(0x89);
current.week=read_byte(0x8b);
current.year=read_byte(0x8d);
}

相关帖子

沙发
fengfeng| | 2009-10-28 17:52 | 只看该作者
老外说:看不明白,光看read()里的for循环似乎就有问题,循环第一圈就return了?

使用特权

评论回复
板凳
awmc_m| | 2009-10-28 20:41 | 只看该作者
。。。。。。。。。
只看了一个函数。
iic写应该高位开始写,所以第一个函数就错了

使用特权

评论回复
地板
草履虫| | 2009-10-28 23:05 | 只看该作者
送你一个。
#define IIC_SDA                P1_7
#define IIC_SCL                P1_6
#define        SET_SCL                P1_6        =        1
#define        CLR_SCL                P1_6        =        0
#define        SET_SDA                P1_7        =        1
#define        CLR_SDA                P1_7        =        0

/************************************************************************
    Function :                 bool I2CStart(byte bDevice)
          Description :         Start Routine
                        timing : SCL ^^^^|___|^|___|^|__~__|^|___|^|___|^|__~
                                  SDA ^^|____/A6 \_/A5 \_~_/A0 \_/R/W\_/ACK\_~
                                      (S)
                                        |<--- start condition
    Parameter :         bDevice(8-bit slave address)
    Return    :         TRUE  : successful with ACK from slave
                        FALSE  : bus (SCL = 0) or ACK failure
************************************************************************/
bool fgI2CStart(byte bDevice)
{
  SET_SDA;                                                        // make sure SDA released
        TimeDelay2us();
  SET_SCL;                                                        // make sure SCL released
        TimeDelay2us();
  CLR_SDA;                                                        // start condition here
        TimeDelay2us();
  CLR_SCL;                                                        // ready for clocking
        TimeDelay2us();
  return(fgI2CSend(bDevice));                        // slave address & R/W transmission
}
/************************************************************************
    Function :         void I2CStop(void)
          Description :         IIC Stop Routine
                        timing : SCL ___|^^^^^
                                  SDA xx___|^^^
                                     (P)
                                      |<--- stop condition
    Parameter : NONE
    Return    : NONE
************************************************************************/
void vI2CStop(void)
{
          CLR_SDA;                                                        // ready for stop condition
        TimeDelay2us();
          SET_SCL;                                                        // ready for stop condition
        TimeDelay2us();
          SET_SDA;                                                        // stop condition here
        TimeDelay2us();
}
/************************************************************************
    Function :                 bool fgI2LSendByte(byte bValue)
          Description :   Send Routine
                        timing : SCL ___|^|___|^|__~__|^|
                                  SDA __/D0 \_/D1 \_~_/D7 \_
    Parameter :         bValue(8-bit output data)
    Return    :                TRUE: Send Data success
                                    FALSE: ACK Error
************************************************************************/
bool fgI2CSend(byte bValue)
{
        byte bBitMask = 0x80;
        // step 1 : 8-bit data transmission
        while(bBitMask){
                if(bBitMask & bValue){
                   SET_SDA;
          }
          else{
                  CLR_SDA;
          }
          TimeDelay2us();
          SET_SCL;                                                // data clock in
          TimeDelay2us();
          CLR_SCL;                                                // ready for next clock in
          TimeDelay2us();
          bBitMask = bBitMask >> 1;                // MSB first & timing delay
  }
  SET_SDA;                                                        // release SDA for ACK polling
  TimeDelay2us();
  SET_SCL;                                                        // start ACK polling
        TimeDelay2us();
  bBitMask = 200;                                                // time out protection
  do{
                TimeDelay2us();       
        }while(IIC_SDA && --bBitMask);                // wait for ACK, SDA=0 or bitMask=0->jump to this loop
        CLR_SCL;                                                        // end ACK polling
        TimeDelay2us();
  if(!bBitMask)
                return FALSE;                        // return TRUE if ACK detected
        return TRUE;                                                // return FALSE if time out
}
/************************************************************************
    Function :                 void I2CRead(byte *prValue, byte fgSeq_Read)
        Description :         IIC Read Routine
                        timing : SCL  ___|^|___|^|__~__|^|___|^|__
                                  SDA __/D7 \_/D6 \_~_/D0 \_/ACK\_
    Parameter :         *prValue(8-bit point input value)
                                    fgSeq_Read: 0:         NACK here (for single byte read)
                                                            1:        ACK here for Sequential Read
    Return    :         NONE
************************************************************************/

void vI2CRead(byte *prValue, bool fgSeq_Read)
{
        byte bBitMask = 0x80;
        *prValue = 0;                                 // reset data buffer
        SET_SDA;                                                        // make sure SDA released
        TimeDelay2us();
        // step 1 : 8-bit data reception
        while(bBitMask)
        {
                SET_SCL;                                                // data clock out
            TimeDelay2us();
            if(IIC_SDA){
                        *prValue = *prValue | bBitMask; // Get all data
            }                                                                // non-zero bits to buffer
            CLR_SCL;                                                // ready for next clock out
            TimeDelay2us();
            bBitMask = bBitMask >> 1;                // shift bit mask & clock delay
        }
        // step 2 : acknowledgement to slave
        if(fgSeq_Read){
            CLR_SDA;                                                // ACK here for Sequential Read
        }
        else{
                SET_SDA;                                                // NACK here (for single byte read)
        }
        TimeDelay2us();
        SET_SCL;                                                        // NACK clock out
        TimeDelay2us();
          CLR_SCL;                                                        // ready for next clock out
        TimeDelay2us();
          SET_SDA;                                                        // release SDA
        TimeDelay2us();
}
程序不是抄来就能用的。
IIC程序还要涉及时序问题。
并不全是拿来主义。

使用特权

评论回复
5
爱学小生|  楼主 | 2009-10-29 22:18 | 只看该作者
先谢过了  我在研究研究

使用特权

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

本版积分规则

32

主题

138

帖子

2

粉丝