lpc1114驱动RDA5820 FM收音机程序

  • 学习4402次

rda5820.h文件:

#ifndef __RDA5820_H
#define __RDA5820_H?? ? 

#define RDA5820_R00?? ??? ?0X00? //读出16个位的ID =0X5820
#define RDA5820_R02?? ??? ?0X02? //DHIZ[15],DMUTE[14],MONO[13],BASS[12],SEEKUP[9],SEEK[8],SKMODE[7],CLK_MODE[6:4],ANTENNA[2],SOFTRESET[1],ENABLE[0]
#define RDA5820_R03?? ??? ?0X03? //CHAN[15:6],TUNE[4],BAND[3:2],SPACE[1:0]
#define RDA5820_R04?? ??? ?0X04? //STCIEN[14](搜索完成中断),DE[11],I2S_ENABLE[6],GPIO3~1[5:0]
#define RDA5820_R05?? ??? ?0X05? //INT_MODE[15],SEEKTH[14:8](设定信号强度),LNA_PORT_SEL[7:6],LNA_ICSEL_BIT[5:4],VOLUME[3:0];
#define RDA5820_R0A?? ??? ?0X0A? //STC[14](搜索完成标志),SF[13](搜索失败标志),ST[10](立体声标志),READCHAN[9:0](频率).
#define RDA5820_R0B?? ??? ?0X0B? //RSSI[15:9](信号强度),FM_TRUE[8](站点标志),FM_READY[7](fm就绪标志)
#define RDA5820_R40?? ??? ?0X40? //AUTO_SEEK[15](全/半自动搜台),CHAN_SCAN[14](搜空/忙台),CHIP_FUN[3:0](工作模式)
#define RDA5820_R41?? ??? ?0X41? //MEM_CLR[15](内存清除),RPT_TIME[14:9](RDS应答次数),MEM_DEPTH[8:0](FIFO大小,最大256)
#define RDA5820_R42?? ??? ?0X42? //TX_SIG_GAIN[10:8],TX_PA_GAIN[5:0](发射功率).
#define RDA5820_R4A?? ??? ?0X4A? //MEM_INTMOD[4:0]
#define RDA5820_R4B?? ??? ?0X4B? //CHAN_NUM[7:0](仅在全自动搜台有效,代表电台数量).
#define RDA5820_R4C?? ??? ?0X4C? //TX_AUDIO_DEV[15:0]
#define RDA5820_R4D?? ??? ?0X4D? //TX_PILOT_DEV[15:0]
#define RDA5820_R4E?? ??? ?0X4E? //TX_RDS_DEV[15:0]
#define RDA5820_R53?? ??? ?0X53? //CHAN_BOTTON[10:0](通道最低频率,100kHZ为单位)
#define RDA5820_R54?? ??? ?0X54? //CHAN_TOP[10:0](通道最高频率,100kHZ为单位)
#define RDA5820_R64?? ??? ?0X64? //RDS_RXMOD[4:3](FIFO模式[10]或者寄存器模式[11])?? ??? ? 

#define RDA5820_READ??? 0X23? //读RDA5820
#define RDA5820_WRITE?? ?0X22? //写RDA5820

uint8_t RDA5820_Init(void);?? ??? ??? ??? ??? ?//初始化?? ??? ? ?? ??? ??? ??? ??? ?
void RDA5820_WR_Reg(uint8_t addr,uint16_t val);?? ?//写RDA5820寄存器
uint16_t RDA5820_RD_Reg(uint8_t addr);?? ??? ??? ?//读RDA5820寄存器
void RDA5820_RX_Mode(void);?? ??? ??? ??? ?//设置RDA5820为RX模式
void RDA5820_TX_Mode(void);?? ??? ??? ??? ?//设置RDA5820为TX模式
uint8_t RDA5820_Rssi_Get(void);
void RDA5820_Mute_Set(uint8_t mute);
void RDA5820_Rssi_Set(uint8_t rssi);
void RDA5820_Vol_Set(uint8_t vol);
void RDA5820_TxPAG_Set(uint8_t gain);
void RDA5820_TxPGA_Set(uint8_t gain);
void RDA5820_Band_Set(uint8_t band);
void RDA5820_Space_Set(uint8_t spc);
void RDA5820_Freq_Set(uint16_t freq);
uint16_t RDA5820_Freq_Get(void);

#endif

rda5820.c文件:

#include "lpc11xx.h"
#include "rda5820.h"
#include "i2c.h"

//初始化
//0,初始化成功;
//其他,初始化失败.
uint8_t RDA5820_Init(void)
{
?? ?uint16_t id;
?? ?I2C_Init();?? ??? ??? ??? ??? ??? ?//初始化I2C口?? ?

?? ?id=RDA5820_RD_Reg(RDA5820_R00);?? ?//读取ID =0X5805

?? ?if(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/0x1420 
?? ? ?? ?RDA5820_WR_Reg(0X16,0XC000);?? ??? ?// ?
?? ? ?? ?RDA5820_WR_Reg(0X1C,0X3126);?? ??? ?// 
?? ? ?? ?RDA5820_WR_Reg(0X22,0X9C24);?? ??? ?//fm_true 
?? ? ?? ?RDA5820_WR_Reg(0X47,0XF660) ;?? ??? ?//tx rds 
??? ?}else return 1;//初始化失败
?? ?return 0;
}
//写RDA5820寄存器?? ??? ??? ??? ??? ??? ? ?? ??? ??? ??? ??? ?
void RDA5820_WR_Reg(uint8_t addr,uint16_t val)
{
??? 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_t RDA5820_RD_Reg(uint8_t addr)
{
?? ?uint16_t res;

??? 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);??? ??? ??? ?//读高字节,发送ACK?? ? ?
? ?? ?res<<=8;
? ?? ?res|=I2C_Read_Byte(0);??? ??? ??? ?//读低字节,发送NACK?? ??? ??? ??? ??? ??? ??? ?? ?
? ?? ?I2C_Stop();?? ??? ??? ??? ??? ??? ?//产生一个停止条件 
?? ?return res;?? ??? ??? ??? ??? ??? ?//返回读到的数据
}
//设置RDA5820为RX模式
void RDA5820_RX_Mode(void)
{
?? ?uint16_t temp;
?? ?temp=RDA5820_RD_Reg(0X40);?? ??? ?//读取0X40的内容
?? ?temp&=0xfff0;?? ??? ??? ??? ??? ?//RX 模式? ?
?? ?RDA5820_WR_Reg(0X40,temp) ;?? ??? ?//FM RX模式 
}?? ??? ??? ?
//设置RDA5820为TX模式
void RDA5820_TX_Mode(void)
{
?? ?uint16_t temp;
?? ?temp=RDA5820_RD_Reg(0X40);?? ??? ?//读取0X40的内容
?? ?temp&=0xfff0;
?? ?temp|=0x0001;?? ??? ??? ??? ???? //TX 模式
?? ?RDA5820_WR_Reg(0X40,temp) ;?? ??? ?//FM TM 模式 
}
//得到信号强度
//返回值范围:0~127
uint8_t RDA5820_Rssi_Get(void)
{
?? ?uint16_t temp;
?? ?temp=RDA5820_RD_Reg(0X0B);?? ??? ?//读取0X0B的内容
?? ?return temp>>9;???????????????? //返回信号强度
}
//设置音量ok
//vol:0~15;
void RDA5820_Vol_Set(uint8_t vol)
{
?? ?uint16_t temp;
?? ?temp=RDA5820_RD_Reg(0X05);?? ??? ?//读取0X05的内容
?? ?temp&=0XFFF0;
?? ?temp|=vol&0X0F;?? ??? ??? ??? ?? ??? ??? ? 
?? ?RDA5820_WR_Reg(0X05,temp) ;?? ??? ?//设置音量?? ?
}
//静音设置
//mute:0,不静音;1,静音
void RDA5820_Mute_Set(uint8_t mute)
{
?? ?uint16_t temp;
?? ?temp=RDA5820_RD_Reg(0X02);?? ??? ?//读取0X02的内容
?? ?if(mute)temp|=1<<14;
?? ?else temp&=~(1<<14);?? ??? ??? ??? ?? ??? ??? ? 
?? ?RDA5820_WR_Reg(0X02,temp) ;?? ??? ?//设置MUTE?? ?
}
//设置灵敏度
//rssi:0~127;
void RDA5820_Rssi_Set(uint8_t rssi)
{
?? ?uint16_t temp;
?? ?temp=RDA5820_RD_Reg(0X05);?? ??? ?//读取0X05的内容
?? ?temp&=0X80FF;
?? ?temp|=(uint16_t)rssi<<8;?? ??? ??? ??? ?? ??? ??? ? 
?? ?RDA5820_WR_Reg(0X05,temp) ;?? ??? ?//设置RSSI?? ?
}
//设置TX发送功率
//gain:0~63
void RDA5820_TxPAG_Set(uint8_t gain)
{
?? ?uint16_t temp;
?? ?temp=RDA5820_RD_Reg(0X42);?? ??? ?//读取0X42的内容
?? ?temp&=0XFFC0;
?? ?temp|=gain;?? ??? ??? ??? ?? ??? ??? ?//GAIN
?? ?RDA5820_WR_Reg(0X42,temp) ;?? ??? ?//设置PA的功率
}
//设置TX 输入信号增益
//gain:0~7
void RDA5820_TxPGA_Set(uint8_t gain)
{
?? ?uint16_t temp;
?? ?temp=RDA5820_RD_Reg(0X42);?? ??? ?//读取0X42的内容
?? ?temp&=0XF8FF;
?? ?temp|=gain<<8;?? ??? ??? ??? ??? ?//GAIN
?? ?RDA5820_WR_Reg(0X42,temp) ;?? ??? ?//设置PGA
}
//设置RDA5820的工作频段
//band:0,87~108Mhz;1,76~91Mhz;2,76~108Mhz;3,用户自定义(53H~54H)
void RDA5820_Band_Set(uint8_t band)
{
?? ?uint16_t temp;
?? ?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,保留
void RDA5820_Space_Set(uint8_t spc)
{
?? ?uint16_t temp;
?? ?temp=RDA5820_RD_Reg(0X03);?? ??? ?//读取0X03的内容
?? ?temp&=0XFFFC;
?? ?temp|=spc;?? ??? ??? ???? ?
?? ?RDA5820_WR_Reg(0X03,temp) ;?? ??? ?//设置BAND
}
//设置RDA5820的频率
//freq:频率值(单位为10Khz),比如10805,表示108.05Mhz
void RDA5820_Freq_Set(uint16_t freq)
{
?? ?uint16_t temp;
?? ?uint8_t spc=0,band=0;
?? ?uint16_t fbtm,chan;
?? ?temp=RDA5820_RD_Reg(0X03);?? ?//读取0X03的内容
?? ?temp&=0X001F;
?? ?band=(temp>>2)&0x03;?? ??? ?//得到频带
?? ?spc=temp&0x03; ?? ??? ??? ??? ?//得到分辨率
?? ?if(spc==0)spc=10;
?? ?else if(spc==1)spc=20;
?? ?else spc=5;
?? ?if(band==0)fbtm=8700;
?? ?else if(band==1||band==2)fbtm=7600;
?? ?else 
?? ?{
?? ??? ?fbtm=RDA5820_RD_Reg(0X53);//得到bottom频率
?? ??? ?fbtm*=10;
?? ?}
?? ?if(freq<fbtm)return;
?? ?chan=(freq-fbtm)/spc;?? ??? ?//得到CHAN应该写入的值
?? ?chan&=0X3FF;?? ??? ??? ??? ?//取低10位?? ? ?
?? ?temp|=chan<<6;
?? ?temp|=1<<4;?? ??? ??? ??? ??? ?//TONE ENABLE?? ??? ??? ???? ?
?? ?RDA5820_WR_Reg(0X03,temp) ;?? ?//设置频率
?? ?delay_ms(20);?? ??? ??? ??? ?//等待20ms
?? ?while((RDA5820_RD_Reg(0X0B)&(1<<7))==0);//等待FM_READY

}
//得到当前频率
//返回值:频率值(单位10Khz)
uint16_t RDA5820_Freq_Get(void)
{
?? ?uint16_t temp;
?? ?uint8_t spc=0,band=0;
?? ?uint16_t fbtm,chan;
?? ?temp=RDA5820_RD_Reg(0X03);?? ??? ?//读取0X03的内容
?? ?chan=temp>>6;? ?
?? ?band=(temp>>2)&0x03;?? ??? ?//得到频带
?? ?spc=temp&0x03; ?? ??? ??? ??? ?//得到分辨率
?? ?if(spc==0)spc=10;
?? ?else if(spc==1)spc=20;
?? ?else spc=5;
?? ?if(band==0)fbtm=8700;
?? ?else if(band==1||band==2)fbtm=7600;
?? ?else 
?? ?{
?? ??? ?fbtm=RDA5820_RD_Reg(0X53);//得到bottom频率
?? ??? ?fbtm*=10;
?? ?}
??? ?temp=fbtm+chan*spc;?? ??? ??? ??? ? 
?? ?return temp;//返回频率值
}

main.c文件:

#include "lpc11xx.h"
#include "ili9325.h"
#include "w25q16.h"
#include "rda5820.h"

#define KEY1_DOWN (LPC_GPIO1->DATA&(1<<9))!=(1<<9)
#define KEY2_DOWN (LPC_GPIO1->DATA&(1<<10))!=(1<<10)

int main()
{
?? ?uint16_t freqset=0;
?? ?uint8_t i,rssi;
?? ?
?? ?LCD_Init();
?? ?LCD_Clear(WHITE);
?? ?W25Q16_Init();?? ??? // 初始化字库芯片W25Q16
?? ?LCD_ShowString(20, 5, "START");
?? ?delay_ms(600);
?? ?LCD_ShowString(20, 5, "???? ");
?? ?if(RDA5820_Init()==0)
?? ?{
?? ??? ?LCD_ShowString(20, 5, "OK");
?? ?}
?? ?else 
?? ?{
?? ??? ?LCD_ShowString(20, 5, "NO");
?? ?}
?? ?
?? ?RDA5820_Band_Set(0);?? ?//设置频段为87~108Mhz
?? ?RDA5820_Space_Set(0);?? ?//设置步进为100Khz
?? ?RDA5820_TxPGA_Set(3);?? ?//信号增益设置为3
?? ?RDA5820_TxPAG_Set(63);?? ?//发射功率为最大.
?? ?RDA5820_RX_Mode();?? ??? ?//设置为接收模式 ?
?? ?
?? ?freqset=8800;?? ??? ??? ??? ?//默认为88Mhz
?? ?RDA5820_Freq_Set(freqset);?? ?//设置频率
?? ?
?? ?while(1)
?? ?{
?? ??? ?if(KEY1_DOWN)
?? ??? ?{
?? ??? ??? ?delay_ms(30);
?? ??? ??? ?if(KEY1_DOWN)
?? ??? ??? ?{
?? ??? ??? ??? ?if(freqset>8700)freqset-=10;
?? ??? ??? ??? ?LCD_ShowNum(100,30,freqset,5);
?? ??? ??? ??? ?RDA5820_Freq_Set(freqset);?? ?//设置频率
?? ??? ??? ?}
?? ??? ?}
?? ??? ?else if(KEY2_DOWN)
?? ??? ?{
?? ??? ??? ?delay_ms(30);
?? ??? ??? ?if(KEY2_DOWN)
?? ??? ??? ?{
?? ??? ??? ??? ?if(freqset<10800)freqset+=10;
?? ??? ??? ??? ?LCD_ShowNum(100,30,freqset,5);
?? ??? ??? ??? ?RDA5820_Freq_Set(freqset);?? ?//设置频率
?? ??? ??? ?}
?? ??? ?}
?? ??? ?i++;
?? ??? ?delay_ms(10);?? ? ?
?? ??? ?if(i==200)//每两秒左右检测一次信号强度信息
?? ??? ?{
?? ??? ??? ?i=0;
?? ??? ??? ?rssi=RDA5820_Rssi_Get();?? ??? ??? ??? ?//得到信号强度
?? ??? ??? ?LCD_ShowNum(100,230,rssi,3);?? ??? ??? ?//显示信号强度
?? ??? ?}
?? ?}
}

发表评论