You are on page 1of 11

1.

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>&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 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);

// cau hinh cho 10 kenh adc1


ADC_RegularChannelConfig(ADC1,
ADC_RegularChannelConfig(ADC1,
ADC_RegularChannelConfig(ADC1,
ADC_RegularChannelConfig(ADC1,
ADC_RegularChannelConfig(ADC1,
ADC_RegularChannelConfig(ADC1,
ADC_RegularChannelConfig(ADC1,
ADC_RegularChannelConfig(ADC1,
ADC_RegularChannelConfig(ADC1,
ADC_RegularChannelConfig(ADC1,

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
*/

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****/
Tr li km Trch dn

2.

08-26-2012 11:05 AM#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.

08-26-2012 05:07 PM#3

tanphuc

Senior Member
Ngy tham gia
May 2011
Bi vit
164
Thanks
108
Thanked 131 Times in 53 Posts

Th 1 l bn cha cu hnh my chn nhn tn hiu analog tng ng vi cc knh cho


ng, ch mi cu hnh cho chn GPIOA.2.
Th 2 l ci phn cu hnh chuyn i ADC bn lm vy l ch c c knh cui cng l
knh 10 thi. Bn c th lm theo cch ny, n gin hn. (ci ny mnh cng google ra
thi v p dng ok)
Phn khai bo ADC ban u tng t, ch khc ch ADC_ContinuousConvMode =
DISABLE
Code:
/* ADC1 Configuration ------------------------------------------------------*/
/* ADC1 and ADC2 operate independently */
ADC_InitStructure.ADC_Mode = ADC_Mode_Independent;
/* Disable the scan conversion so we do one at a time */
ADC_InitStructure.ADC_ScanConvMode = DISABLE;
/* Don't do contimuous conversions - do them on demand */
ADC_InitStructure.ADC_ContinuousConvMode = DISABLE;
/* Start conversin by software, not an external trigger */
ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None;
/* Conversions are 12 bit - put them in the lower 12 bits of the result */
ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
/* Say how many channels would be used by the sequencer */
ADC_InitStructure.ADC_NbrOfChannel = 1;

Sau bn to 1 hm ly v gi tr ADC ca knh n


Code:

u16 readADC1(u8 channel)


{
ADC_RegularChannelConfig(ADC1, channel, 1, ADC_SampleTime_1Cycles5);
// Start the conversion
ADC_SoftwareStartConvCmd(ADC1, ENABLE);
// Wait until conversion completion
while(ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC) == RESET);
// Get the conversion value
return ADC_GetConversionValue(ADC1);
}

Sau trong chng trnh chnh ch cn gi hm readADC1(knh cn chuyn).


Bo m chy ok.
Ngun code: http://www.micromouseonline.com/2009...#axzz24e2EqEy8
Tr li km Trch dn

The Following User Says Thank You to tanphuc For This Useful Post:

5.

32bit (08-26-2012)
6.

08-26-2012 07:48 PM#4

bavinhlhu

Member
Ngy tham gia
Jul 2010
Bi vit
99
Thanks
1
Thanked 20 Times in 15 Posts

Gi bi tanphuc

Th 1 l bn cha cu hnh my chn nhn tn hiu analog tng ng vi cc knh cho


ng, ch mi cu hnh cho chn GPIOA.2.
Th 2 l ci phn cu hnh chuyn i ADC bn lm vy l ch c c knh cui cng l
knh 10 thi. Bn c th lm theo cch ny, n gin hn. (ci ny mnh cng google ra
thi v p dng ok)
Phn khai bo ADC ban u tng t, ch khc ch ADC_ContinuousConvMode =
DISABLE
Code:
/* ADC1 Configuration ------------------------------------------------------*/
/* ADC1 and ADC2 operate independently */
ADC_InitStructure.ADC_Mode = ADC_Mode_Independent;
/* Disable the scan conversion so we do one at a time */
ADC_InitStructure.ADC_ScanConvMode = DISABLE;
/* Don't do contimuous conversions - do them on demand */
ADC_InitStructure.ADC_ContinuousConvMode = DISABLE;
/* Start conversin by software, not an external trigger */
ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None;
/* Conversions are 12 bit - put them in the lower 12 bits of the result */
ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
/* Say how many channels would be used by the sequencer */
ADC_InitStructure.ADC_NbrOfChannel = 1;

Sau bn to 1 hm ly v gi tr ADC ca knh n


Code:
u16 readADC1(u8 channel)
{
ADC_RegularChannelConfig(ADC1, channel, 1, ADC_SampleTime_1Cycles5);
// Start the conversion
ADC_SoftwareStartConvCmd(ADC1, ENABLE);
// Wait until conversion completion

while(ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC) == RESET);


// Get the conversion value
return ADC_GetConversionValue(ADC1);
}

Sau trong chng trnh chnh ch cn gi hm readADC1(knh cn chuyn).


Bo m chy ok.
Ngun code: http://www.micromouseonline.com/2009...#axzz24e2EqEy8
cm n bn nhiu lm.
mnh th test li , kiu ny c v d lm hn nh.
Tr li km Trch dn

7.

08-26-2012 07:51 PM#5

bavinhlhu

Member
Ngy tham gia
Jul 2010
Bi vit
99
Thanks
1
Thanked 20 Times in 15 Posts

Gi bi dien44

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);

cm n bn "dien44" mnh th nh.


cng nhn DMA mnh qu ha.
Tr li km Trch dn

8.

10-19-2012 07:48 PM#6

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

ADC_Multi.jpg (99.2 KB, 4 xem)

Copy of main.txt (9.7 KB, 6 xem)

File nh km
Tr li km Trch dn

9.

10-21-2012 08:07 AM#7

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.

10-22-2012 08:44 PM#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

ca bc dien44 c phi l delay ngay sau mi ln convert???


Tr li km Trch dn

12.

10-22-2012 08:57 PM#9

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

project ca em y (c chnh sa nhng vn ko n


thua) http://code.google.com/p/armvn-repo/...w.rar&can=2&q=
Tr li km Trch dn

You might also like