전체상품목록 바로가기

본문 바로가기

오늘하루 열지않기


현재 위치

  1. 고객센터
  2. 자료실

자료실

자료실입니다.

게시판 상세
제목 [PIC 프로그래밍] 11. PIC 타미어 TMR0
작성자 가치창조기술 (ip:)
  • 작성일 2011-08-19 17:10:11
  • 추천 추천하기
  • 조회수 6350
평점 0점

TIMER TMR0

타이머 TMR0는 실제적으로 다양한 어플리케이션을 가지고 있고 거의 대부분의 프로그램이 사용을 합니다. 펄스를 생성하거나, 시간을 측정하거나, 외부의 펄스나 이벤트를 카운트하는 프로그램을 작성할때 매우 편리하고 사용하기가 쉽습니다.

타이머 TMR0 모듈은 8비트 타이머/카운터이며 아래와 같은 특징을 가지고 있습니다.:

  • 8-bit 타이머/카운터
  • 8-bit 프리스케일러 (Watchdog timer와 공유)
  • 프로그램가능한 내장 혹은 외장 클럭소스
  • 오버플로우시 인터럽트
  • 프로그램가능한 외부 clock edge선택

아래 그림은 TMR0의 스키마틱으로 TMR0 동작을 결정하는 비트를 보여주고 있습니다. 이러한 비트는 OPTION_REG 레지스터에 저장되어 있습니다.

OPTION_REG Register

  • RBPU - PORTB Pull-up 활성화 비트
    • 0 - PORTB pull-up resistor 비활성화
    • 1 - PORTB 핀 pull-up resistor에 연결가능
  • INTEDG - Interrupt Edge Select bit
    • 0 - INT pin의 라이징 에지시 인터럽트
    • 1 - INT pin의 폴링 에지시 인터럽트
  • T0CS - TMR0 Clock Select bit
    • 0 - RA4 pin을 통해 펄스를 TMR0 타이머/카운터에 인가
    • 1 - 타이머는 내부 사이클 클럭(Fosc/4)을 사용
  • T0SE - TMR0 Source Edge Select bit
    • 0 - TMR0 pin이 하이에서 로우로 전이시 증가.
    • 1 - TMR0 pin이 로우에서 하이로 전이시 증가.
  • PSA - Prescaler Assignment bit
    • 0 - Prescaler는 WDT에 할당
    • 1 - Prescaler는 TMR0 timer/counter에 할당
  • PS2, PS1, PS0 - Prescaler Rate Select bit
    • Prescaler rate는 이 세가지 비트를 조합하여 조절합니다.
PS2 PS1 PS0 TMR0 WDT
0 0 0 1:2 1:1
0 0 1 1:4 1:2
0 1 0 1:8 1:4
0 1 1 1:16 1:8
1 0 0 1:32 1:16
1 0 1 1:64 1:32
1 1 0 1:128 1:64
1 1 1 1:256 1:128

PSA 비트가 클리어되면 프리스케일러는 TMR0 타이머/카운터에 아래 그림과 같이 할당됩니다.

Let's do it in mikroC...

// In this example, TMR0 is configured as a timer and prescaler is assigned to it.

unsigned cnt; // Define variable cnt
void interrupt() { // Interrupt routine
cnt++; // Interrupt causes cnt to be incremented by 1
TMR0 = 155; // Timer (or counter) TMR0 returns its initial value
INTCON = 0x20; // Bit T0IE is set, bit T0IF is cleared
}

void main() {
OPTION_REG = 0x04; // Prescaler (1:32) is assigned to the timer TMR0
TMR0 = 155; // Timer T0 counts from 155 to 255
INTCON = 0xA0; // Enable interrupt TMR0
...
...

// In the following example,TMR0 is configured as counter and prescaler is assigned to it

OPTION_REG = 0x20; // Prescaler (1:2) is assigned to the counter TMR0
TMR0 = 155; // Counter T0 counts from 155 to 255
INTCON = 0xA0; // Enable interrupt TMR0
...
...

PSA 비트가 셋팅되면, 프리스케일러는 아래의 그림과 같이 와치독 타이머에 할당됩니다.

Let's do it in mikroC...

// In this example, prescaler (1:64) is assigned to Watch-dog timer.

void main() {
OPTION_REG = 0x0E; // Prescaler is assigned to WDT (1:64)
asm CLRWDT; // Assembly command to reset WDT
...
...
asm CLRWDT; // Assembly command to reset WDT
...

Additionally it is also worth mentioning:

  • When the prescaler is assigned to the timer/counter, any write to the TMR0 register will clear the prescaler.
  • When the prescaler is assigned to the watch-dog timer, the CLRWDT instruction will clear both the prescaler and WDT.
  • Write to the TMR0 register, used as timer, will not cause the pulse counting to start immediately, but with two instruction cycles delay. Accordingly, it is necessary to adjust the value written to the TMR0 register.
  • When the microcontroller is set in sleep mode, the clock oscillator is turned off. Overflow cannot occur since there are no pulses to count. This is why the TMR0 overflow interrupt cannot wake up the processor from Sleep mode.
  • When used as an external clock counter, without prescaler, a minimal pulse length or a delay between two pulses must be 2 Tosc + 20 nS (Tosc is the oscillator clock signal period).
  • When used as an external clock counter with prescaler, a minimal pulse length or interval between two pulses is only 10nS.
  • The 8-bit prescaler register is not available to the user, which means that it cannot be directly read or written to.
  • When changing the prescaler assignment from TMR0 to the watch-dog timer, the following instruction sequence written in assembly language must be executed in order to prevent the microcontroller from reset:
    BANKSEL TMR0
    CLRWDT ;CLEAR WDT
    CLRF TMR0 ;CLEAR TMR0 AND PRESCALER
    BANKSEL OPTION_REG
    BSF OPTION_REG,PSA ;PRESCALER IS ASSIGNED TO WDT
    CLRWDT ;CLEAR WDT
    MOVLW b'11111000' ;SELECT BITS PS2,PS1,PS0 AND CLEAR
    ANDWF OPTION_REG,W ;THEM BY 'LOGIC AND' INSTRUCTION
    IORLW b'00000101' ;BITS PS2, PS1, AND PS0 SET
    MOVWF OPTION_REG ;PRESCALER RATE TO 1:32
  • Likewise, when changing the prescaler assignment from the WDT to TMR0, the following instruction sequence, also written in assembly language, must be executed:
    BANKSEL TMR0
    CLRWDT ;CLEAR WDT AND PRESCALER
    BANKSEL OPTION_REG
    MOVLW b'11110000' ;SELECT ONLY BITS PSA,PS2,PS1,PS0
    ANDWF OPTION_REG,W ;CLEAR THEM BY 'LOGIC AND' INSTRUCTION
    IORLW b'00000011' ;PRESCALER RATE IS 1:16
    MOVWF OPTION_REG

정리하면

TMR0 를 적절히 이용하기 위해서는 다음의 절차가 필요합니다.

Step 1: 모드선택:

  • T0CS bit of the OPTION_REG 레지스터의 T0CS비트에 의해 타이머 모드가 선택됩니다. (T0CS: 0=timer, 1=counter).
  • 프리스케일러가 사용될때는 OPTION_REG 레지스터의 PSA비트가 클리어되어 타이머/카운터에 할당되어야 합니다. prescaler rate는 PS2-PS0 비트에 의해 설정됩니다.
  • 인터럽트사용시, INTCON레지스터의 GIE, TMR0IE 비트는 셋팅되어야 합니다.

Step 2: 측정과 카운트

시간을 측정하기 위해서:

  • TMR0레지스터를 리셋
  • TMR0레지스터를 읽음으로 경과된 시간이 측정됨
  • INTCON레지스터의 플래그 비트 TMR0IF는 TMR0 레지스터가 오퍼플로우 될때 마다 자동적으로 셋팅되며, 인터럽트가 발생합니다.

펄스를 카운트하기 위해서:

  • OPTION_REG 레지스터의 TOSE 비트에 의해 선택된 RA4핀에서 펄스 극성이 카운트 됩니다. (T0SE: 0=positive, 1=negative pulses).
  • 펄스의 숫자는 TMR0 레지스터에서 읽을 수도 있습니다. 프리스케일러와 인터럽트는 타이머모드에서와 같이 비슷한 방법으로 동작합니다.

 

제품정보: http://ubiquitics.co.kr/front/php/product.php?product_no=163&main_cate_no=1&display_group=2

가치창조기술 | www.ubiquitics.co.kr | www.vctec.co.kr

 

 

첨부파일
비밀번호 삭제하려면 비밀번호를 입력하세요.
관리자게시 스팸신고 스팸해제
목록 삭제 수정 답변
댓글 수정

비밀번호 :

수정 취소

/ byte

댓글 입력

댓글달기이름 :비밀번호 : 관리자답변보기

확인

/ byte

왼쪽의 문자를 공백없이 입력하세요.(대소문자구분)

관리자에게만 댓글 작성 권한이 있습니다.

 

이전 제품  
다음 제품