标题:
STM32F103 AD转换程序(注释很详细)
[打印本页]
作者:
SQ5000
时间:
2018-7-6 14:00
标题:
STM32F103 AD转换程序(注释很详细)
STM32F103 AD转换程序
/* Includes ------------------------------------------------------------------*/
#include "stm32f10x.h"
#include "stm32_eval.h"
#include "stm32f10x_conf.h"
#include <stdio.h>
//u* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
#define DR_ADDRESS ((uint32_t)0x4001244C) //ADC1 DR寄存器基地址
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
USART_InitTypeDef USART_InitStructure; //串口初始化结构体声明
ADC_InitTypeDef ADC_InitStructure; //ADC初始化结构体声明
DMA_InitTypeDef DMA_InitStructure; //DMA初始化结构体声明
__IO uint16_t ADCConvertedValue; // ADC为12位模数转换器,只有ADCConvertedValue的低12位有效
/* Private function prototypes -----------------------------------------------*/
void ADC_GPIO_Configuration(void);
#ifdef __GNUC__
/* With GCC/RAISONANCE, small printf (option LD Linker->Libraries->Small printf
set to 'Yes') calls __io_putchar() */
#define PUTCHAR_PROTOTYPE int __io_putchar(int ch)
#else
#define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)
#endif /* __GNUC__ */
/* Private functions ---------------------------------------------------------*/
static void Delay_ARMJISHU(__IO uint32_t nCount)
{
for (; nCount != 0; nCount--);
}
//==================================================================
//变量定义
double Str_V,End_V,In_V,Pwm;
u16 ADCConvertedValueLocal;
int main(void)
{
//USAT\RT1===============================================================================
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;
STM_EVAL_COMInit(COM1, &USART_InitStructure);
/* Output a message on Hyperterminal using printf function */
printf("\n\r\n\r\n\r\n\r");
printf("\n\r **************华蓝盾公司************************");
printf("\n\r");
printf("\n\r ==============直流电机驱动======================= \n\r");
printf("\n\r");
/* Enable DMA1 clock */
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE); //使能DMA时钟
/* DMA1 channel1 configuration ----------------------------------------------*/
DMA_DeInit(DMA1_Channel1); //开启DMA1的第一通道
DMA_InitStructure.DMA_PeripheralBaseAddr = DR_ADDRESS; //DMA对应的外设基地址
DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)&ADCConvertedValue; //内存存储基地址
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC; //DMA的转换模式为SRC模式,由外设搬移到内存
DMA_InitStructure.DMA_BufferSize = 1; //DMA缓存大小,1个
DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable; //接收一次数据后,设备地址禁止后移
DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Disable; //关闭接收一次数据后,目标内存地址后移
DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord; //定义外设数据宽度为16位
DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord; //DMA搬移数据尺寸,HalfWord就是为16位
DMA_InitStructure.DMA_Mode = DMA_Mode_Circular; //转换模式,循环缓存模式。
DMA_InitStructure.DMA_Priority = DMA_Priority_High; //DMA优先级高
DMA_InitStructure.DMA_M2M = DMA_M2M_Disable; //M2M模式禁用
DMA_Init(DMA1_Channel1, &DMA_InitStructure);
/* Enable DMA1 channel1 */
DMA_Cmd(DMA1_Channel1, ENABLE);
/* Enable ADC1 and GPIOC clock */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1 | RCC_APB2Periph_GPIOC, ENABLE); //使能ADC和GPIOC时钟
/* ADC1 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外部开关,关闭状态
ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right; //对齐方式,ADC为12位中,右对齐方式
ADC_InitStructure.ADC_NbrOfChannel = 1; //开启通道数,1个
ADC_Init(ADC1, &ADC_InitStructure);
/* ADC1 regular channel13 configuration */
ADC_RegularChannelConfig(ADC1, ADC_Channel_13, 1, ADC_SampleTime_55Cycles5); ///13
//ADC通道组, 第13个通道 采样顺序1,转换时间
/* Enable ADC1 DMA */
ADC_DMACmd(ADC1, ENABLE); //ADC命令,使能
/* Enable ADC1 */
ADC_Cmd(ADC1, ENABLE); //开启ADC1
/* 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); //连续转换开始,ADC通过DMA方式不断的更新RAM区。
//=============================================================================================================
Str_V =0.5;
End_V =2.5;
while (1)
{
ADCConvertedValueLocal = ADCConvertedValue;
In_V = 3.3* ADCConvertedValueLocal/4096 ; //计算等效电平
printf("\r\n 电压值1:%1.3f",In_V );
if (In_V <= 0.5)
{
Pwm =0;
printf("\r\n 当前脉宽= %0.3f" ,Pwm );
}
if( In_V>=2.5 )
{
Pwm =0.9;
printf("\r\n 当前脉宽= %0.3f" ,Pwm );
}
if( (In_V <2.5 ) && (In_V >0.5))
{
Pwm = 0.9*(In_V-0.5)/2 ;
printf("\r\n 当前脉宽= %0.3f" ,Pwm );
}
Delay_ARMJISHU(8000000);
}
}
/**
* @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(EVAL_COM1, (uint8_t) ch);
/* Loop until the end of transmission */
while (USART_GetFlagStatus(EVAL_COM1, USART_FLAG_TC) == RESET)
{}
return ch;
}
#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
/**
* @brief Configures the different GPIO ports.
* @param None
* @retval None
*/
void ADC_GPIO_Configuration(void) //ADC配置函数
{
GPIO_InitTypeDef GPIO_InitStructure;
/* Configure PC.00 (ADC Channel10) as analog input -------------------------*/
//PC0 作为模拟通道10输入引脚
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0; //管脚1
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;//输入模式
GPIO_Init(GPIOC, &GPIO_InitStructure); //GPIO组
}
/**
* @}
*/
/**
* @}
*/
/******************* (C) COPYRIGHT 2010 STMicroelectronics *****END OF FILE****/
复制代码
AD转换.rar
2018-7-7 01:34 上传
点击文件名下载附件
下载积分: 黑币 -5
252.64 KB, 下载次数: 99, 下载积分: 黑币 -5
作者:
独角天赐
时间:
2018-12-3 21:44
太复杂了,ad不是一个很麻烦的东西吧,这个程序感觉对菜鸟一点都不友好
作者:
laigs218
时间:
2018-12-5 11:49
感谢楼主的分享。
作者:
YQ_S
时间:
2019-1-16 20:20
总算是看完了,但是得消化一下
作者:
西点钟灵毓秀
时间:
2019-1-17 08:40
只可惜时单路的ADC
欢迎光临 (http://www.51hei.com/bbs/)
Powered by Discuz! X3.1