Menu

TEA5767 FM Radio I2C library

2014-11-30
2014-12-02
  • Dimitris Katsaounis

    The TEA5767HL is a single-chip electronically tuned FM stereo radio for low-voltage applications with fully integrated IF selectivity and demodulation. The radio is completely adjustment-free and only requires a minimum of small and low cost external components. The radio can be tuned to the European, US and Japanese FM bands.

    The Library can be used with RDA5807P chip in TEA5767 Mode

    #define TEA5767_AddrWrite 192
    #define TEA5767_AddrRead 193

    Dim RadioConfigReg(5)
    Dim RadioStatusReg(5)
    Dim pll as Long
    Dim RFreqI as Byte
    Dim RFreqD as Byte
    Dim RadioFreq as Word

    Sub SetRadioFreq (In RFreqI, In RFreqD) 'RFreqI Integral part of frequency, RFreqD Decimal part of frequency
    pll=RFreqI*1000000+((RFreqD*100000)/10)+225000
    RadioFreq=pll/8192
    if SysCalcTempX >= 50 then
    RadioFreq=[word]RadioFreq +1
    end if
    RadioConfigReg(2)=RadioFreq%256
    RadioConfigReg(1)=RadioFreq/256
    Call RadioWrite
    End Sub

    Sub ReadRadioFreq (Out RadioFreq, Out RFreqI, Out RFreqD)
    Call RadioRead
    temp=RadioStatusReg(1) AND 63
    RadioFreq=256*temp+RadioStatusReg(2)
    pll=RadioFreq*8192
    RFreqI=pll/1000000
    RFreqD=(pll-RFreqI*1000000-255000)/10000
    if RFreqD>=100 then
    RFreqD=[word]RFreqD/10
    if SysCalcTempX >= 5 then
    RFreqD=[word]RFreqD+1
    end if
    end if
    End Sub

    Sub RadioInit
    SeekTimes=0 'After 2 attempts stop seeking
    RadioFreq=10709 '87.5MHZ
    RadioConfigReg(1)=0 'No MUTE, Not in search mode
    RadioConfigReg(1)=RadioFreq/256
    RadioConfigReg(2)=RadioFreq%256
    RadioConfigReg(3)=16 'No mute channels , Stereo , High side LO injection
    RadioConfigReg(4)=18 'Stereo Noise Canceling, XTAL=32.768KHz, US_Europe Band
    RadioConfigReg(5)=0
    Call RadioWrite
    End sub

    Sub Seek (In SeekDir) 'Up Down
    Call SetRadioFreq (RFreqI, RFreqD)
    RadioConfigReg(1).7=1
    RadioConfigReg(1).6=1
    if SeekDir="Up" then
    RadioConfigReg(3).7=1
    else
    RadioConfigReg(3).7=0
    end if
    RadioConfigReg(3).6=0
    RadioConfigReg(3).5=1
    Call RadioWrite
    ContinueSeek:
    wait 50 ms
    Call RadioRead
    if RadioStatusReg(1).7=0 then Goto ContinueSeek
    Call ReadRadioFreq (RadioFreq, RFreqI, RFreqD)
    if RadioStatusReg(1).7=1 then
    if RadioStatusReg(1).6=1 then
    if SeekDir="Up" then
    Call SetRadioFreq (87, 50)
    else
    Call SetRadioFreq (108, 00)
    end if
    RadioConfigReg(1).7=1
    RadioConfigReg(1).6=1
    if SeekDir="Up" then
    RadioConfigReg(3).7=1
    else
    RadioConfigReg(3).7=0
    end if
    RadioConfigReg(3).6=0
    RadioConfigReg(3).5=1
    Call RadioWrite
    wait 100 ms
    SeekTimes=SeekTimes+1
    if SeekTimes>=2 then
    SeekTimes=0
    Exit Sub
    end if
    Goto ContinueSeek
    end if
    RadioConfigReg(1).7=0
    RadioConfigReg(1).6=0
    Call RadioWrite
    SeekTimes=0
    Exit Sub
    end if
    End sub

    Sub SeekUp
    Call SetRadioFreq (RFreqI, RFreqD)
    RadioConfigReg(1).7=1
    RadioConfigReg(1).6=1
    RadioConfigReg(3).7=1
    RadioConfigReg(3).6=0
    RadioConfigReg(3).5=1
    Call RadioWrite
    ContinueSeek:
    wait 50 ms
    Call RadioRead
    if RadioStatusReg(1).7=0 then Goto ContinueSeek
    Call ReadRadioFreq (RadioFreq, RFreqI, RFreqD)
    if RadioStatusReg(1).7=1 then
    if RadioStatusReg(1).6=1 then
    Call SetRadioFreq (87, 50)
    RadioConfigReg(1).7=1
    RadioConfigReg(1).6=1
    RadioConfigReg(3).7=1
    RadioConfigReg(3).6=0
    RadioConfigReg(3).5=1
    Call RadioWrite
    wait 100 ms
    SeekTimes=SeekTimes+1
    if SeekTimes>=2 then
    SeekTimes=0
    Exit Sub
    end if
    Goto ContinueSeek
    end if
    RadioConfigReg(1).7=0
    RadioConfigReg(1).6=0
    Call RadioWrite
    SeekTimes=0
    Exit Sub
    end if
    End sub

    Sub SeekDown
    Call SetRadioFreq (RFreqI, RFreqD)
    RadioConfigReg(1).7=1
    RadioConfigReg(1).6=1
    RadioConfigReg(3).7=0
    RadioConfigReg(3).6=0
    RadioConfigReg(3).5=1
    Call RadioWrite
    ContinueSeek:
    wait 50 ms
    Call RadioRead
    if RadioStatusReg(1).7=0 then Goto ContinueSeek
    Call ReadRadioFreq (RadioFreq, RFreqI, RFreqD)
    if RadioStatusReg(1).7=1 then
    if RadioStatusReg(1).6=1 then
    Call SetRadioFreq (108, 00)
    RadioConfigReg(1).7=1
    RadioConfigReg(1).6=1
    RadioConfigReg(3).7=0
    RadioConfigReg(3).6=0
    RadioConfigReg(3).5=1
    Call RadioWrite
    wait 100 ms
    SeekTimes=SeekTimes+1
    if SeekTimes>=2 then
    SeekTimes=0
    Exit Sub
    end if
    Goto ContinueSeek
    end if
    RadioConfigReg(1).7=0
    RadioConfigReg(1).6=0
    Call RadioWrite
    SeekTimes=0
    Exit Sub
    end if
    End sub

    Sub SeekStop
    RadioConfigReg(1).6=0
    Call RadioWrite
    End sub

    Sub RadioMono
    RadioConfigReg(3).3=1
    Call RadioWrite
    End sub

    Sub RadioStereo
    RadioConfigReg(3).3=0
    Call RadioWrite
    End sub

    Sub Mute
    RadioConfigReg(1).7=1
    Call RadioWrite
    End sub

    Sub UnMute
    RadioConfigReg(1).7=0
    Call RadioWrite
    End sub

    Sub RadioStandby
    RadioConfigReg(4).6=1
    Call RadioWrite
    End sub

    Sub RadioPlay
    RadioConfigReg(4).6=0
    Call RadioWrite
    End sub

    Sub JapaneseBand ' 76-90MHz
    RadioConfigReg(4).5=1
    Call RadioWrite
    End sub

    Sub US_EuropeBand ' 87.5-108MHz
    RadioConfigReg(4).5=0
    Call RadioWrite
    End sub

    Function StationFound
    Call RadioRead
    if RadioStatusReg(1).7=1 and RadioStatusReg(1).6=0 then
    StationFound=True
    else
    StationFound=False
    end if
    End Function

    Function RadioBandLimit
    Call RadioRead
    if RadioStatusReg(1).6=1 then
    RadioBandLimit=True
    else
    RadioBandLimit=False
    end if
    End Function

    Function StereoReception
    Call RadioRead
    if RadioStatusReg(3).7=1 then
    StereoReception=True
    else
    StereoReception=False
    end if
    End Function

    Sub RadioWrite
    I2CSTART
    I2CSEND ( TEA5767_AddrWrite, True )
    I2CSEND (RadioConfigReg(1), ACK)
    I2CSEND (RadioConfigReg(2), ACK)
    I2CSEND (RadioConfigReg(3), ACK)
    I2CSEND (RadioConfigReg(4), ACK)
    I2CSEND (RadioConfigReg(5), ACK)
    I2CStop
    End sub

    Sub RadioRead
    I2CSTART
    I2CSEND ( TEA5767_AddrRead , True)
    I2CReceive (RadioStatusReg(1) , ACK )
    I2CReceive (RadioStatusReg(2) , ACK )
    I2CReceive (RadioStatusReg(3) , ACK )
    I2CReceive (RadioStatusReg(4) , ACK )
    I2CReceive (RadioStatusReg(5) , NACK )
    I2CStop
    End sub

    My example program:

    'Chip Settings
    #chip 18F2550, 8
    #config FOSC_INTOSC_HS, WDT=OFF, LVP=OFF, MCLRE=OFF, BOR=OFF

    'Includes
    #include "TEA5767.h"

    'Defines (Constants)
    #define LCD_NO_RW
    #define LCD_IO 4
    #define LCD_RS PORTB.5
    #define LCD_Enable PORTB.4
    #define LCD_DB4 PORTB.3
    #define LCD_DB5 PORTB.2
    #define LCD_DB6 PORTB.1
    #define LCD_DB7 PORTB.0
    #define I2C_MODE Master
    #define I2C_DATA PORTA.3
    #define I2C_CLOCK PORTA.4

    'Variables

    Dim RadioConfigReg(5)
    Dim RadioStatusReg(5)
    Dim pll as Long
    Dim RFreqI as Byte
    Dim RFreqD as Byte
    Dim RadioFreq as Word

    'Directions
    Dir PortB out
    dir PortA Out
    dir PortC Out

    wait 1 s
    cls
    locate 0,4: print "FM Radio"
    locate 1,4: print "TEA5767"
    wait 2 s
    cls
    Call RadioInit
    Call SetRadioFreq (87, 50)
    locate 0,0
    print RadioFreq
    locate 1,0
    xx=pll/10000
    print xx
    wait 5 s

    start:
    call SeekUp
    wait 1 s
    Call RadioRead
    cls
    locate 0,4: print RFreqI:print",":print RFreqD:print "MHz"
    locate 1,0: print RadioFreq
    locate 1,6: print RadioStatusReg(1).7
    locate 1,7: print RadioStatusReg(1).6
    wait 10 s

    goto start

     
    • Peter

      Peter - 2014-11-30

      That looks neater than my way of doing it :)

       

      Last edit: Peter 2014-11-30
    • Anobium

      Anobium - 2014-12-01

      This look very good.

      Can we publish as part of the next GCB release? I will add headers etc. for you.

      Would it be possible for you to post the example and the library as a zip as I want to ensure I have the good code with no formatting removed.

      Thank you.

       
  • Dimitris Katsaounis

    Thanks Anobium

    Here my zip file

     
  • Anobium

    Anobium - 2014-12-01

    @Dimitris. Are you able to share a device/product URL? I see many different devices on eBay but it would be good to known what you used.

    Thank you.

     

Log in to post a comment.