○ PIC 타이머
- PIC18F4520는 3개의 타이머를 가짐.
TIMER0 | 8-bit | OPTION_REG | TMR0 | 0.2usec | 13.107ms |
TIMER1 | 16-bit | T1CON | TMR1H,TMR1L | 0.2usec | 104.857ms |
TIMER2 | 8-bit | T2CON | TMR2 | 0.2usec | 819usec |
딜레이에 따른 타이머 계산
Delay = TimerCount * tick
Count = (Delay/tick)
RegValue = TimerMax- Count
RegValue = TimerMax-(Delay/tick) = TimerMax - (Delay/((Prescalar *4)/Fosc))
RegValue = TimerMax-((Delay * Fosc)/(Prescalar*4))
TIMER0 | 8-bit | RegValue = 256-((Delay * Fosc)/(Prescalar*4)) |
TIMER1 | 16-bit | RegValue = 65536-((Delay * Fosc)/(Prescalar*4)) |
TIMER2 | 8-bit | RegValue = 256-((Delay * Fosc)/(Prescalar*4)) |
○ Timer 0
- 8-bit timer/counter
- Readable and writable
- 8-bit software programmable prescaler
- Internal or external clock select
- Interrupt on overflow from FFh to 00h
- Edge select for external clock
○ Timer0 레지스터
REG | This registers is used to configure the TIMER0 Prescalar, Clock Source etc |
TMR0 | This register holds the timer count value which will be incremented depending on prescalar configuration |
INTCON | This register contains the Timer0 overflow flag(TMR0IF) and corresponding Inetrrupt Enable flag(TMR0IE). |
7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
RBPU | INTEDG | T0CS | T0SE | PSA | PS2 | PS1 | PS0 |
RBPU: NA for Timers
INTEDG: NA for Timers
T0CS: TMR0 Clock Source Select bit
1-Transition on T0CKI pin
0-Internal instruction cycle clock (CLKO)
T0SE: TMR0 Source Edge Select bit
1-Increment on high-to-low transition on T0CKI pin
0-Increment on low-to-high transition on T0CKI pin
PSA: Prescaler Assignment bit
1-Prescaler is assigned to the WDT
0-Prescaler is assigned to the Timer0
PS2:PS0: Prescaler Rate Select bits
INTCON | |||||||
7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
GIE | PIE | TMR0IE | INTE | RBIE | TMR0IF | INTF | RBIF |
GIE: Global Interrupt Enable bit
1-Enables all unmasked interrupts
0-Disables all interrupts
PIE: Peripheral Interrupt Enable bit
1-Enables all unmasked peripheral interrupts
0-Disables all peripheral interrupts
TMR0IE: TMR0 Overflow Interrupt Enable bit
1-Enables the TMR0 interrupt
0-Disables the TMR0 interrupt
INTE: NA for Timers
RBIE: NA for Timers
TMR0IF: TMR0 Overflow Interrupt Flag bit
1-TMR0 register has overflowed (must be cleared in software)
0-TMR0 register did not overflow
INTF: NA for Timers
RBIF: NA for Timers
○ 샘플 코드
: 1ms 딜레이를 사용한 LED 블링킹 코드 샘플
클럭:20Mhz, 인터럽트 주기 1ms, 분주비(Prescalar) : 32
RegValue = 256-(Delay * Fosc)/(Prescalar*4)) = 256-((1ms * 20Mhz)/(32*4)) = 256-156=100
#include<pic16f877a.h>
char value = 0;
#define SBIT_PS2 2
void interrupt timer_isr()
{
if(TMR0IF==1)
{
value=~value; // blinking the LEDs
TMR0 = 101; // Load the timer Value
TMR0IF=0; // Clear timer interrupt flag
}
}
void main()
{
TRISD=0x00; //COnfigure PORTD as output to blink the LEDs
OPTION_REG = (1<<SBIT_PS2); // Timer0 with external freq and 32 as prescalar
TMR0=100; // Load the time value for 1ms delay
TMR0IE=1; //Enable timer interrupt bit in PIE1 register
GIE=1; //Enable Global Interrupt
PEIE=1; //Enable the Peripheral Interrupt
while(1)
{
PORTD = value;
}
}
'공학속으로 > 마이컴' 카테고리의 다른 글
포트 제어 (LED 켜기/끄기) 방법 (0) | 2020.03.12 |
---|---|
PIC18F4520 EEPROM (0) | 2020.03.04 |
8051 모든 인터럽트 (0) | 2020.03.04 |
외부 인터럽트 (0) | 2020.03.04 |
8051/4051 Timers (0) | 2020.03.04 |
댓글