Professional Documents
Culture Documents
khi ADC
cho cc bn.
mnh ang tm hiu phn ADC ca stm32f103rd.
c 1 knh no y th ok ri, nhng m c nhiu knh th sao n cho kt qu sai v d
l 10kenh ca adc1 , bi vit ca mnh y, khi m phng n chy lon ln ht , khi c
kt qu th khng bit l knh no ang chuyn i na , mong cc cao th gip vi.
/**
**************************************************
****************************
* @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>© 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 4096
/* 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 Interface,
initialize the PLL and update the SystemFrequency variable. */
//SystemInit(); // no need because SystemInit() is already called 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;
/* Configure the system clocks */
RCC_Configuration();
/*
* configure UART pins
*/
/* Enable UART clock, if using USART2 or USART3 ... we must use
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USARTx, ENABLE) */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
/* Configure PA9 for USART Tx as alternate function push-pull */
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_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_HardwareFlowControl_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.02 (ADC Channel2) as analog input -------------------------*/
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
GPIO_Init(GPIOA, &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 = DISABLE;
ADC_InitStructure.ADC_ContinuousConvMode = ENABLE;
ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None;
ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
ADC_InitStructure.ADC_NbrOfChannel = 10;
ADC_Init(ADC1, &ADC_InitStructure);
ADC_Channel_0,
ADC_Channel_1,
ADC_Channel_2,
ADC_Channel_3,
ADC_Channel_4,
ADC_Channel_5,
ADC_Channel_6,
ADC_Channel_7,
ADC_Channel_8,
ADC_Channel_9,
1, ADC_SampleTime_55Cycles5);
2, ADC_SampleTime_55Cycles5);
3, ADC_SampleTime_55Cycles5);
4, ADC_SampleTime_55Cycles5);
5, ADC_SampleTime_55Cycles5);
6, ADC_SampleTime_55Cycles5);
7, ADC_SampleTime_55Cycles5);
8, ADC_SampleTime_55Cycles5);
9, ADC_SampleTime_55Cycles5);
10, ADC_SampleTime_55Cycles5);
/* 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)
{
/* get adc raw value */
adc_raw_val = ADC_GetConversionValue(ADC1);// gia tri adc doc ve l kenh nao????
/* calculate voltage value */
potentionmeter_val = adc_raw_val*ADC_VREF/ADC_12BIT_FACTOR;
/* Output a message on Hyperterminal using printf function */
printf("\n\rPoten_Volt = %.2fV\n\r", (f32)potentionmeter_val/100.0);
/* Insert delay */
//Delay(5);
}
}
#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
*/
2.
dien44
Senior Member
Ngy tham gia
Feb 2012
ang
Dong Nai; Ben Tre; Tp HCM - Viet Nam
Bi vit
137
Thanks
105
Thanked 165 Times in 74 Posts
Gi bi bavinhlhu
cho cc bn.
mnh ang tm hiu phn ADC ca stm32f103rd.
c 1 knh no y th ok ri, nhng m c nhiu knh th sao n cho kt qu sai v d l
10kenh ca adc1 , bi vit ca mnh y, khi m phng n chy lon ln ht , khi c kt
qu th khng bit l knh no ang chuyn i na , mong cc cao th gip vi.
...................................
/******************* (C) COPYRIGHT 2010 ARMVietNam *****END OF FILE****/
Cho bn, Vn ny cng l vn m dien44 gp lc trc.
Trong code pha trn ca bn. bn cu hnh ti 10 knh chuyn i ADC, nhng bn
cha cu hnh pin tng ng cho n.
Nu s dng ch bnh thng, c c nhiu knh th dien44 cng khng bit,
lc trc lm th ch c c ci cui cng, v th bn nn dng ch DMA cho n
t ng lu vo b nh. dien44 gi nh vy, bn tm hiu th xem sao. cu hnh DMA
th tng t nh sau:
Code:
DMA_InitTypeDef DMA_InitStructure;
DMA_DeInit(DMA1_Channel1);
DMA_InitStructure.DMA_PeripheralBaseAddr = ADC1_DR_Address;// l a ch ca
thanh ghi cha gi tr chuyn i ADC, bn phi nh ngha gi tr ny, xem thm
datasheet nh
DMA_InitStructure.DMA_MemoryBaseAddr = (u32)&AD_Value;// bin cha gi tr
chuyn i, trng hp bn s dng nhiu knh th bin ny l mng
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC;
DMA_InitStructure.DMA_BufferSize = 2; // s buffer, tng ng vi s knh chuyn
i
DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord;
DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord;
DMA_InitStructure.DMA_Mode = DMA_Mode_Circular;
DMA_InitStructure.DMA_Priority = DMA_Priority_High;
DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
DMA_Init(DMA1_Channel1, &DMA_InitStructure);
DMA_Cmd(DMA1_Channel1, ENABLE);
....(^:^).........Sharing is Getting..........(^:^)...
My Page: OViet.VN
Tr li km Trch dn
3.
The Following User Says Thank You to dien44 For This Useful Post:
32bit (08-26-2012)
4.
tanphuc
Senior Member
Ngy tham gia
May 2011
Bi vit
164
Thanks
108
Thanked 131 Times in 53 Posts
The Following User Says Thank You to tanphuc For This Useful Post:
5.
32bit (08-26-2012)
6.
bavinhlhu
Member
Ngy tham gia
Jul 2010
Bi vit
99
Thanks
1
Thanked 20 Times in 15 Posts
Gi bi tanphuc
7.
bavinhlhu
Member
Ngy tham gia
Jul 2010
Bi vit
99
Thanks
1
Thanked 20 Times in 15 Posts
Gi bi dien44
8.
tieulong12a3_hl
Junior Member
Ngy tham gia
Feb 2012
ang
Tm tr ti H Ni
Bi vit
21
Thanks
16
Thanked 3 Times in 3 Posts
Cc bc , code ca em ch thm s chn v config thm knh ADC v chy. Tuy nhin
vn li c v tr tru l cc knh ADC c chuyn i th qu nhanh trong khi gi tr
chuyn i li c cp nht qu chm. dn n hin th kt qu sai.Bc no gip e vi!!
Trong khi ch i A Phng add vo group chia s ti liu e up tm file main.c ln y
vy(c project vt qu dung lng cho php
)
nh nh km
File nh km
Tr li km Trch dn
9.
dien44
Senior Member
Ngy tham gia
Feb 2012
ang
Dong Nai; Ben Tre; Tp HCM - Viet Nam
Bi vit
137
Thanks
105
Thanked 165 Times in 74 Posts
Gi bi tieulong12a3_hl
Cc bc , code ca em ch thm s chn v config thm knh ADC v chy. Tuy nhin
vn li c v tr tru l cc knh ADC c chuyn i th qu nhanh trong khi gi tr
chuyn i li c cp nht qu chm. dn n hin th kt qu sai.Bc no gip e vi!!
Trong khi ch i A Phng add vo group chia s ti liu e up tm file main.c ln y
vy(c project vt qu dung lng cho php
)
Cho bn!!.
Lc trc, dien44 cng gp trng hp nh bn. Tuy nhin, c l code ca bn a ln
y cha y nn mnh cng k bit phi t vn th no. Nhng vn y ca bn
l p ng thi gian thc khng kp ( bn thay i u vo th u ra cng thay i nhng
chm hn ), dien44 mc li ny v tm 3 ngy mi ra. Ch 1 cu lnh duy nht l
"Delay(xx)" t ng ch l c. Bi v cc gi tr u vo c chuyn i lin tc, cho
nn c rt nhiu gi tr hm printf in ra mn hnh, v chnh l do ny lm cho chng
trnh ca bn khng p ng kp. nh vy th ta ch cn in t qung, th th u ra s
p ng kp ngay. Thn cho!!
....(^:^).........Sharing is Getting..........(^:^)...
My Page: OViet.VN
Tr li km Trch dn
10.
The Following User Says Thank You to dien44 For This Useful Post:
tieulong12a3_hl (10-22-2012)
11.
tieulong12a3_hl
Junior Member
Ngy tham gia
Feb 2012
ang
Tm tr ti H Ni
Bi vit
21
Thanks
16
Thanked 3 Times in 3 Posts
12.
tieulong12a3_hl
Junior Member
Ngy tham gia
Feb 2012
ang
Tm tr ti H Ni
Bi vit
21
Thanks
16
Thanked 3 Times in 3 Posts