[PIC®/AVR®/dsPIC®产品] PIC16F驱动Si4703 实现FM收音机

[复制链接]
 楼主| 玛尼玛尼哄 发表于 2024-9-27 22:47 | 显示全部楼层 |阅读模式
游客,如果您要查看本帖隐藏内容请回复

  1. /*
  2. * This is the main program.
  3. * Copyright (C)2015  D.N. Amerasinghe
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include <xc.h>
  19. #include <stdio.h>
  20. #include <stdlib.h>

  21. #include "uart.h"
  22. #include "i2c.h"
  23. #include "si4703.h"
  24. #include "hd44780i2c.h"

  25. #define _XTAL_FREQ      20000000 // 20MHz Clock


  26. #pragma config BOREN = ON, CPD = OFF, FOSC = HS, WDTE = OFF, CP = OFF, LVP = OFF, PWRTE = ON

  27. uint16_t rssi = 0;
  28. uint8_t scale = 0;
  29. //uint8_t buff[16] = {0};

  30. eeprom uint16_t last_channel;

  31. void init()
  32. {
  33.     //Disabling the A2D converter
  34.     ADCON0bits.ADON = 0;

  35.     //PORTC = 0x00;
  36.     TRISD = 0x00;
  37.     PORTD = 0x00;
  38.    
  39.     TRISBbits.TRISB0 = 1;
  40.     TRISBbits.TRISB1 = 1;
  41.     TRISBbits.TRISB2 = 0;
  42.    
  43.     PORTBbits.RB2 = 0;
  44.    
  45.     //Enabling Timer 1 interrupt
  46.     T1CONbits.TMR1ON  = 1;
  47.     T1CONbits.T1CKPS0 = 1; //
  48.     T1CONbits.T1CKPS1 = 1;
  49.    
  50.     PIE1bits.TMR1IE  = 1;
  51.     INTCONbits.PEIE  = 1;
  52.     INTCONbits.GIE   = 1;
  53. }

  54. void interrupt isr()
  55. {
  56.     if(PIR1bits.TMR1IF){
  57.         scale++;
  58.         //Send data in 15s interval
  59.         //if( scale == 20 ){
  60.             
  61.             //scale = 0;
  62.         //}
  63.         //uint16_t rssi = si4703_getrssi();
  64.         
  65.         //printf("RSSI = %d\r\n", rssi);
  66.         
  67. //        if(rssi & 0x0100){
  68. //            //printf("Stereo\r\n");
  69. //            PORTBbits.RB2 = 1;
  70. //        }else{
  71. //            //printf("Mono\r\n");
  72. //            PORTBbits.RB2 = 0;
  73. //        }
  74.        //if( scale == 20 ){
  75.            //last_channel = si4703_getchannel();
  76.        //}
  77.         
  78.        if( scale == 70 ){
  79.             hd44780_lightoff();
  80.             
  81.             //PIE1bits.TMR1IE  = 0;
  82.             //PIR1bits.TMR1IF = 0;
  83.             
  84.             scale = 0;
  85.        }
  86.         

  87.         
  88.         //hd44780_puts_at(buff, 0);
  89.         // Re-enabling interrupt
  90.         PIR1bits.TMR1IF = 0;
  91.     }
  92. }

  93. uint8_t debounce(uint8_t port, uint8_t pin)
  94. {
  95.     uint8_t count = 0;
  96.    
  97.     while( !(port & (1 << pin)) && count < 5 ){
  98.         //PORTBbits.RB2 = 0;
  99.         __delay_ms(10);
  100.         count++;
  101.     }
  102.    
  103.     if(count == 5){
  104.         return 1;
  105.     }else{
  106.         return 0;
  107.     }
  108. }

  109. int main(int argc, char** argv)
  110. {
  111.     uint8_t btn_pressed = 1;
  112.     uint8_t buff[16] = {0};
  113.     //uint8_t buff[16] = {0};
  114.     uint16_t channel = 0;
  115.     uint8_t whole = 0, frac = 0;

  116.     init();
  117.     uart_init();
  118.    
  119.     si4703_init();
  120.    
  121.     hd44780_init();
  122.    
  123.     //hd44780_putchar_at('X', 8);
  124.     //sprintf(buff, "%s\n", "TEST");
  125.     //hd44780_puts("TEST STRING\n");
  126.     //hd44780_puts("TEST STRING");
  127.     //hd44780_puts(buff);        
  128.    
  129.     hd44780_lighton();
  130.    
  131.     si4703_tune( last_channel + 875 );

  132.     while(1){
  133.         
  134.         rssi = si4703_getrssi();
  135.         
  136.         //if( scale == 1 ){
  137.         sprintf(buff, "%d", rssi & 0x00FF);
  138.         hd44780_puts_at(buff, 14);
  139.             
  140.         //si4703_get_rds(buff);
  141.         //sprintf(buff, "%s", buff);
  142.         //hd44780_newline();
  143.         //hd44780_puts_at(buff, 0);
  144.             //scale = 0;
  145.         //}
  146.         
  147.         
  148.         
  149.         if(rssi & 0x0100){
  150.             //printf("Stereo\r\n");
  151.             PORTBbits.RB2 = 1;
  152.         }else{
  153.             //printf("Mono\r\n");
  154.             PORTBbits.RB2 = 0;
  155.         }
  156.         //printf("RSSI = %d\r\n", rssi);
  157.         
  158. //        if(rssi & 0x0100){
  159. //            //printf("Stereo\r\n");
  160. //            PORTBbits.RB2 = 1;
  161. //        }else{
  162. //            //printf("Mono\r\n");
  163. //            PORTBbits.RB2 = 0;
  164. //        }
  165.         
  166.         if(debounce(PORTB, 1)){
  167.             btn_pressed = 1;
  168.             //PORTBbits.RB2 = 0;
  169.             //btn1_pressed = 1;
  170.             hd44780_lighton();
  171.             //TMR1 = 0;
  172.             scale = 0;
  173.             si4703_seek(0);
  174.             //last_channel = si4703_getchannel();
  175.             //continue;
  176.             //__delay_ms(200);
  177.             //while( debounce(PORTB, RB1) );
  178.         }
  179.         if(debounce(PORTB, 0)){
  180.             //printf("RB0 pressed\r\n");
  181.             btn_pressed = 1;
  182.             hd44780_lighton();
  183.             //TMR1 = 0;
  184.             scale = 0;
  185.             si4703_seek(1);
  186.             //last_channel = si4703_getchannel();
  187.         }
  188.         

  189.         if(!btn_pressed)
  190.             continue;
  191.             
  192.         
  193.         channel = si4703_getchannel();
  194.         float chn = ((channel * 0.1) + 87.5);
  195.         whole = chn;
  196.         float f1 = chn - whole;
  197.         frac = (uint8_t)(f1 * 10);
  198.         //whole = ((channel * 0.1) + 875)/10;
  199.         //F = S x C + L
  200.         //sprintf(buff, "%f", ((channel * 0.1) + 875)/10);
  201.         sprintf(buff, "%d.%dMhz ", whole, frac);
  202.         //hd44780_clear();
  203.         hd44780_puts_at(buff, 4);

  204.         
  205.         //__delay_ms(200);
  206.        last_channel = si4703_getchannel();
  207.        btn_pressed = 0;
  208.       
  209.     }

  210.     return (EXIT_SUCCESS);
  211. }


本帖子中包含更多资源

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

×
jiekou001 发表于 2024-9-29 14:46 | 显示全部楼层
这个芯片跟RDA5807比怎么样?哪个效果更好。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

196

主题

3258

帖子

2

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

196

主题

3258

帖子

2

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