Advertisement
Guest User

Untitled

a guest
Oct 21st, 2019
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.28 KB | None | 0 0
  1. // vim: sw=4 ts=4
  2.  
  3. #include "stm32f10x.h"
  4. #include "stm32f10x_spi.h"
  5. #include "stm32f10x_flash.h"
  6. #include "stm32f10x_adc.h"
  7.  
  8. #include <math.h>
  9.  
  10. void configRCC()
  11. {
  12. ErrorStatus status;
  13. RCC_DeInit();
  14. RCC_HSEConfig(RCC_HSE_ON);
  15. status = RCC_WaitForHSEStartUp();
  16.  
  17. if(status == SUCCESS)
  18. {
  19. FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);
  20. FLASH_SetLatency(FLASH_Latency_2);
  21.  
  22. RCC_HCLKConfig(RCC_SYSCLK_Div1);
  23. RCC_PCLK2Config(RCC_HCLK_Div1);
  24. RCC_PCLK1Config(RCC_HCLK_Div2);
  25. RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9);
  26. RCC_PLLCmd(ENABLE);
  27.  
  28. while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET) {;}
  29. RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);
  30.  
  31. while(RCC_GetSYSCLKSource() != 0x08) {;}
  32. }
  33. }
  34.  
  35. void initADCs()
  36. {
  37. ADC_InitTypeDef ADC_InitStructure;
  38. GPIO_InitTypeDef GPIO_InitStructure;
  39.  
  40. RCC_ADCCLKConfig(RCC_PCLK2_Div2); //zegar 72/6=12
  41. RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE); //odblokowanie zegara
  42.  
  43. ADC_DeInit(ADC1);
  44. ADC_InitStructure.ADC_Mode = ADC_Mode_Independent;
  45. ADC_InitStructure.ADC_ScanConvMode = DISABLE;
  46. ADC_InitStructure.ADC_ContinuousConvMode = DISABLE;
  47. ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None;
  48. ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
  49. ADC_InitStructure.ADC_NbrOfChannel = 1;
  50. ADC_Init(ADC1, &ADC_InitStructure);
  51. ADC_Cmd(ADC1, ENABLE);
  52. }
  53.  
  54. void initDACs()
  55. {
  56. SPI_InitTypeDef spiInitStructure;
  57. GPIO_InitTypeDef GPIO_InitStructure;
  58.  
  59. RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI2, ENABLE);
  60. RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB, ENABLE);
  61.  
  62. spiInitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_2;
  63. spiInitStructure.SPI_CPHA = SPI_CPHA_1Edge;
  64. spiInitStructure.SPI_CPOL = SPI_CPOL_Low;
  65. spiInitStructure.SPI_DataSize = SPI_DataSize_16b;
  66. spiInitStructure.SPI_Direction = SPI_Direction_1Line_Tx;
  67. spiInitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
  68. spiInitStructure.SPI_Mode = SPI_Mode_Master;
  69. spiInitStructure.SPI_NSS = SPI_NSS_Soft;
  70.  
  71. SPI_Init(SPI2, &spiInitStructure);
  72. SPI_Cmd(SPI2, ENABLE);
  73.  
  74. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;
  75. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  76. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  77. GPIO_Init( GPIOA, &GPIO_InitStructure);
  78.  
  79. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13 | GPIO_Pin_15;
  80. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  81. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  82. GPIO_Init( GPIOB, &GPIO_InitStructure);
  83. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12;
  84. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  85. GPIO_Init( GPIOB, &GPIO_InitStructure);
  86. }
  87.  
  88. void writeDAC_R(__IO uint16_t val)
  89. {
  90. __IO uint32_t speed;
  91.  
  92. val &= 0x0FFF;
  93. val |= 0x7000;
  94.  
  95. while(SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_TXE) == RESET);
  96. GPIO_ResetBits(GPIOB, GPIO_Pin_12);
  97. SPI_I2S_SendData(SPI2, val);
  98. for(speed=0; speed<4; speed++) {;}
  99. GPIO_SetBits(GPIOB,GPIO_Pin_12);
  100. }
  101.  
  102. void writeDAC_L(__IO uint16_t val)
  103. {
  104. __IO uint32_t speed;
  105.  
  106. val &= 0x0FFF;
  107. val |= 0x7000;
  108.  
  109. while(SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_TXE) == RESET);
  110. GPIO_ResetBits(GPIOA, GPIO_Pin_8);
  111. SPI_I2S_SendData(SPI2, val);
  112. for(speed=0; speed<4; speed++) {;}
  113. GPIO_SetBits(GPIOA,GPIO_Pin_8);
  114. }
  115.  
  116. void gpioInit()
  117. {
  118. //struktura inicjujaca
  119. GPIO_InitTypeDef GPIO_InitStructure;
  120. //dolacz sygnal zegarowy do modulu portu A oraz B
  121. RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOB,ENABLE);
  122. //konfiguracja diod LED - port B
  123. GPIO_InitStructure.GPIO_Pin=GPIO_Pin_5|GPIO_Pin_6|GPIO_Pin_7|GPIO_Pin_8|GPIO_Pin_9;
  124. GPIO_InitStructure.GPIO_Speed=GPIO_Speed_2MHz;
  125. GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;
  126. GPIO_Init(GPIOB,&GPIO_InitStructure);
  127. //konfiguracja diod LED - PORT A
  128. GPIO_InitStructure.GPIO_Pin=GPIO_Pin_11|GPIO_Pin_12;
  129. GPIO_InitStructure.GPIO_Speed=GPIO_Speed_2MHz;
  130. GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;
  131. GPIO_Init(GPIOA,&GPIO_InitStructure);
  132. //konfiguracja przyciskow
  133. GPIO_InitStructure.GPIO_Pin=GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_10|GPIO_Pin_11;
  134. GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
  135. GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IPU;
  136. GPIO_Init(GPIOB,&GPIO_InitStructure);
  137.  
  138. GPIO_InitStructure.GPIO_Pin=GPIO_Pin_0;
  139. GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
  140. GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AIN;
  141. GPIO_Init(GPIOA,&GPIO_InitStructure);
  142. }
  143.  
  144. int main(void)
  145. {
  146. /*
  147. __IO uint32_t step1=0, i;
  148. __IO int16_t f[8000];
  149. __IO double arg=0.0;
  150. __IO double step=0.0;
  151.  
  152. int32_t dacValue;
  153.  
  154. gpioInit();
  155. configRCC();
  156. initDACs();
  157.  
  158.  
  159. for (i=0; i<8000; i++)
  160. {
  161. arg = 500.0 * sin(M_TWOPI*i/44100.0);
  162. f[i]= arg;
  163. }
  164.  
  165. while(1)
  166. {
  167.  
  168. dacValue = 2000;
  169. dacValue += f[step1];
  170. step1 += 1;
  171. step1 = step1 % 8000;
  172.  
  173. writeDAC_R(dacValue);
  174. writeDAC_L(dacValue);
  175. }
  176. */
  177. gpioInit();
  178. configRCC();
  179. initDACs();
  180. initADCs();
  181.  
  182. ADC_RegularChannelConfig(ADC1, ADC_Channel_0, 1, ADC_SampleTime_1Cycles5);
  183.  
  184. ADC_ResetCalibration(ADC1);
  185. while(ADC_GetResetCalibrationStatus(ADC1));
  186.  
  187. ADC_StartCalibration(ADC1);
  188. while(ADC_GetCalibrationStatus(ADC1));
  189. uint16_t sygnal;
  190. while(1)
  191. {
  192.  
  193.  
  194.  
  195. sygnal = ADC_GetConversionValue(ADC1);
  196. ADC_ClearFlag(ADC1, ADC_FLAG_EOC);
  197.  
  198. ADC_SoftwareStartConvCmd(ADC1, ENABLE);
  199. while(ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC) == RESET);
  200. writeDAC_L(sygnal);
  201. //writeDAC_R(sygnal);
  202.  
  203. }
  204. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement