TEA5767_main.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. #include "TEA5767_main.h"
  2. #define max_freq 108000
  3. #define min_freq 87500
  4. unsigned char radio_write_data[5] = {0x29, 0xc2, 0x20, 0x11, 0x00}; //要写入TEA5767的数据
  5. //unsigned char radio_write_data[5]={0x2a,0xb6,0x40,0x11,0x40}; //要写入TEA5767的数据
  6. unsigned char radio_read_data[5]; //TEA5767读出的状态
  7. unsigned int default_pll = 0x301d; //0x29f9; //默认存台的pll,95.8MHz
  8. //unsigned int max_pll=0x339b; //108MHz时的pll,
  9. //unsigned int min_pll=9000; //70MHz时的pll
  10. unsigned long frequency;
  11. unsigned int pll;
  12. void radio_write ( void )
  13. {
  14. unsigned char i;
  15. iic_start();
  16. iic_write8bit ( 0xc0 ); //TEA5767写地址
  17. if ( !iic_testack() )
  18. {
  19. for ( i = 0; i < 5; i++ )
  20. {
  21. iic_write8bit ( radio_write_data[i] );
  22. iic_ack();
  23. }
  24. }
  25. iic_stop();
  26. }
  27. //由频率计算PLL
  28. void get_pll ( void )
  29. {
  30. unsigned char hlsi;
  31. // unsigned int twpll=0;
  32. hlsi = radio_write_data[2] & 0x10; //HLSI位
  33. if ( hlsi )
  34. pll = ( unsigned int ) ( ( float ) ( ( frequency + 225 ) * 4 ) / ( float ) 32.768 ); //频率单位:k
  35. else
  36. pll = ( unsigned int ) ( ( float ) ( ( frequency - 225 ) * 4 ) / ( float ) 32.768 ); //频率单位:k
  37. }
  38. //由PLL计算频率
  39. void get_frequency ( void )
  40. {
  41. unsigned char hlsi;
  42. unsigned int npll = 0;
  43. npll = pll;
  44. hlsi = radio_write_data[2] & 0x10;
  45. if ( hlsi )
  46. frequency = ( unsigned long ) ( ( float ) ( npll ) * ( float ) 8.192 - 225 ); //频率单位:KHz
  47. else
  48. frequency = ( unsigned long ) ( ( float ) ( npll ) * ( float ) 8.192 + 225 ); //频率单位:KHz
  49. }
  50. //读TEA5767状态,并转换成频率
  51. void radio_read ( void )
  52. {
  53. unsigned char i;
  54. unsigned char temp_l, temp_h;
  55. pll = 0;
  56. iic_start();
  57. iic_write8bit ( 0xc1 ); //TEA5767读地址
  58. if ( !iic_testack() )
  59. {
  60. for ( i = 0; i < 5; i++ )
  61. {
  62. radio_read_data[i] = iic_read8bit();
  63. iic_ack();
  64. }
  65. }
  66. iic_stop();
  67. temp_l = radio_read_data[1];
  68. temp_h = radio_read_data[0];
  69. temp_h &= 0x3f;
  70. pll = temp_h * 256 + temp_l;
  71. get_frequency();
  72. }
  73. //手动设置频率,mode=1,+0.1MHz; mode=0:-0.1MHz ,不用考虑TEA5767用于搜台的相关位:SM,SUD
  74. void search ( bool mode )
  75. {
  76. radio_read();
  77. if ( mode )
  78. {
  79. frequency += 10;
  80. if ( frequency > max_freq )
  81. frequency = min_freq;
  82. }
  83. else
  84. {
  85. frequency -= 10;
  86. if ( frequency < min_freq )
  87. frequency = max_freq;
  88. }
  89. get_pll();
  90. radio_write_data[0] = pll / 256;
  91. radio_write_data[1] = pll % 256;
  92. radio_write_data[2] = 0x20;
  93. radio_write_data[3] = 0x11;
  94. radio_write_data[4] = 0x00;
  95. radio_write();
  96. }
  97. //自动搜台,mode=1,频率增加搜台; mode=0:频率减小搜台,不过这个好像不能循环搜台
  98. /*void auto_search(bit mode)
  99. {
  100. radio_read();
  101. get_pll();
  102. if(mode)
  103. radio_write_data[2]=0xa0;
  104. else
  105. radio_write_data[2]=0x20;
  106. radio_write_data[0]=pll/256+0x40;
  107. radio_write_data[1]=pll%256;
  108. radio_write_data[3]=0x11;
  109. radio_write_data[4]=0x00;
  110. radio_write();
  111. radio_read();
  112. while(!(radio_read_data[0]&0x80)) //搜台成功标志
  113. {
  114. delay(1000);
  115. radio_read();
  116. //disp_freq(9,1);
  117. PutNum(frequency);
  118. }
  119. } */
  120. #if 0
  121. void TEA5767_main ( void )
  122. {
  123. //UART_Init();//串口初始化
  124. //UART_Send_Str("调频收音机\n");
  125. //radio_write();
  126. //while(1)
  127. {
  128. if ( K11.Res == SHORT_KEY_FLAG )
  129. {
  130. K11.Res = NO_KEY_FLAG;
  131. search ( 1 );
  132. radio_read();
  133. sprintf ( n5110_string[4], "FM:%.1fMHz ", frequency * 1.0 / 1000 );
  134. }
  135. if ( K12.Res == SHORT_KEY_FLAG )
  136. {
  137. K12.Res = NO_KEY_FLAG;
  138. search ( 0 );
  139. radio_read();
  140. sprintf ( n5110_string[4], "FM:%.1fMHz ", frequency * 1.0 / 1000 );
  141. }
  142. }
  143. }
  144. #endif