RDA5807m+IIC收音机51单片机C程序

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

RDA5807m驱动程序+IIC程序/******************************************************************************************介绍:RD5807M收音机程序供电3.3v主控使用51单片机显示使用LCD1602**作者:胖子**时间:2016-1-5**地点:桂林电子科技大学*****************************************************************************************/#includereg51.h#includestring.h#includeintrins.h#defineucharunsignedchar#defineuintunsignedint#defineulintunsignedlongint#definelintlongintucharcodexian[4][4]={1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16};ucharcodehang[]={0xfe,0xfd,0xfb,0xf7};//矩阵键盘扫描使用//RDA的寄存器地址#defineRDA_R000X00//读出16个位的ID=0X5800#defineRDA_R020X02//DHIZ[15],DMUTE[14]静音,MONO[13]声道,BASS[12]重低音,SEEKUP[9],SEEK[8],SKMODE[7],CLK_MODE[6:4]时钟源选择,SOFTRESET[1]软复位,ENABLE[0]电源使能#defineRDA_R030X03//CHAN[15:6],TUNE[4],BAND[3:2],SPACE[1:0]设置频率带宽步长#defineRDA_R040X04//STCIEN[14],DE[11],I2Senable[6],#defineRDA_R050X05//INT_MODE[15],SEEKTH[14:8](设定自动搜索信号强度阀值),LNA_PORT_SEL[7:6]=0b10,LNA_ICSEL_BIT[5:4],VOLUME[3:0]音量;#defineRDA_R0A0X0A//STC[14]seekcompleteSF[13]seekfailreadchan[9:0]当前频道#defineRDA_R0B0X0B//RSSI[15:9],FMTRUE[8]当前频道是一个节目台#defineRDA_READ0X23//读RDA5807#defineRDA_WRITE0X22//写RDA5807//IO操作函数sbitSDA=P2^1;sbitSCL=P2^0;sbitRW=P1^1;sbitRS=P1^0;sbitEN=P2^5;ucharcodea[]=FM:.;ucharcodeb[]=Vol:RSSI:;ucharcodeshu[]=0123456789;ucharnum;voiddelayms(uintx)//延迟程序{uinti,j;for(i=x;i0;i--)for(j=113;j0;j--);}/***********************************************矩阵键盘程序********************************************************************/ucharScanKey()//矩阵键盘扫描返回值是当前按键数值如没按下则返回值为零{uintx,y;for(x=0;x4;x++){uchartemp,gaowei;P3=hang[x];//分行置零temp=P3&0xf0;if(temp!=0xf0){delayms(10);if(temp!=0xf0){gaowei=P3/16;//判断是第几列的按键按下,将数据装入高四位switch(gaowei){case0xe:y=0;break;case0xd:y=1;break;case0xb:y=2;break;case0x7:y=3;break;}while(temp!=0xf0){temp=P3&0xf0;}returnxian[x][y];}}}return0;}/*******************************************************LCD1602程序************************************************************************/voidwrite_com(unsignedcharcom){RS=0;P0=com;delayms(5);EN=1;delayms(5);EN=0;}voidwrite_data(unsignedchardate){RS=1;P0=date;delayms(5);EN=1;delayms(5);EN=0;}voidinit_1602(){P0=0xc0;RW=0;EN=0;write_com(0x38);//éè??16*2??ê?£?5*7μ??ó£?8??êy?Y?úwrite_com(0x0c);//éè???a??ê?£?2???ê?1a±êwrite_com(0x06);//D′ò???×?·?oóμ??·?????ó1write_com(0x01);//??ê???á?£?êy?Y??????á?}voidDisplayFrq(floatFrq)//显示频率{uintF;F=Frq*10;write_com(0x80+3);write_data(shu[F/1000]);delayms(1);write_data(shu[F/100%10]);delayms(1);write_data(shu[F/10%10]);delayms(1);write_com(0x80+7);write_data(shu[F%10]);delayms(1);}voidDisplayVol(uintVol)//显示音量{write_com(0x80+0x40+4);write_data(shu[Vol/10]);delayms(1);write_data(shu[Vol%10]);delayms(1);}voidDisplay_mute()//显示静音{write_com(0x80+0x40+4);write_data('x');delayms(1);write_data('x');delayms(1);}voidDisplay_RSSI(uintRSSI)//显示信号强度{write_com(0x80+0x40+14);write_data(shu[RSSI/10]);delayms(1);write_data(shu[RSSI%10]);delayms(1);}/*----------------------------------------IIC通信程序---------------------------------------------------*/voidIIC_delayms()//用于IIC延时{_nop_();_nop_();_nop_();_nop_();}voidOpenIIC()//IIC启动信号{SDA=1;SCL=1;IIC_delayms();SDA=0;IIC_delayms();SCL=0;}voidCloseIIC()//IIC停止信号{SCL=0;SDA=0;IIC_delayms();SCL=1;SDA=1;IIC_delayms();}ucharIIC_Wait_Ack(void)//IIC发送字节后等待从机发送响应信{ucharucErrTime=0;SDA=1;IIC_delayms();SCL=1;IIC_delayms();while(SDA==1){ucErrTime++;if(ucErrTime250){CloseIIC();return1;}}SCL=0;return0;}voidIIC_Ack(void)//发送应答信号{SCL=0;SDA=0;//0±íê?ó|′eIIC_delayms();SCL=1;IIC_delayms();SCL=0;}voidIIC_NAck(void)//IIC非应答信号{SCL=0;SDA=1;IIC_delayms();SCL=1;IIC_delayms();SCL=0;}voidIICsendByte(uchartxd)//IIC·写一个字节{uchart;SCL=0;for(t=0;t8;t++){if(((txd&0x80)7)==1)SDA=1;elseSDA=0;txd=1;IIC_delayms();SCL=1;IIC_delayms();SCL=0;IIC_delayms();}}ucharIICReadByte(unsignedcharack)//IIC读一个字节{unsignedchari,receive=0;SDA=1;//51单片机讲引脚置高可设为输入引脚for(i=0;i8;i++){SCL=0;IIC_delayms();SCL=1;receive=1;if(SDA==1)receive++;IIC_delayms();}if(!ack)IIC_NAck();elseIIC_Ack();returnreceive;}uintReadReg(ucharregAddr)//芯片读寄存器{uintbuf;OpenIIC();IICsendByte(RDA_WRITE);//发送芯片地址方向为写IIC_Wait_Ack();IICsendByte(regAddr);//发送寄存器地址IIC_Wait_Ack();OpenIIC();IICsendByte(RDA_READ);//发送芯片地址方向为读IIC_Wait_Ack();buf=IICReadByte(1);buf=buf8;buf=buf|IICReadByte(0);CloseIIC();returnbuf;}voidWriteReg(ucharregAddr,uintval)//芯片写寄存器{OpenIIC();IICsendByte(RDA_WRITE);//发送芯片地址方向为写IIC_Wait_Ack();IICsendByte(regAddr);//发送寄存器地址IIC_Wait_Ack();IICsendByte(val8);IIC_Wait_Ack();IICsendByte(val&0XFF);IIC_Wait_Ack();CloseIIC();}voidVol_Set(ucharvol)//音量设置0~15{uinttemp=0;temp=ReadReg(RDA_R05);temp&=0xfff0;WriteReg(0x05,vol|temp);}voidMute_Set(ucharmute)//静音设置1为静音0为不静音{uinttemp=0;temp=ReadReg(0X02);if(!mute)temp|=114;elsetemp&=~(114);WriteReg(0X02,temp);}voidBass_Set(ucharbass)//频带设置{uinttemp=0;temp=ReadReg(0X02);if(bass)temp|=112;elsetemp&=~(112);WriteReg(0X02,temp);}ucharRssi_Get(void)//信号强度获取0~63{uinttemp=0;temp=ReadReg(0X0B);temp=(temp9)&0x7f;returntemp;}voidSeekth_Set(uintrssi)//自动搜台信号阈值强度0~15默认为8数值越低搜到的台越多{uinttemp;rssi=rssi&0xf;temp=Re

1 / 16
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功