'( ################################################################################ project: FM Radio - RDA5807M chip -------------------------------------------------------------------------------- name : RDA5807M_Test2.bas copyright : (c) Chris Hirt, OE3HBW, Austria, JN87AQ, 2021 purpose : Controller for FM Radio with RDA5807M micro : ATmega644-20PU programmer : homebrew (STK200/STK300 emulation) compiler : MCS BASCOM-AVR 2.0.8.4 - 001 flash : 5% code build : V0.92 @ 11092021 (11. Sep. 2021) status : Test -------------------------------------------------------------------------------- ########################### HARDWARE ########################################### '----------------------------- uC ATmega644P 40 PDIP 20 MHz PIN 01 PB0 PORTB.0 T0 / XCK0 02 PB1 PORTB.1 T1 / CLKO 03 PB2 PORTB.2 INT2 / AIN0 04 PB3 PORTB.3 OC0A / AIN1 05 PB4 PORTB.4 OC0B / SS 06 PB5 MOSI ISP MOSI 07 PB6 MISO ISP MISO 08 PB7 SCK ISP SCK 09 /RESET ISP RST 10 VCC +5V 11 GND 0V 12 XTAL2 Quartz 16.0000 MHz 13 XTAL1 Quartz 16.0000 MHz 14 PD0 PORTD.0 RXD --> RS232 15 PD1 PORTD.1 TXD --> RS232 16 PD2 PORTD.2 INT0 / RXD1 17 PD3 PORTD.3 INT1 / TXD1 18 PD4 PORTD.4 OC1B / XCK1 19 PD5 PORTD.5 OC1A 20 PD6 PORTD.6 OC2B / ICP 21 PD7 PORTD.7 OC2A 22 PC0 PORTC.0 TWI SCL --> I2C Bus SCL RDA5807M 23 PC1 PORTC.1 TWI SDA --> I2C Bus SDA RDA5807M 24 PC2 PORTC.2 TCK --> LCD RS 25 PC3 PORTC.3 TMS --> LCD E 26 PC4 PORTC.4 TDO 27 PC5 PORTC.5 TDI 28 PC6 PORTC.6 TOSC1 29 PC7 PORTC.7 TOSC2 30 AVCC VCC +5V 31 AGDN GND 0V 32 AREF ADC REF 33 PA7 PORTA.7 ADC7 --> LCD DB7 34 PA6 PORTA.6 ADC6 --> LCD DB6 35 PA5 PORTA.5 ADC5 --> LCD DB5 36 PA4 PORTA.4 ADC4 --> LCD DB4 37 PA3 PORTA.3 ADC3 38 PA2 PORTA.2 ADC2 39 PA1 PORTA.1 ADC1 40 PA0 PORTA.0 ADC0 ----------------------------- LCD YL162-90 PIN 01 VSS GND 02 VDD +5V 03 V0 -2.5V ! 04 RS Register Select 05 R/W Read/Write --> GND 06 E Enable 07 D0 (Data 0, 4-Bit mod open) 08 D1 (Data 1, 4-Bit mod open) 09 D2 (Data 2, 4-Bit mod open) 10 D3 (Data 3, 4-Bit mod open) 11 D4 (Data 4) 12 D5 (Data 5) 13 D6 (Data 6) 14 D7 (Data 7) 15 Background Light + BL2 Background Light - ----------------------------- RDA5807M Modul RRD-102 V2.0 PIN 01 SDA 02 SCL 03 NC 04 NC 05 VDD 06 GND 07 ROUT 08 LOUT 09 RCK 10 Ant ################################################################################ ') '$PROG &HFF,&HD7,&HD9,&HFC 'Take care! Fuse Bits !!! $regfile = "m644pdef.dat" 'ATmega644P $crystal = 16000000 '16.0 MHz 'stack and framesize not optimized! $hwstack = 128 $framesize = 128 $swstack = 128 $lib "I2C_TWI.lbx" 'include TWI Lib; force BASCOM to use the hardware TWI '----- Config I2C -------------------------------------------------------------- Config SDA = PortC.1 'Configures a port pin for use as serial data SDA Config SCL = PortC.0 'Configures a port pin for use as serial clock SCL Config Twi = 400000 'TWI = I2C clock speed is 400 kHz '----- Config LCD -------------------------------------------------------------- Config LCD = 16 * 2 Config LCDmode = Port Config LCDbus = 4 Config LCDpin = Pin , Db4 = PortA.4 , Db5 = PortA.5 , Db6 = PortA.6 , Db7 = PortA.7 , Rs = PortC.2 , E = PortC.3 '----- Config Ports ------------------------------------------------------------ Config PORTD.7 = Output 'Port D.7 Output --> Buzzer '----- Config RDA5807M --------------------------------------------------------- 'RDA5807M Register @ RDA Datasheet RDA5807M V1.8 August 2014 'My default binary register values for European FM broadcast '---- Writeable Register ---- const RegH02 = &b1000_0010_0000_0101 'register h02 default value 'Reg02_DHIZ = 1 Audio Output normal 'Reg02_DMUTE = 0 Mute 'Reg02_MONO = 0 Stereo 'Reg02_BASS = 0 Bass Boost disabled 'Reg02_RCLK_NCM = 0 Clock always supplied 'Reg02_RCLK_DIM = 0 Not direkt input mode 'Reg02_SEEKUP = 1 Seek up 'Reg02_SEEK = 0 Disable seek 'Reg02_SKMODE = 0 Wrap band limit 'Reg02_CLKMODE = 000 32.7680 kHz Quartz 'Reg02_RDS_EN = 0 RDS/RBDS disable 'Reg02_NEWMETHD = 1 New demodulation method enable 'Reg02_SOFT_RST = 0 Not Soft_Reset 'Reg02_ENABLE = 1 Power Up enable const RegH03 = &b0000_0000_0000_0000 'register h03 default value 'Reg03_CHAN = 0000000000 Channel (FM Europe, 100 kHz) 'Reg03_DIRMODE = 0 Direct control mode disabled 'Reg03_TUNE = 0 Tune disabled 'Reg03_BAND = 00 Band select 87-108 MHz 'Reg03_SPACE = 00 Channel spacing 100 kHz const RegH04 = &b0000_1010_0000_0000 'register h04 default value 'Reg04_RSVD = 0 Reserved 'Reg04_STCIEN = 0 Seek/Tune complete interrupt disable 'Reg04_RBDS = 0 RDS only 'Reg04_DE = 1 Deemphasis 75 us 'Reg04_RDSFIFOC = 0 No clear RDS FIFO 'Reg04_SOFTMUTE = 1 Softmute enable 'Reg04_AFCD = 0 AFC enable 'Reg04_RSVD = 000000000 Reserved (I2S, GPIO 1-3) const RegH05 = &b1000_1000_1010_1011 'register h05 default value 'Reg05_INTMODE = 1 Interrupt mode 'Reg05_SEEKMODE = 00 Default seek mode (not RSSI seek mode) 'Reg05_RSVD = 0 Reserved 'Reg05_SEEKTH = 1000 Seek SNR value 'Reg05_LNA_PORT = 10 LNAP (positiv) 'Reg05_LNA_IC = 10 LNA current 2.5 mA 'Reg05_VOLUME = 1011 DAC gain control (Volume 0-15 log) 11 const RegH06 = &b0000_0000_0000_0000 'register h06 default value 'Reg06_RSVD = 0 Reserved 'Reg06_OPENMODE = 00 Open behind registers reading function 'Reg06_I2S = 0000000000000 I2S parameter const RegH07 = &b0100_0010_0000_0010 'register h07 default value 'Reg07_RSVD = 0 Reserved 'Reg07_THSOFTBL = 10000 Noise soft blend threshold (2 dB steps) 'Reg07_6550MODE = 1 Band 65-76 MHz (normal mode) 'Reg07_RSVD = 0 Reserved 'Reg07_SEEKTHOD = 000000 Mode disable (only with RSSI seek mode) 'Reg07_SOFTBLEN = 1 Softblend enable 'Reg07_FREQMODE = 0 Frequence setting normal const RegH08 = &b0000_0000_0000_0000 'register h08 default value 'Reg_FREQ_DRCT = &b0000_0000_0000_0000 Frequency in expert mode '---- Read only Register ----- 'Reg00 = &b1000_0101_0000_0100 'Reg00_CHIPID = &b1000_0101_0000_0100 = &h5804 'Reg0A = &b0000_0100_0000_0000 'Reg0A_RDSR = 0 RDS/RBDS groupnot ready 'Reg0A_STC = 0 Seek/Tune complete flag (not complete) 'Reg0A_SF = 0 Seek succesfull 'Reg0A_RDSS = 0 RDS decoder not synchronized 'Reg0A_BLK_E = 0 RDS Block E not found 'Reg0A_ST = 1 Stereo indicator - Stereo 'Reg0A_READCHAN = 0000000000 Read channel 'Reg0B = &b0000_0000_0000_0000 'Reg0B_RSSI = 0000000 RSSI 'Reg0B_FM_TRUE = 0 Current channel is not a station 'Reg09_FM_READY = 0 FM not ready 'Reg0B_RSVD = 00 Reserved 'Reg0B_ABCDE = 0 Block ID of register h0C, h0D, h0E, h0F 'Reg0B_BLERA = 00 RDS_DATA_0 - Null errors requiring correction 'Reg0B_BLERB = 00 RDS_DATA_1 - Null errors requiring correction 'Reg0C = &b0101_1000_0000_0011 'Reg0C_RDSA = &h5803 'Reg0D = &b0101_1000_0000_0100 'Reg0D_RDSB = &h5804 'Reg0E = &b0101_1000_0000_1000 'Reg0E_RDSC = &h5808 'Reg0F = &b0101_1000_0000_0100 'Reg0F_RDSD = &h5804 '---- RDA5807M chip I2C address ---- 'Device Address = h10 for sequential read/write mode 'Device Address = h11 for random access read/write mode 'Sequential read/write mode Const SEQ_WriteAdr = &h20 Const SEQ_ReadAdr = &h21 'Random Access read/write mode Const RND_WriteAdr = &h22 Const RND_ReadAdr = &h23 '------------------------------------------------------------------------------- 'FM BC Band in Europe Const FMstart = 87000000 'Start frequency = 87 MHz Const FMend = 108000000 'End frequency = 108 MHz '----- FM Frequencies and BC stations at JN87AQ ---------- 'FMfreq = 88.2 MHz == BC Ö3 'FMfreq = 90.3 MHz == BC Ö1 'FMfreq = 92.4 MHz == BC FM4 'FMfreq = 95.8 MHz == BC Ö2 NÖ 'FMfreq = 102.9 MHz == BC Kronehit Const FMfreq1 = 88200000 'Test frequeny 1 Const FMfreq2 = 90300000 'Test frequeny 2 Const FMfreq3 = 92400000 'Test frequeny 3 '----- Declarations ------------------------------------------------------------ Dim Freq As DWord 'FM frequency in Hz Dim hB As Byte Dim lB As Byte Dim regW As Word Dim ChipID As Word Dim STC As Byte Dim fx As DWord Dim ChanDW As DWord Dim ChanW As Word Dim ChanH As Byte Dim ChanL As Byte Dim MT As Word Dim rssi As Word Dim SS As Word Dim Vol As Word Dim BB As Word Declare Sub I2CwriteRegister(ByVal reg As Byte , ByVal cont As Word) Declare Function I2CreadRegister(ByVal reg As Byte) As Word Declare Sub RDA5807M_readID() Declare Sub RDA5807M_SetMyDefault() Declare Sub RDA5807M_Init() Declare Sub RDA5807M_ReadSTC() Declare Sub RDA5807M_SetMute() Declare Sub RDA5807M_SetFrequency(ByVal newF As DWord , ByVal fspace As DWord) Declare Function RDA5807M_GetRSSI() As Byte Declare Sub RDA5807M_SetVolume(ByVal VolNew As Word) Declare Sub RDA5807M_SetBassBoost() '####################### Main ################################################## Cls 'Clear display Cursor Off Noblink 'Hide cursor LCD "RDA5807M Radio" Lowerline LCD "OE3HBW" I2Cinit 'Initializes the I2C SCL and SDA pins Wait 3 Cls Call RDA5807M_readID() 'Read Chip ID If ERR = 1 Then 'I2C Error LCD "I2C Error" Wait 5 End End If If ChipID <> &h5804 Then LCD "ChipID Error" 'ChipID Error Wait 5 End End If Call RDA5807M_Init() 'Initialized RDA5807M '---- The following sequencies are only for test useful ---- Freq = FMfreq1 'Frequency 88.2 MHz only for test Call RDA5807M_SetFrequency(Freq , 100000) LCD "Frequenz" Lowerline LCD Freq ; " Hz " Wait 3 Call RDA5807M_SetMute() 'Change Mute state Cls Freq = FMfreq2 'Frequency 90.3 MHz only for test Call RDA5807M_SetFrequency(Freq , 100000) LCD "Frequenz" Lowerline LCD Freq ; " Hz " Wait 3 'Show Frequency Call RDA5807M_SetMute() 'Change Mute state Cls Freq = FMfreq3 'Frequency 92.4 MHz only for tTest Call RDA5807M_SetFrequency(Freq , 100000) LCD "Frequenz" Lowerline LCD Freq ; " Hz " Wait 3 'Show Frequency Cls LCD "RSSI" Lowerline SS = RDA5807M_GetRSSI() LCD SS ; " units" Call RDA5807M_SetVolume(&b1111) 'Test Volume Control Wait 2 Call RDA5807M_SetVolume(&b0000) Wait 2 Call RDA5807M_SetVolume(&b1111) Call RDA5807M_SetBassBoost() 'and Bass too End 'of program '######################## Subs ################################################# Sub I2CwriteRegister(ByVal reg As Byte , ByVal cont As Word) 'write cont in register reg I2cstart 'START condition I2cwbyte RND_WriteAdr 'RDA5807M address / random write mode I2cwbyte reg 'Register reg I2cwbyte High(cont) 'High Byte of cont I2cwbyte Low(cont) 'Low Byte of cont I2cstop 'STOP condition End Sub Function I2CreadRegister(ByVal reg As Byte) As Word 'read from reg register I2cstart 'START condition I2cwbyte RND_WriteAdr 'RDA5807M address / random write mode I2cwbyte reg 'Register reg I2cstop 'STOP condition I2cstart 'START condition I2cwbyte RND_ReadAdr 'RDA5807M address / random read mode I2crbyte hB , ack 'read high Byte I2crbyte lB , Nack 'read low Byte I2cstop 'STOP condition regW = hb 'hb in word LSB shift regW , left , 8 'shift to MSB regW = regW + lB 'lB in LSB I2CreadRegister = regW 'return register Word End Function Sub RDA5807M_readID() 'Read Chip ID ChipID = I2CreadRegister(&h00) End Sub Sub RDA5807M_SetMyDefault() 'Set my default values I2cstart 'START condition I2cwbyte SEQ_WriteAdr 'RDA5807M address / sequential write mode I2cwbyte High(RegH02) '&b1000_0010 I2cwbyte Low(RegH02) '&b0000_0101 I2cwbyte High(RegH03) '&b0000_0000 I2cwbyte Low(RegH03) '&b0000_0000 I2cwbyte High(RegH04) '&b0000_1010 I2cwbyte Low(RegH04) '&b0000_0000 I2cwbyte High(RegH05) '&b1000_1000 I2cwbyte Low(RegH05) '&b1010_1011 I2cwbyte High(RegH06) '&b0000_0000 I2cwbyte Low(RegH06) '&b0000_0000 I2cwbyte High(RegH07) '&b0100_0010 I2cwbyte Low(RegH07) '&b0000_0010 I2cwbyte High(RegH08) '&b0000_0000 I2cwbyte Low(RegH08) '&b0000_0000 I2cstop 'STOP condition End Sub Sub RDA5807M_Init() 'Initialize chip Call I2CwriteRegister(&h02 , &b0000_0000_0000_0010) 'SOFT_RESET - reset chip Waitms 50 'Wait 50 ms Call I2CwriteRegister(&h02 , &b0000_0000_0000_0001) 'ENABLE = 1 - Power On Waitms 600 'Stabilization Call RDA5807M_SetMyDefault() 'Set my default values End Sub Sub RDA5807M_ReadSTC() 'Read Seek/Tune complete flag STC = &h00 'STC = 0 --> Seek/Tune not complete While STC = &h00 I2cstart 'START condition I2cwbyte RND_WriteAdr 'RDA5807M address / random write mode I2cwbyte &h0A 'Register h0A I2cstop 'STOP condition I2cstart 'START condition I2cwbyte RND_ReadAdr 'RDA5807M address / random read mode I2crbyte STC , Nack 'read high Byte in STC Byte I2cstop 'STOP condition STC = STC AND &b0100_0000 'STC = 1 --> Seek/Tune complete flag WEnd End Sub Sub RDA5807M_SetMute() 'Set mute/unmute - change state MT = I2CreadRegister(&h02) If MT.14 = 1 Then 'No mute - normnal operation MT = MT XOR &b0100_0000_0000_0000 'Change to mute Else 'Mute MT = MT OR &b0100_0000_0000_0000 'Change to normal operation End If Call I2CwriteRegister(&h02 , MT) 'Write Mute state End Sub Sub RDA5807M_SetFrequency(ByVal newF As DWord , ByVal fspace As DWord) 'Set Frequency If newF < FMstart Then newF = FMstart 'Lowest frequency of EU Band If newF > FMend Then newF = FMend 'Highest frequency of EU Band fx = newF - FMstart 'Difference frequency ChanDW = fx / fspace 'Channel ChanW = ChanDW 'Typecasting Shift ChanW , right , 2 'Shift right - Channel in High Byte ChanH = Low(ChanW) ChanW = ChanDW 'Typecasting Shift ChanW , left , 6 'Shift left - Channel in Low Byte ChanL = Low(ChanW) ChanL = ChanL OR &b0001_0000 'Chan LowByte with Tuning enable Bit = 1 I2cstart 'START condition I2cwbyte RND_WriteAdr 'RDA5807M address / random write mode I2cwbyte &h03 'Register h03 I2cwbyte ChanH 'High Byte I2cwbyte ChanL 'Low Byte I2cstop Call RDA5807M_ReadSTC() 'Wait for Seek/Tune complete Call RDA5807M_SetMute() 'Change Mute state End Sub Function RDA5807M_GetRSSI() As Byte 'Return radio station strength information rssi = I2CreadRegister(&h0B) Shift rssi , right , 10 RDA5807M_GetRSSI = rssi End Function Sub RDA5807M_SetVolume(ByVal VolNew As Word) 'Set Volume on Register h05 VolNew = VolNew AND &b0000_0000_0000_1111 Vol = I2CreadRegister(&h05) Vol = Vol XOR &b0000_0000_0000_1111 Vol = Vol OR VolNew Call I2CwriteRegister(&h05, Vol) End Sub Sub RDA5807M_SetBassBoost() 'Set BassBoost on Register h02 BB = I2CreadRegister(&h02) If BB.12 = 1 Then BB = BB OR &b0001_0000_0000_0000 Else BB = BB XOR &b0001_0000_0000_0000 End If Call I2CwriteRegister(&h02 , BB) End Sub '############################ End Of File ######################################