#include "timer.h" #include "led.h" #include "wifi.h" extern char smog_value_buf[7]; //�洢����Ũ��ֵ���������ݴ��� extern char temp_value_buf[7]; //�洢�¶�ֵ extern char beam_value_buf[7]; //�洢����ǿ��ֵ //ͨ�ö�ʱ���жϳ�ʼ�� //����ʱ��ѡ��ΪAPB1��2������APB1Ϊ36M //arr���Զ���װֵ�� //psc��ʱ��Ԥ��Ƶ�� //����ʹ�õ��Ƕ�ʱ��3! void TIM3_Int_Init(u16 arr,u16 psc) { TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; NVIC_InitTypeDef NVIC_InitStructure; RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE); //ʱ��ʹ�� TIM_TimeBaseStructure.TIM_Period = arr; //��������һ�������¼�װ�����Զ���װ�ؼĴ������ڵ�ֵ ������5000Ϊ500ms TIM_TimeBaseStructure.TIM_Prescaler =psc; //����������ΪTIMxʱ��Ƶ�ʳ�����Ԥ��Ƶֵ 10Khz�ļ���Ƶ�� TIM_TimeBaseStructure.TIM_ClockDivision = 0; //����ʱ�ӷָ�:TDTS = Tck_tim TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //TIM���ϼ���ģʽ TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure); //����TIM_TimeBaseInitStruct��ָ���IJ�����ʼ��TIMx��ʱ�������λ TIM_ITConfig( //ʹ�ܻ���ʧ��ָ����TIM�ж� TIM3, //TIM2 TIM_IT_Update , ENABLE //ʹ�� ); NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn; //TIM3�ж� NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 3; //��ռ���ȼ�0�� NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3; //�����ȼ�3�� NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQͨ����ʹ�� NVIC_Init(&NVIC_InitStructure); //����NVIC_InitStruct��ָ���IJ�����ʼ������NVIC�Ĵ��� TIM_Cmd(TIM3, ENABLE); //ʹ��TIMx���� } //ÿ��300ms����һ������ void TIM3_IRQHandler(void) //TIM3�ж� { if (TIM_GetITStatus(TIM3, TIM_IT_Update) != RESET) //���ָ����TIM�жϷ������:TIM �ж�Դ { TIM_ClearITPendingBit(TIM3, TIM_IT_Update ); //���TIMx���жϴ�����λ:TIM �ж�Դ } } /** * @description: Pwm * @param {u16} arr * @param {u16} psc * @return {*} */ void TIM3_PWM_Init(u16 arr,u16 psc) { GPIO_InitTypeDef GPIO_InitStructure; TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; TIM_OCInitTypeDef TIM_OCInitStructure; RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE); //ʹÄܶ¨Ê±Æ÷3ʱÖÓ RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB | RCC_APB2Periph_AFIO, ENABLE); //ʹÄÜGPIOÍâÉèºÍAFIO¸´Óù¦ÄÜÄ£¿éʱÖÓ GPIO_PinRemapConfig(GPIO_PartialRemap_TIM3, ENABLE); //Timer3²¿·ÖÖØÓ³Éä TIM3_CH2->PB5 //ÉèÖøÃÒý½ÅΪ¸´ÓÃÊä³ö¹¦ÄÜ,Êä³öTIM3 CH2µÄPWMÂö³å²¨ÐÎ GPIOB.5 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1; //TIM_CH2 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //¸´ÓÃÍÆÍìÊä³ö GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOB, &GPIO_InitStructure);//³õʼ»¯GPIO //³õʼ»¯TIM3 TIM_TimeBaseStructure.TIM_Period = arr; //ÉèÖÃÔÚÏÂÒ»¸ö¸üÐÂʼþ×°Èë»î¶¯µÄ×Ô¶¯ÖØ×°ÔؼĴæÆ÷ÖÜÆÚµÄÖµ TIM_TimeBaseStructure.TIM_Prescaler =psc; //ÉèÖÃÓÃÀ´×÷ΪTIMxʱÖÓƵÂʳýÊýµÄÔ¤·ÖƵֵ TIM_TimeBaseStructure.TIM_ClockDivision = 0; //ÉèÖÃʱÖÓ·Ö¸î:TDTS = Tck_tim TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //TIMÏòÉϼÆÊýģʽ TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure); //¸ù¾ÝTIM_TimeBaseInitStructÖÐÖ¸¶¨µÄ²ÎÊý³õʼ»¯TIMxµÄʱ¼ä»ùÊýµ¥Î» //³õʼ»¯TIM3 Channel2 PWMģʽ TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2; //Ñ¡Ôñ¶¨Ê±Æ÷ģʽ:TIMÂö³å¿í¶Èµ÷ÖÆģʽ2 TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //±È½ÏÊä³öʹÄÜ TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; //Êä³ö¼«ÐÔ:TIMÊä³ö±È½Ï¼«ÐÔ¸ß TIM_OC2Init(TIM3, &TIM_OCInitStructure); //¸ù¾ÝTÖ¸¶¨µÄ²ÎÊý³õʼ»¯ÍâÉèTIM3 OC2 TIM_OC2PreloadConfig(TIM3, TIM_OCPreload_Enable); //ʹÄÜTIM3ÔÚCCR2ÉϵÄԤװÔؼĴæÆ÷ TIM_Cmd(TIM3, ENABLE); //ʹÄÜTIM3 }