//延时标志
#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 TIM5_Init_Query(u8 type)
{  
    TIM_TimeBaseInitTypeDef Tim5;  
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM5,ENABLE);  
    Tim5.TIM_Period=1;
    if(type==CALC_TYPE_S) //延时以S为单位时,时钟频率57600Hz,外部需要1250次计时  
    {  
        Tim5.TIM_Prescaler=57600-1; //预分频 72MHz / 57600= 1250Hz  
    }else if(type==CALC_TYPE_MS)  
    {  
        Tim5.TIM_Prescaler=2880-1; //25000Hz ,定时器计数25次为ms  
    }else if(type==CALC_TYPE_US)
    {     
        Tim5.TIM_Prescaler=72-1; //1MHz ,计数1次为us  
    }else  
    {  
        Tim5.TIM_Prescaler=7200-1;  
    }  
    Tim5.TIM_ClockDivision=0;  
    Tim5.TIM_CounterMode=TIM_CounterMode_Down; //向下计数  
    TIM_TimeBaseInit(TIM5,&Tim5);         
}  
  
void TIM5_S_CALC(uint32_t s)  
{  
    u16 counter=(s*1250)&0xFFFF; //前提定时器时钟为1250Hz  
    TIM_Cmd(TIM5,ENABLE);  
    TIM_SetCounter(TIM5,counter); //设置计数值  
      
    while(counter>1)  
    {  
        counter=TIM_GetCounter(TIM5);  
    }  
    TIM_Cmd(TIM5,DISABLE);  
}  
  
void TIM5_MS_CALC(uint32_t ms)  
{  
    u16 counter=(ms*25)&0xFFFF;   
    TIM_Cmd(TIM5,ENABLE);  
    TIM_SetCounter(TIM5,counter); //设置计数值  
      
    while(counter>1)  
    {  
        counter=TIM_GetCounter(TIM5);  
    }  
    TIM_Cmd(TIM5,DISABLE);  

  
void TIM5_US_CALC(uint32_t us)  
{  
    u16 counter=us&0xffff;  
    TIM_Cmd(TIM5,ENABLE);  
    TIM_SetCounter(TIM5,counter); //设置计数值  
  
    while(counter>1)  
    {  
        counter=TIM_GetCounter(TIM5);  
    }  
    TIM_Cmd(TIM5,DISABLE);  
}

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

#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;
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12 ;
  GPIO_InitStructure.GPIO_Mode =  GPIO_Mode_IN_FLOATING;//输入模式可能有问题
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_Init(GPIOA, &GPIO_InitStructure);
}

void GPIO_Conf_out(void)
{
  GPIO_InitTypeDef GPIO_InitStructure;
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12 ;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_Init(GPIOA, &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;
  _delay_us(G);
  GPIO_Conf_out();
  GPIO_ResetBits(GPIOA,GPIO_Pin_12);// Drives DQ low
  _delay_us(H);
  GPIO_SetBits(GPIOA, GPIO_Pin_12);// Releases the bus
  _delay_us(I);
  GPIO_Conf_in();
  result = GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_12);  // Sample for presence pulse from slave
  _delay_us(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(GPIOA,GPIO_Pin_12);// Drives DQ low
    _delay_us(A);
    GPIO_SetBits(GPIOA, GPIO_Pin_12);// Releases the bus
    _delay_us(B); // Complete the time slot and 10us recovery
  }
  else
  {
    // Write '0' bit
    GPIO_ResetBits(GPIOA,GPIO_Pin_12);// Drives DQ low
    _delay_us(C);
    GPIO_SetBits(GPIOA, GPIO_Pin_12);// Releases the bus
    _delay_us(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(GPIOA,GPIO_Pin_12);// Drives DQ low
  _delay_us(A);
  GPIO_SetBits(GPIOA, GPIO_Pin_12);// Releases the bus
  _delay_us(E);
  GPIO_Conf_in();
  result = GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_12)& 0x01;// Sample the bit value from the slave
  _delay_us(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
  _delay_us(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)
  
  _delay_us(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(0x20);
  OWWriteByte(0x00);
        
  // read the page data
  for (i = 0; i < 8; i++)
  {
    OWWriteByte(page_data[i]);
  }
  //crc
  rstatus[0] = OWReadByte();
  rstatus[1] = OWReadByte();

_delay_us(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();

_delay_us(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(0x20);
  OWWriteByte(0x00); // TA2 (always zero for DS2432)
  OWWriteByte(0x07);

_delay_us(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];

}

void main(void)
  System_Configuration();
  TIM5_Init_Query(CALC_TYPE_US);
  for(int i=0;i<8;i++) pagedata[i] = 0x40+i;
  write_2431_pagedata(1, pagedata);
  for(int i=0;i<8;i++) pagedata[i] = 0x00;
  read_2431_pagedata(1, pagedata);
 while(1);

stm32读写 DS2431相关推荐

  1. STM32读写内部flash

    概念:计算机中最小的信息单位是bit,也就是一个二进制位,8个bit组成一个Byte,也就是1个字节, 1个存储单元存放1个字节,每个存储单元对应一个32位(bit)地址,所以重要的话说三遍:对于32 ...

  2. STM32读写内部Flash(介绍+附代码)

    概述 内部flash读写详解 一.介绍 首先我们需要了解一个内存映射: stm32的flash地址起始于0x0800 0000,结束地址是0x0800 0000加上芯片实际的flash大小,不同的芯片 ...

  3. FPGA配置芯片EPCS读写操作--STM32读写

    注意事项: (1)首先STM32需要设置nCE和nConfig信号,即nCE置高,nConfig拉低,获得EPCS的控制权,而后对EPCS操作,操作完成后需要释放这两个管脚,即nCE拉低,nConfi ...

  4. STM32读写W25Q

    文章目录 硬件外观 引脚说明 代码摘要 代码获取 硬件外观 引脚说明 关于如何接线? 1,CS 2,MISO 3,3.3V 4,GND 5,MOSI 6,SCK 7,3.3V 8,3.3V 关于如何查 ...

  5. STM32读写保护详细解锁指南

    前段时间,在淘宝买了几片stm32f429vet6,某猫价格由高到低排序,选了最便宜的,结果踩到了坑. 写着所谓的进口散装,价格比市价便宜一半.当焊到板子上,用swd仿真,有趣的事情发生了,在设置好各 ...

  6. 关于STM32读写flash内容时进入Hardfault的问题

    读写flash时尤其要注意地址的问题. 1,不能将内容写入flash已下载的程序的相同地址,尽可能的将地址设置的靠后: 2,注意不同容量flash的大小,读写地址不要超出flash主存储块的最大地址, ...

  7. STM32读写24C02遇到的问题

    这几天在弄I2C,读取24C02的数据.我默默的敲完了代码,然后仿真. 代码就下面两行,就是写一个字节,然后读取. I2C_EE_BufferWrite( &write,100, 1); I2 ...

  8. STM32内部flash详解(1)

    STM32 内部FLAsh概述 今天说一下STM32中的内部flash. 当我们把写好的代码下载MCU中,这个代码时存放在flash中的.当芯片重启复位上电后,会通过内核对flash进行代码的加载运行 ...

  9. STM32 Flash 永久用户数据空间

    /*********************************************************************************  *               ...

最新文章

  1. Java集合查找Map,java:使用hashmap或其他一些java集合创建查找...
  2. python polygon函数_Python 人脸识别就多简单,看这个就够了!
  3. Error: Call requires API level 11 (current min is 8): android.app.Activity#onCreateView
  4. 机器学习-分类算法-逻辑回归13
  5. Mysql数据库(七)——mysql高阶语句(上)
  6. .NET 时间轴:从出生到巨人
  7. ORA-00257+mysql_ORA-00257错误的解决办法
  8. HTTP Status 500 - 问题
  9. windows下安装HTK3.4
  10. LabVIEW之安装队列工具包AMC安装问题解决
  11. 算法:插入排序、归并排序、快速排序、堆排序
  12. 将视频或动态壁纸设置成桌面
  13. 关于@Result注解的说明
  14. 【WWW2021】图知识蒸馏
  15. C#测试网络连接测试
  16. android 2.3.6Gallary源码导入到Eclipse中编译
  17. Visual Studio 默认编码为 utf-8
  18. 【HNU分布式与云计算系统】MPI实现矩阵乘矩阵运算
  19. Java开发面试题目,Java面试网络问题
  20. 学校计算机使用维护管理制度,计算机教室使用管理制度

热门文章

  1. 技术节系列 | 支付账务清结算系统设计
  2. Mac OS X El Capitan beta 6 + Xcode 7 beta 4 的问题有解了
  3. Java重发机制的实现
  4. Vision MLP之RaftMLP Do MLP-based Models Dream of Winning Over Computer Vision
  5. 计算机test的应用,使用 Speedtest 准确测试电脑和手机网速
  6. 1分钟学会在 C# 中将 PPTX/PPT 转换为 PNG 图像
  7. 学习笔记7--车辆转向系统
  8. SAP S4HANA F-02报错:更正统一日记账分类账的定制设置
  9. PS第一课:熟悉工作区
  10. R与神经网络之Neuralnet包