色婷婷AⅤ一区二区三区|亚洲精品第一国产综合亚AV|久久精品官方网视频|日本28视频香蕉

          新聞中心

          EEPW首頁 > 嵌入式系統 > 設計應用 > STM32定時器溢出模式計時設置

          STM32定時器溢出模式計時設置

          作者: 時間:2016-11-19 來源:網絡 收藏
          環(huán)境:

          本文引用地址:http://cafeforensic.com/article/201611/318492.htm

          主機:WIN7

          開發(fā)環(huán)境:MDK4.23

          MCU:STM32F103CBT6

          源代碼1:

          說明:定時器采用TIM2,時鐘源為內部8M晶振,向上溢出模式.此定時器用在延時函數,最小可以延時1us,故沒有采用中斷形式定時.

          初始化代碼:

          RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2,ENABLE);//重新將Timer設置為缺省值TIM_DeInit(TIM2);//采用內部時鐘給TIM2提供時鐘源TIM_InternalClockConfig(TIM2);timInitStruct.TIM_ClockDivision = TIM_CKD_DIV2;   			//定時器基準頻率8MHztimInitStruct.TIM_Prescaler = 8 - 1;           				//計數頻率為1us跳轉1次timInitStruct.TIM_CounterMode = TIM_CounterMode_Up; 		//向上計數timInitStruct.TIM_RepetitionCounter = 0;timInitStruct.TIM_Period = 0; 								//這個值實際上就是TIMX->ARR,延時開始時重新設定即可TIM_TimeBaseInit(TIM2, &timInitStruct);TIM_ITConfig(TIM2, TIM_IT_Update, ENABLE); 					//計數溢出時觸發(fā)中斷TIM_Cmd(TIM2, ENABLE);				 						//開啟計數器

          延時函數代碼:

          //延時1us//最大65536usvoid _delay_us(uint16_t t){TIM_SetCounter(TIM2,0);TIM_SetAutoreload(TIM2,t - 1);//等待時間到while (TIM_GetITStatus(TIM2,TIM_FLAG_Update) == 0);//停止計數TIM_SetAutoreload(TIM2,0);//清中斷標志TIM_ClearITPendingBit(TIM2,TIM_FLAG_Update);}//延遲1ms//最大65msvoid _delay_ms(uint8_t t){if (t > 65){t = 65;}_delay_us(t * 1000);}

          源代碼2:

          說明:定時器采用TIM3,時鐘源為內部8M晶振,向上溢出模式.此定時器用在長時間計時,最小計時間隔為100us,故采用中斷形式定時.

          初始化代碼:

          //中斷NVIC設置:允許中斷,設置優(yōu)先級NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn;    			//更新事件NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;   //搶占優(yōu)先級0NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;          //響應優(yōu)先級1NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;             //允許中斷NVIC_Init(&NVIC_InitStructure);                             //寫入設置RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3,ENABLE);//重新將Timer設置為缺省值TIM_DeInit(TIM3);//采用內部時鐘給TIM3提供時鐘源TIM_InternalClockConfig(TIM3);timInitStruct.TIM_ClockDivision = TIM_CKD_DIV2;   			//定時器基準頻率8MHztimInitStruct.TIM_Prescaler = 800 - 1;           			//計數頻率為100us跳轉1次timInitStruct.TIM_CounterMode = TIM_CounterMode_Up; 		//向上計數timInitStruct.TIM_RepetitionCounter = 0;timInitStruct.TIM_Period = 0; 								//這個值實際上就是TIMX->ARR,延時開始時重新設定即可TIM_TimeBaseInit(TIM3, &timInitStruct);//清溢出中斷標志TIM_ClearFlag(TIM3, TIM_FLAG_Update);//禁止ARR預裝載緩沖器TIM_ARRPreloadConfig(TIM3, DISABLE);//開啟中斷TIM_ITConfig(TIM3, TIM_IT_Update, ENABLE); 				TIM_Cmd(TIM3, ENABLE);				 						//開啟計數器

          中斷函數代碼:

          //TIM3中斷void TIM3_IRQHandler(void){if(TIM_GetITStatus(TIM3, TIM_IT_Update) == SET){TIM3->ARR = 0;//清中斷溢出標志TIM_ClearITPendingBit(TIM3,TIM_FLAG_Update);//發(fā)送信號Sig_Table.stop_require = 1;}}




          評論


          技術專區(qū)

          關閉