新闻  |   论坛  |   博客  |   在线研讨会
收音机源程序
我就是我lxw | 2012-09-23 15:56:36    阅读:1686   发布文章

void Ftun_Seek(enum DIRECTION direction, uint8 ams, uint8 singlestep)
{  
    static uint32 step,max,min;
    static uint8 i=0;                       //Total numbers of preset stations
    uint8 j=0;
    static uint32 freq;
    static uint16 stepnum;                  //Step numbers going on of seeking action
    static uint32 temp_preset_level[64];    //Buffer of preset stations,MSB 8 bit stores level,
                                            //LSB 24 bit stores frequency
    switch (SeekState)
    {
        case Seek_Idle:
            Ftun_TunetoFrequency(CurrentBand,CurrentFrequency,ATomIC_I2C_WriteMode_Preset);
            stepnum = 0;
            status = (status & 0xf0) | Status_Idle;
            SeekState = Seek_Configure;
            break;
        case Seek_Configure:
            switch (CurrentBand)             //setting up of tuning step and edge limit subject to different bands
            {
                case Band_LW:   step = LWSeekStep;
                    max = LWmax;
                    min = LWmin;
                    break;
                case Band_MW:   step = MWSeekStep;
                    max = MWmax;
                    min = MWmin;
                    break;
                case Band_FM:   step = FMSeekStep;
                    max = FMmax;
                    min = FMmin;
                    break;
                case Band_SW:   step = SWSeekStep;
                    max = SWmax;
                    min = SWmin;
                    break;
                case Band_OIRT: step = OIRTSeekStep;
                    max = OIRTmax;
                    min = OIRTmin;
                    break;
            }

            while (temp_preset_level[j])                         //Clear preset stations buffer
                temp_preset_level[j++] = 0;
            stepnum = 0;
            i = 0;
            j = 0;
            SeekState = Seek_Request;
            if (singlestep == TRUE)                             //Single step tuning up or down
                status = (status & 0xf0)| ((uint8)Status_Idle);
            else
                if (ams == TRUE)                                //Auto search and store
                status = status | ((uint8)Status_AMS);
            else                                            //Seek up or down
                status = status | ((uint8)Status_Seek);
            break;
        case Seek_Request:
            if (direction == UP)
                freq=CurrentFrequency+step;
            else
                freq=CurrentFrequency-step;
            if (freq > max)
                freq= min;
            if (freq < min)
                freq = max;
            if ((stepnum++) <= (max-min)/step)
            {
                SeekState = Seek_Change_Freqency;
            }
            else                                                //No available stations within the whole band
                if (ams == TRUE)
                SeekState = Seek_AMS;
            else
                SeekState = Seek_Idle;
            break;
        case Seek_Change_Freqency:
            Ftun_TunetoFrequency(CurrentBand,freq,ATomIC_I2C_WriteMode_Search);
            CurrentFrequency = freq;
            if (singlestep == FALSE)                            //Seek action, begins to wait level result
                SeekState = Seek_Wait_Level;
            else                                                //Single step tuning action, turn to Idle
            {
                SeekState = Seek_Idle;
            }
            break;
        case Seek_Wait_Level:
            if (CurrentBand == Band_LW || CurrentBand == Band_MW || CurrentBand == Band_SW)
                seek_count = SeekDelay_AM;
            else
                seek_count = SeekDelay_FM;
            SeekState = Seek_Check_Level;
            break;
        case Seek_Check_Level:
            if (seek_count > 0) break;                           //Waiting until timer runs up
            if (Ftun_CheckLevel(CurrentBand) == TRUE)            //Meet level condition
            {
                if (CurrentBand == Band_FM || CurrentBand == Band_OIRT)
                {
                    if (Check_USN_WAM == TRUE)                   //Going on with checking USN and WAM
                        SeekState = Seek_Check_USN;
                    else                                        //Check IF counter
                        SeekState = Seek_Wait_IFC;
                }
                else                                            //USN and WAM are involved only in FM
                    SeekState = Seek_Wait_IFC;
            }
            else                                                //Not an available station, going on seeking
                SeekState = Seek_Request;
            break;
        case Seek_Check_USN:
            if (getUSN() <= SeekCondition_FM_USN)
                SeekState = Seek_Check_WAM;
            else
                SeekState = Seek_Request;
            break;
        case Seek_Check_WAM:
            if (getWAM() <= SeekCondition_FM_WAM)
                SeekState = Seek_Wait_IFC;
            else
                SeekState = Seek_Request;
            break;
        case Seek_Wait_IFC:
            if (getIFCounter(FALSE) == Read_Bit_IFCREADY)        //Waiting until 32ms IF counter result is available
                SeekState = Seek_Check_IFC;
            break;
        case Seek_Check_IFC:
            if (getIFCounter(TRUE) <= SeekCondition_Ifcount)
            {
                if (ams == TRUE)
                    SeekState = Seek_AMS;
                else
                    SeekState = Seek_Idle;
            }
            else
                SeekState = Seek_Request;
            break;
        case Seek_AMS:
            if (stepnum <= (max-min)/step)
            {

                temp_preset_level[i++] = (getLevel() << 24) | CurrentFrequency; //save each available station to temp buffer
                SeekState = Seek_Request;
            }
            else
            {
                PresetSort(i,temp_preset_level,&Preset_Freq[CurrentBand][0]);   //sort total stations according to field
                                                                                //strength (LEVEL) and save to Preset array
                SeekState = Seek_Idle;
            }
            break;

        default:
            break;
    }
}

参与讨论
登录后参与讨论
推荐文章
最近访客