這是之前學32的時候寫的,那時候學了32以後感受32真是太強大了,比51強的沒影。關於dma網上有許多的資料,關於dma採集ad網上也有不少。親們搜搜,這裏只貼代碼了,其實我也想詳詳細細地敘述一番,可是本身自己打字就慢,還有好多事情要作!代碼是我親自都在板子上測試過的,,固然粘貼/複製過去可能也不會盡如人意,知識這東西老是有許多道不清說不明的東西在裏頭,每每老是不經一番徹骨寒,哪得梅花撲鼻香。推薦一本書吧!這是野火出的。緩存
這本書自從在圖書館借來就歷來沒有再放回去,老是在續借。像是在打廣告了函數
#include <stm32f10x.h> #include "ADC1.h" #include "DMA1.h" #include "USART1.h" #include "time.h" #include "stdio.h" extern uint32_t SendBuff; float ADC_Received; uint32_t ADC_Received1; uint8_t ADC_Received2[11]; //printf函數從新定向,方便在程序中使用 int fputc(int ch, FILE *f) { USART_SendData(USART1, (unsigned char) ch); while (!(USART1->SR & USART_FLAG_TXE)); return (ch); } void usart_putchar(uint8_t ch) { USART_SendData(USART1,ch); while(USART_GetFlagStatus(USART1,USART_FLAG_TXE) == RESET); } int main() { ADC1_Config(); DMA_Config(); USART1_Config(); while(1) { // ADC_Received = (float)ADC_GetConversionValue(ADC1)*3.3/4069; // ADC_Received1 = ADC_Received * 1000000000; ADC_Received = (float)SendBuff*3.3/4069; ADC_Received1 = ADC_Received * 1000000000; ADC_Received2[0]=(ADC_Received1/1000000000 + 0x30); //usart_putchar(0x2e); ADC_Received2[1]=(ADC_Received1%1000000000/100000000 + 0x30); ADC_Received2[2]=(ADC_Received1%1000000000%100000000/10000000 + 0x30); ADC_Received2[3]=(ADC_Received1%1000000000%100000000%10000000/1000000 + 0x30); ADC_Received2[4]=(ADC_Received1%1000000000%100000000%10000000%1000000/100000 + 0x30); ADC_Received2[5]=(ADC_Received1%1000000000%100000000%10000000%1000000%100000/10000 + 0x30); ADC_Received2[6]=(ADC_Received1%1000000000%100000000%10000000%1000000%100000%10000/1000 + 0x30); ADC_Received2[7]=(ADC_Received1%1000000000%100000000%10000000%1000000%100000%10000%1000/100 + 0x30); ADC_Received2[8]=(ADC_Received1%1000000000%100000000%10000000%1000000%100000%10000%1000%100/10 + 0x30); ADC_Received2[9]=(ADC_Received1%10 + 0x30); ADC_Received2[10]=0x0d; USART_DMACmd(USART1, USART_DMAReq_Tx, ENABLE); // delay_ms(1000); // USART_DMACmd(USART1, USART_DMAReq_Tx, DISABLE); // delay_ms(1000); // ADC_Received = (float) SendBuff/4069*3.3; // ADC_Received = (u16)ADC1 -> DR; // ADC_Received = (float)ADC_Received/4069*3.3; // printf("\r\n v = %f V \r\n",ADC_Received); // while(!ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC )); // usart_putchar('\r'); // usart_putchar('\n'); // usart_putchar(0x0d); // usart_putchar(0x0a); // printf("\r"); // printf("\n"); // printf("\r\n V = %fv\r\n",ADC_Received); } } #include "ADC1.h" void ADC1_Config(void) { ADC_InitTypeDef ADC_InitStructure; RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE); ADC1_Gpio_Config(); ADC_DeInit(ADC1); //復位 ADC1,將外設 ADC1 的所有寄存器重設爲缺省值 // ADC1 配置 ADC_InitStructure.ADC_Mode = ADC_Mode_Independent; //ADC1工做在獨立模式 ADC_InitStructure.ADC_ScanConvMode = ENABLE;//使能掃描 ADC_InitStructure.ADC_ContinuousConvMode = ENABLE;;//ADC轉換工做在連續模式 ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None;//由軟件控制轉換 ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;//轉換數據右對齊 ADC_InitStructure.ADC_NbrOfChannel = 1;//轉換通道爲通道1 ADC_Init(ADC1, &ADC_InitStructure); //初始化ADC //ADC1選擇信道0,順續等級1,採樣時間239.5個週期 ADC_RegularChannelConfig(ADC1, ADC_Channel_0, 1, ADC_SampleTime_28Cycles5); //打開ADC1 ADC_Cmd(ADC1, ENABLE); //重置ADC1校準寄存器 ADC_ResetCalibration(ADC1); //等待ADC1校準重置完成 while(ADC_GetResetCalibrationStatus(ADC1)); //開始ADC1校準 ADC_StartCalibration(ADC1); //等待ADC1校準完成 while(ADC_GetCalibrationStatus(ADC1)); //使能ADC1軟件開始轉換 ADC_SoftwareStartConvCmd(ADC1, ENABLE); //配置ADC時鐘=PCLK2 1/6 12MHz RCC_ADCCLKConfig(RCC_PCLK2_Div6); //使能ADC1模塊DMA ADC_DMACmd(ADC1, ENABLE); } static void ADC1_Gpio_Config(void) { GPIO_InitTypeDef GPIO_InitStructure; RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN; GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0; GPIO_Init(GPIOA , &GPIO_InitStructure); } #include "DMA1.h" /* 其餘函數裏 USART_DMACmd(USART1, USART_DMAReq_Tx, ENABLE); */ uint32_t SendBuff; extern float ADC_Received; extern uint8_t ADC_Received2[11]; //描述 :DMA 串口的初始化配置 void DMA_Config(void) { //初始化結構體 DMA_InitTypeDef DMA_InitStructure; //開啓DMA時鐘 RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE); //配置DMA中斷 NVIC_Config(); //設置DMA源:地址 DMA_InitStructure.DMA_PeripheralBaseAddr = (u32)&ADC1 -> DR; //*內存地址(要傳輸的變量的指針) DMA_InitStructure.DMA_MemoryBaseAddr = (u32)&SendBuff; //外設做爲數據傳輸的來源 DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC; //指定 DMA 通道的 DMA 緩存的大小,單位爲數據單位。 DMA_InitStructure.DMA_BufferSize = 1; //*外設地址不增 DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable; //*內存地址不增 DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Disable; //*外設數據單位數據寬度爲 16 位 DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord; //*內存數據單位數據寬度爲 16 位 DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord; //*DMA模式:一次傳輸/循環 // DMA_Mode_Circular 工做在循環緩存模式 // DMA_Mode_Normal 工做在正常緩存模式 // DMA_InitStructure.DMA_Mode = DMA_Mode_Normal ; DMA_InitStructure.DMA_Mode = DMA_Mode_Circular; //*優先級:高 DMA_InitStructure.DMA_Priority = DMA_Priority_High; //*禁止內存到內存的傳輸 DMA_InitStructure.DMA_M2M = DMA_M2M_Disable; //*配置DMA1的1通道 DMA_Init(DMA1_Channel1, &DMA_InitStructure); //使能DMA DMA_Cmd (DMA1_Channel1,ENABLE); //配置DMA發送完成後產生中斷 // DMA_ITConfig(DMA1_Channel1,DMA_IT_TC,ENABLE); //****************************************************/// //****************************************************/// //****************************************************/// //設置DMA源:地址 DMA_InitStructure.DMA_PeripheralBaseAddr = (u32)&USART1 -> DR; //*內存地址(要傳輸的變量的指針) DMA_InitStructure.DMA_MemoryBaseAddr = (u32)ADC_Received2; //外設做爲數據傳輸的目的地 DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST; //指定 DMA 通道的 DMA 緩存的大小,單位爲數據單位。 DMA_InitStructure.DMA_BufferSize = 11; //*外設地址不增 DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable; //*內存地址不增 DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable; //*外設數據單位數據寬度爲 16 位 DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte; //*內存數據單位數據寬度爲 16 位 DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte; //*DMA模式:一次傳輸/循環 // DMA_Mode_Circular 工做在循環緩存模式 // DMA_Mode_Normal 工做在正常緩存模式 // DMA_InitStructure.DMA_Mode = DMA_Mode_Normal ; DMA_InitStructure.DMA_Mode = DMA_Mode_Circular; //*優先級:中 DMA_InitStructure.DMA_Priority = DMA_Priority_Medium; //*禁止內存到內存的傳輸 DMA_InitStructure.DMA_M2M = DMA_M2M_Disable; //*配置DMA1的4通道 DMA_Init(DMA1_Channel4, &DMA_InitStructure); //使能DMA DMA_Cmd (DMA1_Channel4,ENABLE); //配置DMA發送完成後產生中斷 // DMA_ITConfig(DMA1_Channel1,DMA_IT_TC,ENABLE); } #include "time.h" /**************** 延時多少個 1/72 us ****************/ void delay_1_72us(uint32_t time) { SysTick -> LOAD = (u32) time; //定時器賦初值 SysTick -> CTRL = 0x00000005; //選擇72MHz 並 打開定時器 while(!(SysTick -> CTRL & 0x00010000));//等待計數到零 SysTick -> CTRL = 0x00000004;//關閉定時器 } void delay_ms(u32 time) { while(time -- ) { delay_1_72us(72000); } } #include "USART1.h" //描述 :USART1 GPIO 配置,工做模式配置。115200 8-N-1 void USART1_Config(void) { GPIO_InitTypeDef GPIO_InitStructure; USART_InitTypeDef USART_InitStructure; // 打開 USART1 的時鐘 RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 | RCC_APB2Periph_GPIOA, ENABLE); // USART1 Tx (PA_9) 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); // USART1 Tx (PA_10) GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;//浮空輸入 GPIO_Init(GPIOA, &GPIO_InitStructure); /* USART1 mode config */ 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_Init(USART1, &USART_InitStructure); USART_Cmd(USART1, ENABLE); }