'( ################################################################################ project: FM Radio - RDA5807M chip -------------------------------------------------------------------------------- name : RDA5807M_Test6.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 : V36 @ 14122021 (14. Dec. 2021) status : Test Seek Function -------------------------------------------------------------------------------- ########################### 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 RDA5807M chip ---------------------------------------------------- 'All information according to the document RDA5807M Single-Chip Broadcast FM 'Radio Tuner Rev. 1.8 - August 2014 from RDA Microelectronics 2006 'RSVD is the abbreviation for reserved register bits with undocumented function '---- Documented writeable registers ---- const RegH02 = &b1000_0000_0000_0101 'register h02 default value 'DHIZ = 1 Audio Output normal 'DMUTE = 0 Mute on 'MONO = 0 Stereo on 'BASS = 0 Bass Boost disabled 'RCLK_NCM = 0 Clock always supplied 'RCLK_DIM = 0 Not direkt input mode (Quartz is used) 'SEEKUP = 0 Seek down 'SEEK = 0 Disable seek 'SKMODE = 0 Wrap at band limit 'CLKMODE = 000 32.7680 kHz Quartz 'RDS_EN = 0 RDS/RBDS disable 'NEWMETHD = 1 New demodulation method enable ??? 'SOFT_RST = 0 Not Soft_Reset 'ENABLE = 1 Power Up enable const RegH03 = &b0100_1111_1100_0000 'register h03 default value 'CHAN = 0100111111 Channel (default = h013F) 'DIRMODE = 0 Direct Control Mode disabled ??? 'TUNE = 0 Tune disabled 'BAND = 00 Band select 87-108 MHz (BC Europe) 'SPACE = 00 Channel spacing 100 kHz (BC Europe) const RegH04 = &b0000_1110_0000_0000 'register h04 default value 'RSVD = 0 Reserved 'STCIEN = 0 Seek/Tune complete interrupt disable --> no GPIO2 'RBDS = 0 RDS only 'RDSFIFOE = 0 RDS FIFO Mode disable 'DE = 1 Deemphasis 50 us (BC Europe) 'RDSFIFOC = 1 Clear RDS FIFO 'SOFTMUTE = 1 Softmute enable 'AFCD = 0 AFC enable 'RSVD = 00000000 Reserved (I2S, GPIO 1-3) const RegH05 = &b1000_1000_1010_1011 'register h05 default value 'INTMODE = 1 Interrupt mode ??? 'SEEKMODE = 00 New seek mode on; RSSI Seek mode off 'RSVD = 0 Reserved 'SEEKTH = 1000 Seek SNR threshold value 'LNA_PORT = 10 LNAP (positiv input) 'LNA_IC = 10 LNA current 2.5 mA (what is better ?) 'VOLUME = 1011 DAC gain control (Volume 0-15 log) const RegH06 = &b0000_0000_0000_0000 'register h06 default value 'RSVD = 0 Reserved 'OPENMODE = 00 Only reading reserved registers 'I2S = 0000000000000 I2S parameter --> no I2S hardware ???? const RegH07 = &b0100_0010_0000_0010 'register h07 default value 'RSVD = 0 Reserved 'THSOFTBL = 10000 Noise soft blend threshold (2 dB steps) '6550MODE = 1 Default = 1 'RSVD = 0 Reserved 'SEEKTHOD = 000000 Threshold for old Seek mode (RSSI mode) 'SOFTBLEN = 1 Softblend enable 'FREQMODE = 0 No frequence seting in direct mode const RegH08 = &b0000_0000_0000_0000 'register h08 default value 'FREQ_DRCT No direct frequency setting '---- Documented read-only registers ----- 'Reg00 = &b1000_0101_0000_0100 'CHIPID = &b1000_0101_0000_0100 = &h5804 'Reg0A = &b0000_0100_0000_0000 'RDSR = 0 RDS/RBDS group not ready 'STC = 0 Seek/Tune complete flag (not complete) 'SF = 0 Seek succesfull 'RDSS = 0 RDS decoder not synchronized 'BLK_E = 0 RDS Block E not found 'ST = 1 Stereo indicator - Stereo 'READCHAN = 0100111111 Channel value (default = h013F) 'Reg0B = &b0000_0000_0000_0000 'RSSI = 0000000 RSSI 'FM_TRUE = 0 Current channel is not a station 'FM_READY = 0 FM not ready 'RSVD = 00 Reserved 'ABCDE = 0 Block ID of register h0C, h0D, h0E, h0F 'BLERA = 00 RDS_DATA_0 - Null errors requiring correction 'BLERB = 00 RDS_DATA_1 - Null errors requiring correction 'Reg0C = RDSA RDS Block A 'Reg0D = RDSB RDS Block B 'Reg0E = RDSC RDS Block C 'Reg0F = RDSD RDS Block D '---- 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 '---- Frequency range for BC in Europe ---- Const FMstart = 87500000 'Start frequency in Hz Const FMend = 108000000 'End frequency in Hz '----- Declarations ------------------------------------------------------------ Dim Freq As DWord 'FM frequency in Hz Dim hB As Byte Dim lB As Byte Dim regW As Word Dim stat1 As Word Dim stat2 As Word Dim STC As Byte Dim SF As Byte Dim RChan As Word Dim FMtrue As Byte Dim Reg As Word Dim Vol As Word Dim STHW As Word Dim STO As Byte Dim fspace As DWord Dim Chan As DWord Dim Val1 As DWord Dim Value As DWord Dim MT As Word Dim rssi 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 Function Chan2Freq(ByVal Chan As Word) As DWord Declare Sub RDA5807M_SetMyDefault() Declare Sub RDA5807M_Init() Declare Sub RDA5807M_SetMuteOn() Declare Sub RDA5807M_SetMuteOff() Declare Sub RDA5807M_SetVolume(ByVal VolNew As Word) Declare Sub RDA5807M_SetBassBoostOn() Declare Sub RDA5807M_SetBassBoostOff() Declare Sub RDA5807M_GetStatus() Declare Sub RDA5807M_SetSNRthres(ByVal STH As Byte) Declare Sub RDA5807M_SetSeekParam() Declare Sub RDA5807M_Seek() '####################### Main ################################################## Cls 'Clear display Cursor Off Noblink 'Hide cursor I2Cinit 'initializes the I2C SCL and SDA pins Wait 1 Lowerline LCD "Seek-Test" Call RDA5807M_Init() 'initialized RDA5807M Call RDA5807M_SetBassBoostOn() 'bass on Call RDA5807M_SetVolume(&b1111) 'full volume Call RDA5807M_Seek() 'go to search mode 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_SetMyDefault() 'Set my default values for Expert Mode I2cstart 'START condition I2cwbyte SEQ_WriteAdr 'RDA5807M address / sequential write mode I2cwbyte High(RegH02) I2cwbyte Low(RegH02) I2cwbyte High(RegH03) I2cwbyte Low(RegH03) I2cwbyte High(RegH04) I2cwbyte Low(RegH04) I2cwbyte High(RegH05) I2cwbyte Low(RegH05) I2cwbyte High(RegH06) I2cwbyte Low(RegH06) I2cwbyte High(RegH07) I2cwbyte Low(RegH07) I2cwbyte High(RegH08) I2cwbyte Low(RegH08) 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 Waitms 50 End Sub Sub RDA5807M_SetMuteOn() 'Set mute - change state to Mute On MT = I2CreadRegister(&h02) If MT.14 = 1 Then 'No mute - normnal operation MT = MT XOR &b0100_0000_0000_0000 'Change to mute End If Call I2CwriteRegister(&h02 , MT) 'Write Mute state End Sub Sub RDA5807M_SetMuteOff() 'Set unmute - change state to normal operation MT = I2CreadRegister(&h02) If MT.14 = 0 Then 'Mute MT = MT XOR &b0100_0000_0000_0000 'Change to normal operation End If Call I2CwriteRegister(&h02 , MT) 'Write Mute state End Sub Sub RDA5807M_SetBassBoostOn() 'Set BassBoost on BB = I2CreadRegister(&h02) If BB.12 = 0 Then BB = BB XOR &b0001_0000_0000_0000 End If Call I2CwriteRegister(&h02 , BB) End Sub Sub RDA5807M_SetBassBoostOff() 'Set BassBoost off BB = I2CreadRegister(&h02) If BB.12 = 1 Then BB = BB XOR &b0001_0000_0000_0000 End If Call I2CwriteRegister(&h02 , BB) End Sub Sub RDA5807M_SetVolume(ByVal VolNew As Word) 'Set Volume (0000 --> 1111) Vol = I2CreadRegister(&h05) VolNew = VolNew AND &b0000_0000_0000_1111 Vol = Vol OR VolNew Call I2CwriteRegister(&h05, Vol) End Sub Sub RDA5807M_SetSNRthres(ByVal STH As Byte) 'Set Seek SNR Threshold value Reg = I2CreadRegister(&h05) If STH > 15 Then STH = 15 'Threshold Max = 15 STHW = STH 'BASCOM Typecasting Shift STHW , left, 8 'Shift left to [8-11] Reg = Reg OR STHW Call I2CwriteRegister(&h05 , Reg) 'Write new threshold value End Sub Sub RDA5807M_GetStatus() 'Read the Tune/Seek status stat1 = I2CreadRegister(&h0A) 'Read RegH0A stat2 = I2CreadRegister(&h0B) 'Read RegH0B Waitms 20 STO = stat1.10 'Stereo indicator (Mono = 0) STC = stat1.14 'Seek/Tune complete flag SF = stat1.13 'Seek fail flag RSSI < SeekTh ! FMtrue = stat2.8 'Station found flag rssi = stat2 AND &b1111_1110_0000_0000 'RSSI [9-15] Shift rssi , right , 9 RChan = stat1 AND &b0000_0011_1111_1111 'Channel of the station End Sub Sub RDA5807M_SetSeekParam() 'Set Seek values Reg = I2CreadRegister(&h02) Reg.9 = 1 'SeekUp = 1 --> Seek up Reg.8 = 1 'Seek = 1 --> Enable Reg.7 = 1 'SKmode = 1 --> stop seeking at band limit Call I2CwriteRegister(&h02 , Reg) 'Start Seek End Sub Function Chan2Freq(ByVal Chan As Word) As DWord Freq = fspace * Chan Freq = Freq + 87000000 Chan2Freq = Freq End Function Sub RDA5807M_Seek() 'Scan Europe BC Band Chan = 5 'Start-freqency 87 MHz fspace = 100000 'Space = 100 kHz Call RDA5807M_SetMuteOn() 'Mute Call RDA5807M_SetSNRthres(&b0000_1000) 'set threshold value Call RDA5807M_SetSeekParam() 'set Seek parameters val1 = Chan Shift val1 , left , 6 value = val1 + &b0000_0000_0001_0000 'set Tune Bit Call I2CwriteRegister(&h03, value) 'write register H03 Freq = Chan2Freq(Chan) While Freq < FMend Waitms 50 'wait 50 ms for PLL to lock in Call RDA5807M_GetStatus() 'read RegH0A, RegH0B If STC = 1 AND FMtrue = 1 Then 'only for test ! Call RDA5807M_SetMuteOff() Locate 1 , 1 LCD RChan ; " RChan" Wait 3 Cls 'For final radio code write here the evaluation and 'storage routines for RChan, ST and RSSI etc End If Chan = Chan + 1 val1 = Chan Shift val1 , left , 6 value = val1 + &b0000_0000_0001_0000 'set Tune Bit Call I2CwriteRegister(&h03, value) 'write register H03 Call RDA5807M_SetMuteOn() 'only for test ! Freq = Chan2Freq(Chan) Locate 2 , 1 LCD Freq ; " Freq" WEnd End Sub '############################ End Of File ######################################