Real-Time Clock. * Real-Time Computing, edited by Duncan A. Mellichamp, Van Nostrand Reinhold

Similar documents
Section 14. Compare/Capture/PWM (CCP)

ETEC Digital Controls PIC Lab 10 Pulse Width Modulation

Hi Hsiao-Lung Chan Dept Electrical Engineering Chang Gung University, Taiwan

AVR Timer/Counter. Prof Prabhat Ranjan DA-IICT, Gandhinagar

8-bit Microcontroller. Application Note. AVR134: Real-Time Clock (RTC) using the Asynchronous Timer. Features. Theory of Operation.

APPLICATION NOTE. Atmel AVR134: Real Time Clock (RTC) Using the Asynchronous Timer. Atmel AVR 8-bit Microcontroller. Introduction.

AVR131: Using the AVR s High-speed PWM. Introduction. Features. AVR 8-bit Microcontrollers APPLICATION NOTE

AVR134: Real Time Clock (RTC) using the Asynchronous Timer. 8-bit Microcontrollers. Application Note. Features. 1 Introduction

SPI. Overview and Use of the PICmicro Serial Peripheral Interface. Getting Started: SPI

Section 29. Real-Time Clock and Calendar (RTCC)

Lesson-16: Real time clock DEVICES AND COMMUNICATION BUSES FOR DEVICES NETWORK

Section 7. Oscillator

Timer A (0 and 1) and PWM EE3376

Hello, and welcome to this presentation of the STM32L4 reset and clock controller.

CoE3DJ4 Digital Systems Design. Chapter 4: Timer operation

AN1229. Class B Safety Software Library for PIC MCUs and dspic DSCs OVERVIEW OF THE IEC STANDARD INTRODUCTION

AVR1321: Using the Atmel AVR XMEGA 32-bit Real Time Counter and Battery Backup System. 8-bit Microcontrollers. Application Note.

8-Bit Flash Microcontroller for Smart Cards. AT89SCXXXXA Summary. Features. Description. Complete datasheet available under NDA

Application Note AN-1187

Chapter 9 Latches, Flip-Flops, and Timers

HT46R14A Single Phase AC Induction Motor Frequency Converter Application

Flash Microcontroller. Memory Organization. Memory Organization

8741A UNIVERSAL PERIPHERAL INTERFACE 8-BIT MICROCOMPUTER

DP8570A DP8570A Timer Clock Peripheral (TCP)

Section 8. Interrupts

Chapter 13. PIC Family Microcontroller

AN3371 Application note

Experiment # 9. Clock generator circuits & Counters. Eng. Waleed Y. Mousa

Tutorial for MPLAB Starter Kit for PIC18F

MODULE BOUSSOLE ÉLECTRONIQUE CMPS03 Référence :

Small Hardware Development and Prototyping Board for the SX28

PIC Programming in Assembly. (

Serial Communications

Interfacing Of PIC 18F252 Microcontroller with Real Time Clock via I2C Protocol

8051 hardware summary

AN108 IMPLEMENTING A REALTIME CLOCK. Relevant Devices. Introduction. Key Points. Overview

ARM Thumb Microcontrollers. Application Note. Software ISO 7816 I/O Line Implementation. Features. Introduction

Atmel Norway XMEGA Introduction

Chapter 6 PLL and Clock Generator

Accurate Measurement of the Mains Electricity Frequency

AN974 APPLICATION NOTE

Hello, welcome to this presentation of the low power timer, or LPTMR, module for Kinetis MCUs. In this session you ll learn about the LPTMR, it s

Microtronics technologies Mobile:

Section bit A/D Converter

PIC16F87X. 28/40-pin 8-Bit CMOS FLASH Microcontrollers. Devices Included in this Data Sheet: Pin Diagram PDIP. Microcontroller Core Features:

Monitoring of Intravenous Drip Rate

Develop a Dallas 1-Wire Master Using the Z8F1680 Series of MCUs

Interrupts and the Timer Overflow Interrupts Huang Sections What Happens When You Reset the HCS12?

Flexible Active Shutter Control Interface using the MC1323x

GCLK (Generic Clock Management) PM (Power Management) SYSCTRL (Clock Source Control) The following devices can use this module:

Technical Note. Micron NAND Flash Controller via Xilinx Spartan -3 FPGA. Overview. TN-29-06: NAND Flash Controller on Spartan-3 Overview

Debouncing Switches. Mechanical switches are one of the most common interfaces to a uc.

Sequential Logic Design Principles.Latches and Flip-Flops

PIC16F87X Data Sheet. 28/40-Pin 8-Bit CMOS FLASH Microcontrollers Microchip Technology Inc. DS30292C

PIC16CR7X Data Sheet. 28/40-Pin, 8-Bit CMOS ROM Microcontrollers Microchip Technology Inc. DS21993A

AXI Performance Monitor v5.0

HT1632C 32 8 &24 16 LED Driver


Section 17. UART HIGHLIGHTS UART. This section of the manual contains the following major topics:

RENESAS TECHNICAL UPDATE

DS1104 R&D Controller Board

AN141 SMBUS COMMUNICATION FOR SMALL FORM FACTOR DEVICE FAMILIES. 1. Introduction. 2. Overview of the SMBus Specification. 2.1.

PIC16F627A/628A/648A Data Sheet

Below is a diagram explaining the data packet and the timing related to the mouse clock while receiving a byte from the PS-2 mouse:

PIC16F62X. FLASH-Based 8-Bit CMOS Microcontrollers. Devices included in this data sheet: Special Microcontroller Features: High Performance RISC CPU:

MACHINE ARCHITECTURE & LANGUAGE

DS1621 Digital Thermometer and Thermostat

PIC16F627A/628A/648A Data Sheet

AVR055: Using a 32kHz XTAL for run-time calibration of the internal RC. 8-bit Microcontrollers. Application Note. Features.

8-Bit CMOS Microcontrollers with A/D Converter

Software Real Time Clock Implementation on MC9S08LG32

Low-Power Pin Sampling Techniques Using Kinetis L Series

AVR1003: Using the XMEGA Clock System. 8-bit Microcontrollers. Application Note. Features. 1 Introduction

1/22/16. You Tube Video. Definitions. Duty Cycle: on-time per period (specified in per cent)

Palaparthi.Jagadeesh Chand. Associate Professor in ECE Department, Nimra Institute of Science & Technology, Vijayawada, A.P.

AN585. A Real-Time Operating System for PICmicro Microcontrollers INTRODUCTION. Why do I Need a Real-Time Kernel? What is Multitasking Anyway?

MicroMag3 3-Axis Magnetic Sensor Module

Using Arduino Microcontrollers to Sense DC Motor Speed and Position

Counters. Present State Next State A B A B

8051 MICROCONTROLLER COURSE

Freescale Semiconductor, Inc. Product Brief Integrated Portable System Processor DragonBall ΤΜ

Using The PIC I/O Ports

Lab 4: Pulse Width Modulation and Introduction to Simple Virtual Worlds (PWM) (PWM)

Microcontroller Code Example Explanation and Words of Wisdom For Senior Design

Evaluating the Accuracy of Maxim Real-Time Clocks (RTCs)

Section bit A/D Converter

Latches, the D Flip-Flop & Counter Design. ECE 152A Winter 2012

Chapter 02: Computer Organization. Lesson 04: Functional units and components in a computer organization Part 3 Bus Structures

Application Note. 8-bit Microcontrollers. AVR091: Replacing AT90S2313 by ATtiny2313. Features. Introduction

Backup Power Domain. AN Application Note. Introduction

Section 15. Quadrature Encoder Interface (QEI)

71M6521 Energy Meter IC. Real Time Clock Compensation. The Challenge. The RTC in the 71M6521D/F. Theory of Operation APPLICATION NOTE

Computer Organization and Components

Implementing SPI Master and Slave Functionality Using the Z8 Encore! F083A

Glitch Free Frequency Shifting Simplifies Timing Design in Consumer Applications

An Introduction to MPLAB Integrated Development Environment

Freescale Semiconductor, I

MONOCHROME RGB YCbCr VIDEO DIGITIZER

1. Learn about the 555 timer integrated circuit and applications 2. Apply the 555 timer to build an infrared (IR) transmitter and receiver

Transcription:

REAL-TIME CLOCK

Real-Time Clock The device is not a clock! It does not tell time! It has nothing to do with actual or real-time! The Real-Time Clock is no more than an interval timer connected to the computer I/O interface so that the computer can be informed each time a particular interval of time has gone by. * * Real-Time Computing, edited by Duncan A. Mellichamp, Van Nostrand Reinhold

Real-Time Clock (Continued) Computer must be programmed to use the information correctly! Example If clock interval is 1 second, a counter within the computer can be set equal to time, one could easily program a routine to give the time on a 12 or 24-hour basis * Real-Time Computing, edited by Duncan A. Mellichamp, Van Nostrand Reinhold

Line Clock Simple form of Real-Time Clock Use 60Hz from line to generate 1/60 second clock Half-Wave rectifier 60hz 60 Hz Pulse * Real-Time Computing, edited by Duncan A. Mellichamp, Van Nostrand Reinhold

Programmable Clock (primitive) 1MHz or 10MHz XTAL OSC Divide by 10 Divide by 10 Divide by 10 Divide by 10 Divide by 10 10ms..1s 1s. Multiplexer Interrupt to CPU Or connect to I/P * Real-Time Computing, edited by Duncan A. Mellichamp, Van Nostrand Reinhold

Programmable Timer/Counter Clock Presetable UP or Down N-bit Counter CPU DATA Bus RD,WR, CS Overflow output

Programmable Timer/Counter Example: 8 bit Up Counter (0-255) clock input =1MHz or 1us. wanted to interrupt the CPU after 200us. If one load the counter with (256-200) = 56, after 200 clocks ----> output =1 on overflow! Therefore cpu can be interrupted after 200us.

Programmable Timer/Counter Typically Timer:---> known clock input (Internal) Counter: --> unknown clock (External)

TIMERs/COUNTERs PIC16F877 Timer0 Timer1 Timer2

Timer0 Module 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/WDT Block diagram

Timer0 - Prescaler Assignment

Watch Dog Timer Run on internal RC If enable during SLEEP mode, WDT will continue running and will be able to wake up the processor on Time-out! With Prescaler (Post) set to 1:128, typical maximum delay time can be approximately 18*128 ms or over 2 seconds!

Timer0 -TMR0 -Register All instructions writing to the TMR0 will clear the prescaler count, but not change the prescaler assignment! i.e. clrf TMR0, movwf TMR0,.etc. The TMR0 interrupt is generated when the TMR0 register overflows from FFh to 00h TMR0 interrupt cannot awaken the processor from SLEEP (the timer is off during the SLEEP)

Timer0 - Registers associated

Example: Timer0 and Rotary Encoder Objective Set time (minute/second) for the clock (Lab Assignment) * Fast turning will set the Minutes * Slow turning will set the seconds Use Timer0 to distinguish fast turning from slow turning of the rotary encoder

Recall Rotary Encoder: Example Phase Difference One cycle ON/OFF determines CCW/CW

Recall INT_EXT Interrupt : Example. Main Program... enable_interrupts(int_ext); enable_interrupts(global);...

Recall INT_EXT Interrupt : Example #int_ext EXT_INT_ISR() { // one interrupt per cycle // determine direction by reading the another bit // do what is necessary // exit } Note: CCS will reset INTE flag and re-enable GIE. These do not applied to #int_global!

Example: Timer0 and Rotary Encoder (continued) Assume Rotary Encoder generates 32 pulse per revolution One of the outputs of the encoder connects to Rb0/INT Maximum rate of turning is one turn in 0.5 second. i.e. Generates interrupt up to 32*1/2 = 64 times a second or every 1/64 = 15.625 milliseconds.

Example: Timer0 and Rotary Encoder (continued) Assume Timer0 will be used to detect Fast/Slow turning Set prescaler to divide input freq. by 256 (or input period *256) Use internal clock (4MHz/4 or 1us) The TMR0 will increment every 1* 256 = 256us or about 1/4 of ms. TMR0 will count through its 256 counts in 256*256 or about 66 ms.

Example: Timer0 and Rotary Encoder (continued) #int_ext EXT_INT_ISR() { // one interrupt per cycle // determine direction by reading the another bit // clears INTF // checks the T0IF flag to see if TMR0 has gone through 256 // counts since the last RB0/INT. // if so ----> increment/decrement second // else ----> increment/decrement minute // clear TMR0 and T0IF // exit }

Timer1 Module 16-bit timer/counter (TMR1H,TMR1L) Readable and writable (both) Internal or external clock select Interrupt on overflow from FFFFh to 0000h Reset from CCP module trigger Programmable Prescaler (1,2,4, and 8) Sync and Asyn Counter mode

Timer1Block diagram

Timer1 - T1CON - Control Register

Timer1 Oscillator Low power Oscillator rated upto 200kHz Primary intended for a 32kHz Will run during SLEEP

Timer1 - TMR1H:TMR1L The register pair (TMR1H:TMR1L) increments from 0000h to FFFFH and rolls over to 0000H Interrupt if enabled (TMR1IE) on overflow (set TMR1IF)

Timer1 - Registers associated

Timer1and Sleep Mode When PIC is in SLEEP mode, internal clock stop (reducing power consumption) Timer1 includes the pins and oscillator circuit to allow a 32,768-Hz crystal to serve as its external clock source (by pass the synchronizer) TMR1 will overflow at 2, 4, 8, or 16-seconds depending on prescaler value used) and the CPU wake up, initiates the startup of the internal clock which may take as long as 1000 internal clock cycles before the next instruction is executed!

Timer1and Sleep Mode

Example: Timer1and Sleep Mode PIC16F877 is waiting for an external event to occur, it goes into the sleep mode for 16 seconds at a time. Wake up only to check the event and to increment a variable to keep track of time then go back to sleep again 4MHz clock maximum supply current is 4mA Maximum supply current on sleep mode is 42uA If CpU is asleep roughly 15.99 S out of 16.00 S Then the average current is 42(15.99/16)+4000(.01/16) = 44.5 ua!

Timer2 Module 8-bit timer (TMR2) 8-bit period register (PR2) Readable and writable (both) Interrupt on TMR2 match of PR2 Programmable Prescaler (1,4, and 16) Programmable postscaler (1 to 16) Can be use as the PWM time-base for PWM mode of the CCP module SSP module optional use of TMR2 output to generate clock shift

Timer2 Block diagram

Timer2 - T2CON - Control Register

Timer2 - TMR2 The prescaler and postscaler counters are cleared when any of the following occurs: A write to the TMR2 A write to the T2CON Any device reset TMR2 is not cleared when T2CON is written

Timer2 - Registers associated

CAPTURE/COMPARE/PWM MODULES PIC16f877 has two Capture/Compare/PWM (CCP) modules (CCP! & CCP2) Each CCP module contains a 16-bit register which can operate as a: 16-bit Capture register 16-bit Compare register PWM master/slave Duty Cycle register

CCP MODULES Both the CCP1 and CCP2 modules are identical in operation, with the exception being the operation of the special event trigger

CCP1 MODULE Capture/Compare/PWM Register1 (CCPR1) is comprised of two 8-bit registers: CCPR1L (low byte) and CCPR1H (high byte) The CCP1CON register controls the operation of CCP1 The special event trigger is generated by a compare match and will reset Timer1.

CCPxCON Registers CCP1CON REGISTER/CCP2CON REGISTER (ADDRESS: 17h/1dh)

CAPTURE MODE In Capture mode, CCPR1H:CCPR1L captures the 16-bit value of the TMR1 register when an event occurs on pin RC2/CCP1. An event is defined as: Every falling edge Every 4th rising edge Every rising edge Every 16th rising edge An event is selected by control bits CCP1M3:CCP1M0 (CCP1CON<3:0>) When a capture is made, the interrupt flag bit CCP1IF (PIR1<2>) is set The interrupt flag must be cleared in software. If another capture occurs before the value in register CCPR1 is read, the old captured value will be lost.

CAPTURE MODE : RC2/CCP1 In Capture mode, the RC2/CCP1 pin should be configured as an input by setting the TRISC<2> bit Note: If the RC2/CCP1 pin is configured as an output, a write to the port can cause a capture condition.

CAPTURE MODE : Timer1 selection Timer1 must be running in timer mode or synchronized counter mode for the CCP module to use the capture feature. In asynchronous counter mode, the capture operation may not work.

CAPTURE MODE

Example: use of CAPTURE MODE Period Measurement CCP1 operates in capture mode, interrupt on rising edge T=? Start, read counts from CCPRx (Init_Count) Stop, read counts from CCPRx (Final_count) Period = (Final_count - Init_Count) * refrence clock to the TIMER1 Assume that the period is shorter than 65535 * reference clock

Example: use of CAPTURE MODE Main Program. //setup Timer 1 //setup CCPRx for Capture //mode on rising edge //clear Period_Measu_done flag //enable appropriate interrupt. If (Period_Measu_done) repot_period; loop.. CCPx ISR.. // If first interrupt { // save_ccpxr into Init_Count; // exit ;} // else { // calculate the period; // set Period_Measu_done flag; // disable CCPxIE; // exit; }..

Compare Mode In Compare mode, the 16-bit CCPR1 register value is constantly compared against the TMR1 register pair value When a match occurs, the RC2/CCP1 pin is: Driven high Driven low Remains unchanged The action on the pin is based on the value of control bits CCP1M3:CCP1M0 (CCP1CON<3:0>) At the same time, interrupt flag bit CCP1IF is set.

COMPARE MODE : RC2/CCP1 In Compare mode, the RC2/CCP1 pin should be configured as an output by clearing the TRISC<2> bit Note: Clearing the CCP1CON register will force the RC2/CCP1 compare output latch to the default low level. This is not the data latch.

COMPARE MODE : Timer1 selection Timer1 must be running in timer mode or synchronized counter mode for the CCP module to use the compare feature. In asynchronous counter mode, the compare operation may not work.

COMPARE MODE : Software Interrupt mode When Generate Software Interrupt mode is chosen, the CCP1 pin is not affected. The CCPIF bit is set causing a CCP interrupt (if enabled)

COMPARE MODE : SPECIAL EVENT TRIGGER In this mode, an internal hardware trigger is generated, which may be used to initiate an action. The special event trigger output of CCP1 resets the TMR1 register pair This allows the CCPR1 register to effectively be a 16-bit programmable period register for Timer1 The special event trigger output of CCP2 resets the TMR1 register pair and starts an A/D conversion (if the A/D module is enabled) Note: The special event trigger from the CCP1and CCP2 modules will not set interrupt flag bit TMR1IF (PIR1<0>)

Compare Mode

CCPxCON Registers CCP1CON REGISTER/CCP2CON REGISTER (ADDRESS: 17h/1dh)

Example: use of Capture and Compare mode Period Measurement (If Period > 65535* reference clock) CCP1 operates in capture mode, interrupt on rising edge 65536 T=? Start, read counts from CCPR1 (Init_Count), then save it it CCPR2 CCP2 operates in Compare mode (software interrupt), interrupts every 65536 clocks. Update CYCLES Stop, read counts from CCPRx (Final_count) Period = (Final_count - Init_Count+(CYCLES*65536))* Timer1_clock

Example: SquareWave Generator with Compare mode 1kHz 500 clocks* 500 clocks On interrupt, toggle bit 0 of CCPxCON add 500 to CCPRx reset CCPxIF * Assume - PIC run at 4 MHz or 1uS instruction cycle - Timer1 is on and the input is from the internal clock

Example: Trigger Special Event /Compare mode No need to reload CCPRx since the CCPx resets TMR1

Pulse Width Modulation (PWM) Mode In pulse width modulation mode, the CCPx pin produces up to a 10-bit resolution PWM output Since the CCP1 pin is multiplexed with the PORTC data latch, the TRISC<2> bit must be cleared to make the CCP1 pin an output.

Pulse Width Modulation (PWM) Mode

Pulse Width Modulation (PWM) Mode A PWM output has a time-base (period) and a time that the output stays high (duty cycle) The frequency of the PWM is the inverse of the period (1/period)

PWM Period The PWM period is specified by writing to the PR2 register The PWM period can be calculated using the following formula: PWM period = [(PR2) + 1] 4 TOSC (TMR2 prescale value) PWM frequency is defined as 1 / [PWM period] When TMR2 is equal to PR2, the following three events occur on the next increment cycle: TMR2 is cleared The CCP1 pin is set (exception: if PWM duty cycle = 0%, the CCP1 pin will not be set) The PWM duty cycle is latched from CCPR1L into CCPR1H

PWM Resolution Maximum PWM resolution (bits) for a given PWM frequency:

SET UP for PWM Operation Set the PWM period by writing to the PR2 register Set the PWM duty cycle by writing to the CCPR1L register and CCP1CON<5:4> bits Make the CCP1 pin an output by clearing the TRISC<2> bit Set the TMR2 prescale value and enable Timer2 by writing to T2CON. Configure the CCP1 module for PWM operation.

REGISTERS ASSOCIATED WITH CAPTURE, COMPARE, AND TIMER1

REGISTERS ASSOCIATED WITH PWM AND TIMER2