stm32 DA 數(shù)模轉(zhuǎn)換
stm32并不是所有的型號都有DAC功能,只有stm32f103xC/D/E系列 才有DAC轉(zhuǎn)換功能。由于庫函數(shù)手冊中沒有相關(guān)說明,所以只能通過在MDK提供的相關(guān)庫文件 stm32f10x_dac.c 中找到相應(yīng)的DAC函數(shù),做了一番嘗試。
折騰了一天,實現(xiàn)了DAC的轉(zhuǎn)換和三角波的輸出。我使用的是 stm32rct6的芯片,有兩個DAC通道,在PA4和PA5兩個引腳上。
DAC的庫函數(shù)配置相關(guān)參數(shù)說明:
voidDAC_Configuration(void){DAC_InitTypeDefDAC_InitStructure;DAC_InitStructure.DAC_Trigger=DAC_Trigger_Software;//軟件觸發(fā),不使用定時器或者外部中斷等觸發(fā)DAC_InitStructure.DAC_WaveGeneration=DAC_WaveGeneration_None;//不產(chǎn)生三角波(DAC_Wave_Triangle)或者噪聲波(DAC_Wave_Noise)DAC_InitStructure.DAC_OutputBuffer=DAC_OutputBuffer_Enable;//提高驅(qū)動能力可以打開緩沖DAC_Init(DAC_Channel_1,&DAC_InitStructure);DAC_DMACmd(DAC_Channel_1,DISABLE);//不使用DMADAC_Cmd(DAC_Channel_1,ENABLE);}
DAC_InitStructure.DAC_Trigger:觸發(fā)方式??蛇x的外部觸發(fā)源一共有八個。
六個是定時器觸發(fā):TIM2,TIM4,TIM5,TIM6,TIM7和TIM8。
剩下兩個分別是:EXTI線路9和軟件觸發(fā)。
DAC_InitStructure.DAC_WaveGeneration:波形發(fā)生器。STM32內(nèi)部集成了兩個幅度可調(diào)的波形發(fā)生器,可以產(chǎn)生三角波(DAC_Wave_Triangle)和噪聲波(DAC_Wave_Noise)。如果我們使用自定義的緩沖區(qū)輸出波形,就不需要配置這個參數(shù)或者使其為DAC_WaveGeneration_None.
DAC_InitStructure.DAC_OutputBuffer:即是否使用輸出緩存。輸出緩存的功能主要用來減小輸出阻抗,是STM32的DAC無需外部運(yùn)放就可以直接驅(qū)動負(fù)載。
相關(guān)參數(shù)在stm32f10x_dac.h中可以找到。
這里實現(xiàn):
設(shè)置DAC輸出固定電壓
設(shè)置DAC輸出三角波形
stm32 DA 數(shù)模轉(zhuǎn)換代碼:
#include"stm32f10x.h"#include"stdio.h"#defineDAC_TEST_11//設(shè)置DAC輸出固定電壓#defineDAC_TEST_20//設(shè)置DAC輸出三角波形voidRCC_Configuration(void);voidGPIO_Configuration(void);voidUSART_Configuration(void);voidDAC_Configuration(void);voidTIM_Configuration(void);voidDelay(u32us);intmain(void){RCC_Configuration();USART_Configuration();GPIO_Configuration();DAC_Configuration();#ifDAC_TEST_1DAC_SetChannel1Data(DAC_Align_12b_R,4095);//刷新DA值,數(shù)據(jù)右對齊取值范圍0~4095//板子上接的參考電壓時3.3v所以4095時為3.3vDAC_SoftwareTriggerCmd(DAC_Channel_1,ENABLE);//軟件觸發(fā),DA值更新printf("rnTheValueis:%drn",DAC_GetDataOutputValue(DAC_Channel_1));//讀取DAC引腳輸出的值#elifDAC_TEST_2TIM_Configuration();#endifwhile(1);}voidDAC_Configuration(void){DAC_InitTypeDefDAC_InitStructure;#ifDAC_TEST_1DAC_InitStructure.DAC_Trigger=DAC_Trigger_Software;//軟件觸發(fā),不使用定時器或者外部中斷等觸發(fā)DAC_InitStructure.DAC_WaveGeneration=DAC_WaveGeneration_None;//不產(chǎn)生三角波(DAC_Wave_Triangle)或者噪聲波(DAC_Wave_Noise)DAC_InitStructure.DAC_OutputBuffer=DAC_OutputBuffer_Enable;//提高驅(qū)動能力可以打開緩沖DAC_Init(DAC_Channel_1,&DAC_InitStructure);#elifDAC_TEST_2DAC_InitStructure.DAC_Trigger=DAC_Trigger_T2_TRGO;//選擇定時器2作外部觸發(fā)源DAC_InitStructure.DAC_WaveGeneration=DAC_Wave_Triangle;//產(chǎn)生三角波DAC_InitStructure.DAC_LFSRUnmask_TriangleAmplitude=DAC_TriangleAmplitude_2047;//三角波的高為2047最高可以為4095DAC_InitStructure.DAC_OutputBuffer=DAC_OutputBuffer_Disable;//無輸出緩沖提高驅(qū)動能力可以打開緩沖DAC_Init(DAC_Channel_1,&DAC_InitStructure);#endifDAC_DMACmd(DAC_Channel_1,DISABLE);//不使用DMADAC_Cmd(DAC_Channel_1,ENABLE);}voidTIM_Configuration(void){TIM_TimeBaseInitTypeDefTIM_TimeBaseStructure;TIM_TimeBaseStructInit(&TIM_TimeBaseStructure);TIM_TimeBaseStructure.TIM_Period=72;//初裝值72每秒產(chǎn)生1000000次更新TIM_TimeBaseStructure.TIM_Prescaler=0x0;TIM_TimeBaseStructure.TIM_ClockDivision=0x0;TIM_TimeBaseStructure.TIM_CounterMode=TIM_CounterMode_Up;TIM_TimeBaseInit(TIM2,&TIM_TimeBaseStructure);TIM_SelectOutputTrigger(TIM2,TIM_TRGOSource_Update);//使用更新事件作為觸發(fā)輸出TIM_Cmd(TIM2,ENABLE);}voidDelay(u32us)//vu321us一次{u32time=100*us/7;while(--time);}voidGPIO_Configuration(void){GPIO_InitTypeDefGPIO_InitStructure;GPIO_InitStructure.GPIO_Pin=GPIO_Pin_4;GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;GPIO_Init(GPIOA,&GPIO_InitStructure);GPIO_InitStructure.GPIO_Pin=GPIO_Pin_9;GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF_PP;GPIO_Init(GPIOA,&GPIO_InitStructure);GPIO_InitStructure.GPIO_Pin=GPIO_Pin_10;GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IN_FLOATING;GPIO_Init(GPIOA,&GPIO_InitStructure);}voidRCC_Configuration(void){/*定義枚舉類型變量HSEStartUpStatus*/ErrorStatusHSEStartUpStatus;/*復(fù)位系統(tǒng)時鐘設(shè)置*/RCC_DeInit();/*開啟HSE*/RCC_HSEConfig(RCC_HSE_ON);/*等待HSE起振并穩(wěn)定*/HSEStartUpStatus=RCC_WaitForHSEStartUp();/*判斷HSE起是否振成功,是則進(jìn)入if()內(nèi)部*/if(HSEStartUpStatus==SUCCESS){/*選擇HCLK(AHB)時鐘源為SYSCLK1分頻*/RCC_HCLKConfig(RCC_SYSCLK_Div1);/*選擇PCLK2時鐘源為HCLK(AHB)1分頻*/RCC_PCLK2Config(RCC_HCLK_Div1);/*選擇PCLK1時鐘源為HCLK(AHB)2分頻*/RCC_PCLK1Config(RCC_HCLK_Div2);/*設(shè)置FLASH延時周期數(shù)為2*/FLASH_SetLatency(FLASH_Latency_2);/*使能FLASH預(yù)取緩存*/FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);/*選擇鎖相環(huán)(PLL)時鐘源為HSE1分頻,倍頻數(shù)為9,則PLL輸出頻率為8MHz*9=72MHz*/RCC_PLLConfig(RCC_PLLSource_HSE_Div1,RCC_PLLMul_9);/*使能PLL*/RCC_PLLCmd(ENABLE);/*等待PLL輸出穩(wěn)定*/while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY)==RESET);/*選擇SYSCLK時鐘源為PLL*/RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);/*等待PLL成為SYSCLK時鐘源*/while(RCC_GetSYSCLKSource()!=0x08);}/*打開APB2總線上的GPIOA時鐘*/RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_AFIO|RCC_APB2Periph_USART1,ENABLE);RCC_APB1PeriphClockCmd(RCC_APB1Periph_DAC|RCC_APB1Periph_TIM2,ENABLE);}voidUSART_Configuration(void){USART_InitTypeDefUSART_InitStructure;USART_ClockInitTypeDefUSART_ClockInitStructure;USART_ClockInitStructure.USART_Clock=USART_Clock_Disable;USART_ClockInitStructure.USART_CPOL=USART_CPOL_Low;USART_ClockInitStructure.USART_CPHA=USART_CPHA_2Edge;USART_ClockInitStructure.USART_LastBit=USART_LastBit_Disable;USART_ClockInit(USART1,&USART_ClockInitStructure);USART_InitStructure.USART_BaudRate=9600;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);}intfputc(intch,FILE*f){USART_SendData(USART1,(u8)ch);while(USART_GetFlagStatus(USART1,USART_FLAG_TC)==RESET);returnch;}