lpc1114驅(qū)動(dòng)RDA5820 FM收音機(jī)程序
掃描二維碼
隨時(shí)隨地手機(jī)看文章
rda5820.h文件:
#ifndef__RDA5820_H#define__RDA5820_H#defineRDA5820_R000X00//讀出16個(gè)位的ID=0X5820#defineRDA5820_R020X02//DHIZ[15],DMUTE[14],MONO[13],BASS[12],SEEKUP[9],SEEK[8],SKMODE[7],CLK_MODE[6:4],ANTENNA[2],SOFTRESET[1],ENABLE[0]#defineRDA5820_R030X03//CHAN[15:6],TUNE[4],BAND[3:2],SPACE[1:0]#defineRDA5820_R040X04//STCIEN[14](搜索完成中斷),DE[11],I2S_ENABLE[6],GPIO3~1[5:0]#defineRDA5820_R050X05//INT_MODE[15],SEEKTH[14:8](設(shè)定信號(hào)強(qiáng)度),LNA_PORT_SEL[7:6],LNA_ICSEL_BIT[5:4],VOLUME[3:0];#defineRDA5820_R0A0X0A//STC[14](搜索完成標(biāo)志),SF[13](搜索失敗標(biāo)志),ST[10](立體聲標(biāo)志),READCHAN[9:0](頻率).#defineRDA5820_R0B0X0B//RSSI[15:9](信號(hào)強(qiáng)度),FM_TRUE[8](站點(diǎn)標(biāo)志),FM_READY[7](fm就緒標(biāo)志)#defineRDA5820_R400X40//AUTO_SEEK[15](全/半自動(dòng)搜臺(tái)),CHAN_SCAN[14](搜空/忙臺(tái)),CHIP_FUN[3:0](工作模式)#defineRDA5820_R410X41//MEM_CLR[15](內(nèi)存清除),RPT_TIME[14:9](RDS應(yīng)答次數(shù)),MEM_DEPTH[8:0](FIFO大小,最大256)#defineRDA5820_R420X42//TX_SIG_GAIN[10:8],TX_PA_GAIN[5:0](發(fā)射功率).#defineRDA5820_R4A0X4A//MEM_INTMOD[4:0]#defineRDA5820_R4B0X4B//CHAN_NUM[7:0](僅在全自動(dòng)搜臺(tái)有效,代表電臺(tái)數(shù)量).#defineRDA5820_R4C0X4C//TX_AUDIO_DEV[15:0]#defineRDA5820_R4D0X4D//TX_PILOT_DEV[15:0]#defineRDA5820_R4E0X4E//TX_RDS_DEV[15:0]#defineRDA5820_R530X53//CHAN_BOTTON[10:0](通道最低頻率,100kHZ為單位)#defineRDA5820_R540X54//CHAN_TOP[10:0](通道最高頻率,100kHZ為單位)#defineRDA5820_R640X64//RDS_RXMOD[4:3](FIFO模式[10]或者寄存器模式[11])#defineRDA5820_READ0X23//讀RDA5820#defineRDA5820_WRITE0X22//寫RDA5820uint8_tRDA5820_Init(void);//初始化voidRDA5820_WR_Reg(uint8_taddr,uint16_tval);//寫RDA5820寄存器uint16_tRDA5820_RD_Reg(uint8_taddr);//讀RDA5820寄存器voidRDA5820_RX_Mode(void);//設(shè)置RDA5820為RX模式voidRDA5820_TX_Mode(void);//設(shè)置RDA5820為TX模式uint8_tRDA5820_Rssi_Get(void);voidRDA5820_Mute_Set(uint8_tmute);voidRDA5820_Rssi_Set(uint8_trssi);voidRDA5820_Vol_Set(uint8_tvol);voidRDA5820_TxPAG_Set(uint8_tgain);voidRDA5820_TxPGA_Set(uint8_tgain);voidRDA5820_Band_Set(uint8_tband);voidRDA5820_Space_Set(uint8_tspc);voidRDA5820_Freq_Set(uint16_tfreq);uint16_tRDA5820_Freq_Get(void);#endif
rda5820.c文件:
#include"lpc11xx.h"#include"rda5820.h"#include"i2c.h"http://初始化//0,初始化成功;//其他,初始化失敗.uint8_tRDA5820_Init(void){uint16_tid;I2C_Init();//初始化I2C口id=RDA5820_RD_Reg(RDA5820_R00);//讀取ID=0X5805if(id==0X5805)//讀取ID正確{RDA5820_WR_Reg(RDA5820_R02,0x0002);//軟復(fù)位delay_ms(50);RDA5820_WR_Reg(RDA5820_R02,0xC001);//立體聲,上電delay_ms(600);//等待時(shí)鐘穩(wěn)定RDA5820_WR_Reg(RDA5820_R05,0X884F);//搜索強(qiáng)度8,LNAN,1.8mA,VOL最大RDA5820_WR_Reg(0X07,0X7800);//RDA5820_WR_Reg(0X13,0X0008);//RDA5820_WR_Reg(0X15,0x1420);//VCO設(shè)置0x17A0/0x1420RDA5820_WR_Reg(0X16,0XC000);//RDA5820_WR_Reg(0X1C,0X3126);//RDA5820_WR_Reg(0X22,0X9C24);//fm_trueRDA5820_WR_Reg(0X47,0XF660);//txrds}elsereturn1;//初始化失敗return0;}//寫RDA5820寄存器voidRDA5820_WR_Reg(uint8_taddr,uint16_tval){I2C_Start();I2C_Send_Byte(RDA5820_WRITE);//發(fā)送寫命令I(lǐng)2C_Wait_Ack();I2C_Send_Byte(addr);//發(fā)送地址I2C_Wait_Ack();I2C_Send_Byte(val>>8);//發(fā)送高字節(jié)I2C_Wait_Ack();I2C_Send_Byte(val&0XFF);//發(fā)送低字節(jié)I2C_Wait_Ack();I2C_Stop();//產(chǎn)生一個(gè)停止條件}//讀RDA5820寄存器uint16_tRDA5820_RD_Reg(uint8_taddr){uint16_tres;I2C_Start();I2C_Send_Byte(RDA5820_WRITE);//發(fā)送寫命令I(lǐng)2C_Wait_Ack();I2C_Send_Byte(addr);//發(fā)送地址I2C_Wait_Ack();I2C_Start();I2C_Send_Byte(RDA5820_READ);//發(fā)送讀命令I(lǐng)2C_Wait_Ack();res=I2C_Read_Byte(1);//讀高字節(jié),發(fā)送ACKres<<=8;res|=I2C_Read_Byte(0);//讀低字節(jié),發(fā)送NACKI2C_Stop();//產(chǎn)生一個(gè)停止條件returnres;//返回讀到的數(shù)據(jù)}//設(shè)置RDA5820為RX模式voidRDA5820_RX_Mode(void){uint16_ttemp;temp=RDA5820_RD_Reg(0X40);//讀取0X40的內(nèi)容temp&=0xfff0;//RX模式RDA5820_WR_Reg(0X40,temp);//FMRX模式}//設(shè)置RDA5820為TX模式voidRDA5820_TX_Mode(void){uint16_ttemp;temp=RDA5820_RD_Reg(0X40);//讀取0X40的內(nèi)容temp&=0xfff0;temp|=0x0001;//TX模式RDA5820_WR_Reg(0X40,temp);//FMTM模式}//得到信號(hào)強(qiáng)度//返回值范圍:0~127uint8_tRDA5820_Rssi_Get(void){uint16_ttemp;temp=RDA5820_RD_Reg(0X0B);//讀取0X0B的內(nèi)容returntemp>>9;//返回信號(hào)強(qiáng)度}//設(shè)置音量ok//vol:0~15;voidRDA5820_Vol_Set(uint8_tvol){uint16_ttemp;temp=RDA5820_RD_Reg(0X05);//讀取0X05的內(nèi)容temp&=0XFFF0;temp|=vol&0X0F;RDA5820_WR_Reg(0X05,temp);//設(shè)置音量}//靜音設(shè)置//mute:0,不靜音;1,靜音voidRDA5820_Mute_Set(uint8_tmute){uint16_ttemp;temp=RDA5820_RD_Reg(0X02);//讀取0X02的內(nèi)容if(mute)temp|=1<<14;elsetemp&=~(1<<14);RDA5820_WR_Reg(0X02,temp);//設(shè)置MUTE}//設(shè)置靈敏度//rssi:0~127;voidRDA5820_Rssi_Set(uint8_trssi){uint16_ttemp;temp=RDA5820_RD_Reg(0X05);//讀取0X05的內(nèi)容temp&=0X80FF;temp|=(uint16_t)rssi<<8;RDA5820_WR_Reg(0X05,temp);//設(shè)置RSSI}//設(shè)置TX發(fā)送功率//gain:0~63voidRDA5820_TxPAG_Set(uint8_tgain){uint16_ttemp;temp=RDA5820_RD_Reg(0X42);//讀取0X42的內(nèi)容temp&=0XFFC0;temp|=gain;//GAINRDA5820_WR_Reg(0X42,temp);//設(shè)置PA的功率}//設(shè)置TX輸入信號(hào)增益//gain:0~7voidRDA5820_TxPGA_Set(uint8_tgain){uint16_ttemp;temp=RDA5820_RD_Reg(0X42);//讀取0X42的內(nèi)容temp&=0XF8FF;temp|=gain<<8;//GAINRDA5820_WR_Reg(0X42,temp);//設(shè)置PGA}//設(shè)置RDA5820的工作頻段//band:0,87~108Mhz;1,76~91Mhz;2,76~108Mhz;3,用戶自定義(53H~54H)voidRDA5820_Band_Set(uint8_tband){uint16_ttemp;temp=RDA5820_RD_Reg(0X03);//讀取0X03的內(nèi)容temp&=0XFFF3;temp|=band<<2;RDA5820_WR_Reg(0X03,temp);//設(shè)置BAND}//設(shè)置RDA5820的步進(jìn)頻率//band:0,100Khz;1,200Khz;3,50Khz;3,保留voidRDA5820_Space_Set(uint8_tspc){uint16_ttemp;temp=RDA5820_RD_Reg(0X03);//讀取0X03的內(nèi)容temp&=0XFFFC;temp|=spc;RDA5820_WR_Reg(0X03,temp);//設(shè)置BAND}//設(shè)置RDA5820的頻率//freq:頻率值(單位為10Khz),比如10805,表示108.05MhzvoidRDA5820_Freq_Set(uint16_tfreq){uint16_ttemp;uint8_tspc=0,band=0;uint16_tfbtm,chan;temp=RDA5820_RD_Reg(0X03);//讀取0X03的內(nèi)容temp&=0X001F;band=(temp>>2)&0x03;//得到頻帶spc=temp&0x03;//得到分辨率if(spc==0)spc=10;elseif(spc==1)spc=20;elsespc=5;if(band==0)fbtm=8700;elseif(band==1||band==2)fbtm=7600;else{fbtm=RDA5820_RD_Reg(0X53);//得到bottom頻率fbtm*=10;}if(freq>6;band=(temp>>2)&0x03;//得到頻帶spc=temp&0x03;//得到分辨率if(spc==0)spc=10;elseif(spc==1)spc=20;elsespc=5;if(band==0)fbtm=8700;elseif(band==1||band==2)fbtm=7600;else{fbtm=RDA5820_RD_Reg(0X53);//得到bottom頻率fbtm*=10;}temp=fbtm+chan*spc;returntemp;//返回頻率值}