- //延时标志
- #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);
复制代码
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
一周热门 更多>