#include "led.h"
#include "delay.h"
#include "key.h"
#include "sys.h"
#include "lcd.h"
#include "usart.h"
#include "lsens.h"
#include "adc.h"
#include "dac.h"
void myPwmHandler(uint16_t prer,uint16_t value);
void pwmDacSet(u16 vol);
int main(void)
{
u16 pwmval=0;
u16 adcx=0;
u8 t=0;
u8 key=0;
float temp=0;
delay_init();
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
uart_init(115200);
LED_Init();
KEY_Init();
LCD_Init();
myPwmHandler(0,255);
myAdcInit();
TIM_SetCompare1(TIM1,0);
LCD_ShowString(60,150,200,16,16,"PWM VAL:");
LCD_ShowString(60,170,200,16,16,"DAC VOL:0.000V");
LCD_ShowString(60,190,200,16,16,"ADC VOL:0.000V");
while(1)
{
t++;
key=KEY_Scan(0);
if(key==WKUP_PRES)
{
if(pwmval<250)pwmval+=10;
TIM_SetCompare1(TIM1,pwmval);
}
if(key==KEY1_PRES)
{
if(pwmval>10)pwmval-=10;
else pwmval=0;
TIM_SetCompare1(TIM1,pwmval);
}
if(t==10||key==KEY1_PRES||key==WKUP_PRES)
{
adcx=TIM_GetCapture1(TIM1);
LCD_ShowxNum(124,150,adcx,4,16,0);
temp=(float)(adcx*(3.3/256));
adcx=temp;
LCD_ShowxNum(124,170,temp,1,16,0);
temp-=adcx;
temp*=1000;
LCD_ShowxNum(140,170,temp,3,16,0X80);
adcx=adcArc(ADC_Channel_1,20);
temp=(float)adcx*(3.3/4096);
adcx=temp;
LCD_ShowxNum(124,190,temp,1,16,0);
temp-=adcx;
temp*=1000;
LCD_ShowxNum(140,190,temp,3,16,0X80);
t=0;
}
}
}
void myPwmHandler(uint16_t prer,uint16_t value)
{
GPIO_InitTypeDef GPIO_InitStruct;
TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStruct;
TIM_OCInitTypeDef TIM_OCInitStruct;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
GPIO_InitStruct.GPIO_Mode=GPIO_Mode_AF_PP;
GPIO_InitStruct.GPIO_Pin=GPIO_Pin_8;
GPIO_InitStruct.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(GPIOA,&GPIO_InitStruct);
TIM_TimeBaseInitStruct.TIM_Prescaler=prer;
TIM_TimeBaseInitStruct.TIM_CounterMode=TIM_CounterMode_Up;
TIM_TimeBaseInitStruct.TIM_Period=value;
TIM_TimeBaseInitStruct.TIM_ClockDivision=0;
TIM_TimeBaseInit(TIM1, &TIM_TimeBaseInitStruct);
TIM_OCInitStruct.TIM_OCMode=TIM_OCMode_PWM2;
TIM_OCInitStruct.TIM_OCPolarity=TIM_OCPolarity_Low;
TIM_OCInitStruct.TIM_OutputState=TIM_OutputState_Enable;
TIM_OCInitStruct.TIM_Pulse=0;
TIM_OC1Init(TIM1, &TIM_OCInitStruct);
TIM_OC1PreloadConfig(TIM1, TIM_OCPreload_Enable);
TIM_ARRPreloadConfig(TIM1,ENABLE);
TIM_CtrlPWMOutputs(TIM1,ENABLE);
TIM_Cmd(TIM1,ENABLE);
}
void pwmDacSet(u16 vol)
{
float temp=vol;
temp/=100;
temp=((temp*256)/3.3);
TIM_SetCompare1(TIM1,temp);
}
|