关于RAD5820的C语言程序,iic协议
资料打包下载:
mic-rda20160823.rar
(36.52 KB, 下载次数: 22)
主程序预览:
- #define MAIN_Fosc 24000000UL //24M
- #include "STC15Fxxxx.H"
- #include <i2c.h>
- /*I2C interface
- sbit SCL = P1^5;
- sbit SDA = P1^6;*/
- //clk out STC --> RDA SUPORT 32.768KHz/12M/24M/13M/26M/19.2/38.4M
- sbit RCLK = P5^4; //RST /SysClk0 /SS_3 /CMP+ RDA5820clk 12M
- //HID interface
- sbit VOICE = P2^3; //HZ 1S/ TIME 3S
- sbit BACK = P0^4; //BACK 1S/ VOL- 3S
- sbit NEXT = P5^2; //NEXT 1S/ VOL+ 3S
- /*i2c
- #define max_freq 108000
- #define min_freq 87500*/
- unsigned char RDA_rest[3]={0x02,0x00,0x02,}; //要写入RDA5820的数据
- unsigned char radio_write_data[3]={0x02,0x00,0x02,};
- //unsigned char radio_write_data[5]={0x2a,0xb6,0x40,0x11,0x40}; //要写入TEA5767的数据
- unsigned char radio_read_data[5]; //TEA5767读出的状态
- unsigned int default_pll=0x301d; //0x29f9; //默认存台的pll,95.8MHz
- //unsigned int max_pll=0x339b; //108MHz时的pll,
- //unsigned int min_pll=9000; //70MHz时的pll
- unsigned long frequency;
- unsigned int pll;
- /*void delay(unsigned int time)
- {
- while(time--);
- }*/
- void delay1ms (unsigned int b)//1ms
- {
- for(b;b>0;b--)
- {unsigned int i;
- for(i=0;i<300;i++)
- {;}
- }
- }
- void radio_write(void)
- {
- unsigned char i;
- iic_start();
- iic_write8bit(0x22); //RDA5820写地址
- if(!iic_testack())
- {
- for(i=0;i<3;i++)
- {
- iic_write8bit(radio_write_data[i]);
- iic_ack();
- }
- }
- iic_stop();
- }
- //由频率计算PLL
- void get_pll(void)
- {
- unsigned char hlsi;
- // unsigned int twpll=0;
- hlsi=radio_write_data[2]&0x10; //HLSI位
- if (hlsi)
- pll=(unsigned int)((float)((frequency+225)*4)/(float)32.768); //频率单位:k
- else
- pll=(unsigned int)((float)((frequency-225)*4)/(float)32.768); //频率单位:k
- }
- //由PLL计算频率
- void get_frequency(void)
- {
- unsigned char hlsi;
- unsigned int npll=0;
- npll=pll;
- hlsi=radio_write_data[2]&0x10;
- if (hlsi)
- frequency=(unsigned long)((float)(npll)*(float)8.192-225); //频率单位:KHz
- else
- frequency=(unsigned long)((float)(npll)*(float)8.192+225); //频率单位:KHz
- }
- //读TEA5767状态,并转换成频率
- void radio_read(void)
- {
- unsigned char i;
- unsigned char temp_l,temp_h;
- pll=0;
- iic_start();
- iic_write8bit(0x23); //RDA5820读地址
- if(!iic_testack())
- {
- for(i=0;i<5;i++)
- {
- radio_read_data[i]=iic_read8bit();
- iic_ack();
- }
- }
- iic_stop();
- temp_l=radio_read_data[1];
- temp_h=radio_read_data[0];
- temp_h&=0x3f;
- pll=temp_h*256+temp_l;
- get_frequency();
- }
- /*
- //手动设置频率,mode=1,+0.1MHz; mode=0:-0.1MHz ,不用考虑TEA5767用于搜台的相关位:SM,SUD
- void search(bit mode)
- {
- radio_read();
- if(mode)
- {
- frequency+=10;
- if(frequency>max_freq)
- frequency=min_freq;
- }
- else
- {
- frequency-=10;
- if(frequency<min_freq)
- frequency=max_freq;
- }
- get_pll();
- radio_write_data[0]=pll/256;
- radio_write_data[1]=pll%256;
- radio_write_data[2]=0x20;
- radio_write_data[3]=0x11;
- radio_write_data[4]=0x00;
- radio_write();
- }*/
- //自动搜台,mode=1,频率增加搜台; mode=0:频率减小搜台,不过这个好像不能循环搜台
- void auto_search(bit mode)
- {
- radio_read();
- get_pll();
- if(mode)
- radio_write_data[2]=0xa0;
- else
- radio_write_data[2]=0x20;
- radio_write_data[0]=pll/256+0x40;
- radio_write_data[1]=pll%256;
- radio_write_data[2]=0x20; //...
- radio_write_data[3]=0x11;
- radio_write_data[4]=0x00;
- radio_write();
- radio_read();
- while(!(radio_read_data[0]&0x80)) //搜台成功标志
- {
- delay1ms(1000);
- radio_read();
- //disp_freq(9,1);
-
- }
- }
- void main()
- {
- /*P0M1 = 0; P0M0 = 0;
- P1M1 = 0; P1M0 = 0;
- P2M1 = 0; P2M0 = 0;
- P3M1 = 0; P3M0 = 0;
- P4M1 = 0; P4M0 = 0;
- P5M1 = 0; P5M0 = 0;
- P6M1 = 0; P6M0 = 0;
- P7M1 = 0; P7M0 = 0; */
-
- radio_write();
- while(1)
- {
- if(NEXT==0)
- {
- delay1ms(15); //延时15ms
- if(NEXT==0)
- while(NEXT==0);//等待按键释放
- auto_search(1);
- radio_read();
-
- }
- if(BACK==0)
- {
- delay1ms(15); //延时15ms
- if(BACK==0)
- while(BACK==0);//等待按键释放
- auto_search(0);
- radio_read();
-
- }
- }
- }
复制代码
|