Advertisement
Guest User

Untitled

a guest
Oct 26th, 2016
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.63 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.  
  14. #include <mbed.h>
  15.  
  16. #include "i2c-lib.h"
  17. #include "si4735-lib.h"
  18.  
  19. //************************************************************************
  20.  
  21. // Direction of I2C communication
  22. #define R 0b00000001
  23. #define W 0b00000000
  24.  
  25. Serial pc( USBTX, USBRX );
  26.  
  27. #pragma GCC diagnostic ignored "-Wunused-but-set-variable"
  28.  
  29. int main( void )
  30. {
  31. uint8_t S1, S2, RSSI, SNR, MULT, CAP;
  32. uint8_t ack = 0;
  33.  
  34. DigitalIn but9(PTC9);
  35. DigitalIn but10(PTC10);
  36.  
  37. I2C_Init();
  38.  
  39. pc.baud( 115200 );
  40. pc.printf( "K64F-KIT ready...\r\n" );
  41.  
  42. // communication with 8 bit expander PCF8574
  43.  
  44. // start communication
  45. I2C_Start();
  46.  
  47. // PCF8574 addressing
  48. // The address is composed from 3 parts!
  49. //ack = I2C_Output( HWADR_PCF8574 | A012 | W );
  50.  
  51. // Check ack! Return value must be 0!
  52. // ....
  53.  
  54. //ack = I2C_Output( Any_8_bit_value );
  55. // selected LEDs should light
  56.  
  57. // stop communication
  58. I2C_Stop();
  59.  
  60. if ( ( ack = SI4735_Init() ) != 0 )
  61. {
  62. pc.printf( "Initialization of SI4735 finish with error (%d)\r\n", ack );
  63. return 0;
  64. }
  65. else
  66. pc.printf( "SI4735 initialized.\r\n" );
  67.  
  68. pc.printf( "\nTunig of radio station...\r\n" );
  69.  
  70. // Required frequency in MHz * 100
  71. int freq = 10140; // Radiozurnal
  72.  
  73. // Tuning of radio station
  74. I2C_Start();
  75. ack |= I2C_Output( SI4735_address | W );
  76. ack |= I2C_Output( 0x20 ); // FM_TUNE_FREQ
  77. ack |= I2C_Output( 0x00 ); // ARG1
  78. ack |= I2C_Output( freq >> 8 ); // ARG2 - FreqHi
  79. ack |= I2C_Output( freq & 0xff ); // ARG3 - FreqLo
  80. ack |= I2C_Output( 0x00 ); // ARG4
  81. I2C_Stop();
  82. // Check ack!
  83. // if...
  84.  
  85. // Tuning process inside SI4735
  86. wait_ms( 100 );
  87. printf( "... station tuned.\r\n\n" );
  88.  
  89. // Example of reading of tuned frequency
  90. I2C_Start();
  91. ack |= I2C_Output( SI4735_address | W );
  92. ack |= I2C_Output( 0x22 ); // FM_TUNE_STATUS
  93. ack |= I2C_Output( 0x00 ); // ARG1
  94. // repeated start
  95. I2C_Start();
  96. // change direction of communication
  97. ack |= I2C_Output( SI4735_address | R );
  98. // read data
  99. S1 = I2C_Input();
  100. I2C_Ack();
  101. S2 = I2C_Input();
  102. I2C_Ack();
  103. freq = (int) I2C_Input() << 8;
  104. I2C_Ack();
  105. freq |= I2C_Input();
  106. I2C_Ack();
  107. RSSI = I2C_Input();
  108. I2C_Ack();
  109. SNR = I2C_Input();
  110. I2C_Ack();
  111. MULT = I2C_Input();
  112. I2C_Ack();
  113. CAP = I2C_Input();
  114. I2C_NAck();
  115. I2C_Stop();
  116.  
  117. int volume = 63;
  118. //int ledky = 0b11111111;
  119. int n = 0;
  120. //int tpm = 0;
  121.  
  122. while(1)
  123. {
  124. if (!but9)
  125. {
  126. while(!but9)
  127. {
  128. if(volume < 63)
  129. {
  130. volume += 7;
  131. /*tmp++;
  132. if(tmp == 8)
  133. {
  134. if (ledky == 0b00000000)
  135. {
  136. ledky = 0b00000001;
  137. }
  138. ledky <<= 1;
  139. ledky |= 0b00000001;
  140. tmp = 0;
  141. I2C_Start();
  142. ack = I2C_Output(0b01000000 | 000 | W);
  143. I2C_Output( ledky );
  144. I2C_Stop();
  145. }*/
  146. }
  147.  
  148. else
  149. {
  150. volume = 63;
  151. }
  152.  
  153. I2C_Start();
  154. ack |= I2C_Output( SI4735_address | W );
  155. ack |= I2C_Output( 0x12 );
  156. ack |= I2C_Output( 0x00 );
  157. ack |= I2C_Output( 0x40 );
  158. ack |= I2C_Output( 0x00 );
  159. ack |= I2C_Output( 0x00 );
  160. ack |= I2C_Output( volume );
  161. I2C_Stop();
  162.  
  163. if (n < 8)
  164. {
  165. n++;
  166. }
  167. I2C_Start();
  168. ack = I2C_Output(0b01000000 | 000 | W);
  169. ack = I2C_Output(pow(2, n) -1);
  170. I2C_Stop();
  171. }
  172. }
  173.  
  174. if (!but10)
  175. {
  176. while(!but10)
  177. {
  178. if(volume > 0)
  179. {
  180. volume -= 7;
  181. /*tmp--;
  182. if(tmp == 0)
  183. {
  184. ledky >>= 1;
  185. tmp = 8;
  186. I2C_Start();
  187. ack = I2C_Output(0b01000000 | 000 | W);
  188. I2C_Output( ledky );
  189. I2C_Stop();
  190. }*/
  191. }
  192. else
  193. {
  194. volume = 0;
  195. }
  196.  
  197. I2C_Start();
  198. ack |= I2C_Output( SI4735_address | W );
  199. ack |= I2C_Output( 0x12 );
  200. ack |= I2C_Output( 0x00 );
  201. ack |= I2C_Output( 0x40 );
  202. ack |= I2C_Output( 0x00 );
  203. ack |= I2C_Output( 0x00 );
  204. ack |= I2C_Output( volume );
  205. I2C_Stop();
  206.  
  207. if (n > 0)
  208. n--;
  209. I2C_Start();
  210. ack = I2C_Output(0b01000000 | 000 | W);
  211. ack = I2C_Output(pow(2, n)- 1);
  212. I2C_Stop();
  213. }
  214. }
  215. }
  216.  
  217. if ( ack != 0 )
  218. printf( "Communication error!\r\n" );
  219. else
  220. printf( "Current tuned frequency: %d.%dMHz\r\n", freq / 100, freq % 100 );
  221.  
  222. return 0;
  223. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement