新聞中心

        AD7705程序

        作者: 時間:2016-11-30 來源:網絡 收藏

        //--------------------------------------------------------------------------
        //-------------------------讀取據函數--------------------------
        //入口參數:指向main()主函數定義的ad_data變量的常量指針
        //--------------------------------------------------------------------------
        voidReadData7705(unsignedint*constpdata)
        {
        unsignedintdata=0,fiter[5]={0};
        unsignedlonginttemp=0;
        unsignedchari=0,sort_flag=1;

        AD_CS0;
        _delay_us(5);

        start_AD7705();

        for(i=0;i<5;i++)
        {
        start_timer0();
        _delay_ms(2);
        while(AD_DRDY)
        {
        if(time_count>=time_read_data)
        {
        stop_timer0();
        AD_CS1;
        return;
        }
        }
        stop_timer0();

        write_AD7705_byte(RD_DATA_REG);
        fiter[i]=read_AD7705_word();
        }
        AD_CS1;

        sort_flag=1;
        while(sort_flag)
        {
        sort_flag=0;
        for(i=0;i<4;i++)
        {
        if(fiter[i]>fiter[i+1])
        {
        data=fiter[i];
        fiter[i]=fiter[i+1];
        fiter[i+1]=data;
        sort_flag=1;
        }
        }
        }

        temp=((unsignedlongint)fiter[1]+(unsignedlongint)fiter[2]
        +(unsignedlongint)fiter[3])/3;

        data=(unsignedint)temp;

        if((data<0xfff0)&&(data&0x000f)>0x0008)
        {
        data>>=4;
        data++;
        }
        else
        {
        data>>=4;
        }

        if(1!=scale)
        {
        data-=0x0800;
        }

        *pdata=data&0x0fff;
        }

        //---------------------------------------------------------------------------------------
        //校準命令格式
        //STXDataLongCommandCodeParameterCheckSumETX
        //0x55數據長度(2)量程指示00H/01HCRC16(2)0x0D

        voidAD7705_calibration(void)
        {
        unsignedcharreadtimes=0;

        unsignedcharcali_scale=0;

        unsignedlonginttemp=0;

        unsignedcharcoefficient[8]={0};//test[8]={0};

        //16位校驗和的臨時變量
        unsignedintcrcvalue=0;

        AD_CS1;

        cali_scale=command[2];//獲取上位機發送的要校準的量程類型

        eeprom_busy_wait();
        eeprom_read_block(&coefficient[0],(void*)(ADDR_EEPROM_1+(cali_scale-1)*10),8);
        crcvalue=checksum(&coefficient[0],6);
        if((coefficient[7]*256+coefficient[6])!=crcvalue)
        {
        readtimes++;
        }

        if(1==readtimes)
        {
        eeprom_busy_wait();
        eeprom_read_block(&coefficient[0],(void*)(ADDR_EEPROM_2+(cali_scale-1)*10),8);
        crcvalue=checksum(&coefficient[0],6);//
        if((coefficient[7]*256+coefficient[6])!=crcvalue)
        {
        readtimes++;
        }
        }

        if(2==readtimes)
        {
        eeprom_busy_wait();
        eeprom_read_block(&coefficient[0],(void*)(ADDR_EEPROM_3+(cali_scale-1)*10),8);
        }

        ADDR409_MASK;
        AD_CS0;
        _delay_us(5);
        reset_AD7705();

        write_AD7705_byte(WR_CLOCK_REG);
        write_AD7705_byte(CLOCK_REG_SET);

        if(ZERO_CALIBRATION==command[3])//校準命令為零校準
        {
        write_AD7705_byte(WR_SETUP_REG);
        write_AD7705_byte(text_of_setup[cali_scale-1]|SYS_ZERO_CALI);

        //等待校準完成
        start_timer0();
        while(time_countstop_timer0();

        while(AD_DRDY);

        //讀OFFSET寄存器
        write_AD7705_byte(RD_OFFSET_REG);
        temp=read_AD7705_dword();

        if(cali_scale==scale)
        {
        ZS=temp;NO_CALI_TYPE=NO_FULL_CALIBRATION;
        }

        coefficient[0]=(unsignedchar)(temp%256);
        coefficient[1]=(unsignedchar)((temp/256)%256);
        coefficient[2]=(unsignedchar)((temp/65536)%256);
        }
        elseif(FULL_CALIBRATION==command[3])
        {

        temp=(unsignedlongint)(coefficient[0])+(unsignedlongint)(coefficient[1])*256
        +(unsignedlongint)(coefficient[2])*65536;

        write_AD7705_byte(WR_OFFSET_REG);
        write_AD7705_dword(temp);

        write_AD7705_byte(WR_SETUP_REG);
        write_AD7705_byte(text_of_setup[cali_scale-1]|SYS_FULL_CALI);

        //等待校準完成
        start_timer0();
        while(time_countstop_timer0();

        while(AD_DRDY);//
        //讀FULL寄存器
        write_AD7705_byte(RD_FULL_REG);
        temp=read_AD7705_dword();

        if(cali_scale==scale)
        {
        GS=temp;
        NO_CALI_TYPE=ALREADY_CALIBRATION;
        }

        coefficient[3]=(unsignedchar)(temp%256);
        coefficient[4]=(unsignedchar)((temp/256)%256);
        coefficient[5]=(unsignedchar)((temp/65536)%256);
        }
        else
        {
        AD_CS1;
        _delay_us(5);
        return;
        }

        AD_CS1;
        _delay_us(5);

        crcvalue=checksum(&coefficient[0],6);
        coefficient[6]=(unsignedchar)(crcvalue%256);//
        coefficient[7]=(unsignedchar)(crcvalue/256);

        eeprom_busy_wait();
        eeprom_write_block(&coefficient[0],(void*)(ADDR_EEPROM_1+(cali_scale-1)*10),8);

        eeprom_busy_wait();
        eeprom_write_block(&coefficient[0],(void*)(ADDR_EEPROM_2+(cali_scale-1)*10),8);

        eeprom_busy_wait();
        eeprom_write_block(&coefficient[0],(void*)(ADDR_EEPROM_3+(cali_scale-1)*10),8);

        Txout(&coefficient[0]);//輸出校準數據給上位機

        return;
        }


        //
        voidstart_AD7705(void)
        {
        reset_AD7705();

        //寫OFFSET寄存器
        write_AD7705_byte(WR_OFFSET_REG);
        write_AD7705_dword(ZS);

        //寫滿量程校準寄存器
        write_AD7705_byte(WR_FULL_REG);
        write_AD7705_dword(GS);

        //CLOCK寄存器設置,無分頻,50HZ輸出更新速率
        write_AD7705_byte(WR_CLOCK_REG);
        write_AD7705_byte(CLOCK_REG_SET);

        //寫設置寄存器
        write_AD7705_byte(WR_SETUP_REG);
        write_AD7705_byte(text_of_setup[scale-1]);

        start_timer0();
        while(time_countstop_timer0();
        }

        上一頁 1 2 下一頁

        關鍵詞: AD7705轉換數

        評論


        技術專區

        關閉
        主站蜘蛛池模板: 清苑县| 牟定县| 泸水县| 巴里| 扶余县| 融水| 德昌县| 报价| 津市市| 酒泉市| 太仆寺旗| 黔江区| 延川县| 阳高县| 栾城县| 闵行区| 安图县| 阜城县| 黎城县| 阳高县| 扶风县| 东乡族自治县| 泰和县| 永济市| 宕昌县| 贵州省| 怀远县| 承德县| 新丰县| 龙泉市| 眉山市| 牡丹江市| 安西县| 镇江市| 盐边县| 隆子县| 赞皇县| 平和县| 马山县| 和政县| 绥德县|