lpc1114驱动RDA5820 FM收音机程序
扫描二维码
随时随地手机看文章
rda5820.h文件:
#ifndef__RDA5820_H#define__RDA5820_H#defineRDA5820_R000X00//读出16个位的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](设定信号强度),LNA_PORT_SEL[7:6],LNA_ICSEL_BIT[5:4],VOLUME[3:0];#defineRDA5820_R0A0X0A//STC[14](搜索完成标志),SF[13](搜索失败标志),ST[10](立体声标志),READCHAN[9:0](频率).#defineRDA5820_R0B0X0B//RSSI[15:9](信号强度),FM_TRUE[8](站点标志),FM_READY[7](fm就绪标志)#defineRDA5820_R400X40//AUTO_SEEK[15](全/半自动搜台),CHAN_SCAN[14](搜空/忙台),CHIP_FUN[3:0](工作模式)#defineRDA5820_R410X41//MEM_CLR[15](内存清除),RPT_TIME[14:9](RDS应答次数),MEM_DEPTH[8:0](FIFO大小,最大256)#defineRDA5820_R420X42//TX_SIG_GAIN[10:8],TX_PA_GAIN[5:0](发射功率).#defineRDA5820_R4A0X4A//MEM_INTMOD[4:0]#defineRDA5820_R4B0X4B//CHAN_NUM[7:0](仅在全自动搜台有效,代表电台数量).#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);//设置RDA5820为RX模式voidRDA5820_TX_Mode(void);//设置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"//初始化//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);//软复位delay_ms(50);RDA5820_WR_Reg(RDA5820_R02,0xC001);//立体声,上电delay_ms(600);//等待时钟稳定RDA5820_WR_Reg(RDA5820_R05,0X884F);//搜索强度8,LNAN,1.8mA,VOL最大RDA5820_WR_Reg(0X07,0X7800);//RDA5820_WR_Reg(0X13,0X0008);//RDA5820_WR_Reg(0X15,0x1420);//VCO设置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);//发送写命令I2C_Wait_Ack();I2C_Send_Byte(addr);//发送地址I2C_Wait_Ack();I2C_Send_Byte(val>>8);//发送高字节I2C_Wait_Ack();I2C_Send_Byte(val&0XFF);//发送低字节I2C_Wait_Ack();I2C_Stop();//产生一个停止条件}//读RDA5820寄存器uint16_tRDA5820_RD_Reg(uint8_taddr){uint16_tres;I2C_Start();I2C_Send_Byte(RDA5820_WRITE);//发送写命令I2C_Wait_Ack();I2C_Send_Byte(addr);//发送地址I2C_Wait_Ack();I2C_Start();I2C_Send_Byte(RDA5820_READ);//发送读命令I2C_Wait_Ack();res=I2C_Read_Byte(1);//读高字节,发送ACKres<<=8;res|=I2C_Read_Byte(0);//读低字节,发送NACKI2C_Stop();//产生一个停止条件returnres;//返回读到的数据}//设置RDA5820为RX模式voidRDA5820_RX_Mode(void){uint16_ttemp;temp=RDA5820_RD_Reg(0X40);//读取0X40的内容temp&=0xfff0;//RX模式RDA5820_WR_Reg(0X40,temp);//FMRX模式}//设置RDA5820为TX模式voidRDA5820_TX_Mode(void){uint16_ttemp;temp=RDA5820_RD_Reg(0X40);//读取0X40的内容temp&=0xfff0;temp|=0x0001;//TX模式RDA5820_WR_Reg(0X40,temp);//FMTM模式}//得到信号强度//返回值范围:0~127uint8_tRDA5820_Rssi_Get(void){uint16_ttemp;temp=RDA5820_RD_Reg(0X0B);//读取0X0B的内容returntemp>>9;//返回信号强度}//设置音量ok//vol:0~15;voidRDA5820_Vol_Set(uint8_tvol){uint16_ttemp;temp=RDA5820_RD_Reg(0X05);//读取0X05的内容temp&=0XFFF0;temp|=vol&0X0F;RDA5820_WR_Reg(0X05,temp);//设置音量}//静音设置//mute:0,不静音;1,静音voidRDA5820_Mute_Set(uint8_tmute){uint16_ttemp;temp=RDA5820_RD_Reg(0X02);//读取0X02的内容if(mute)temp|=1<<14;elsetemp&=~(1<<14);RDA5820_WR_Reg(0X02,temp);//设置MUTE}//设置灵敏度//rssi:0~127;voidRDA5820_Rssi_Set(uint8_trssi){uint16_ttemp;temp=RDA5820_RD_Reg(0X05);//读取0X05的内容temp&=0X80FF;temp|=(uint16_t)rssi<<8;RDA5820_WR_Reg(0X05,temp);//设置RSSI}//设置TX发送功率//gain:0~63voidRDA5820_TxPAG_Set(uint8_tgain){uint16_ttemp;temp=RDA5820_RD_Reg(0X42);//读取0X42的内容temp&=0XFFC0;temp|=gain;//GAINRDA5820_WR_Reg(0X42,temp);//设置PA的功率}//设置TX输入信号增益//gain:0~7voidRDA5820_TxPGA_Set(uint8_tgain){uint16_ttemp;temp=RDA5820_RD_Reg(0X42);//读取0X42的内容temp&=0XF8FF;temp|=gain<<8;//GAINRDA5820_WR_Reg(0X42,temp);//设置PGA}//设置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的内容temp&=0XFFF3;temp|=band<<2;RDA5820_WR_Reg(0X03,temp);//设置BAND}//设置RDA5820的步进频率//band:0,100Khz;1,200Khz;3,50Khz;3,保留voidRDA5820_Space_Set(uint8_tspc){uint16_ttemp;temp=RDA5820_RD_Reg(0X03);//读取0X03的内容temp&=0XFFFC;temp|=spc;RDA5820_WR_Reg(0X03,temp);//设置BAND}//设置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的内容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;//返回频率值}