STM32 數(shù)字電壓表LM317可調(diào)電源
老師出的練手的題,在原來(lái)的函數(shù)發(fā)生器基礎(chǔ)上增加了ADC功能。依然使用DMA 傳送,每次傳送16個(gè)數(shù)據(jù)完成后進(jìn)中斷, 標(biāo)記ADC_Ok 然后
本文引用地址:http://butianyuan.cn/article/201610/310911.htmmain中進(jìn)行一個(gè)簡(jiǎn)單的軟件濾波,最后送液晶顯示,完成后要重新開(kāi)啟DMA 。
R2 我這次用的是10K 的 電壓范圍是可以從0 開(kāi)始的 而公式是Vo=1.25(1+ R2/R1) 。感覺(jué)這是因?yàn)橹灰oADJ一個(gè)參考電壓便可以使LM317有值輸出的。保證R1≥0.83KΩ,R2≤23.74KΩ 便可維持一個(gè)最小工作電流,當(dāng)317穩(wěn)壓塊的輸出電流小于其最小穩(wěn)定工作電流時(shí),317穩(wěn)壓塊就不能正常工作最小穩(wěn)定工作電流的值一般為1.5mA。
#include "stm32f10x.h"
#include "HAL.H"
#define ADC1_DR_Address ((uint32_t)0x4001244C)
/* Private variables ---------------------------------------------------------*/
DMA_InitTypeDef DMA_InitStructure;
u16 ADCConvertedValue[16];
volatile bool ADC_Ok;
void ADC_Configuration()
{
ADC_InitTypeDef ADC_InitStructure;
/* ADCCLK = PCLK2/4 */
RCC_ADCCLKConfig(RCC_PCLK2_Div4);
/* Enable DMA1 clock */
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);
/* Enable ADC1 clock */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1 , ENABLE);
/* DMA1 channel1 configuration ----------------------------------------------*/
DMA_DeInit(DMA1_Channel1);
DMA_InitStructure.DMA_PeripheralBaseAddr = ADC1_DR_Address;
DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)&ADCConvertedValue;
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC;
DMA_InitStructure.DMA_BufferSize = 16;
DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord;
DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord;
DMA_InitStructure.DMA_Mode = DMA_Mode_Normal; //普通模式 完成后就會(huì)停止
DMA_InitStructure.DMA_Priority = DMA_Priority_High;
DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
DMA_Init(DMA1_Channel1, &DMA_InitStructure);
DMA_ITConfig(DMA1_Channel1, DMA_IT_TC, ENABLE); //DMA通道1傳輸完成中斷
/* Enable DMA1 channel1 */
DMA_Cmd(DMA1_Channel1, ENABLE);
/* 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_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
ADC_InitStructure.ADC_NbrOfChannel = 1;
ADC_Init(ADC1, &ADC_InitStructure);
/* ADC1 regular channel14 configuration 就是PC4腳 為模擬量輸入 注意下邊轉(zhuǎn)換周期間隔 周期越長(zhǎng) 輸入ADC的阻抗越大,最大239的話大約370KΩ 而最小的周期時(shí)只有 1KΩ多,輸入阻抗太小在后邊的分壓電路變不好設(shè)計(jì)了,這點(diǎn)要注意*/
ADC_RegularChannelConfig(ADC1, ADC_Channel_14, 1, ADC_SampleTime_239Cycles5);
/* Enable ADC1 DMA */
ADC_DMACmd(ADC1, ENABLE);
/* Enable ADC1 */
ADC_Cmd(ADC1, ENABLE);
/* Enable ADC1 reset calibaration register 下邊進(jìn)行校準(zhǔn)ADC*/
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);
}
/**************************************************************
** 函數(shù)名:ADC_DigitFilter
** 功能:軟件濾波
** 注意事項(xiàng):取NO的2/5作為頭尾忽略值,注意N要大于5,否則不會(huì)去頭尾
***************************************************************/
u16 ADC_DigitFilter(u16* buf,u8 no)
{
u8 i,j;
u16 tmp;
u8 cut_no=0;
//冒泡法排序
for(i=0;i {
for(j=0;j {
if(buf[j]>buf[j+1])
{
/*tmp=buf[j];
buf[j]=buf[j+1];
buf[j+1]=tmp;*/
buf[j]=buf[j]^buf[j+1];
buf[j+1]=buf[j]^buf[j+1];
buf[j]=buf[j]^buf[j+1];
}
}
}
if(no>5)
{
cut_no=no/5;
}
//求平均
tmp=0;
for(i=cut_no;i tmp+=buf[i];
return(tmp/(no-2*cut_no));
}
//重新允許DMA,
void DMAReConfig(void)
{
DMA_DeInit(DMA1_Channel1);
DMA_Init(DMA1_Channel1, &DMA_InitStructure);
DMA_ITConfig(DMA1_Channel1, DMA_IT_TC, ENABLE);
DMA_Cmd(DMA1_Channel1, ENABLE);
}
下邊的便是分壓小板
void DMA1_Channel1_IRQHandler(void)
{
if(DMA_GetITStatus(DMA1_IT_TC1))
{
DMA_ClearITPendingBit(DMA1_IT_GL1); //清除全部中斷標(biāo)志
ADC_Ok=TRUE;// DMA_Cmd(DMA1_Channel1, DISABLE);
}
}
評(píng)論