|
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
評分
-
查看全部評分
|