废话不多说直接上代码,如果有不懂的地方可以私信博主。
#include "rtc.h"
#include "stm32f10x.h"
#include "stdio.h"
#include "usart.h"
#include "lcd.h"
void RTC_Init(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_InitStructure.NVIC_IRQChannel = RTC_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR | RCC_APB1Periph_BKP, ENABLE);
PWR_BackupAccessCmd(ENABLE);
BKP_DeInit();
RCC_LSICmd(ENABLE);
RCC_RTCCLKConfig(RCC_RTCCLKSource_LSI);
RCC_RTCCLKCmd(ENABLE);
RTC_WaitForSynchro();
RTC_WaitForLastTask();
RTC_ITConfig(RTC_IT_SEC, ENABLE);
RTC_WaitForLastTask();
RTC_SetPrescaler(40000);
RTC_WaitForLastTask();
RTC_SetCounter(11*3600+59*60+50);
RTC_WaitForLastTask();
}
void RTC_IRQHandler(void)
{
if(RTC_GetITStatus(RTC_IT_SEC)==1)
{
RTC_ClearITPendingBit(RTC_IT_SEC);
if(RTC_GetCounter()== 0x00015180)
RTC_SetCounter(0x0),
RTC_WaitForLastTask();
}
}
void Time_Display(void)
{
u8 string[20];
uint32_t THH = 0, TMM = 0, TSS = 0;
uint32_t TimeVar=RTC_GetCounter();
/* Reset RTC Counter when Time is 23:59:59 */
if (TimeVar== 0x00015180)
{
TimeVar=0;
RTC_SetCounter(0x0);
/* Wait until last write operation on RTC registers has finished */
RTC_WaitForLastTask();
}
/* Compute hours */
THH = TimeVar / 3600;
/* Compute minutes */
TMM = (TimeVar % 3600) / 60;
/* Compute seconds */
TSS = (TimeVar % 3600) % 60;
sprintf((char*)string,"TIME:%.2d %.2d %.2d",THH,TMM,TSS);
Usart_SendString(string);
LCD_DisplayStringLine(Line3,(u8*)string);
}
void _24c01_write(u8 adress, u8 data)
{
I2CStart();
I2CSendByte(0xa0);
I2CWaitAck();
I2CSendByte(adress);
I2CWaitAck();
I2CSendByte(data);
I2CWaitAck();
I2CStop();
}
u8 _24c01_read(u8 adress)
{
u8 temp;
I2CStart();
I2CSendByte(0xa0);
I2CWaitAck();
I2CSendByte(adress);
I2CWaitAck();
I2CStart();
I2CSendByte(0xa1);
I2CWaitAck();
temp=I2CReceiveByte();
I2CWaitAck();
I2CStop();
return temp;
}
#include "usart.h"
#include "stm32f10x.h"
void Usart_Init(void)
{
USART_InitTypeDef USART_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/* Configure USARTy Tx as alternate function push-pull */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
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(USART2, &USART_InitStructure);
USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);
USART_Cmd(USART2, ENABLE);
}
void Usart_SendString(u8* str)
{
int index=0;
do
{
USART_SendData(USART2,str[index]);
while(USART_GetFlagStatus(USART2,USART_FLAG_TXE)==0);
index++;
}
while(str[index]!=0);
}
u8 RXBUF[20];
u16 RXCNT=0;
extern u8 usart_flag;
extern u8 usart_begin;
extern u16 usart_time;
void USART2_IRQHandler(void)
{
u8 temp;
if(USART_GetITStatus(USART2,USART_IT_RXNE)==1)
{
USART_ClearITPendingBit(USART2,USART_IT_RXNE);
temp=USART_ReceiveData(USART2);
if(RXCNT==20)
{
usart_flag=1;
}
else
{
RXBUF[RXCNT]=temp;
RXCNT++;
if(temp=="\r"||temp=="\n")usart_flag=1;
}
}
}
void TIM2_Init(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
TIM_TimeBaseStructure.TIM_Period = 999;
TIM_TimeBaseStructure.TIM_Prescaler = 71;
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
TIM_ITConfig(TIM2, TIM_IT_Update, ENABLE);
TIM_Cmd(TIM2, ENABLE);
}
void TIM2_IRQHandler(void)
{
if(TIM_GetITStatus(TIM2, TIM_IT_Update)==1)
{
TIM_ClearITPendingBit(TIM2, TIM_IT_Update);
}
}
//配置
//key
void KEY_Init(void)
{
EXTI_InitTyp**加粗样式**eDef EXTI_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOB, ENABLE);
/* Configure PA.00 pin as input floating */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 |GPIO_Pin_8 ;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1 |GPIO_Pin_2 ;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOB, &GPIO_InitStructure);
/* Enable AFIO clock */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);
/* Connect EXTI0 Line to PA.00 pin */
GPIO_EXTILineConfig(GPIO_PortSourceGPIOA, GPIO_PinSource0);
EXTI_InitStructure.EXTI_Line = EXTI_Line0;
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling;
EXTI_InitStructure.EXTI_LineCmd = ENABLE;
EXTI_Init(&EXTI_InitStructure);
/* Enable and set EXTI0 Interrupt to the lowest priority */
NVIC_InitStructure.NVIC_IRQChannel = EXTI0_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x01;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x02;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
GPIO_EXTILineConfig(GPIO_PortSourceGPIOA, GPIO_PinSource8);
EXTI_InitStructure.EXTI_Line = EXTI_Line8;
EXTI_InitStructure.EXTI_LineCmd = ENABLE;
EXTI_Init(&EXTI_InitStructure);
NVIC_InitStructure.NVIC_IRQChannel = EXTI9_5_IRQn;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
GPIO_EXTILineConfig(GPIO_PortSourceGPIOB, GPIO_PinSource1);
EXTI_InitStructure.EXTI_Line = EXTI_Line1;
EXTI_InitStructure.EXTI_LineCmd = ENABLE;
EXTI_Init(&EXTI_InitStructure);
NVIC_InitStructure.NVIC_IRQChannel = EXTI1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
GPIO_EXTILineConfig(GPIO_PortSourceGPIOB, GPIO_PinSource2);
EXTI_InitStructure.EXTI_Line = EXTI_Line2;
EXTI_InitStructure.EXTI_LineCmd = ENABLE;
EXTI_Init(&EXTI_InitStructure);
NVIC_InitStructure.NVIC_IRQChannel = EXTI2_IRQn;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
void EXTI0_IRQHandler(void) //key1
{
if(EXTI_GetITStatus(EXTI_Line0) != RESET)
{
Delay_Ms(50);
if(KEY1 == 0)
{
}
EXTI_ClearITPendingBit(EXTI_Line0);
}
}
void EXTI9_5_IRQHandler(void)//key2
{
if(EXTI_GetITStatus(EXTI_Line8) != RESET)
{
Delay_Ms(50);
if(KEY2 == 0)
{
}
EXTI_ClearITPendingBit(EXTI_Line8);
}
}
void EXTI1_IRQHandler(void) //key3
{
if(EXTI_GetITStatus(EXTI_Line1) != RESET)
{
Delay_Ms(50);
if(KEY3 == 0)
{
}
EXTI_ClearITPendingBit(EXTI_Line1);
}
}
void EXTI2_IRQHandler(void)//key4
{
if(EXTI_GetITStatus(EXTI_Line2) != RESET)
{
Delay_Ms(50);
if(KEY4 == 0)
{
}
EXTI_ClearITPendingBit(EXTI_Line2);
}
}
void Capture_TIM2(void)
{
TIM_ICInitTypeDef TIM_ICInitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStruct;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
/* Enable the TIM3 global Interrupt */
NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
/* TIM3 channel 2 pin (PA.07) configuration */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1|GPIO_Pin_2;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
TIM_TimeBaseInitStruct.TIM_Period = 0xFFFF;//最大计数值
TIM_TimeBaseInitStruct.TIM_Prescaler = 71; // 分频系数=72 1us计数一次
TIM_TimeBaseInitStruct.TIM_ClockDivision = TIM_CKD_DIV1;//不需要再分频
TIM_TimeBaseInitStruct.TIM_CounterMode = TIM_CounterMode_Up;//向上计数模式
TIM_TimeBaseInitStruct.TIM_RepetitionCounter = 0x0000;//初始值为0
TIM_TimeBaseInit(TIM2,&TIM_TimeBaseInitStruct);
TIM_ICInitStructure.TIM_Channel = TIM_Channel_2;
TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising;
TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI;
TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1;
TIM_ICInitStructure.TIM_ICFilter = 0x0;
TIM_ICInit(TIM2, &TIM_ICInitStructure);
TIM_ICInitStructure.TIM_Channel = TIM_Channel_3;
TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising;
TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI;
TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1;
TIM_ICInitStructure.TIM_ICFilter = 0x0;
TIM_ICInit(TIM2, &TIM_ICInitStructure);
/* TIM enable counter */
TIM_Cmd(TIM2, ENABLE);
/* Enable the CC2 Interrupt Request */
TIM_ITConfig(TIM2, TIM_IT_CC2, ENABLE);
TIM_ITConfig(TIM2, TIM_IT_CC3, ENABLE);
}
__IO uint16_t IC2ReadValue1 = 0, IC2ReadValue2 = 0;
__IO uint16_t CaptureNumber2 = 0;
__IO uint32_t Capture2 = 0;
__IO uint32_t TIM2Freq2 = 0;
__IO uint16_t IC3ReadValue1 = 0, IC3ReadValue2 = 0;
__IO uint16_t CaptureNumber3 = 0;
__IO uint32_t Capture3 = 0;
__IO uint32_t TIM2Freq3 = 0;
void TIM2_IRQHandler(void)
{
if(TIM_GetITStatus(TIM2, TIM_IT_CC2))
{
if(CaptureNumber2 == 0)
{
/* Get the Input Capture value */
IC2ReadValue1 = TIM_GetCapture2(TIM2);
CaptureNumber2 = 1;
}
else if(CaptureNumber2 == 1)
{
/* Get the Input Capture value */
IC2ReadValue2 = TIM_GetCapture2(TIM2);
/* Capture computation */
if (IC2ReadValue2 > IC2ReadValue1)
{
Capture2 = (IC2ReadValue2 - IC2ReadValue1);
}
else
{
Capture2 = ((0xFFFF - IC2ReadValue1) + IC2ReadValue2);
}
/* Frequency computation */
TIM2Freq2 = (uint32_t) SystemCoreClock/72 / Capture2;
CaptureNumber2 = 0;
}
TIM_ClearITPendingBit(TIM2, TIM_IT_CC2);
}
if(TIM_GetITStatus(TIM2, TIM_IT_CC3))
{
if(CaptureNumber3 == 0)
{
/* Get the Input Capture value */
IC3ReadValue1 = TIM_GetCapture3(TIM2);
CaptureNumber3 = 1;
}
else if(CaptureNumber3 == 1)
{
/* Get the Input Capture value */
IC3ReadValue2 = TIM_GetCapture3(TIM2);
/* Capture computation */
if (IC3ReadValue2 > IC3ReadValue1)
{
Capture3 = (IC3ReadValue2 - IC3ReadValue1);
}
else
{
Capture3 = ((0xFFFF - IC3ReadValue1) + IC3ReadValue2);
}
/* Frequency computation */
TIM2Freq3 = (uint32_t) SystemCoreClock/72 / Capture3;
CaptureNumber3 = 0;
}
TIM_ClearITPendingBit(TIM2, TIM_IT_CC3);
}
}
void PWM_TIM(u16 fre, float duty)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_AFIO , ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7 ;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
TIM_TimeBaseStructure.TIM_Period = 1000000/fre-1;
TIM_TimeBaseStructure.TIM_Prescaler = 72-1;
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = duty*1000000/fre;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OC2Init(TIM3, &TIM_OCInitStructure);
TIM_Cmd(TIM3, ENABLE);
}
#define ADC1_DR_Address ((uint32_t)0x4001244C)
u16 adc_val[20][2]={0};
u16 adc_aver_val[2]={0};
void Adc_Init(void)
{
ADC_InitTypeDef ADC_InitStructure;
DMA_InitTypeDef DMA_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_ADCCLKConfig(RCC_PCLK2_Div6);
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1 | RCC_APB2Periph_GPIOA| RCC_APB2Periph_GPIOB, ENABLE);
NVIC_InitStructure.NVIC_IRQChannel = DMA1_Channel1_IRQn; //配置DMA1中断
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4 | GPIO_Pin_5;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
GPIO_Init(GPIOB, &GPIO_InitStructure);
/* DMA1 channel1 configuration ----------------------------------------------*/
DMA_DeInit(DMA1_Channel1);
DMA_InitStructure.DMA_PeripheralBaseAddr = ADC1_DR_Address;//外设基地址 ADC_DR寄存器
DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)&adc_val;//内存地址 数组
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC;//方向外设传向内存
DMA_InitStructure.DMA_BufferSize = 40;//缓冲区数组长度
DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;//内存数据块
DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord;//半字16位
DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord;
DMA_InitStructure.DMA_Mode = DMA_Mode_Circular;//循环
DMA_InitStructure.DMA_Priority = DMA_Priority_High;//高优先级
DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
DMA_Init(DMA1_Channel1, &DMA_InitStructure);
/* Enable DMA1 channel1 */
DMA_Cmd(DMA1_Channel1, ENABLE); //开启使能DMA
DMA_ITConfig(DMA1_Channel1,DMA_IT_TC, ENABLE); //使能DMA传输完成中断
/* 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 = 2;//三通道
ADC_Init(ADC1, &ADC_InitStructure);
/* ADC1 regular channels configuration */
ADC_RegularChannelConfig(ADC1, ADC_Channel_4, 1, ADC_SampleTime_239Cycles5);//ADC规则通道配置
ADC_RegularChannelConfig(ADC1, ADC_Channel_5, 2, ADC_SampleTime_239Cycles5);
/* Enable ADC1 DMA */
ADC_DMACmd(ADC1, ENABLE);
/* Enable ADC1 */
ADC_Cmd(ADC1, ENABLE);
/* Enable ADC1 reset calibration register */
ADC_ResetCalibration(ADC1);
/* Check the end of ADC1 reset calibration register */
while(ADC_GetResetCalibrationStatus(ADC1));
/* Start ADC1 calibration */
ADC_StartCalibration(ADC1);
/* Check the end of ADC1 calibration */
while(ADC_GetCalibrationStatus(ADC1));
/* Start ADC1 Software Conversion */
ADC_SoftwareStartConvCmd(ADC1, ENABLE);//开启软件转换
}
void DMA1_Channel1_IRQHandler(void)
{
if(DMA_GetITStatus(DMA1_IT_TC1))
{
int sum =0;
int i,j;
for(i=0;i<2;i++)
{
for(j=0;j<20;j++)
sum+=adc_val[j][i],adc_val[j][i]=0;
adc_aver_val[i]=sum/20;
sum=0;
}
DMA_ClearITPendingBit(DMA1_IT_TC1);
}
}
adc.c
#include "adc_DMA.h"
#define ADC1_DR_Address ((uint32_t)0x4001244C)
uint16_t Adc_Number[30][3];
uint16_t After_filter[3];
void Adc_DMA_Init(void){
ADC_InitTypeDef ADC_InitStructure;
DMA_InitTypeDef DMA_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_ADCCLKConfig(RCC_PCLK2_Div6);
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1 | RCC_APB2Periph_GPIOA| RCC_APB2Periph_GPIOB, ENABLE);
NVIC_InitStructure.NVIC_IRQChannel = DMA1_Channel1_IRQn; //配置DMA1中断
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4 | GPIO_Pin_5;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
GPIO_Init(GPIOB, &GPIO_InitStructure);
/* DMA1 channel1 configuration ----------------------------------------------*/
DMA_DeInit(DMA1_Channel1);
DMA_InitStructure.DMA_PeripheralBaseAddr = ADC1_DR_Address;
DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)&Adc_Number;
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC;
DMA_InitStructure.DMA_BufferSize = 90;
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_Circular;
DMA_InitStructure.DMA_Priority = DMA_Priority_High;
DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
DMA_Init(DMA1_Channel1, &DMA_InitStructure);
/* Enable DMA1 channel1 */
DMA_Cmd(DMA1_Channel1, ENABLE);
DMA_ITConfig(DMA1_Channel1,DMA_IT_TC, 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 = 3;
ADC_Init(ADC1, &ADC_InitStructure);
/* ADC1 regular channels configuration */
ADC_RegularChannelConfig(ADC1, ADC_Channel_4, 1, ADC_SampleTime_239Cycles5);
ADC_RegularChannelConfig(ADC1, ADC_Channel_5, 2, ADC_SampleTime_239Cycles5);
ADC_RegularChannelConfig(ADC1, ADC_Channel_8, 3, ADC_SampleTime_239Cycles5);
/* Enable ADC1 DMA */
ADC_DMACmd(ADC1, ENABLE);
/* Enable ADC1 */
ADC_Cmd(ADC1, ENABLE);
/* Enable ADC1 reset calibration register */
ADC_ResetCalibration(ADC1);
/* Check the end of ADC1 reset calibration register */
while(ADC_GetResetCalibrationStatus(ADC1));
/* Start ADC1 calibration */
ADC_StartCalibration(ADC1);
/* Check the end of ADC1 calibration */
while(ADC_GetCalibrationStatus(ADC1));
/* Start ADC1 Software Conversion */
ADC_SoftwareStartConvCmd(ADC1, ENABLE);//开启软件转换
}
void DMA1_Channel1_IRQHandler(void)
{
if(DMA_GetITStatus(DMA1_IT_TC1) != RESET)//DMA缓冲区获取完ADC的采样值后 进行次数为30的平均滤波
{
int sum = 0;
u8 i,j;
for(i=0;i<3;i++)
{
for (j=0;j<30;j++)
{
sum += Adc_Number[j][i];
}
After_filter[i]=sum/30;
sum=0;
}
DMA_ClearITPendingBit(DMA1_IT_TC1);
}
}
float GetVolt(u16 advalue)
{
return advalue*3.3/4095;
}
adc.h
/* Define to prevent recursive inclusion -------------------------------------*/
#ifndef __ADC_H
#define __ADC_H
/* Includes ------------------------------------------------------------------*/
#include "main.h"
#define ADC1_DR_Address ((uint32_t)0x4001244C)
void Adc_Init(void);
extern __IO uint16_t ADCConvertedValue[3];
#endif
seg.h
#ifndef __SEG_H
#define __SEG_H
#include "stm32f10x.h"
#define SER_H GPIO_SetBits(GPIOA,GPIO_Pin_1)
#define SER_L GPIO_ResetBits(GPIOA,GPIO_Pin_1)
#define RCK_H GPIO_SetBits(GPIOA,GPIO_Pin_2)
#define RCK_L GPIO_ResetBits(GPIOA,GPIO_Pin_2)
#define SCK_H GPIO_SetBits(GPIOA,GPIO_Pin_3)
#define SCK_L GPIO_ResetBits(GPIOA,GPIO_Pin_3)
void Seg_Init(void);
void Seg_Show(u8 N1,u8 N2,u8 N3);
#endif
seg.c
#include "seg.h"
#include "stm32f10x.h"
u8 Seg7[17] = {0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c, 0x39,0x4f,0x79,0x78,0x00}; //0~9 A~F 全灭
void Seg_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
}
void Seg_Show(u8 num1, u8 num2, u8 num3)
{
int i=0;
u8 num=Seg7[num3];
for(i=0;i<8;i++)
{
if(num&0x80)SER_H;
else SER_L;
SCK_H;
num<<=1;
SCK_L;
}
num=Seg7[num2];
for(i=0;i<8;i++)
{
if(num&0x80)SER_H;
else SER_L;
SCK_H;
num<<=1;
SCK_L;
}
num=Seg7[num1];
for(i=0;i<8;i++)
{
if(num&0x80)SER_H;
else SER_L;
SCK_H;
num<<=1;
SCK_L;
}
RCK_H;
RCK_L;
}
button.h
#ifndef __BUTTON_H
#define __BUTTON_H
#include "stm32f10x.h"
void Button_Init(void);
u16 Get_ADC(void);
u16 Get_Button(void);
u8 scan_button(void);
#endif
button.c
#include "stm32f10x.h"
#include "button.h"
void Button_Init(void)
{
ADC_InitTypeDef ADC_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
RCC_ADCCLKConfig(RCC_PCLK2_Div6);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1 | RCC_APB2Periph_GPIOA, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
GPIO_Init(GPIOA, &GPIO_InitStructure);
ADC_InitStructure.ADC_Mode = ADC_Mode_Independent;
ADC_InitStructure.ADC_ScanConvMode = DISABLE;
ADC_InitStructure.ADC_ContinuousConvMode = DISABLE;
ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None;
ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
ADC_InitStructure.ADC_NbrOfChannel = 1;
ADC_Init(ADC1, &ADC_InitStructure);
ADC_RegularChannelConfig(ADC1, ADC_Channel_5, 1, ADC_SampleTime_239Cycles5);
ADC_Cmd(ADC1, ENABLE);
ADC_ResetCalibration(ADC1);
while(ADC_GetResetCalibrationStatus(ADC1));
ADC_StartCalibration(ADC1);
while(ADC_GetCalibrationStatus(ADC1));
}
u16 Get_ADC(void)
{
u16 adc;
ADC_SoftwareStartConvCmd(ADC1, ENABLE);
while(!ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC));
adc=ADC_GetConversionValue(ADC1);
ADC_SoftwareStartConvCmd(ADC1, DISABLE);
return adc;
}
u16 Get_Button(void)
{
int i=0;
u16 sum=0;
for(i=0;i<10;i++)
sum+=Get_ADC();
sum/=10;
return sum;
}
u8 scan_button(void)
{
u16 adc_val=Get_Button();
Delay_Ms(30);
if(adc_val>0&&adc_val<0x0020)return 1;
if(adc_val>0x0020&&adc_val<0x0270)return 2;
if(adc_val>0x0270&&adc_val<0x0500)return 3;
if(adc_val>0x0500&&adc_val<0x0780)return 4;
if(adc_val>0x0780&&adc_val<0x0A20)return 5;
if(adc_val>0x0A20&&adc_val<0x0C00)return 6;
if(adc_val>0x0C00&&adc_val<0x0E20)return 7;
if(adc_val>0x0E20&&adc_val<0x0FC0)return 8;
return 0;
}
trao.h
#ifndef __EX_H
#define __EX_H
#include "stm32f10x.h"
#define TrDO GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_3)
void TrAO_Init(void);
float Get_TrAO(void);
#endif
trao.c
#include "stm32f10x.h"
#include "trao.h"
void TrAO_Init(void)
{
ADC_InitTypeDef ADC_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
RCC_ADCCLKConfig(RCC_PCLK2_Div6);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1 | RCC_APB2Periph_GPIOA, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
GPIO_Init(GPIOA, &GPIO_InitStructure);
ADC_InitStructure.ADC_Mode = ADC_Mode_Independent;
ADC_InitStructure.ADC_ScanConvMode = DISABLE;
ADC_InitStructure.ADC_ContinuousConvMode = DISABLE;
ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None;
ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
ADC_InitStructure.ADC_NbrOfChannel = 1;
ADC_Init(ADC1, &ADC_InitStructure);
ADC_RegularChannelConfig(ADC1, ADC_Channel_4, 1, ADC_SampleTime_239Cycles5);
ADC_Cmd(ADC1, ENABLE);
ADC_ResetCalibration(ADC1);
while(ADC_GetResetCalibrationStatus(ADC1));
ADC_StartCalibration(ADC1);
while(ADC_GetCalibrationStatus(ADC1));
}
u16 Get_ADC(void)
{
u16 adc;
ADC_SoftwareStartConvCmd(ADC1, ENABLE);
while(!ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC));
adc=ADC_GetConversionValue(ADC1);
ADC_SoftwareStartConvCmd(ADC1, DISABLE);
return adc;
}
float Get_TrAO(void)
{
int i=0;
u16 sum=0;
float trao=0;
for(i=0;i<10;i++)
sum+=Get_ADC();
sum/=10;
trao=(float)sum*3.3/4095;
trao=trao*10000/(3.3-trao);
return trao;
}
float ds18b20_read(void)
{
u8 val[2];
u8 s=0;
u8 i = 0;
s16 x = 0;
float Temp = 0;
__disable_irq();
ow_reset();
ow_byte_wr(OW_SKIP_ROM);
ow_byte_wr(DS18B20_CONVERT);
delay_us(750000);
ow_reset();
ow_byte_wr( OW_SKIP_ROM );
ow_byte_wr ( DS18B20_READ );
for ( i=0 ;i<2; i++) {
val[i] = ow_byte_rd();
}
__enable_irq();
if(val[1] >7)
{
val[0] = ~val[0];
val[1] = ~val[1];
s = 0;
}
else
s = 1;
x = val[1];
x <<= 8;
x |= val[0];
Temp = (float)x*0.0625;//12位时的精度
if(s) return Temp;
else return -Temp;
}
u32 dht_val=0;
void DHT11_Show(void)
{
dht_val=dht11_read();
sprintf(str,"DHT_RH:%3d.%-.2d",dht_val>>24,(dht_val>>16)&0xff);
LCD_DisplayStringLine(Line3,(u8*)str);
sprintf(str,"DHT_TH:%3d.%-.2d",(dht_val>>8)&0xff,(dht_val)&0xff);
LCD_DisplayStringLine(Line4,(u8*)str);
Delay_Ms(2000);
}
u8 LIS302DL_Read(u8 add){
u8 temp=0;
I2CStart();
I2CSendByte(0x38);
I2CWaitAck();
I2CSendByte(add);
I2CWaitAck();
I2CStart();
I2CSendByte(0x39); //I2C读取地址
I2CWaitAck();
temp = I2CReceiveByte();
I2CWaitAck();
I2CStop();
return temp;
}
void LIS302DL_Write(u8 add,u8 date){
I2CStart();
I2CSendByte(0x38);
I2CWaitAck();
I2CSendByte(add);
I2CWaitAck();
I2CSendByte(date);
I2CWaitAck();
I2CStop();
}
void LIS302DL_Init(void){
LIS302DL_Write(0x20,0x47);
LIS302DL_Write(0x21,0x00);
LIS302DL_Write(0x22,0xC0);
LIS302DL_Write(0x32,0x28);
LIS302DL_Write(0x33,0x28);
LIS302DL_Write(0x30,0x00);
}
u8 LIS302DL_Num[3];
u8* LIS302DL_GetVal(void){
if(LIS302DL_Read(0x27)&0x08){//读取状态寄存器 XYZ数据可用时
LIS302DL_Num[0] = (LIS302DL_Read(0x29)); // OUT_X
LIS302DL_Num[1] = (LIS302DL_Read(0x2B)); // OUT_Y
LIS302DL_Num[2] = (LIS302DL_Read(0x2D)); // OUT_Z
}
return LIS302DL_Num;
}
其他函数
_Bool LIS302DL_Check(void){ //检测模块存不存在
if(LIS302DL_Read(0x0F)) return 1;
else return 0;
}
u8* text_san;
text_san=LIS302DL_GetVal();
sprintf((char*)str,"The X Val:%2.2f",text_san[0]/18.);
LCD_DisplayStringLine(Line2,(u8*)str);
sprintf((char*)str,"The Y Val:%2.2f",text_san[1]/18.);
LCD_DisplayStringLine(Line4,(u8*)str);
sprintf((char*)str,"The Z Val:%2.2f",text_san[2]/18.);
LCD_DisplayStringLine(Line6,(u8*)str);
Delay_Ms(200);