STM32 數(shù)字電壓表 LM317 可調(diào)電源
main中進行一個簡單的軟件濾波,最后送液晶顯示,完成后要重新開啟DMA 。
本文引用地址:http://m.butianyuan.cn/article/201612/325210.htmR2 我這次用的是10K 的 電壓范圍是可以從0 開始的 而公式是Vo=1.25(1+R2/R1) 。感覺這是因為只要給ADJ一個參考電壓便可以使LM317有值輸出的。保證R1≥0.83KΩ,R2≤23.74KΩ 便可維持一個最小工作電流,當(dāng)317穩(wěn)壓塊的輸出電流小于其最小穩(wěn)定工作電流時,317穩(wěn)壓塊就不能正常工作最小穩(wěn)定工作電流的值一般為1.5mA。
#include "stm32f10x.h"
#include "HAL.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()
{
/* 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;//普通模式 完成后就會停止
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)換周期間隔 周期越長 輸入ADC的阻抗越大,最大239的話大約370KΩ 而最小的周期時只有 1KΩ多,輸入阻抗太小在后邊的分壓電路變不好設(shè)計了,這點要注意*/
ADC_RegularChannelConfig(ADC1, ADC_Channel_14, 1, ADC_SampleTime_239Cycles5);
ADC_RegularChannelConfig(ADC1, ADC_Channel_14, 1, ADC_SampleTime_239Cycles5);
/* Enable ADC1 DMA */
ADC_DMACmd(ADC1, ENABLE);
/* Enable ADC1 */
ADC_Cmd(ADC1, ENABLE);
ADC_DMACmd(ADC1, ENABLE);
/* Enable ADC1 */
ADC_Cmd(ADC1, ENABLE);
/* Enable ADC1 reset calibaration register 下邊進行校準ADC*/
ADC_ResetCalibration(ADC1);
/* Check the end of ADC1 reset calibration register */
while(ADC_GetResetCalibrationStatus(ADC1));
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_StartCalibration(ADC1);
/* Check the end of ADC1 calibration */
while(ADC_GetCalibrationStatus(ADC1));
/* Start ADC1 Software Conversion */
ADC_SoftwareStartConvCmd(ADC1, ENABLE);
}
/**************************************************************
** 函數(shù)名:ADC_DigitFilter
** 功能:軟件濾波
** 注意事項:取NO的2/5作為頭尾忽略值,注意N要大于5,否則不會去頭尾
***************************************************************/
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;
}
** 函數(shù)名:ADC_DigitFilter
** 功能:軟件濾波
** 注意事項:取NO的2/5作為頭尾忽略值,注意N要大于5,否則不會去頭尾
***************************************************************/
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));
}
tmp=0;
for(i=cut_no;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 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);//清除全部中斷標志
ADC_Ok=TRUE;// DMA_Cmd(DMA1_Channel1, DISABLE);
}
}
評論