Вы находитесь на странице: 1из 6

/**

******************************************************************************
* @file
ADC/Potentiometer/main.c
* @author
ARMVN Application Team
* @version
V1.0.0
* @date
14/03/2010
* @brief
Main program body.
******************************************************************************
* @copy
*
* THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
* WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
* TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY
* DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
* FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
* CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
*
* <h2><center>&copy; COPYRIGHT 2009 ARMVietNam</center></h2>
*/
/*
********************************************************************************
*****************************************************
*
INCLUDED FILES
*
********************************************************************************
*****************************************************
*/
#include <stdio.h>
#include "main.h"
/** @addtogroup STM32F10x_GEM3M_Examples
* @{
*/
/** @addtogroup ADC_Potentiometer
* @{
*/
/*
********************************************************************************
*****************************************************
*
PRIVATE DEFINE
*
********************************************************************************
*****************************************************
*/
/* constant for adc resolution is 12 bit = 4096 */
#define ADC_12BIT_FACTOR
4095
/* constant for adc threshold value 3.3V */
#define ADC_VREF
330
/*
********************************************************************************
*****************************************************

*
DATA TYPE DEFINE
*
********************************************************************************
*****************************************************
*/
#ifndef f32
#define f32
float
#endif
/*
********************************************************************************
*****************************************************
*
PRIVATE VARIABLES
*
********************************************************************************
*****************************************************
*/
GPIO_InitTypeDef GPIO_InitStructure;
ADC_InitTypeDef ADC_InitStructure;
USART_InitTypeDef USART_InitStructure;
/*
********************************************************************************
*****************************************************
*
LOCAL FUNCTIONS
*
********************************************************************************
*****************************************************
*/
/**
* @brief
Configures the different system clocks.
* @param
None
* @retval
None
*/
void RCC_Configuration(void)
{
/* Setup the microcontroller system. Initialize the Embedded Flash Inter
face,
initialize the PLL and update the SystemFrequency variable. */
//SystemInit();
// no need because SystemInit() is already calle
d in start up code
/* Enable GPIOA clocks */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
}
/**
* @brief
Inserts a delay time with resolution is 10 milisecond..
* @param
nCount: specifies the delay time length.
* @retval
None
*/
void Delay(__IO uint32_t num)
{
__IO uint32_t index = 0;
/* default system clock is 72MHz */
for(index = (720000 * num); index != 0; index--)

{
}
}
/**
* @brief Retargets the C library printf function to the USART.
* @param None
* @retval None
*/
PUTCHAR_PROTOTYPE
{
/* Place your implementation of fputc here */
/* e.g. write a character to the USART */
USART_SendData(USART1, (uint8_t) ch);
/* Loop until the end of transmission */
while (USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET)
{}
return ch;
}
/*
********************************************************************************
*****************************************************
*
GLOBAL FUNCTIONS
*
********************************************************************************
*****************************************************
*/
/**
* @brief
Main program.
* @param
None
* @retval
None
*/
int main(void)
{
u16 adc_raw_val = 0;
u16 potentionmeter_val = 0;
float x;
/* Configure the system clocks */
RCC_Configuration();
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOC, &GPIO_InitStructure);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_14;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOB, &GPIO_InitStructure);
/*
*
configure UART pins
*/
/* Enable UART clock, if using USART2 or USART3 ... we must use RCC_APB1
PeriphClockCmd(RCC_APB1Periph_USARTx, ENABLE) */

RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE);
/* Configure PA9 for USART Tx as alternate function push-pull */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/* Configure PA10 for USART Rx as input floating */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/* USARTx configured as follow:
- BaudRate = 115200 baud
- Word Length = 8 Bits
- One Stop Bit
- No parity
- Hardware flow control disabled (RTS and CTS signals)
- Receive and transmit enabled
*/
USART_InitStructure.USART_BaudRate = 115200;
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
USART_InitStructure.USART_StopBits = USART_StopBits_1;
USART_InitStructure.USART_Parity = USART_Parity_No;
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowContro
l_None;
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
/* USART configuration */
USART_Init(USART1, &USART_InitStructure);
/* Enable USART */
USART_Cmd(USART1, ENABLE);
/*
*
*/

configure ADC pins

/* Configure PA.01 (ADC Channel2) as analog input ------------------------*/


GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
GPIO_Init(GPIOB, &GPIO_InitStructure);
/* Configure clocks for ADC and GPIO PORT */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE);

/* ADCx configuration -----------------------------------------------------*/


ADC_InitStructure.ADC_Mode = ADC_Mode_Independent;
ADC_InitStructure.ADC_ScanConvMode = ENABLE;
ADC_InitStructure.ADC_ContinuousConvMode = ENABLE;
ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None;
ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
ADC_InitStructure.ADC_NbrOfChannel = 1;
ADC_Init(ADC1, &ADC_InitStructure);
/* ADC1 Regular Channel 1 Configuration */
ADC_RegularChannelConfig(ADC1, ADC_Channel_8, 1, ADC_SampleTime_55Cycles
5);
/* Enable ADC1 */
ADC_Cmd(ADC1, ENABLE);
/* Enable Vrefint channel enable temperature sensor for ADC module */
ADC_TempSensorVrefintCmd(ENABLE);
/* Enable ADC1 reset calibaration register */
ADC_ResetCalibration(ADC1);
/* Check the end of ADC1 reset calibration register */
while(ADC_GetResetCalibrationStatus(ADC1));
/* Start ADC1 calibaration */
ADC_StartCalibration(ADC1);
/* Check the end of ADC1 calibration */
while(ADC_GetCalibrationStatus(ADC1));
/* Start ADC1 Software Conversion */
ADC_SoftwareStartConvCmd(ADC1, ENABLE);
/* start main program */
while (1)
{
USART_ReceiveData(USART1);
USART_SendData(USART3, 'a');
GPIO_SetBits(GPIOB, GPIO_Pin_14);
Delay(10);
GPIO_ResetBits(GPIOB, GPIO_Pin_14);
Delay(10);
/* get adc raw value */
adc_raw_val = ADC_GetConversionValue(ADC1);
/* calculate voltage value */
potentionmeter_val = (adc_raw_val*ADC_VREF/ADC_12BIT_FACTOR);
x = (potentionmeter_val/100.0)*2;
//x = (potentionmeter_val/100.0)*2;
/* Output a message on Hyperterminal using printf function */
printf("\n\r%.2fV\n\r", x);
/* Insert delay */
Delay(1);
if(x < 3.8)
{
//GPIO_WriteBit(GPIOC, GPIO_Pin_5, (BitAction)1);
//GPIO_SetBits(GPIOC, GPIO_Pin_5);
GPIO_ResetBits(GPIOC, GPIO_Pin_5);

//GPIOC->ODR&=~(1<<5);
}
else if( x > 3.9)
{
//GPIOC->ODR=(1<<5);
//GPIO_WriteBit(GPIOC, GPIO_Pin_5, (BitAction )0);
//GPIO_ResetBits(GPIOC, GPIO_Pin_5);
GPIO_SetBits(GPIOC, GPIO_Pin_5);
//Delay(1);
}
}
}
#ifdef USE_FULL_ASSERT
/**
* @brief Reports the name of the source file and the source line number
* where the assert_param error has occurred.
* @param file: pointer to the source file name
* @param line: assert_param error line source number
* @retval None
*/
void assert_failed(uint8_t* file, uint32_t line)
{
/* User can add his own implementation to report the file name and line number
,
ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
/* Infinite loop */
while (1)
{
}
}
#endif
/**
* @}
*/
/**
* @}
*/
/******************* (C) COPYRIGHT 2010 ARMVietNam *****END OF FILE****/

Вам также может понравиться