DS2431 修改网上版本 读写(已测试可用)

延时部分感觉不是很合理,这里只是一个DEMO,不过测试可用

主要是修改了网上的例程而来,如有侵权问题请联系删除

#define CALC_TYPE_S 1
#define CALC_TYPE_MS 2
#define CALC_TYPE_US 3
/*--------------------------------------------------------------------------------------
         DS2431                :
--------------------------------------------------------------------------------------*/
#define DS2431_FAMILY_ID            0x2D      // DS2431   
#define DS2431_WRITE_SCRATCHPAD     0x0F      //             
#define DS2431_READ_SCRATCHPAD      0xAA      //              
#define DS2431_COPY_SCRATCHPAD      0x55      //              EEPROM 
#define DS2431_READ_MEMORY          0xF0      //   EEPROM    


/*--------------------------------------------------------------------------------------
         DS2431            :
--------------------------------------------------------------------------------------*/
#define DS2431_ERROR                -1000     //     ,DS2431      
#define DS2431_SET_SUCCESSFUL       0x00      //   DS2431         
#define DS2431_SET_FAILED           0x01      //   DS2431         
#define DS2431_FAILED_MAX_NUMBER    0x08      //   DS2431         


#define DS2431_P0_ADDRESS           0x0000    // DS2431     0    
#define DS2431_P1_ADDRESS           0x0020    // DS2431     1    
#define DS2431_P2_ADDRESS           0x0040    // DS2431     2    
#define DS2431_P3_ADDRESS           0x0060    // DS2431     3    
#define DS2431_MAX_ADDRESS          0x007F    // DS2431       




void TIM3_Init_Query(u8 type)
{  
    TIM_TimeBaseInitTypeDef Tim3;  
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3,ENABLE);  
    Tim3.TIM_Period=1;
    if(type==CALC_TYPE_S) //   S    ,    57600Hz,    1250     
    {  
        Tim3.TIM_Prescaler=57600-1; //    72MHz / 57600= 1250Hz  
    }else if(type==CALC_TYPE_MS)  
    {  
        Tim3.TIM_Prescaler=2880-1; //25000Hz ,     25  ms  
    }else if(type==CALC_TYPE_US)
    {     
        Tim3.TIM_Prescaler=72-1; //1MHz ,  1  us  
    }else  
    {  
        Tim3.TIM_Prescaler=7200-1;  
    }  
    Tim3.TIM_ClockDivision=0;  
    Tim3.TIM_CounterMode=TIM_CounterMode_Down; //      
    TIM_TimeBaseInit(TIM3,&Tim3);         
}  
  
void TIM3_S_CALC(uint32_t s)  
{  
    u16 counter=(s*1250)&0xFFFF; //        1250Hz  
    TIM_Cmd(TIM3,ENABLE);  
    TIM_SetCounter(TIM3,counter); //       
      
    while(counter>1)  
    {  
        counter=TIM_GetCounter(TIM3);  
    }  
    TIM_Cmd(TIM3,DISABLE);  
}  
  
void TIM3_MS_CALC(uint32_t ms)  
{  
    u16 counter=(ms*25)&0xFFFF;   
    TIM_Cmd(TIM3,ENABLE);  
    TIM_SetCounter(TIM3,counter); //       
      
    while(counter>1)  
    {  
        counter=TIM_GetCounter(TIM3);  
    }  
    TIM_Cmd(TIM3,DISABLE);  

  
void TIM3_US_CALC(uint32_t us)  
{  
    u16 counter=us&0xffff;  
    TIM_Cmd(TIM3,ENABLE);  
    TIM_SetCounter(TIM3,counter); //       
  
    while(counter>1)  
    {  
        counter=TIM_GetCounter(TIM3);  
    }  
    TIM_Cmd(TIM3,DISABLE);  



/*******************************************************************************
* Function Name  : Delay
* Description    : Inserts a delay time.
* Input          : nTime: specifies the delay time length, in milliseconds.
* Output         : None
* Return         : None
*******************************************************************************/
void TIM3_uUS_CALC(u32 nTime)
{
    u16 counter=nTime&0xffff;  
    TIM_Cmd(TIM3,ENABLE);  
    TIM_SetCounter(TIM3,counter); //       
  
    while(counter>1)  
    {  
        counter=TIM_GetCounter(TIM3);  
    }  
    TIM_Cmd(TIM3,DISABLE);  
}






void delaybUs(u32 x) {
unsigned int _dcnt; 
      _dcnt=(x*8);
      while(_dcnt-- > 0) 
      { continue; }
     }


#define RomCodeLen 8
u8 RomCode[RomCodeLen];
u8 Oid[8],pagedata[8];



u32 A,B,C,D,E,F,G,H,I,J;


//IO      
void GPIO_Conf_in(void)
{

  GPIO_InitTypeDef GPIO_InitStructure;

RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE); //ʹÄÜPB,PE¶Ë¿ÚʱÖÓ

  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1 ;
  GPIO_InitStructure.GPIO_Mode =  GPIO_Mode_IN_FLOATING;//         
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_Init(GPIOB, &GPIO_InitStructure);
}


void GPIO_Conf_out(void)
{
  GPIO_InitTypeDef GPIO_InitStructure;

RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE); //ʹÄÜPB,PE¶Ë¿ÚʱÖÓ


  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1 ;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_Init(GPIOB, &GPIO_InitStructure);
}


//-----------------------------------------------------------------------------
// Set the 1-Wire timing to 'standard' (standard=1) or 'overdrive' (standard=0).
//
void SetSpeed(int standard)
{
  // Adjust tick values depending on speed
  if (standard)
  {
    // Standard Speed
    A = 6 ;
    B = 64 ;
    C = 60 ;
    D = 10 ;
    E = 9 ;
    F = 55 ;
    G = 0;
    H = 480 ;
    I = 70 ;
    J = 410 ;
  }
  else
  {
    // Overdrive Speed
    A = 2 ;
    B = 8 ;
    C = 8 ;
    D = 3 ;
    E = 1 ;
    F = 7 ;
    G = 3 ;
    H = 70 ;
    I = 9 ;
    J = 40 ;
  }
}


//-----------------------------------------------------------------------------
// Generate a 1-Wire reset, return 1 if no presence detect was found,
// return 0 otherwise.
// (NOTE: Does not handle alarm presence from DS2404/DS1994)
//
int OWTouchReset(void)
{
  int result;
  delaybUs(G);
  GPIO_Conf_out();
  GPIO_ResetBits(GPIOB,GPIO_Pin_1);// Drives DQ low
  delaybUs(H);
  GPIO_SetBits(GPIOB, GPIO_Pin_1);// Releases the bus
  delaybUs(I);
  GPIO_Conf_in();
  result = GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_1);  // Sample for presence pulse from slave
  delaybUs(J); // Complete the reset sequence recovery
  return result; // Return sample presence pulse result
}


//-----------------------------------------------------------------------------
// Send a 1-Wire write bit. Provide 10us recovery time.
//
void OWWriteBit(int bit)
{
  GPIO_Conf_out();
  if (bit)
  {
    // Write '1' bit
    GPIO_ResetBits(GPIOB,GPIO_Pin_1);// Drives DQ low
    delaybUs(A);
    GPIO_SetBits(GPIOB, GPIO_Pin_1);// Releases the bus
    delaybUs(B); // Complete the time slot and 10us recovery
  }
  else
  {
    // Write '0' bit
    GPIO_ResetBits(GPIOB,GPIO_Pin_1);// Drives DQ low
    delaybUs(C);
    GPIO_SetBits(GPIOB, GPIO_Pin_1);// Releases the bus
    delaybUs(D);
  }
}


//-----------------------------------------------------------------------------
// Read a bit from the 1-Wire bus and return it. Provide 10us recovery time.
//
int OWReadBit(void)
{
  int result;
  GPIO_Conf_out();
  GPIO_ResetBits(GPIOB,GPIO_Pin_1);// Drives DQ low
  delaybUs(A);
  GPIO_SetBits(GPIOB, GPIO_Pin_1);// Releases the bus
  delaybUs(E);
  GPIO_Conf_in();
  result = GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_1)& 0x01;// Sample the bit value from the slave
  delaybUs(F); // Complete the time slot and 10us recovery
  
  return result;
}


//-----------------------------------------------------------------------------
// Write 1-Wire data byte
//
void OWWriteByte(int data)
{
  int loop;
  
  // Loop to write each bit in the byte, LS-bit first
  for (loop = 0; loop < 8; loop++)
  {
    OWWriteBit(data & 0x01);
    
    // shift the data byte for the next bit
    data >>= 1;
  }
}


//-----------------------------------------------------------------------------
// Read 1-Wire data byte and return it
//
int OWReadByte(void)
{
  int loop, result=0;
  
  for (loop = 0; loop < 8; loop++)
  {
    // shift the result to get it ready for the next bit
    result >>= 1;
    
    // if result is one, then set MS bit
    if (OWReadBit())
      result |= 0x80;
  }
  return result;
}


//-----------------------------------------------------------------------------
// Write a 1-Wire data byte and return the sampled result.
//
int OWTouchByte(int data)
{
  int loop, result=0;
  
  for (loop = 0; loop < 8; loop++)
  {
    // shift the result to get it ready for the next bit
    result >>= 1;
    
    // If sending a '1' then read a bit else write a '0'
    if (data & 0x01)
    {
      if (OWReadBit())
        result |= 0x80;
    }
    else
    OWWriteBit(0);
    
    // shift the data byte for the next bit
    data >>= 1;
  }
  return result;
}


//-----------------------------------------------------------------------------
// Write a block 1-Wire data bytes and return the sampled result in the same
// buffer.
//
void OWBlock(unsigned char *data, int data_len)
{
  int loop;
  
  for (loop = 0; loop < data_len; loop++)
  {
    data[loop] = OWTouchByte(data[loop]);
  }
}


///CRC  
u8 dscrcCheck(u8* p,u8 len)
{
  uint8_t bit0,cbit,i,j,byte,temp;
  
  temp=0;
  for(j=0;j<len;j++)
  {
    byte=p[j];
    for(i=0;i<8;i++)
    {
      cbit = temp & 0x01;
      bit0 = byte & 0x01;
      
      temp=temp>>1;
      
      if( (cbit^bit0) ) temp^=0x8c;
      
      byte>>=1;
    }
  }
  return temp;
}


//read 2431   
u8 read_2431(void)
{
  int i;
  
  // set the speed to 'standard'
  SetSpeed(1);
  
  // select the device
  if (OWTouchReset()) // Reset the 1-Wire bus
    return 0; // Return if no devices found
  OWWriteByte(0x33); // Send Read ROM command to select single device
  // read the page data
  delaybUs(60);
  for (i = 0; i < 8; i++)
    RomCode[i] = OWReadByte();
  if ( dscrcCheck(RomCode,8) )
  {
    return 0;
  }
  else
  {
    return 1;
  }
}


/
void read_2431_pagedata(unsigned char page, unsigned char *page_data)
{
  unsigned char i;
  // set the speed to 'standard'
  SetSpeed(1);
  
  // select the device
  if (OWTouchReset()) // Reset the 1-Wire bus
  {
    return ; // Return if no devices found
  }
  
  OWWriteByte(0xCC); // Send Skip ROM command to select single device
  OWWriteByte(0xf0); // Read Authentication command
  OWWriteByte((page << 5) & 0xFF); // TA1
  OWWriteByte(0); // TA2 (always zero for DS2432)
  
  delaybUs(100);
  
  // read the page data
  for (i = 0; i < 8; i++)
    page_data[i] = OWReadByte();
  
  if (OWTouchReset()) // Reset the 1-Wire bus
  {
    return ; // Return if no devices found
  }
}




void write_2431_pagedata(unsigned char page, unsigned char *page_data)
{
  unsigned char i,TA1,TA2,E_S;
  unsigned char rstatus[8];


  // set the speed to 'standard'
  SetSpeed(1);


  // select the device
  if (OWTouchReset()) // Reset the 1-Wire bus
  {
    return ; // Return if no devices found
  }


  OWWriteByte(0xCC); // Send Skip ROM command to select single device
  OWWriteByte(0x0F); // Read Authentication command
  //OWWriteByte((page << 5) & 0xFF);
  OWWriteByte(page << 5);
  OWWriteByte(0x00);


  // read the page data
  for (i = 0; i < 8; i++)
  {
    OWWriteByte(page_data[i]);
  }
  //crc
  rstatus[0] = OWReadByte();
  rstatus[1] = OWReadByte();


  delaybUs(200);


  // select the device
  if (OWTouchReset()) // Reset the 1-Wire bus
  {
    return; // Return if no devices found
  }
  OWWriteByte(0xCC); // Send Skip ROM command to select single device
  OWWriteByte(0xAA); 
  TA1 = OWReadByte();
  TA2 = OWReadByte();
  E_S = OWReadByte();


  delaybUs(10);
  for(i=0;i<8;i++)
  {
    //        rdtmp   = OWReadByte();
    Oid[i]=OWReadByte();
  }
  //crc
  rstatus[2] = OWReadByte();
  rstatus[3] = OWReadByte();


  // select the device
  if (OWTouchReset()) // Reset the 1-Wire bus
  {
    return ; // Return if no devices found
  }
  OWWriteByte(0xCC); // Send Skip ROM command to select single device
  OWWriteByte(0x55); // Read Authentication command
  //OWWriteByte((page << 5) & 0xFF); // TA1
  OWWriteByte(page << 5);
  OWWriteByte(0x00); // TA2 (always zero for DS2432)
  OWWriteByte(0x07);


  delaybUs(15000);  //     
  //Wait tPROGMAX for the copy function to complete
  rstatus[4] = OWReadByte();
  
  Oid[2]=TA1;
  Oid[3]=TA2;
  Oid[0]=E_S;
  Oid[4]=rstatus[4];
}

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值