AD7606 STM32F103 hal dma 代码
时间: 2023-08-06 21:11:03 浏览: 155
以下是AD7606 STM32F103 HAL DMA代码的示例:
```c
#include "stm32f1xx_hal.h"
#include "main.h"
/* AD7606相关定义 */
#define AD7606_CLK_GPIO_Port GPIOA
#define AD7606_CLK_Pin GPIO_PIN_5
#define AD7606_nCS_GPIO_Port GPIOA
#define AD7606_nCS_Pin GPIO_PIN_6
#define AD7606_BUSY_GPIO_Port GPIOA
#define AD7606_BUSY_Pin GPIO_PIN_7
#define AD7606_RDY_GPIO_Port GPIOB
#define AD7606_RDY_Pin GPIO_PIN_0
#define AD7606_D0_GPIO_Port GPIOB
#define AD7606_D0_Pin GPIO_PIN_1
#define AD7606_D1_GPIO_Port GPIOB
#define AD7606_D1_Pin GPIO_PIN_2
#define AD7606_D2_GPIO_Port GPIOB
#define AD7606_D2_Pin GPIO_PIN_10
#define AD7606_D3_GPIO_Port GPIOB
#define AD7606_D3_Pin GPIO_PIN_11
#define AD7606_D4_GPIO_Port GPIOB
#define AD7606_D4_Pin GPIO_PIN_12
#define AD7606_D5_GPIO_Port GPIOB
#define AD7606_D5_Pin GPIO_PIN_13
#define AD7606_D6_GPIO_Port GPIOB
#define AD7606_D6_Pin GPIO_PIN_14
#define AD7606_D7_GPIO_Port GPIOB
#define AD7606_D7_Pin GPIO_PIN_15
/* DMA相关定义 */
#define ADC_DMA DMA1
#define ADC_DMA_STREAM DMA1_Channel1
#define ADC_DMA_IRQn DMA1_Channel1_IRQn
/* AD7606读取数据缓冲区大小 */
#define AD7606_BUFFER_SIZE 8192
/* AD7606 DMA读取数据缓冲区 */
uint16_t ad7606_buffer[AD7606_BUFFER_SIZE];
/* AD7606读取数据缓冲区计数器 */
uint32_t ad7606_buffer_count = 0;
/* AD7606 DMA传输完成标志 */
volatile uint8_t ad7606_dma_complete = 0;
/* AD7606 DMA传输完成回调函数 */
void HAL_ADC_ConvCpltCallback(ADC_HandleTypeDef* hadc)
{
ad7606_buffer_count += AD7606_BUFFER_SIZE;
if (ad7606_buffer_count >= AD7606_BUFFER_SIZE)
{
ad7606_dma_complete = 1;
}
}
/* AD7606初始化 */
void ad7606_init(void)
{
/* GPIO初始化 */
GPIO_InitTypeDef GPIO_InitStruct;
GPIO_InitStruct.Pin = AD7606_CLK_Pin | AD7606_nCS_Pin | AD7606_BUSY_Pin;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
GPIO_InitStruct.Pin = AD7606_RDY_Pin | AD7606_D0_Pin | AD7606_D1_Pin | AD7606_D2_Pin | AD7606_D3_Pin | AD7606_D4_Pin | AD7606_D5_Pin | AD7606_D6_Pin | AD7606_D7_Pin;
GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
GPIO_InitStruct.Pull = GPIO_PULLUP;
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
/* DMA初始化 */
__HAL_RCC_DMA1_CLK_ENABLE();
HAL_NVIC_SetPriority(ADC_DMA_IRQn, 0, 0);
HAL_NVIC_EnableIRQ(ADC_DMA_IRQn);
}
/* AD7606 DMA传输 */
void ad7606_dma_read(uint16_t* data, uint32_t count)
{
/* DMA传输结束标志 */
ad7606_dma_complete = 0;
/* ADC初始化 */
ADC_HandleTypeDef hadc;
hadc.Instance = ADC1;
hadc.Init.DataAlign = ADC_DATAALIGN_RIGHT;
hadc.Init.ScanConvMode = ADC_SCAN_DISABLE;
hadc.Init.ContinuousConvMode = ENABLE;
hadc.Init.NbrOfConversion = 1;
hadc.Init.DiscontinuousConvMode = DISABLE;
hadc.Init.ExternalTrigConv = ADC_SOFTWARE_START;
hadc.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;
hadc.Init.ClockPrescaler = ADC_CLOCK_SYNC_PCLK_DIV4;
hadc.Init.Resolution = ADC_RESOLUTION_12B;
hadc.Init.DMAContinuousRequests = ENABLE;
hadc.Init.EOCSelection = ADC_EOC_SINGLE_CONV;
HAL_ADC_Init(&hadc);
/* ADC通道配置 */
ADC_ChannelConfTypeDef sConfig;
sConfig.Channel = ADC_CHANNEL_0;
sConfig.Rank = ADC_REGULAR_RANK_1;
sConfig.SamplingTime = ADC_SAMPLETIME_3CYCLES;
HAL_ADC_ConfigChannel(&hadc, &sConfig);
/* DMA初始化 */
ADC_DMA_STREAM->CCR &= ~DMA_SxCR_EN;
ADC_DMA_STREAM->CNDTR = count;
ADC_DMA_STREAM->CPAR = (uint32_t)&ADC1->DR;
ADC_DMA_STREAM->CMAR = (uint32_t)data;
ADC_DMA_STREAM->CCR &= ~DMA_SxCR_CHSEL_Msk;
ADC_DMA_STREAM->CCR |= (uint32_t)DMA_CHANNEL_0 << DMA_SxCR_CHSEL_Pos;
ADC_DMA_STREAM->CCR &= ~(DMA_SxCR_DIR | DMA_SxCR_PINC | DMA_SxCR_MINC | DMA_SxCR_PSIZE | DMA_SxCR_MSIZE | DMA_SxCR_PL | DMA_SxCR_M2M);
ADC_DMA_STREAM->CCR |= DMA_SxCR_MINC | DMA_SxCR_CIRC | DMA_SxCR_PSIZE_HALF | DMA_SxCR_MSIZE_HALF | DMA_SxCR_PL_VERY_HIGH | DMA_SxCR_TCIE;
ADC_DMA_STREAM->CCR |= DMA_SxCR_EN;
/* 启动ADC转换 */
HAL_ADC_Start(&hadc);
}
int main(void)
{
HAL_Init();
ad7606_init();
/* 等待AD7606准备好 */
while (HAL_GPIO_ReadPin(AD7606_BUSY_GPIO_Port, AD7606_BUSY_Pin) != GPIO_PIN_SET);
/* 读取AD7606数据 */
uint32_t count = 0;
while (count < 65536)
{
ad7606_dma_read(ad7606_buffer + count, AD7606_BUFFER_SIZE);
count += AD7606_BUFFER_SIZE;
while (!ad7606_dma_complete);
}
/* 停止ADC转换 */
HAL_ADC_Stop(&hadc);
while (1)
{
}
}
/* DMA传输完成中断 */
void ADC_DMA_IRQHandler(void)
{
HAL_DMA_IRQHandler(&hdma_adc);
}
```
这是一个简单的示例代码,适用于使用STM32F103芯片和HAL库的AD7606应用。具体的GPIO引脚和DMA配置取决于你的硬件连接和需求。请根据实际情况进行修改。
阅读全文