#include #include #include #include #include #include #include #include #include "RadioKitRDA5820.h" #include "RDA5820.h" #define STEREO #ifdef STEREO #define RDA5820_02H_HIGH_DEFAULT RDA5820_DHIZ|RDA5820_DMUTE_OFF|RDA5820_STEREO|RDA5820_SEEKUP #define RDA5820_02H_HIGH_SEEK_DOWN RDA5820_DHIZ|RDA5820_DMUTE_OFF|RDA5820_STEREO|RDA5820_SEEKDOWN #else // STEREO #define RDA5820_02H_HIGH_DEFAULT RDA5820_DHIZ|RDA5820_DMUTE_OFF|RDA5820_MONO|RDA5820_SEEKUP #define RDA5820_02H_HIGH_SEEK_DOWN RDA5820_DHIZ|RDA5820_DMUTE_OFF|RDA5820_MONO|RDA5820_SEEKDOWN #endif // STEREO #define RDA5820_02H_LOW_DEFAULT RDA5820_SKMODE_AROUND|RDA5820_CLK_MODE_32KHZ|RDA5820_ENABLE #define RDA5820_03H_LOW_DEFAULT RDA5820_BAND_JAPAN|RDA5820_SPACE_100KHZ uchar RDA5820_reg_addr[]={ 0x05, //seek threshold, LNA port, volume, 0x07, 0x13, 0x15, //vco 0x16, 0x1c, 0x22, //FM_TRUE 0x47, //tx rds }; uchar RDA5820_reg_data[] = { 0x88,0x4f, //05h register 0x78,0x00, //07h register 0x00,0x08, //13h register 0x17,0xa0, //15h register 0xc0,0x00, //16h reg... 0x31,0x26, //1ch reg... 0x9c,0x24, //22h reg... // 0x9624, 0xf6,0x60, //47h register // tx rds }; void wait_stc(void); void i2c_write_RDA5820(uchar reg, uchar data[2]) { TWI_start(); TWI_send(ADRS_RDA5820); TWI_send(reg); TWI_send(data[0]); TWI_send(data[1]); TWI_stop(); } void i2c_read_RDA5820(uchar reg, uchar data[2]) { TWI_start(); TWI_send(ADRS_RDA5820); TWI_send(reg); TWI_start(); TWI_send(ADRS_RDA5820+1); data[0] = TWI_recv(0); // ack data[1] = TWI_recv(1); // nack TWI_stop(); } // Init FM Chip void init_fm_chip(void) { uchar RDA5820_data[2]; #ifdef STEREO RDA5820_data[0] = RDA5820_DMUTE_ON |RDA5820_STEREO|RDA5820_SEEKUP; #else // STEREO RDA5820_data[0] = RDA5820_DMUTE_ON |RDA5820_MONO|RDA5820_SEEKUP; #endif // STEREO RDA5820_data[1] = RDA5820_SKMODE_AROUND|RDA5820_CLK_MODE_32KHZ|RDA5820_SOFT_RESET; i2c_write_RDA5820(0x02, RDA5820_data); // Soft Reset wait_timer(10); #ifdef STEREO RDA5820_data[0] = RDA5820_DHIZ|RDA5820_DMUTE_OFF|RDA5820_STEREO|RDA5820_SEEKUP; #else // STEREO RDA5820_data[0] = RDA5820_DHIZ|RDA5820_DMUTE_OFF|RDA5820_MONO|RDA5820_SEEKUP; #endif // STEREO RDA5820_data[1] = RDA5820_SKMODE_AROUND|RDA5820_CLK_MODE_32KHZ|RDA5820_ENABLE; i2c_write_RDA5820(0x02, RDA5820_data); RDA5820_data[0] = RDA5820_DE_50us; RDA5820_data[1] = 0x00; i2c_write_RDA5820(0x04, RDA5820_data); RDA5820_data[0] = 0x88; RDA5820_data[1] = 0x4F; i2c_write_RDA5820(0x05, RDA5820_data); } void RDA5820_RX_Intialization(void) { init_fm_chip(); } // Seek ushort seek(uchar updown) { uchar RDA5820_data[2]; // 現在の周波数を0x0aから読みだして、0x03へ設定 i2c_read_RDA5820(0x0a, RDA5820_data); ushort freq = (((ushort)(RDA5820_data[0] & RDA5820_READCHAN_HIGH)) << 8) | RDA5820_data[1]; RDA5820_data[0]=freq>>2; RDA5820_data[1]=(((freq&0x03)<<6)|RDA5820_BAND_JAPAN|RDA5820_SPACE_100KHZ) ; i2c_write_RDA5820(0x03, RDA5820_data); // SEEK if (updown==UP) { #ifdef STEREO RDA5820_data[0] = RDA5820_DHIZ|RDA5820_DMUTE_OFF|RDA5820_STEREO|RDA5820_SEEKUP|RDA5820_SEEK; #else // STEREO RDA5820_data[0] = RDA5820_DHIZ|RDA5820_DMUTE_OFF|RDA5820_MONO|RDA5820_SEEKUP|RDA5820_SEEK; #endif // STEREO } else { #ifdef STEREO RDA5820_data[0] = RDA5820_DHIZ|RDA5820_DMUTE_OFF|RDA5820_STEREO|RDA5820_SEEKDOWN|RDA5820_SEEK; #else // STEREO RDA5820_data[0] = RDA5820_DHIZ|RDA5820_DMUTE_OFF|RDA5820_MONO|RDA5820_SEEKDOWN|RDA5820_SEEK; #endif // STEREO } RDA5820_data[1] = RDA5820_SKMODE_AROUND|RDA5820_CLK_MODE_32KHZ|RDA5820_ENABLE; i2c_write_RDA5820(0x02, RDA5820_data); wait_stc(); // 0x0aから設定されている周波数を読み込む i2c_read_RDA5820(0x0a, RDA5820_data); freq = (((ushort)(RDA5820_data[0] & RDA5820_READCHAN_HIGH)) << 8) | RDA5820_data[1]; return freq + 760; } // Wait STC Flag void wait_stc(void) { uchar data[2]; for (;;) { wait_timer(1); i2c_read_RDA5820(0x0a, data); if (data[0] & RDA5820_STC) break; } } // Set Frequency void set_freq(ushort freq) { uchar RDA5820_channel_tune[2] ={0, 0}; freq -= 760; RDA5820_channel_tune[0]=freq>>2; RDA5820_channel_tune[1]=(((freq&0x03)<<6)|RDA5820_TUNE|RDA5820_BAND_JAPAN|RDA5820_SPACE_100KHZ) ; //set tune bit i2c_write_RDA5820(0x03, RDA5820_channel_tune); wait_stc(); wait_timer(5); } // Set Volume void set_Volume(uchar vol) { // uchar RDA5820_vol[2]={0x08,0}; uchar RDA5820_vol[2]={0x88,0}; RDA5820_vol[1] = vol|0x40; i2c_write_RDA5820(0x05, RDA5820_vol); wait_stc(); wait_timer(5); }