cancel
Showing results for 
Search instead for 
Did you mean: 

SDADC in STM32F373vct6

than cuong
Associate

hello everybody, I'm a 4th years student and now I'm interesting in stm32. I've started a small project which use SDADC of STM32f373vct6. I've read the referrent manual and configure it as described. But I cant get any result to send through USART1. Please help me to find my mistake . thank you in advanced.

best regards!

#include "main.h"
static __IO uint32_t TimingDelay;
static volatile uint16_t _sdadc_value;
int main()
{
	
	GPIO_config();
	USART1_config();
	SDADC1_configure();
	SDADC_SoftwareStartConv(SDADC1);
	if (SysTick_Config(SystemCoreClock / 1000))
				while (1);
	//USART1_putChar('a');
	//while(USART1_getChar() != 'a');
	while(1)
		{
			_sdadc_value = SDADC_GetConversionValue(SDADC1);
			USART1_putString("sending");
			USART1_putChar(_sdadc_value>>8 | 0x05);
			Delay(300);
			USART1_putChar(_sdadc_value & 0xff);
			USART1_putString("sent");
			Delay(200);
		}
}
/* Function define */
void GPIO_config(void)
	{
		GPIO_InitTypeDef GPIO_InitStructure;
		/* Enable clock for GPIOC */
		RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOC,ENABLE);
		/* Configure GPIOC as Output */
		GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
		GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13;
		GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
		GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
		GPIO_Init(GPIOC, &GPIO_InitStructure);
	}
void USART1_config(void)
	{
		/* Init Struct */
		GPIO_InitTypeDef GPIO_InitStructure;
		USART_InitTypeDef USART1_initStructure;
		/* Enable clock for GPIOC */
		RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOC,ENABLE);
		RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE);
		GPIO_PinAFConfig(GPIOC,GPIO_PinSource4,GPIO_AF_7);
		/* Configure PC4(TX) as Output Push Pull */
		GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
		GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4;
		GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
		GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
		GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
		GPIO_Init(GPIOC, &GPIO_InitStructure);
		/* Configure PC5(RX) as Input floating */
		GPIO_PinAFConfig(GPIOC,GPIO_PinSource5,GPIO_AF_7);
		GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;
		GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
		GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
		GPIO_Init(GPIOC, &GPIO_InitStructure);
		/* Configure USART1 module */
		USART_DeInit(USART1);
		USART1_initStructure.USART_BaudRate = 9600;
		USART1_initStructure.USART_WordLength = USART_WordLength_8b;
		USART1_initStructure.USART_Mode = USART_Mode_Tx|USART_Mode_Rx;
		USART1_initStructure.USART_Parity = USART_Parity_No;
		USART1_initStructure.USART_StopBits = USART_StopBits_1;
		USART1_initStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
		USART_Init(USART1,&USART1_initStructure);
		USART_Cmd(USART1, ENABLE);  
		/* done! */
	}
/* Send data though UART1 */
int USART1_putChar(uint8_t data)
	{
		USART1->TDR = (data & 0xff);
		while(USART_GetFlagStatus(USART1,USART_FLAG_TC) == 0);
		return 1;
	}
/* send a String though UART1 */	
int USART1_putString(uint8_t * data)
	{
		while(*data != 0)
			{
				USART1_putChar(*data);
				data++;
			}
			return 1;
	}	
/* Receive data */
uint8_t USART1_getChar(void)
	{
		while (USART_GetFlagStatus(USART1,USART_FLAG_RXNE) == RESET);
		return ((uint8_t)(USART1->RDR & 0xFF));
	}
/* Delay (ms) Fuction */
void Delay(__IO uint32_t nTime)
{
	TimingDelay = nTime;
	while(TimingDelay != 0);
}
/* Decrement */
void TimingDelay_Decrement(void)
{
	if (TimingDelay != 0x00)
	TimingDelay --;
}
void SDADC1_configure(void)
{
	/* Init Structure */
	GPIO_InitTypeDef GPIOE_InitStruct;
	SDADC_InitTypeDef SDADC1_InitStruct;
	SDADC_AINStructTypeDef SDADC1_AIN_InitStruct;
	/* Enable clock */
	RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOE,ENABLE);
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_SDADC1,ENABLE);
	RCC_SDADCCLKConfig(RCC_SDADCCLK_SYSCLK_Div12);
	SDADC_InitModeCmd(SDADC1,ENABLE);
	/* PE12 & PE13 are configured in Analog mode */
	GPIOE_InitStruct.GPIO_Pin  = GPIO_Pin_12|GPIO_Pin_13;
	GPIOE_InitStruct.GPIO_Mode = GPIO_Mode_AN;
	GPIOE_InitStruct.GPIO_PuPd = GPIO_PuPd_NOPULL;
	GPIO_Init(GPIOE,&GPIOE_InitStruct);
	/* SDADC Init structure definition */
	SDADC1_InitStruct.SDADC_Channel = SDADC_Channel_0;
	SDADC1_InitStruct.SDADC_ContinuousConvMode = ENABLE;
	SDADC1_InitStruct.SDADC_FastConversionMode = ENABLE;
	/* Configure Analog Input */
	SDADC1_AIN_InitStruct.SDADC_InputMode = SDADC_InputMode_Diff;
	SDADC1_AIN_InitStruct.SDADC_Gain = SDADC_Gain_1;
	SDADC1_AIN_InitStruct.SDADC_CommonMode = SDADC_CommonMode_VDDA_2;
	SDADC1_AIN_InitStruct.SDADC_Offset = 0x000007FF;
	SDADC_VREFSelect(SDADC_VREF_VDDA);
	SDADC_Init(SDADC1,&SDADC1_InitStruct);
	SDADC_AINInit(SDADC1,SDADC_Conf_0,&SDADC1_AIN_InitStruct);
	SDADC_ChannelConfig(SDADC1,SDADC_Channel_0,SDADC_Conf_0);
	SDADC_CalibrationSequenceConfig(SDADC1,SDADC_CalibrationSequence_1);
	SDADC_InitModeCmd(SDADC1,DISABLE);
	SDADC_StartCalibration(SDADC1);
	SDADC_Cmd(SDADC1,ENABLE);
}
void SPI_configure(void)
{
	RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA|RCC_AHBPeriph_GPIOB, ENABLE);
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1,ENABLE);
	GPIO_InitTypeDef GPIO_SPI1_InitStruct;
	/**SPI1 GPIO Configuration    
    PA5     ------> SPI1_SCK
    PA6     ------> SPI1_MISO
    PB0     ------> SPI1_MOSI 
   */
	GPIO_SPI1_InitStruct.GPIO_Pin = GPIO_Pin_5|GPIO_Pin_6;
	GPIO_SPI1_InitStruct.GPIO_Mode = GPIO_Mode_AF;
	GPIO_SPI1_InitStruct.GPIO_PuPd = GPIO_PuPd_NOPULL;
	GPIO_SPI1_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init(GPIOA, &GPIO_SPI1_InitStruct);
	
	GPIO_SPI1_InitStruct.GPIO_Pin = GPIO_Pin_0;
	GPIO_SPI1_InitStruct.GPIO_Mode = GPIO_Mode_AF;
	GPIO_SPI1_InitStruct.GPIO_PuPd = GPIO_PuPd_NOPULL;
	GPIO_SPI1_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init(GPIOB, &GPIO_SPI1_InitStruct);
	GPIO_PinAFConfig(GPIOA,GPIO_PinSource5,GPIO_AF_5);
	GPIO_PinAFConfig(GPIOA,GPIO_PinSource6,GPIO_AF_5);
	GPIO_PinAFConfig(GPIOB,GPIO_PinSource0,GPIO_AF_5);
	/*SPI1 configuration*/
	SPI_InitTypeDef SPI1_InitStruct;
	SPI1_InitStruct.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
	SPI1_InitStruct.SPI_CPHA = SPI_CPHA_1Edge;
	SPI1_InitStruct.SPI_CPOL = SPI_CPOL_Low;
	SPI1_InitStruct.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_4; // 72MHz/4 => 18MBits/s
	SPI1_InitStruct.SPI_DataSize = SPI_DataSize_8b;
	SPI1_InitStruct.SPI_FirstBit = SPI_FirstBit_MSB;
	SPI1_InitStruct.SPI_Mode = SPI_Mode_Master;
	SPI1_InitStruct.SPI_NSS = SPI_NSS_Soft;
	SPI_Init(SPI1,&SPI1_InitStruct);
	SPI_Cmd(SPI1,ENABLE);
}
 

0 REPLIES 0