[国产单片机] 有用过收音机RTC6222的吗?调试不通

[复制链接]
1525|0
 楼主| 单模先声 发表于 2022-10-8 15:18 | 显示全部楼层 |阅读模式
这是原理图,RCLK是直接接到单片机晶振32.768K的XOUT脚上。代码附上,也不知道什么原因,根据官方demo的,就是搜不了音


  1. #include "main.h"


  2. //读芯片寄存器
  3. u16 OperationRTC6222_read(u8 reg_offset)                       
  4. {
  5.     u16 reg_value = 0;

  6.     fm_i2c_start();
  7.     fm_i2c_send_byte(0xc8);
  8.     fm_i2c_recv_ack();
  9.     fm_i2c_send_byte(reg_offset);
  10.     fm_i2c_recv_ack();
  11.     fm_i2c_stop();

  12.     delay_ms(10);//RTC6222_Delay(10);
  13.     fm_i2c_start();
  14.     fm_i2c_send_byte(0xc9);
  15.     fm_i2c_recv_ack();

  16.     reg_value = fm_i2c_read_byte();
  17.     fm_i2c_send_ack();
  18.     reg_value = reg_value<<8;
  19.     reg_value +=  fm_i2c_read_byte();
  20.     fm_i2c_send_nack();

  21.     delay_ms(10);//RTC6222_Delay(10);
  22.     fm_i2c_stop();

  23.     return  reg_value;
  24. }
  25. //写芯片寄存器
  26. void OperationRTC6222_write(u16 in_data, u8 reg_offset)
  27. {
  28.     fm_i2c_start();
  29.     fm_i2c_send_byte(0xc8);
  30.     fm_i2c_recv_ack();
  31.     fm_i2c_send_byte(reg_offset);
  32.     fm_i2c_recv_ack();

  33.     fm_i2c_send_byte((u8)(in_data >> 8));
  34.     fm_i2c_recv_ack();
  35.     fm_i2c_send_byte((u8)in_data);
  36.     fm_i2c_recv_ack();

  37.     fm_i2c_stop();
  38. }

  39. u8 RTC6222_online(void)
  40. {
  41.         u8 i;
  42.     for(i=0;i<3;i++) {
  43.         if(0x1688 == OperationRTC6222_read(0)) {
  44.             return 1;
  45.         }
  46.     }
  47.     return 0;
  48. }

  49. void RTC6222_init_(void)
  50. {
  51.     OperationRTC6222_write(0x16AA,0x00); //Power off
  52.     delay_ms(50);

  53.     OperationRTC6222_write(0x96AA,0x00);//Power on
  54.     delay_ms(100);

  55.     OperationRTC6222_write(0x4000,0x02);//vol = 0a ,disable mute

  56.     OperationRTC6222_write(0x1900,0x04);//enable rclk osc

  57.     OperationRTC6222_write(0x656f,0x05);//set seek noise th and spike th ,0x01h=0010bin,00=80(noise_th),01=144(spike_th),and rssi th=0x0c.

  58.                 OperationRTC6222_write(0xB000,0x06);//bit15:ENABLE IC ,bit12:enable check same channal, bit:2-0set rlck choice one from  32k~38.4M
  59.                 OperationRTC6222_write(0xB400,0x07);   //正弦波
  60.     OperationRTC6222_write(0x043C,0x08);//set seek band of freq  start_freq0x043C=1084. x100khz
  61.     OperationRTC6222_write(0x0280,0x09);//set seek start_freq 0x035C=860, x100khz
  62.     OperationRTC6222_write(0x0B10,0x11);//rclk force 0x0Bh= bin1011
  63.     OperationRTC6222_write(0x1108,0x20);//1008 hopping open is better than 1018 nohopping
  64.     delay_ms(100);
  65. //                OperationRTC6222_write((OperationRTC6222_read(0X06)|0X8000),0X06);
  66. //                delay_ms(100);
  67.     //print_all_reg();
  68. }

  69. void RTC6222_init(void)
  70. {
  71.     OperationRTC6222_write(0x16AA,0x00); //Power off
  72.     delay_ms(50);

  73.     OperationRTC6222_write(0x96AA,0x00);//Power on
  74.     delay_ms(100);

  75.     OperationRTC6222_write(0x4000,0x02);//vol = 0a ,disable mute

  76.     OperationRTC6222_write(0x1900,0x04);//enable rclk osc

  77.     OperationRTC6222_write(0x656f,0x05);//set seek noise th and spike th ,0x01h=0010bin,00=80(noise_th),01=144(spike_th),and rssi th=0x0c.

  78.                 OperationRTC6222_write(0xB000,0x06);//bit15:ENABLE IC ,bit12:enable check same channal, bit:2-0set rlck choice one from  32k~38.4M
  79.         //        OperationRTC6222_write(0xB400,0x07);   //正弦波
  80.     OperationRTC6222_write(0x043C,0x08);//set seek band of freq  start_freq0x043C=1084. x100khz
  81.     OperationRTC6222_write(0x0280,0x09);//set seek start_freq 0x035C=860, x100khz
  82.     OperationRTC6222_write(0x0B10,0x11);//rclk force 0x0Bh= bin1011
  83.     OperationRTC6222_write(0x1108,0x20);//1008 hopping open is better than 1018 nohopping
  84.     delay_ms(100);
  85. //                OperationRTC6222_write((OperationRTC6222_read(0X06)|0X8000),0X06);
  86. //                delay_ms(100);
  87.     //print_all_reg();
  88. }

  89. void RTC6222_off(void)
  90. {
  91.     OperationRTC6222_write(0x16AA,0x00);
  92.     delay_ms(50);
  93. }

  94. void RTC6222_set_vol(u8 FM_Volumn)
  95. {
  96.         u16 write_byte=0;
  97.     if(FM_Volumn > 15) {
  98.         FM_Volumn = 15;
  99.     }

  100.      write_byte = 0x4000|(FM_Volumn&0x0f);
  101.     OperationRTC6222_write(write_byte,0x02);
  102.     //printf("RTC6222_set_vol vol=%d,0x%04x reg[2]=0x%04x\n",FM_Volumn,write_byte,OperationRTC6222_read(0x02));
  103. }

  104. void RTC6222_mute(bool is_mute)
  105. {
  106.     u16 write_byte;
  107.     write_byte = OperationRTC6222_read(0x02);
  108.     if (is_mute) {
  109.         write_byte &= ~0x4000;
  110.     }
  111.     else {
  112.         write_byte |= 0x4000;
  113.     }
  114.     OperationRTC6222_write(write_byte,0x02);

  115.     //printf("RTC6222_mute is_mute = %d, reg[2]=0x%04x\n",is_mute,OperationRTC6222_read(0x02));
  116. }

  117. u8 RTC6222_set_freq(u16 channel_freq)
  118. {
  119.     u16 write_byte;
  120.     u8 timeout = 0;
  121.     u16 Seek_Status = 0;
  122.         u16 test=0;
  123.        
  124. #ifndef __RTCFM_STEP_50K__
  125.     channel_freq *= 10;
  126. #endif
  127.     //printf("==RTC6222_set_freq: %d\n",channel_freq);
  128.     OperationRTC6222_write(0x8000|channel_freq,0x03);

  129.     //New Algorithm part 1=on 0=off by Danny 20150724
  130.     do{
  131.         delay_ms(40);               // 8 = 40ms(Default) 2 = 10ms(Speed limit) by Danny 20150724
  132.         timeout++;
  133.         Seek_Status = ((0xFC00 & OperationRTC6222_read(0x0B)) >> 10);   //add by Danny 20150702
  134.         //WATCHDOG_CLR();
  135.                         test=OperationRTC6222_read(0x0A);
  136.     }while ((Seek_Status == 0) && ((0x4000 & OperationRTC6222_read(0x0A))== 0) && (timeout < 200));
  137.     //printf("Seek_Status_STD1= 0x%02X ",Seek_Status);

  138.     Seek_Status = ((0xFC00 & OperationRTC6222_read(0x0B)) >> 10);   //add by Danny 20150702
  139.     //printf("Seek_Status1= 0x%04X ",Seek_Status);

  140.     write_byte = channel_freq & (~0x8000);
  141.     OperationRTC6222_write(write_byte,0x03);

  142.     while(0x4000 & OperationRTC6222_read(0x0A));

  143.     Seek_Status = ((0xFC00 & OperationRTC6222_read(0x0B)) >> 10);   //add by Danny 20150702
  144.     //printf("Seek_Status2= 0x%04X\n ",Seek_Status);
  145.     if(Seek_Status == 0) {           //When Get Seek_Satus BK0_B[15:10] all 6b' = 0, Return 1. add by Danny 20150703
  146.         return 1;
  147.     }
  148.     else {
  149.         return 0;
  150.     }
  151. }

  152. u8 RTC6222_seek_hw(u16 channel_freq)
  153. {
  154.     u16  write_byte;
  155.         u16 read_buf;
  156.     u8 return_value = 0;
  157. #ifndef __RTCFM_STEP_50K__
  158.     channel_freq *= 10;
  159. #endif
  160.     write_byte = OperationRTC6222_read(0x05);
  161.    

  162.     write_byte &= (~0x8000);
  163.     OperationRTC6222_write(write_byte,0x05);

  164.     write_byte |= 0x8000;
  165.     OperationRTC6222_write(write_byte,0x05);

  166.     do {
  167.         read_buf = OperationRTC6222_read(0x0A);
  168.         delay_ms(45);      //time out determined by reg0x04_9:8 seek time 0:40ms 1:160 ms
  169.         //WATCHDOG_CLR();
  170.     }
  171.     while(!(0x4000&read_buf));

  172.     if((0x6000&read_buf) == 0x6000) {//must judge at here and not move it at end of fuction
  173.         return_value = 0;       //reach band top limited, all transport 0xff because app level has handled top limited freq like 110Mhz
  174.     }
  175.     else {
  176.         //printf("\t%5d\t%d\t", 0x7fff&OperationRTC6222_read(0x0C),0x00ff&OperationRTC6222_read(0x0B));
  177.         return_value = 1;       //tell app lever that need to read cur_freq from driver lever.
  178.     }

  179.     write_byte &= (~0x8000);
  180.     OperationRTC6222_write(write_byte,0x05);

  181.     while(0x4000&OperationRTC6222_read(0x0A));

  182.     return return_value;
  183. }

  184. u8 RTC6222_seek(u16 channel_freq)
  185. {
  186. #if FM_SEEK_HW
  187.     return RTC6222_seek_hw(channel_freq);
  188. #else
  189.     u8 Status0 = 0;

  190. #ifdef __RTCFM_STEP_50K_CHOOSE_BEST_FROM_TW0__
  191.     u16 IF_Shift_1 = 0, IF_Shift_2 = 0;
  192.     u8 Status1 =0;
  193.     u8 shiftindex=0; //请看函数头的返回值说明,0表示当前上层传给底层的頻点,1表示上层传给底层的頻点再+50k。在搜到台时来告诉上层存台和放音选择正确的頻点。
  194. #endif

  195.     Status0 = RTC6222_set_freq(channel_freq);  // Check Seek_States is 0 ornot by Danny 20150702
  196.     //delay_5ms(20);

  197.     if(Status0)
  198.             {//Get one ture Channnel
  199. #ifdef __RTCFM_STEP_50K_CHOOSE_BEST_FROM_TW0__
  200.         if((0x0040 & OperationRTC6222_read(0x0E)) != 0)
  201.                 {
  202.             IF_Shift_1 = (~(0x003F & OperationRTC6222_read(0x0E))+1) &0x003F; //2's complement
  203.         }
  204.         else {
  205.             IF_Shift_1 = ((0x007F & OperationRTC6222_read(0x0E))); //2's complement
  206.         }
  207.         Status1 = RTC6222_set_freq(channel_freq+5);

  208.         if(Status1)
  209.                 {
  210.             if((0x0040 & OperationRTC6222_read(0x0E)) != 0)
  211.                     {
  212.                 IF_Shift_2 = (~(0x003F & OperationRTC6222_read(0x0E))+1) &0x003F; //2's complement
  213.             }
  214.             else {
  215.                 IF_Shift_2 = ((0x007F & OperationRTC6222_read(0x0E))); //2's complement
  216.             }


  217.             if(IF_Shift_1 < IF_Shift_2) {
  218.                 RTC6222_set_freq(channel_freq);
  219.                 shiftindex = 0;
  220.             }
  221.             else {
  222.                 RTC6222_set_freq(channel_freq+5);
  223.                 shiftindex = 1;
  224.             }

  225.         }
  226.         else {
  227.             RTC6222_set_freq(channel_freq); //Back Channel 50KHz for Return 1
  228.         }
  229.         RSSIValue = (0x00FF & (OperationRTC6222_read(0x0B)));
  230.         Reg_C = (OperationRTC6222_read(0x0C));
  231.         //printf("freq= %d RSSI= %02d IF_Shift_1= %02d IF_Shift_2= %02d shiftindex= %d Return %d \n",Reg_C,RSSIValue,IF_Shift_1,IF_Shift_2,shiftindex,1+shiftindex);

  232.         return (1+shiftindex);
  233. #else
  234.         return 1;
  235. #endif
  236.     }
  237.     else {
  238.         return 0;
  239.     }
  240. #endif
  241. }

  242. u16 RTC6222_read_cur_seeking_freq(void)
  243. {
  244. #ifdef __RTCFM_STEP_50K__
  245.     return (0x7fff&OperationRTC6222_read(0x0C));
  246. #else
  247.     return (0x7fff&OperationRTC6222_read(0x0C)) / 10;
  248. #endif
  249. }




本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?注册

×
您需要登录后才可以回帖 登录 | 注册

本版积分规则

92

主题

604

帖子

1

粉丝
快速回复 在线客服 返回列表 返回顶部