Jose Javier Calderon
Published © GPL3+

FM Radio TEA5767

Raspberry Pi FM Radio TEA5767

BeginnerFull instructions provided21,533
FM Radio TEA5767

Things used in this project

Hardware components

Raspberry Pi 2 Model B
Raspberry Pi 2 Model B
×1
TEA5767 76-108MHZ FM Stereo Radio Module
×1
Monitor, keyboard, mouse for Pi desktop mode or a laptop if connecting remotely via SSH.
×1
small speaker or earphone
×1
Power adapter running 5V and 2A
×1
A breadboard and a couple of male-to-female connector wires
×1
antenna
×1

Story

Read more

Schematics

Diagram

breadboard

Code

MainPage.xaml

YAML
<Page
    x:Class="FM_Radio_Station.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:FM_Radio_Station"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d">

    <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
        <Canvas HorizontalAlignment="Left" Height="768" VerticalAlignment="Top" Width="1024" Margin="0,2,0,-2">
            <Canvas.Background>
                <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                    <GradientStop Color="Black" Offset="0.518"/>
                    <GradientStop Color="#FF060832" Offset="0.818"/>
                    <GradientStop Color="#FF05073C" Offset="0.201"/>
                    <GradientStop Color="#FF010108" Offset="0.694"/>
                    <GradientStop Color="#FF05060C" Offset="0.328"/>
                </LinearGradientBrush>
            </Canvas.Background>
            <Button x:Name="button" Content="1 FM"  Height="105" Canvas.Left="65" Canvas.Top="603" Width="250" BorderBrush="#FF1B302F" HorizontalAlignment="Center" FontWeight="Medium" FontSize="64" Foreground="White" Click="button_Click">
                <Button.Background>
                    <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                        <GradientStop Color="Black" Offset="0"/>
                        <GradientStop Color="#FF1C1C21" Offset="0.573"/>
                        <GradientStop Color="#FF01020F" Offset="0.956"/>
                    </LinearGradientBrush>
                </Button.Background>
            </Button>
            <Button x:Name="button2" Content="2 FM"  Height="105" Foreground="White" Canvas.Left="380" Canvas.Top="603" Width="250" BorderBrush="#FF1B302F" HorizontalAlignment="Center" FontWeight="Medium" FontSize="64" Click="button2_Click">
                <Button.Background>
                    <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                        <GradientStop Color="Black" Offset="0"/>
                        <GradientStop Color="#FF1C1C21" Offset="0.573"/>
                        <GradientStop Color="#FF01020F" Offset="0.956"/>
                    </LinearGradientBrush>
                </Button.Background>
            </Button>
            <Button x:Name="button3" Content="3 FM" Foreground="White" Height="105" Canvas.Left="690" Canvas.Top="603" Width="250" BorderBrush="#FF1B302F" HorizontalAlignment="Center" FontWeight="Medium"  FontSize="64" Click="button3_Click">
                <Button.Background>
                    <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                        <GradientStop Color="Black" Offset="0"/>
                        <GradientStop Color="#FF1C1C21" Offset="0.573"/>
                        <GradientStop Color="#FF01020F" Offset="0.956"/>
                    </LinearGradientBrush>
                </Button.Background>
            </Button>
            <TextBox x:Name="Title" Height="145" Foreground="WhiteSmoke" TextWrapping="Wrap" Text="FM STATION" Canvas.Top="68" Width="1024" TextAlignment="Center" HorizontalAlignment="Center" VerticalAlignment="Center" FontSize="96" BorderBrush="{ThemeResource AppBarItemPointerOverBackgroundThemeBrush}" SelectionHighlightColor="{x:Null}" Background="{ThemeResource ApplicationForegroundThemeBrush}"/>
            <Button x:Name="button1" Canvas.Left="85" Canvas.Top="263" Height="250" Width="275" Click="button1_Click">
                <Button.Background>
                    <ImageBrush Stretch="Fill" ImageSource="Assets/Actions-media-seek-backward-icon.png"/>
                </Button.Background>
            </Button>
            <Button x:Name="button1_Copy" Canvas.Left="645" Canvas.Top="263" Height="250" Width="275" Click="button1_Copy_Click">
                <Button.Background>
                    <ImageBrush Stretch="Fill" ImageSource="Assets/Actions-media-seek-forward-icon.png"/>
                </Button.Background>
            </Button>
            <TextBox x:Name="Text_Status" Foreground="WhiteSmoke" Height="55" Canvas.Left="10" TextWrapping="Wrap" Text="TextBox" Canvas.Top="543" Width="1004" TextAlignment="Center"  HorizontalAlignment="Center" VerticalAlignment="Center" BorderBrush="{ThemeResource AppBarItemPointerOverBackgroundThemeBrush}" SelectionHighlightColor="{x:Null}" Background="{ThemeResource ApplicationForegroundThemeBrush}" FontSize="32"/>
        </Canvas>

    </Grid>
</Page>

MainPage.xaml.cs

C#
using System;
using System.Threading.Tasks;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.Devices.Enumeration;
using Windows.Devices.I2c;

// The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=402352&clcid=0x409

namespace FM_Radio_Station
{
    /// <summary>
    /// An empty page that can be used on its own or navigated to within a Frame.
    /// </summary>
    public sealed partial class MainPage : Page
    {

        private const byte TEA5756_I2C_ADDR = 0x60;      /* 7-bit I2C address of the TEA5756 */
        private I2cDevice TEA5767;
        double frequency = 106.9;
        double frequency1 = 104.7;
        double frequency2 = 100.7;
        double frequencyCurrent;
        int cof = 32768; //crystal constant


        public MainPage()
        {
            this.InitializeComponent();

            InitI2CTEA5756();
        }


        public async void InitI2CTEA5756()
        {
            try
            {
                var settings = new I2cConnectionSettings(TEA5756_I2C_ADDR);// 0x60 is the I2C device address
                settings.BusSpeed = I2cBusSpeed.FastMode;                       /* 400KHz bus speed */
                string aqs = I2cDevice.GetDeviceSelector("I2C1");  // Get a selector string for bus "I2C1"
                var dis = await DeviceInformation.FindAllAsync(aqs); // Find the I2C bus controller with our selector string
                TEA5767 = await I2cDevice.FromIdAsync(dis[0].Id, settings);    /* Create an I2cDevice with our selected bus controller and I2C settings    */
                mute();

                if (TEA5767 == null)
                {
                    Text_Status.Text = string.Format(
                        "Slave address {0} on I2C Controller {1} is currently in use by " +
                        "another application. Please ensure that no other applications are using I2C.");
                    return;
                }
                else
                {
                    Title.Text = string.Format(" FM 1 " + frequency + " MHz ");
                    Text_Status.Text = string.Format("La Mega station");
                    setFrequency(frequency);

                }

            }
            catch (Exception ex)
            {
                Text_Status.Text = "I2C Initialization failed. Exception: " + ex.Message;
                return;
            }
        }

        void setFrequency(double frequency)
        {
            int frequencyB = (int)(4 * (frequency * 1000000 + 225000) / 32768);
            byte frequencyH = (byte)(frequencyB >> 8);
            byte frequencyL = (byte)(frequencyB & 0xFF);
            byte[] writeBuf = { frequencyH, frequencyL, (byte)0xB0, (byte)0x10, (byte)0x00 };
            TEA5767.Write(writeBuf);
        }

        double getFrequency()
        {
            double frequency = 0.0;
            byte[] readBuf = { (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00 };
            TEA5767.Read(readBuf);
            frequency = ((readBuf[0] & 0x3F) << 8) + readBuf[1];
            // Determine the current frequency using the same high side formula as above
            return Math.Round(Math.Round(frequency * 32768 / 4 - 225000) / 1000000, 1);
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            scan(0);
        }

        private async void scan(int direction)
        {
            double fadd = 0;
            bool scanLoop = true;
            int readyFlag = 0;
            int blf = 0;
            int stereoflag = 0;
            int softMute = 0;
            int standbyFlag = 0;
            int sTune = 0;

            if (direction == 1) //direccion de la flecha de busqueda
                fadd = 0.1;
            else
                fadd = -0.1;

            while (scanLoop)
            {
                frequencyCurrent = getFrequency(); // #get current frequency
                frequencyCurrent = frequencyCurrent + fadd;

                if (frequencyCurrent < 87.5)
                    frequencyCurrent = 108;
                else if (frequencyCurrent > 107.9)
                    frequencyCurrent = 87.5;

                setFrequency(frequencyCurrent);
                Title.Text = string.Format("FM 1 " + (frequencyCurrent) + " MHz");


                await Task.Delay(TimeSpan.FromMilliseconds(0.90));//# give time to finish writing, and then read status
                byte[] results = { (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00 };
                //# give time to finish writing, and then read status
                TEA5767.Read(results);

                readyFlag = (results[0] & 0x80) / 128;
                stereoflag = (results[2] & 0x80) / 128;
                softMute = (results[3] & 0x10) / 10;
                standbyFlag = (results[3] & 0x40) / 64;
                sTune = (results[3] & 0x80) / 128;
                blf = (results[0] & 0x40) / 64;

                if (standbyFlag == 1 && sTune == 1 && readyFlag == 1 &&  blf == 0 )
                {
                    scanLoop = false;
                }
                else
                {
                     scanLoop = true;
                }

            }


        }

        private void button1_Copy_Click(object sender, RoutedEventArgs e)
        {
            scan(1);
        }

        void mute()
        {
            //MUTE
            //data 1 - 0x80 = 1 0 0 0 0 0 0 0
            //data 2 - 0x00 = 0 0 0 0 0 0 0 0
            //data 3 - 0x06 = 0 0 0 0 0 1 1 0
            //data 4 - 0x0A = 0 0 0 0 1 0 1 0
            //data 5 - 0x00 = 0 0 0 0 0 0 0 0

            byte[] data = { (byte)0x80, (byte)0x00, (byte)0x06, (byte)0x0A, (byte)0x00 };
            TEA5767.Write(data);

        }

        private void button_Click(object sender, RoutedEventArgs e)
        {
            setStation(frequency2, "La X");
        }

        private void button2_Click(object sender, RoutedEventArgs e)
        {
            setStation(frequency1, "KQ 105");
        }

        private void button3_Click(object sender, RoutedEventArgs e)
        {
            setStation(frequency, "La Mega station");
        }

        private void setStation(double frequency, String  StationName)
        {
            Title.Text = string.Format(" FM 1 " + frequency + " MHz ");
            Text_Status.Text = string.Format(StationName);
            setFrequency(frequency);
        }    
            
            
    }
}

FM Station Radio

Credits

Jose Javier Calderon

Jose Javier Calderon

1 project • 5 followers
Computer Engineer and Development

Comments