Advertisement
Guest User

Untitled

a guest
Oct 24th, 2017
743
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.12 KB | None | 0 0
  1. // **************************************************************************
  2. //
  3. //               Demo program for labs
  4. //
  5. // Subject:      Computer Architectures and Parallel systems
  6. // Author:       Petr Olivka, petr.olivka@vsb.cz, 08/2016
  7. // Organization: Department of Computer Science, FEECS,
  8. //               VSB-Technical University of Ostrava, CZ
  9. //
  10. // File:         Main programm for I2C bus
  11. //
  12. // **************************************************************************
  13. //projde vsechny stanice fekvence a pro kazdou vypise kvalitu signalu a zobrazi ho na ledkach 88 / 108
  14.  
  15.  
  16. #include <mbed.h>
  17.  
  18. #include "i2c-lib.h"
  19. #include "si4735-lib.h"
  20.  
  21. //************************************************************************
  22.  
  23. // Direction of I2C communication
  24. #define R   0b00000001
  25. #define W   0b00000000
  26.  
  27. Serial pc( USBTX, USBRX );
  28.  
  29. #pragma GCC diagnostic ignored "-Wunused-but-set-variable"
  30.  
  31. double minfreq = 8800;
  32. double maxfreq = 10900;
  33.  
  34. uint8_t ack = 0;
  35.  
  36. void ladit(int freq){
  37.     I2C_Start();
  38.     ack |= I2C_Output( SI4735_address | W );
  39.     ack |= I2C_Output( 0x20 );          // FM_TUNE_FREQ
  40.     ack |= I2C_Output( 0x00 );          // ARG1
  41.     ack |= I2C_Output( freq >> 8 );     // ARG2 - FreqHi
  42.     ack |= I2C_Output( freq & 0xff );   // ARG3 - FreqLo
  43.     ack |= I2C_Output( 0x00 );          // ARG4
  44.     I2C_Stop();
  45.  
  46.     wait_ms( 100 );
  47.     printf( "Frekvence: %d.%dMHz\r\n", freq / 100, freq % 100 );
  48.  
  49. }
  50.  
  51. uint8_t getqual(){
  52.     uint8_t S1, S2, isStation, STBL, RSSI, SNR, MULT, FREQ;
  53.     uint8_t ack = 0;
  54.     I2C_Start();
  55.     ack |= I2C_Output( SI4735_address | W );
  56.     ack |= I2C_Output( 0x23 );
  57.     ack |= I2C_Output( 0x00 );
  58.     I2C_Start();
  59.     ack |= I2C_Output( SI4735_address | R );
  60.     S1 = I2C_Input();
  61.     I2C_Ack();
  62.     S2 = I2C_Input();
  63.     I2C_Ack();
  64.     isStation = I2C_Input() & 1;
  65.     I2C_Ack();
  66.     STBL = I2C_Input();
  67.     I2C_Ack();
  68.     RSSI = I2C_Input();
  69.     I2C_Ack();
  70.     SNR = I2C_Input();
  71.     I2C_Ack();
  72.     MULT = I2C_Input();
  73.     I2C_Ack();
  74.     FREQ = I2C_Input();
  75.     I2C_NAck();
  76.     I2C_Stop();
  77.  
  78.     return SNR;
  79.  
  80. }
  81.  
  82. void ledky(int snr){
  83.  
  84.     I2C_Start();
  85.  
  86.     ack = I2C_Output( 0b01000000 | 0b00001110 | W );
  87.  
  88.     if ( ack != 0 )
  89.         pc.printf("chyba\n");
  90.  
  91.     if (snr == 0){
  92.         I2C_Output( 0b000000000);
  93.     }
  94.     if (snr==1||snr==2){
  95.         I2C_Output( 0b000000001);
  96.     }
  97.     if (snr==3||snr==4){
  98.         I2C_Output( 0b000000011);
  99.     }
  100.     if (snr==5||snr==6){
  101.         I2C_Output( 0b000000111);
  102.     }
  103.     if (snr==7||snr==8){
  104.         I2C_Output( 0b000001111);
  105.     }
  106.     if (snr==9||snr==10){
  107.         I2C_Output( 0b000011111);
  108.     }
  109.     if (snr==11||snr==12){
  110.         I2C_Output( 0b00011111);
  111.     }
  112.     if (snr==13||snr==14){
  113.         I2C_Output( 0b011111111);
  114.     }
  115.     if (snr>14){
  116.         I2C_Output( 0b111111111);
  117.     }
  118.     I2C_Stop();
  119.  
  120. }
  121.  
  122. int main( void )
  123. {
  124.     I2C_Init();
  125.  
  126.     pc.baud( 115200 );
  127.     pc.printf( "K64F-KIT ready...\r\n" );
  128.  
  129.         I2C_Start();
  130.  
  131.  
  132.         I2C_Stop();
  133.  
  134.         if ( ( ack = SI4735_Init() ) != 0 )
  135.         {
  136.             pc.printf( "Initialization of SI4735 finish with error (%d)\r\n", ack );
  137.             return 0;
  138.         }
  139.         else
  140.             pc.printf( "SI4735 initialized.\r\n" );
  141.  
  142.         pc.printf( "\nTunig of radio station...\r\n" );
  143.  
  144.  
  145.  
  146.  
  147.     for(double d=minfreq;d<maxfreq;d++){
  148.         ladit(d);
  149.         int snr = getqual();
  150.         printf("SNR: %i ",snr);
  151.         ledky(snr);
  152.  
  153.     }
  154.    
  155.     return 0;
  156. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement