123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240 |
- #include "IB_Reader.h"
- #include "gpio.h"
- #include "string.h"
- #define IB_READ_ROM (0x33)
- #define IB_MATCH_ROM (0x55)
- #define IB_SEARCH_ROM (0xF0)
- #define IB_SKIP_ROM (0xCC)
- #define IB_WRITE_SCRATCHPAD (0x0F)
- #define IB_READ_SCRATCHPAD (0xAA)
- #define IB_COPY_SCRATCHPAD (0x55)
- #define IB_READ_MEMORY (0xF0)
- static uint8_t rom_id[8]={0};
- static uint8_t oil_type = OIL_TYPE_NULL;
- static uint8_t ib_readerror_count = 0;
- /**
- * @brief 初始化
- * @param 无
- * @retval ACK应答位
- **/
-
- uint8_t OneWire_Init()
- {
- uint8_t Ackbit = 0;
-
- IBTX_WIRE_HIGH; //先将总线拉高
- IBTX_WIRE_LOW; //然后将总线拉低
-
- udelay(480);
-
- IBTX_WIRE_HIGH; //释放总线
- udelay(70); //Delay 70 us
-
- Ackbit=IBRX_WIRE_BIT; //读取应答位
-
- udelay(410); //Delay 500 us
-
- return Ackbit;
-
- }
- /**
- * @brief 发送一位
- * @param Bit
- * @retval 无
- **/
-
- void OneWire_Senbit(uint8_t Bit)
- {
- IBTX_WIRE_LOW; //拉低
- udelay(5); //Delay 10 us(考虑到调用函数需要时间,实际i可以适当取的更大)
-
- //发送一位
- if(0 == Bit){
- IBTX_WIRE_LOW;
- }else{
- IBTX_WIRE_HIGH;
- }
-
- //讲解:从发送0和发送1俩个不同的时序可以看出,如果我们在10us的时候将Bit放在DQ上,如果是0
- // 则DQ一直会被拉低,如果是1,主机也会将DQ拉高,最后等50us后将数据发送即可
-
-
- udelay(50); //Delay 50 us
- IBTX_WIRE_HIGH; //拉高,便于下次发送
- udelay(5); //Delay 50 us
- }
- /**
- * @brief 接受一位
- * @param 无
- * @retval Bit
- **/
-
- uint8_t OneWire_ReceiveBit()
- {
- uint8_t Bit;
- IBTX_WIRE_LOW;
- udelay(3); //Delay 5 us
- IBTX_WIRE_HIGH;
- udelay(5); //Delay 5 us
-
- //解释:读取数据需要在15us内进行(贴近15us的时候),如果是0,DQ会在5us后被从机拉低,
- // 如果是1, DQ会在5us后被从机拉高
-
- Bit=IBRX_WIRE_BIT; //进行数据接受
- udelay(50); //Delay 50 us
-
- return Bit;
-
- }
-
- /**
- * @brief 发送一个字节
- * @param Byte
- * @retval 无
- **/
-
- void OneWire_SenByte(uint8_t Byte)
- {
- uint8_t i;
- for(i=0;i<8;i++)
- {
- OneWire_Senbit(Byte&(0x01<<i));//低位在前
- }
-
- }
- /**
- * @brief 接受一个字节
- * @param 无
- * @retval Byte
- **/
-
- uint8_t OneWire_ReceiveByte()
- {
- uint8_t i;
- uint8_t Byte=0x00;
- for(i=0;i<8;i++)
- {
- if(OneWire_ReceiveBit()){Byte|=(0x01<<i);} //低位在前
- }
-
- return Byte;
-
- }
- uint8_t IBread_OilType(void)
- {
- return 0;
-
- }
- uint8_t IBread_ROMID(void)
- {
- uint8_t Ackbit = 1;
- uint8_t i=0;
- Ackbit = OneWire_Init(); //总线初始化时序
-
- if(0 == Ackbit){
- //printf("IBread_ROMID recv ack \r\n");
- OneWire_SenByte(IB_READ_ROM); //发送读ROMID指令
-
- for(i=0; i<8;i++){
- rom_id[i]=OneWire_ReceiveByte(); //第一个字节为低位
- }
-
- }
- return 0;
-
- }
- uint8_t IBread_Memory(void)
- {
- uint8_t Ackbit = 1;
- uint8_t i=0;
-
- memset(rom_id, 0, sizeof(rom_id));
- Ackbit = OneWire_Init(); //总线初始化时序
-
- if(0 == Ackbit){
-
- OneWire_SenByte(IB_SKIP_ROM);
- OneWire_SenByte(IB_READ_MEMORY);
- OneWire_SenByte(0x00);
- OneWire_SenByte(0x00);
-
- for(i=0; i<2;i++){
- rom_id[i]=OneWire_ReceiveByte(); //第一个字节为低位
- }
-
- //油品两个字节互为补码
- if((0 == (rom_id[0]&rom_id[1])) && (0xFF == (rom_id[0]|rom_id[1]))){
- oil_type = rom_id[0];
- ib_readerror_count = 0;
- }else{
-
- if(((0xFF == rom_id[0]) && (0xFF == rom_id[1])) || ((0x00 == rom_id[0]) && (0x00 == rom_id[1])))
- {
- //断开
- oil_type = OIL_TYPE_NULL;
- ib_readerror_count = 0;
- }else{
- //通信错误 或有干扰
- ib_readerror_count++;
-
- if(ib_readerror_count >= 3){
- ib_readerror_count = 3;
- oil_type = OIL_TYPE_NULL;
- }
-
- }
-
- }
-
- }else{
- //通信错误 无ACK
- ib_readerror_count++;
-
-
- if(ib_readerror_count >= 2){
- ib_readerror_count = 3;
- oil_type = OIL_TYPE_NULL;
- }
- }
- return 0;
-
- }
- void IB_Print(void)
- {
- IBread_ROMID();
-
- printf("memory:[%x][%x][%x][%x][%x][%x][%x][%x] \r\n", rom_id[0],rom_id[1],rom_id[2],rom_id[3],rom_id[4],rom_id[5],rom_id[6],rom_id[7]);
- }
- void IBRead_OilType(void)
- {
- IBread_Memory();
- }
- uint8_t IBGet_OilType(void)
- {
- return oil_type;
- }
|