Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // vim: sw=4 ts=4
- #include "stm32f10x.h"
- #include "stm32f10x_spi.h"
- #include "stm32f10x_flash.h"
- #include "stm32f10x_adc.h"
- #include <math.h>
- void configRCC()
- {
- ErrorStatus status;
- RCC_DeInit();
- RCC_HSEConfig(RCC_HSE_ON);
- status = RCC_WaitForHSEStartUp();
- if(status == SUCCESS)
- {
- FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);
- FLASH_SetLatency(FLASH_Latency_2);
- RCC_HCLKConfig(RCC_SYSCLK_Div1);
- RCC_PCLK2Config(RCC_HCLK_Div1);
- RCC_PCLK1Config(RCC_HCLK_Div2);
- RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9);
- RCC_PLLCmd(ENABLE);
- while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET) {;}
- RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);
- while(RCC_GetSYSCLKSource() != 0x08) {;}
- }
- }
- void initADCs()
- {
- ADC_InitTypeDef ADC_InitStructure;
- GPIO_InitTypeDef GPIO_InitStructure;
- RCC_ADCCLKConfig(RCC_PCLK2_Div2); //zegar 72/6=12
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE); //odblokowanie zegara
- ADC_DeInit(ADC1);
- ADC_InitStructure.ADC_Mode = ADC_Mode_Independent;
- ADC_InitStructure.ADC_ScanConvMode = DISABLE;
- ADC_InitStructure.ADC_ContinuousConvMode = DISABLE;
- ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None;
- ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
- ADC_InitStructure.ADC_NbrOfChannel = 1;
- ADC_Init(ADC1, &ADC_InitStructure);
- ADC_Cmd(ADC1, ENABLE);
- }
- void initDACs()
- {
- SPI_InitTypeDef spiInitStructure;
- GPIO_InitTypeDef GPIO_InitStructure;
- RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI2, ENABLE);
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB, ENABLE);
- spiInitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_2;
- spiInitStructure.SPI_CPHA = SPI_CPHA_1Edge;
- spiInitStructure.SPI_CPOL = SPI_CPOL_Low;
- spiInitStructure.SPI_DataSize = SPI_DataSize_16b;
- spiInitStructure.SPI_Direction = SPI_Direction_1Line_Tx;
- spiInitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
- spiInitStructure.SPI_Mode = SPI_Mode_Master;
- spiInitStructure.SPI_NSS = SPI_NSS_Soft;
- SPI_Init(SPI2, &spiInitStructure);
- SPI_Cmd(SPI2, ENABLE);
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
- GPIO_Init( GPIOA, &GPIO_InitStructure);
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13 | GPIO_Pin_15;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
- GPIO_Init( GPIOB, &GPIO_InitStructure);
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
- GPIO_Init( GPIOB, &GPIO_InitStructure);
- }
- void writeDAC_R(__IO uint16_t val)
- {
- __IO uint32_t speed;
- val &= 0x0FFF;
- val |= 0x7000;
- while(SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_TXE) == RESET);
- GPIO_ResetBits(GPIOB, GPIO_Pin_12);
- SPI_I2S_SendData(SPI2, val);
- for(speed=0; speed<4; speed++) {;}
- GPIO_SetBits(GPIOB,GPIO_Pin_12);
- }
- void writeDAC_L(__IO uint16_t val)
- {
- __IO uint32_t speed;
- val &= 0x0FFF;
- val |= 0x7000;
- while(SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_TXE) == RESET);
- GPIO_ResetBits(GPIOA, GPIO_Pin_8);
- SPI_I2S_SendData(SPI2, val);
- for(speed=0; speed<4; speed++) {;}
- GPIO_SetBits(GPIOA,GPIO_Pin_8);
- }
- void gpioInit()
- {
- //struktura inicjujaca
- GPIO_InitTypeDef GPIO_InitStructure;
- //dolacz sygnal zegarowy do modulu portu A oraz B
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOB,ENABLE);
- //konfiguracja diod LED - port B
- GPIO_InitStructure.GPIO_Pin=GPIO_Pin_5|GPIO_Pin_6|GPIO_Pin_7|GPIO_Pin_8|GPIO_Pin_9;
- GPIO_InitStructure.GPIO_Speed=GPIO_Speed_2MHz;
- GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;
- GPIO_Init(GPIOB,&GPIO_InitStructure);
- //konfiguracja diod LED - PORT A
- GPIO_InitStructure.GPIO_Pin=GPIO_Pin_11|GPIO_Pin_12;
- GPIO_InitStructure.GPIO_Speed=GPIO_Speed_2MHz;
- GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;
- GPIO_Init(GPIOA,&GPIO_InitStructure);
- //konfiguracja przyciskow
- GPIO_InitStructure.GPIO_Pin=GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_10|GPIO_Pin_11;
- GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
- GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IPU;
- GPIO_Init(GPIOB,&GPIO_InitStructure);
- GPIO_InitStructure.GPIO_Pin=GPIO_Pin_0;
- GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
- GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AIN;
- GPIO_Init(GPIOA,&GPIO_InitStructure);
- }
- int main(void)
- {
- /*
- __IO uint32_t step1=0, i;
- __IO int16_t f[8000];
- __IO double arg=0.0;
- __IO double step=0.0;
- int32_t dacValue;
- gpioInit();
- configRCC();
- initDACs();
- for (i=0; i<8000; i++)
- {
- arg = 500.0 * sin(M_TWOPI*i/44100.0);
- f[i]= arg;
- }
- while(1)
- {
- dacValue = 2000;
- dacValue += f[step1];
- step1 += 1;
- step1 = step1 % 8000;
- writeDAC_R(dacValue);
- writeDAC_L(dacValue);
- }
- */
- gpioInit();
- configRCC();
- initDACs();
- initADCs();
- ADC_RegularChannelConfig(ADC1, ADC_Channel_0, 1, ADC_SampleTime_1Cycles5);
- ADC_ResetCalibration(ADC1);
- while(ADC_GetResetCalibrationStatus(ADC1));
- ADC_StartCalibration(ADC1);
- while(ADC_GetCalibrationStatus(ADC1));
- uint16_t sygnal;
- while(1)
- {
- sygnal = ADC_GetConversionValue(ADC1);
- ADC_ClearFlag(ADC1, ADC_FLAG_EOC);
- ADC_SoftwareStartConvCmd(ADC1, ENABLE);
- while(ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC) == RESET);
- writeDAC_L(sygnal);
- //writeDAC_R(sygnal);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement