微波EDA网,见证研发工程师的成长!
首页 > 研发问答 > 嵌入式设计讨论 > MCU和单片机设计讨论 > PWM DAC 用按键控制LED灯呼吸

PWM DAC 用按键控制LED灯呼吸

时间:10-02 整理:3721RD 点击:
#include "stm32f10x.h"
#include <stdio.h>
#include "stm32f10x_usart.h"
#include "stm32f10x_it.h"
/********************************/
void  Delay (u32 nCount)
{
  for(; nCount != 0; nCount--);
}
/************************************************/
/*****************'?ú×ó3ìDò*********************/
void GPIO_Configuration()
{
     GPIO_InitTypeDef GPIO_InitStructure;
           USART_InitTypeDef USART_InitStructure;
           RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_USART1,ENABLE);
        
          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);
          USART_InitStructure.USART_BaudRate=115200;
          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_ITConfig(USART1, USART_IT_RXNE, ENABLE);        
          USART_Cmd(USART1,ENABLE);
          USART_ClearFlag(USART1,USART_FLAG_TC);
}
int fputc(int ch, FILE *f)
{
                USART_SendData(USART1, (uint8_t) ch);
                while (USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);               
                return (ch);
}
/************************************************/
/******************°'?ü×ó3ìDò********************/
void KEY_Init(void)
{
   GPIO_InitTypeDef GPIO_InitStructure;
   RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOE,ENABLE);
   GPIO_InitStructure.GPIO_Pin =GPIO_Pin_3|GPIO_Pin_4;
   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
   GPIO_Init(GPIOE, &GPIO_InitStructure);
        
   GPIO_SetBits(GPIOA,GPIO_Pin_13);  
   GPIO_SetBits(GPIOA,GPIO_Pin_15);
}
void led()
{
   GPIO_InitTypeDef GPIO_InitStructure;
   RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOE,ENABLE);
   GPIO_InitStructure.GPIO_Pin =GPIO_Pin_5;
   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
          GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
   GPIO_Init(GPIOE, &GPIO_InitStructure);
    GPIO_SetBits(GPIOE,GPIO_Pin_5);  
}
/************************************************/
/*************ADC×ó3ìDò**************/
void Adc_Init(void)
{
        
                                ADC_InitTypeDef ADC_InitStructure;
                                GPIO_InitTypeDef GPIO_InitStructure;
                                RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA |RCC_APB2Periph_ADC1  , ENABLE );
                                RCC_ADCCLKConfig(RCC_PCLK2_Div6);
                                GPIO_InitStructure.GPIO_Pin =GPIO_Pin_1;
                                GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
                                GPIO_Init(GPIOA, &GPIO_InitStructure);
                                ADC_DeInit(ADC1);
                                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_Cmd(ADC1, ENABLE);
                                ADC_ResetCalibration(ADC1);
                                while(ADC_GetResetCalibrationStatus(ADC1));
                                ADC_StartCalibration(ADC1);
                                while(ADC_GetCalibrationStatus(ADC1));
}
u16 Get_Adc(u8 ch)
{
                ADC_RegularChannelConfig(ADC1, ch, 1, ADC_SampleTime_55Cycles5 );
                ADC_SoftwareStartConvCmd(ADC1, ENABLE);  
                while(!ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC ));
                return ADC_GetConversionValue(ADC1);
}
u16 Get_Adc_Average(u8 ch,u8 times)
{
                u32 temp_val=0;
                u8 t;
                for(t=0;t<times;t++)
                {   temp_val+=Get_Adc(ch);
                                Delay (2);
                }
                return temp_val/times;
}
/************************************/
void TIM1_PWM_Init()
{
                                        GPIO_InitTypeDef GPIO_InitStructure;
                                        TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
                                        TIM_OCInitTypeDef TIM_OCInitStructure;
                                        RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE);
                                        RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
                                        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;
                                        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
                                        GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
                                        GPIO_Init(GPIOA, &GPIO_InitStructure);
                                        TIM_TimeBaseStructure.TIM_Period = 255;
                                        TIM_TimeBaseStructure.TIM_Prescaler =0;
                                        TIM_TimeBaseStructure.TIM_ClockDivision = 0;
                                        TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
                                        TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure);
                                        TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2;
                                        TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
                                        TIM_OCInitStructure.TIM_Pulse = 0;
                                        TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low;
                                        TIM_OC1Init(TIM1, &TIM_OCInitStructure);
                                        TIM_OC1PreloadConfig(TIM1, TIM_OCPreload_Enable);
                                        TIM_ARRPreloadConfig(TIM1, ENABLE);
                                        TIM_CtrlPWMOutputs(TIM1,ENABLE);  
                                        TIM_Cmd(TIM1, ENABLE);
}
void PWM_DAC_Set(u16 vol)
{
                        float temp=vol;
                        temp/=100;
                        temp=temp*256/3.3;
                        TIM_SetCompare1(TIM1,temp);
}
/************************************/
int main()
{
                        u16 i=0;
      float temp; float temp1;
           float adcx;   float adcx1;
           GPIO_Configuration();     
        Adc_Init();
          KEY_Init();
             led();
           TIM1_PWM_Init();
          TIM_SetCompare1(TIM1,100);
            
        TIM_SetCompare1(TIM1,i);
        printf("\nzhun bei jiu xu\n");
        
        
        while(1)
        {
         if(GPIO_ReadInputDataBit(GPIOE,GPIO_Pin_4)==0)
                                 {
                                           Delay(150000);
                                     if(GPIO_ReadInputDataBit(GPIOE,GPIO_Pin_4)==0)
                                                 {
                                                          i=i+10;
                                                          TIM_SetCompare1(TIM1,i);
                                                         
                                                           adcx1=TIM_GetCapture1(TIM1);
                                                          adcx=Get_Adc_Average(ADC_Channel_1,20);
                                        temp1=(float)adcx1*(3.3/256);
                 temp=(float)adcx*(3.3/4096);
                                                         printf("\n adcx1 =%0.3f  temp1 = %0.3f V ::: adcx =%0.3f    temp = %0.3f V\n",adcx1,temp1,adcx,temp);        
                Delay (0xffff8);Delay (0xffff8);Delay (0xffff8);                                
                                                                if(i>=240)
                                                                {
                      i=10;
                                                                }                        
                                                                                
                                                 }
                                 }
                                 else if(GPIO_ReadInputDataBit(GPIOE,GPIO_Pin_3)==0)
                                 {
                                     Delay(150000);
                                           if(GPIO_ReadInputDataBit(GPIOE,GPIO_Pin_3)==0)
                                                 {
                                                           if(i>0)
                                                                 {
                                                             i=i-10;
                                                          TIM_SetCompare1(TIM1,i);
                                                         
                                                         adcx1=TIM_GetCapture1(TIM1);
                                                          adcx=Get_Adc_Average(ADC_Channel_1,20);
                                        temp1=(float)adcx1*(3.3/256);
                                                                          temp=(float)adcx*(3.3/4096);
                                                            printf("\n adcx1 =%0.3f  temp1 = %0.3f V ::: adcx =%0.3f    temp = %0.3f V\n",adcx1,temp1,adcx,temp);        
                                                                        Delay (0xffff8);Delay (0xffff8);Delay (0xffff8);
                                                                        if(i<=10)
                                                                        {
                          i=240;
                                                                        }
                                                                 }
                                                 }
                                 }                  
  }
}

Copyright © 2017-2020 微波EDA网 版权所有

网站地图

Top