终于搞定了STM32读写DS2431

2019-12-10 18:12发布



  1. //延时标志
  2. #define CALC_TYPE_S 1
  3. #define CALC_TYPE_MS 2
  4. #define CALC_TYPE_US 3
  5. /*--------------------------------------------------------------------------------------
  6. 电可擦除只读存储器DS2431操作控制命令代码的字符化常数定义:
  7. --------------------------------------------------------------------------------------*/
  8. #define DS2431_FAMILY_ID            0x2D      // DS2431家族码
  9. #define DS2431_WRITE_SCRATCHPAD     0x0F      // 写入数据到高速暂存存储器
  10. #define DS2431_READ_SCRATCHPAD      0xAA      // 读取高速暂存存储器中的数据
  11. #define DS2431_COPY_SCRATCHPAD      0x55      // 复制高速暂存存储器的数据到EEPROM中
  12. #define DS2431_READ_MEMORY          0xF0      // 读取EEPROM中的数据

  13. /*--------------------------------------------------------------------------------------
  14. 电可擦除只读存储器DS2431操作参数的字符化常数定义:
  15. --------------------------------------------------------------------------------------*/
  16. #define DS2431_ERROR                -1000     // 错误代码,DS2431数据超出范围
  17. #define DS2431_SET_SUCCESSFUL       0x00      // 设置DS2431操作参数成功返回码
  18. #define DS2431_SET_FAILED           0x01      // 设置DS2431操作参数失败返回码
  19. #define DS2431_FAILED_MAX_NUMBER    0x08      // 设置DS2431最大操作失败次数值

  20. #define DS2431_P0_ADDRESS           0x0000    // DS2431的存储器页0起始地址
  21. #define DS2431_P1_ADDRESS           0x0020    // DS2431的存储器页1起始地址
  22. #define DS2431_P2_ADDRESS           0x0040    // DS2431的存储器页2起始地址
  23. #define DS2431_P3_ADDRESS           0x0060    // DS2431的存储器页3起始地址
  24. #define DS2431_MAX_ADDRESS          0x007F    // DS2431最大可访问地址

  25. void TIM5_Init_Query(u8 type)
  26. {  
  27.     TIM_TimeBaseInitTypeDef Tim5;  
  28.     RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM5,ENABLE);  
  29.     Tim5.TIM_Period=1;
  30.     if(type==CALC_TYPE_S) //延时以S为单位时,时钟频率57600Hz,外部需要1250次计时  
  31.     {  
  32.         Tim5.TIM_Prescaler=57600-1; //预分频 72MHz / 57600= 1250Hz  
  33.     }else if(type==CALC_TYPE_MS)  
  34.     {  
  35.         Tim5.TIM_Prescaler=2880-1; //25000Hz ,定时器计数25次为ms  
  36.     }else if(type==CALC_TYPE_US)
  37.     {     
  38.         Tim5.TIM_Prescaler=72-1; //1MHz ,计数1次为us  
  39.     }else  
  40.     {  
  41.         Tim5.TIM_Prescaler=7200-1;  
  42.     }  
  43.     Tim5.TIM_ClockDivision=0;  
  44.     Tim5.TIM_CounterMode=TIM_CounterMode_Down; //向下计数  
  45.     TIM_TimeBaseInit(TIM5,&Tim5);         
  46. }  
  47.   
  48. void TIM5_S_CALC(uint32_t s)  
  49. {  
  50.     u16 counter=(s*1250)&0xFFFF; //前提定时器时钟为1250Hz  
  51.     TIM_Cmd(TIM5,ENABLE);  
  52.     TIM_SetCounter(TIM5,counter); //设置计数值  
  53.       
  54.     while(counter>1)  
  55.     {  
  56.         counter=TIM_GetCounter(TIM5);  
  57.     }  
  58.     TIM_Cmd(TIM5,DISABLE);  
  59. }  
  60.   
  61. void TIM5_MS_CALC(uint32_t ms)  
  62. {  
  63.     u16 counter=(ms*25)&0xFFFF;   
  64.     TIM_Cmd(TIM5,ENABLE);  
  65.     TIM_SetCounter(TIM5,counter); //设置计数值  
  66.       
  67.     while(counter>1)  
  68.     {  
  69.         counter=TIM_GetCounter(TIM5);  
  70.     }  
  71.     TIM_Cmd(TIM5,DISABLE);  
  72. }
  73.   
  74. void TIM5_US_CALC(uint32_t us)  
  75. {  
  76.     u16 counter=us&0xffff;  
  77.     TIM_Cmd(TIM5,ENABLE);  
  78.     TIM_SetCounter(TIM5,counter); //设置计数值  
  79.   
  80.     while(counter>1)  
  81.     {  
  82.         counter=TIM_GetCounter(TIM5);  
  83.     }  
  84.     TIM_Cmd(TIM5,DISABLE);  
  85. }

  86. /*******************************************************************************
  87. * Function Name  : Delay
  88. * Description    : Inserts a delay time.
  89. * Input          : nTime: specifies the delay time length, in milliseconds.
  90. * Output         : None
  91. * Return         : None
  92. *******************************************************************************/
  93. void _delay_us(u32 nTime)
  94. {
  95.     u16 counter=nTime&0xffff;  
  96.     TIM_Cmd(TIM5,ENABLE);  
  97.     TIM_SetCounter(TIM5,counter); //设置计数值  
  98.   
  99.     while(counter>1)  
  100.     {  
  101.         counter=TIM_GetCounter(TIM5);  
  102.     }  
  103.     TIM_Cmd(TIM5,DISABLE);  
  104. }

  105. #define RomCodeLen 8
  106. u8 RomCode[RomCodeLen];
  107. u8 Oid[8],pagedata[8];

  108. ////////////////////////////////////////////////////////////////////////////////
  109. u32 A,B,C,D,E,F,G,H,I,J;

  110. //IO输入输出配置
  111. void GPIO_Conf_in(void)
  112. {
  113.   GPIO_InitTypeDef GPIO_InitStructure;
  114.   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12 ;
  115.   GPIO_InitStructure.GPIO_Mode =  GPIO_Mode_IN_FLOATING;//输入模式可能有问题
  116.   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  117.   GPIO_Init(GPIOA, &GPIO_InitStructure);
  118. }

  119. void GPIO_Conf_out(void)
  120. {
  121.   GPIO_InitTypeDef GPIO_InitStructure;
  122.   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12 ;
  123.   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  124.   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  125.   GPIO_Init(GPIOA, &GPIO_InitStructure);
  126. }

  127. //-----------------------------------------------------------------------------
  128. // Set the 1-Wire timing to 'standard' (standard=1) or 'overdrive' (standard=0).
  129. //
  130. void SetSpeed(int standard)
  131. {
  132.   // Adjust tick values depending on speed
  133.   if (standard)
  134.   {
  135.     // Standard Speed
  136.     A = 6 ;
  137.     B = 64 ;
  138.     C = 60 ;
  139.     D = 10 ;
  140.     E = 9 ;
  141.     F = 55 ;
  142.     G = 0;
  143.     H = 480 ;
  144.     I = 70 ;
  145.     J = 410 ;
  146.   }
  147.   else
  148.   {
  149.     // Overdrive Speed
  150.     A = 2 ;
  151.     B = 8 ;
  152.     C = 8 ;
  153.     D = 3 ;
  154.     E = 1 ;
  155.     F = 7 ;
  156.     G = 3 ;
  157.     H = 70 ;
  158.     I = 9 ;
  159.     J = 40 ;
  160.   }
  161. }

  162. //-----------------------------------------------------------------------------
  163. // Generate a 1-Wire reset, return 1 if no presence detect was found,
  164. // return 0 otherwise.
  165. // (NOTE: Does not handle alarm presence from DS2404/DS1994)
  166. //
  167. int OWTouchReset(void)
  168. {
  169.   int result;
  170.   _delay_us(G);
  171.   GPIO_Conf_out();
  172.   GPIO_ResetBits(GPIOA,GPIO_Pin_12);// Drives DQ low
  173.   _delay_us(H);
  174.   GPIO_SetBits(GPIOA, GPIO_Pin_12);// Releases the bus
  175.   _delay_us(I);
  176.   GPIO_Conf_in();
  177.   result = GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_12);  // Sample for presence pulse from slave
  178.   _delay_us(J); // Complete the reset sequence recovery
  179.   return result; // Return sample presence pulse result
  180. }

  181. //-----------------------------------------------------------------------------
  182. // Send a 1-Wire write bit. Provide 10us recovery time.
  183. //
  184. void OWWriteBit(int bit)
  185. {
  186.   GPIO_Conf_out();
  187.   if (bit)
  188.   {
  189.     // Write '1' bit
  190.     GPIO_ResetBits(GPIOA,GPIO_Pin_12);// Drives DQ low
  191.     _delay_us(A);
  192.     GPIO_SetBits(GPIOA, GPIO_Pin_12);// Releases the bus
  193.     _delay_us(B); // Complete the time slot and 10us recovery
  194.   }
  195.   else
  196.   {
  197.     // Write '0' bit
  198.     GPIO_ResetBits(GPIOA,GPIO_Pin_12);// Drives DQ low
  199.     _delay_us(C);
  200.     GPIO_SetBits(GPIOA, GPIO_Pin_12);// Releases the bus
  201.     _delay_us(D);
  202.   }
  203. }

  204. //-----------------------------------------------------------------------------
  205. // Read a bit from the 1-Wire bus and return it. Provide 10us recovery time.
  206. //
  207. int OWReadBit(void)
  208. {
  209.   int result;
  210.   GPIO_Conf_out();
  211.   GPIO_ResetBits(GPIOA,GPIO_Pin_12);// Drives DQ low
  212.   _delay_us(A);
  213.   GPIO_SetBits(GPIOA, GPIO_Pin_12);// Releases the bus
  214.   _delay_us(E);
  215.   GPIO_Conf_in();
  216.   result = GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_12)& 0x01;// Sample the bit value from the slave
  217.   _delay_us(F); // Complete the time slot and 10us recovery
  218.   
  219.   return result;
  220. }

  221. //-----------------------------------------------------------------------------
  222. // Write 1-Wire data byte
  223. //
  224. void OWWriteByte(int data)
  225. {
  226.   int loop;
  227.   
  228.   // Loop to write each bit in the byte, LS-bit first
  229.   for (loop = 0; loop < 8; loop++)
  230.   {
  231.     OWWriteBit(data & 0x01);
  232.    
  233.     // shift the data byte for the next bit
  234.     data >>= 1;
  235.   }
  236. }

  237. //-----------------------------------------------------------------------------
  238. // Read 1-Wire data byte and return it
  239. //
  240. int OWReadByte(void)
  241. {
  242.   int loop, result=0;
  243.   
  244.   for (loop = 0; loop < 8; loop++)
  245.   {
  246.     // shift the result to get it ready for the next bit
  247.     result >>= 1;
  248.    
  249.     // if result is one, then set MS bit
  250.     if (OWReadBit())
  251.       result |= 0x80;
  252.   }
  253.   return result;
  254. }

  255. //-----------------------------------------------------------------------------
  256. // Write a 1-Wire data byte and return the sampled result.
  257. //
  258. int OWTouchByte(int data)
  259. {
  260.   int loop, result=0;
  261.   
  262.   for (loop = 0; loop < 8; loop++)
  263.   {
  264.     // shift the result to get it ready for the next bit
  265.     result >>= 1;
  266.    
  267.     // If sending a '1' then read a bit else write a '0'
  268.     if (data & 0x01)
  269.     {
  270.       if (OWReadBit())
  271.         result |= 0x80;
  272.     }
  273.     else
  274.     OWWriteBit(0);
  275.    
  276.     // shift the data byte for the next bit
  277.     data >>= 1;
  278.   }
  279.   return result;
  280. }

  281. //-----------------------------------------------------------------------------
  282. // Write a block 1-Wire data bytes and return the sampled result in the same
  283. // buffer.
  284. //
  285. void OWBlock(unsigned char *data, int data_len)
  286. {
  287.   int loop;
  288.   
  289.   for (loop = 0; loop < data_len; loop++)
  290.   {
  291.     data[loop] = OWTouchByte(data[loop]);
  292.   }
  293. }

  294. ///CRC校验
  295. u8 dscrcCheck(u8* p,u8 len)
  296. {
  297.   uint8_t bit0,cbit,i,j,byte,temp;
  298.   
  299.   temp=0;
  300.   for(j=0;j<len;j++)
  301.   {
  302.     byte=p[j];
  303.     for(i=0;i<8;i++)
  304.     {
  305.       cbit = temp & 0x01;
  306.       bit0 = byte & 0x01;
  307.       
  308.       temp=temp>>1;
  309.       
  310.       if( (cbit^bit0) ) temp^=0x8c;
  311.       
  312.       byte>>=1;
  313.     }
  314.   }
  315.   return temp;
  316. }

  317. //read 2431认证码
  318. u8 read_2431(void)
  319. {
  320.   int i;
  321.   
  322.   // set the speed to 'standard'
  323.   SetSpeed(1);
  324.   
  325.   // select the device
  326.   if (OWTouchReset()) // Reset the 1-Wire bus
  327.     return 0; // Return if no devices found
  328.   OWWriteByte(0x33); // Send Read ROM command to select single device
  329.   // read the page data
  330.   _delay_us(60);
  331.   for (i = 0; i < 8; i++)
  332.     RomCode[i] = OWReadByte();
  333.   if ( dscrcCheck(RomCode,8) )
  334.   {
  335.     return 0;
  336.   }
  337.   else
  338.   {
  339.     return 1;
  340.   }
  341. }

  342. /////////////////////////////////////////////////////////////////////////////////
  343. void read_2431_pagedata(unsigned char page, unsigned char *page_data)
  344. {
  345.   unsigned char i;
  346.   // set the speed to 'standard'
  347.   SetSpeed(1);
  348.   
  349.   // select the device
  350.   if (OWTouchReset()) // Reset the 1-Wire bus
  351.   {
  352.     return ; // Return if no devices found
  353.   }
  354.   
  355.   OWWriteByte(0xCC); // Send Skip ROM command to select single device
  356.   OWWriteByte(0xf0); // Read Authentication command
  357.   OWWriteByte((page << 5) & 0xFF); // TA1
  358.   OWWriteByte(0); // TA2 (always zero for DS2432)
  359.   
  360.   _delay_us(100);
  361.   
  362.   // read the page data
  363.   for (i = 0; i < 8; i++)
  364.     page_data[i] = OWReadByte();
  365.   
  366.   if (OWTouchReset()) // Reset the 1-Wire bus
  367.   {
  368.     return ; // Return if no devices found
  369.   }
  370. }


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

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

  377.   // select the device
  378.   if (OWTouchReset()) // Reset the 1-Wire bus
  379.   {
  380.     return ; // Return if no devices found
  381.   }

  382.   OWWriteByte(0xCC); // Send Skip ROM command to select single device
  383.   OWWriteByte(0x0F); // Read Authentication command
  384.   //OWWriteByte((page << 5) & 0xFF);
  385.   OWWriteByte(0x20);
  386.   OWWriteByte(0x00);
  387.         
  388.   // read the page data
  389.   for (i = 0; i < 8; i++)
  390.   {
  391.     OWWriteByte(page_data[i]);
  392.   }
  393.   //crc
  394.   rstatus[0] = OWReadByte();
  395.   rstatus[1] = OWReadByte();

  396.   _delay_us(200);

  397.   // select the device
  398.   if (OWTouchReset()) // Reset the 1-Wire bus
  399.   {
  400.     return; // Return if no devices found
  401.   }
  402.   OWWriteByte(0xCC); // Send Skip ROM command to select single device
  403.   OWWriteByte(0xAA);
  404.   TA1 = OWReadByte();
  405.   TA2 = OWReadByte();
  406.   E_S = OWReadByte();

  407.   _delay_us(10);
  408.   for(i=0;i<8;i++)
  409.   {
  410.     //        rdtmp   = OWReadByte();
  411.     Oid[i]=OWReadByte();
  412.   }
  413.   //crc
  414.   rstatus[2] = OWReadByte();
  415.   rstatus[3] = OWReadByte();

  416.   // select the device
  417.   if (OWTouchReset()) // Reset the 1-Wire bus
  418.   {
  419.     return ; // Return if no devices found
  420.   }
  421.   OWWriteByte(0xCC); // Send Skip ROM command to select single device
  422.   OWWriteByte(0x55); // Read Authentication command
  423.   //OWWriteByte((page << 5) & 0xFF); // TA1
  424.   OWWriteByte(0x20);
  425.   OWWriteByte(0x00); // TA2 (always zero for DS2432)
  426.   OWWriteByte(0x07);

  427.   _delay_us(15000);  //延时很重要
  428.   //Wait tPROGMAX for the copy function to complete
  429.   rstatus[4] = OWReadByte();
  430.   
  431.   Oid[2]=TA1;
  432.   Oid[3]=TA2;
  433.   Oid[0]=E_S;
  434.   Oid[4]=rstatus[4];

  435. }

  436. void main(void)
  437.   System_Configuration();
  438.   TIM5_Init_Query(CALC_TYPE_US);
  439.   for(int i=0;i<8;i++) pagedata[i] = 0x40+i;
  440.   write_2431_pagedata(1, pagedata);
  441.   for(int i=0;i<8;i++) pagedata[i] = 0x00;
  442.   read_2431_pagedata(1, pagedata);
  443. while(1);

复制代码
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
该问题目前已经被作者或者管理员关闭, 无法添加新回复
12条回答
shijy1977
1楼-- · 2019-12-11 20:05
这个要用到,收藏
dory_m
2楼-- · 2019-12-11 20:16
学习,谢谢!!!
Jerry_Lee
3楼-- · 2019-12-11 23:50
这个会用到,谢谢LZ分享
cqxubo00
4楼-- · 2019-12-12 04:00
 精彩回答 2  元偷偷看……
sdram
5楼-- · 2019-12-12 09:23
mark,备用,谢谢楼主
sunliezhi
6楼-- · 2019-12-12 14:54
STM32读写DS2431,  三克油!

一周热门 更多>