You are on page 1of 8

LP TRNH TIMER CHO STM32 Ch TIMER BASE Bc 1: Cu hnh cho TIMER (vd TIMER 3)

void TIMER3_Configuration(void) { uint16_t Prescalervalue = 0; /* TIM3 clock enable */ RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE); /* Compute the prescaler value */ Prescalervalue = (uint16_t)(SystemCoreClock/10000 - 1); //10KHz /* Time base configuration */ TIM_TimeBaseInitStructure.TIM_Prescaler = 0; TIM_TimeBaseInitStructure.TIM_ClockDivision = 0; TIM_TimeBaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up; TIM_TimeBaseInitStructure.TIM_Period = 100-1; TIM_TimeBaseInit(TIM3, &TIM_TimeBaseInitStructure); /* Prescaler configuration */ TIM_PrescalerConfig(TIM3, PrescalerValue, TIM_PSCReloadMode_Immediate); /* TIM enable counter */ TIM_Cmd(TIM3, ENABLE); /* Clear TIM3 update pending flags */ TIM_ClearFlag(TIM3, TIM_FLAG_Update); /* Enable TIM3 Update interrupts */ TIM_ITConfig(TIM3, TIM_IT_Update, ENABLE); }

Bc 2: Cu hnh ngt NVIC cho TIMER


void NVIC_Configuration(void) { NVIC_InitTypeDef NVIC_InitStructure; /* Enable the TIM3 global Interrupt */ NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); }

Bc 3: Thc thi ngt TIMER

void TIM3_IRQHandler(void) { if(TIM_GetITStatus(TIM3, TIM_IT_Update) == SET) { /* Clear TIM3 Capture compare interrupt pending bit */ TIM_ClearITPendingBit(TIM3, TIM_IT_Trigger); //code here TIM_ClearFlag(TIM3,TIM_FLAG_Update); } }

u tin chng ta tm hiu v timer ca stm32f051r8t6 trn KIT. chc nng cc timer : - timer 1,2,3,14,15,16,17 dng cho cc mc ch : counter, PWM, input capture, compare. - ring timer6 dng cho chc nng counter.

cu trc chung tr timer6. - cc timer c 4 u capture/compare. - c u ra dng PWM hoc compare c iu khin bi timer.

trong bi hng dn ny mnh s hng dn cch s dng TIM6, cu trc c bn ca timer6. - ly xung clock t "internal clock" - xung ni t b dao ng ca chip. - xung nhp c th chia qua b PSC Prescaler. - gi tr counter c lu trong thanh ghi "CNT COUNTER" - u ra khi m trn s lm cho UIF (c bo ngt) ca timer6 =1 - khi s dng ngt phi cho UI =1

khi x ra ngt: - UIF=1, nu c thit lp ngt th chip s thc hin hm ngt.

mnh s dng timer6 o mc logic pc8,pc9 trong mi 500ms. - gi tr counter= 50000uS - b chia tn Prescaler = 480 vi xung nhp ca chip 48Mhz. freq = 48Mhz/480=0.1Mhz, T=10us .

hoc Prescaler = SystemCoreClock/100000-1 - gi tr counter 50000 s trn sau : 50000*10us = 500000us = 0.5s cc file C cn add: - stm32f0xx_tim.c - x l timer - stm32f0xx_misc.c - x l phn ngt ca timer - stm32f0xx_gpio.c - x l phn GPIO - stm32f0xx_rcc - x l clock

#include <stm32f0xx.h> #include <stm32f0xx_tim.h> unsigned char dem; void gpio_setup(void) { // khai bao GPIO_InitStructure = GPIO_InitTypeDef // 2 cach viet la nhu nhau GPIO_InitTypeDef GPIO_InitStructure; /* cap xung cho PC */ RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOC, ENABLE); /* cau hinh PC9 and PC8 la dau ra, khong dung tro keo */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9 | GPIO_Pin_8;// GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT; // GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; // GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; // GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; // GPIO_Init(GPIOC, &GPIO_InitStructure); } void timer_setup(void) { TIM_TimeBaseInitTypeDef timInit; NVIC_InitTypeDef NVIC_InitStructure; //cau hinh tim6 //dau tien enable clock toi tim6 RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM6, ENABLE); //delay 500ms timInit.TIM_Period = 50000 - 1; timInit.TIM_Prescaler = 48 * 10 - 1; //0.1MHz TIM_TimeBaseInit(TIM6, &timInit); //khoi tao xong TIM_Cmd(TIM6, ENABLE); //khoi tao ngat NVIC_InitStructure.NVIC_IRQChannel = TIM6_DAC_IRQn; NVIC_InitStructure.NVIC_IRQChannelPriority = 1; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); TIM6->DIER = 1; //cho phep ngat } int main(void) { gpio_setup(); timer_setup(); while (1) { } } // hm ngat timer6 void TIM6_DAC_IRQHandler(void) { if (TIM_GetITStatus(TIM6, TIM_IT_Update) != RESET) { TIM_ClearITPendingBit(TIM6, TIM_IT_Update); if (dem == 0) { //pc9,pc8 =1

dung PC9,PC8 che do uotput reset pin khi uotput clock 50Mhz khong dung tro keo

// khoi tao PC

GPIO_SetBits(GPIOC, GPIO_Pin_9 | GPIO_Pin_8); dem = 1; } else if (dem == 1) { // pc9,pc8 =0 GPIO_ResetBits(GPIOC, GPIO_Pin_9 | GPIO_Pin_8); dem = 0; } } }

You might also like