30
V3.2 August 10, 2007 Microcontrolador es Configuração de periféricos no PIC18 - temporizadores

Periféricos - Timers

Embed Size (px)

DESCRIPTION

Slides Microprocessadores PIC18F, a respeito de timers

Citation preview

202 ASP / 202 PRC

MicrocontroladoresConfigurao de perifricos no PIC18 - temporizadoresV3.2 August 10, 2007 2006 Microchip Technology Incorporated. All Rights Reserved. Slide n202 PRC1Perifricos comuns no PIC18Portas de I/O digitaisComparador AnalgicoConversor A/DTemporizadores (0,1,2,3)Captura, Comparao,PWM (CCP)Addressable USART (AUSART)Master Synchronous Serial Port (MSSP)Parallel Slave Port (PSP)

2006 Microchip Technology Incorporated. All Rights Reserved. Slide n202 PRC2TimersTimer 0, Timer 1 and Timer 3 ModulesV3.2 August 10, 2007 2006 Microchip Technology Incorporated. All Rights Reserved. Slide n202 PRC3Timer ComparisonTIMER 0TIMERS 1 & 3TIMERS 2 & 4SIZE OF REGISTER8-bits or16-bits16-bits8-bits CLOCK SOURCE(Internal)Fosc/4Fosc/4Fosc/4CLOCK SOURCE(External )T0CKI pinT13CKI pin or Timer 1 oscillator (T1OSC)NoneCLOCK SCALING AVAILABLE(Resolution)Prescaler 8-bits (1:21:256)Prescaler 2-bits (1:1, 1:2, 1:4, 1:8)Prescaler (1:1,1:4,1:16)Postscaler (1:11:16)INTERRUPT EVENTOn overflow FFh00h On overflow FFFFh0000hTMR REG matches PR2CAN WAKE PIC FROM SLEEP?NOYESNO 2006 Microchip Technology Incorporated. All Rights Reserved. Slide n202 PRC4The PIC18 family currently has 2-5 timers available. In all parts, the above chart holds. The PIC18F4520 that we will use in the lab does not implement Timer 4.Propor o calculo do tempo para diferentes valores de clock.Timer 0Modo de compatibilidade com o PIC16Temporizador/Contador de 8-bitsPrescaler programvel de 8-bitsFonte de clock interna ou externaGera interrupo no overflow: 0xFF 0x00T0SET0CKIFOSC/41010ProgrammablePrescalerT0CSPSAT0PS2:T0PS0ClockSyncTMR0L8-bit Data BusTMR0IF 2006 Microchip Technology Incorporated. All Rights Reserved. Slide n202 PRC5Timer 0Modo 16-bitsTemporizador/Contador de 16-bitsModo de leitura/escrita de 16-bitsGera interrupo no overflow: 0xFFFF 0x0000Basicamentes as mesmas funcionalidades que no modo de compatibilidadeT0SET0CKIFOSC/41010ProgrammablePrescalerT0CSPSAT0PS2:T0PS0ClockSyncHigh Byte8-bit Data BusTMR0IFTMR0LTMR0HREADTMR0LWRITETMR0L 2006 Microchip Technology Incorporated. All Rights Reserved. Slide n202 PRC6Operao do Timer 0 T0CON RegisterTMR0ONT08BITT0CST0SEPSAT0PS2:0T0SET0CKIFOSC/41010ProgrammablePrescalerT0CSPSAT0PS2:T0PS0ClockSyncTMR0IFTMR0LDATA BUST08BIT1 = 8 BIT0 = 16 BIT 2006 Microchip Technology Incorporated. All Rights Reserved. Slide n202 PRC7T0CON register controla a operacao do timer 0Em destaque a selecao do modo de operacaoOperao do Timer 0 T0CON RegisterTMR0ONT08BITT0CST0SEPSAT0PS2:0T0SET0CKIFOSC/41010ProgrammablePrescalerT0CSPSAT0PS2:T0PS0ClockSyncHigh ByteTMR0IFTMR0LTMR0HREADTMR0LWRITETMR0LDATA BUST08BIT1 = 8 BIT0 = 16 BIT 2006 Microchip Technology Incorporated. All Rights Reserved. Slide n202 PRC8Operao do Timer 0 TMR0 Clock Source Select1 = TOCK1, 0 = Fosc/4T0CON RegisterTMR0ONT08BITT0CST0SEPSAT0PS2:0T0SET0CKIFOSC/41010ProgrammablePrescalerT0CSPSAT0PS2:T0PS0ClockSyncHigh ByteTMR0IFTMR0LTMR0HREADTMR0LWRITETMR0LDATA BUS 2006 Microchip Technology Incorporated. All Rights Reserved. Slide n202 PRC9Timer0 is mainly controlled by setting bits within the OPTION register.TOCS: Timer0 Clock Source SelectWhen using an external clock source always refer to the electrical specifications of the datasheet to ensure that timing requirements are met.T0SE: Timer0 Source Edge Select bitThis bit provides the option of selecting either the positive or negative going transition edge of the clock source whichever best suits the designPSA: Prescaler Select bitThe prescaler used by Timer0 is also used by the Watchdog Timer.Precautions must be used when switching the prescaler from Timer0 to the Watchdog timer. The Watchdog Timer will reset the PICmicro when it overflows from FFh00h. The datasheet for the PIC16F887 provides an instruction sequence that should be included within the code when switching the prescaler from Timer0 to the Watchdog Timer to avoid generation of an unintended PICmicro reset.PS bits: Prescaler Rate Select bitsThere are 8 prescaler options for the Timer0 module ranging from 1:2 to 1:256 on the PIC16F887. In order to have a 1:1 value the prescaler will need to be assigned to the Watchdog Timer.The prescaler is not readable or writeable but any instruction that writes to the TMR0 register will clear the prescaler.

Operao do Timer 0 Source Edge Select 1 = increment TMR0 on rising edge0 = increment TMR0 on falling edgeT0CON RegisterTMR0ONT08BITT0CST0SEPSAT0PS2:0T0SET0CKIFOSC/41010ProgrammablePrescalerT0CSPSAT0PS2:T0PS0ClockSyncHigh ByteTMR0IFTMR0LTMR0HREADTMR0LWRITETMR0LDATA BUS 2006 Microchip Technology Incorporated. All Rights Reserved. Slide n202 PRC10Timer0 is mainly controlled by setting bits within the OPTION register.TOCS: Timer0 Clock Source SelectWhen using an external clock source always refer to the electrical specifications of the datasheet to ensure that timing requirements are met.T0SE: Timer0 Source Edge Select bitThis bit provides the option of selecting either the positive or negative going transition edge of the clock source whichever best suits the designPSA: Prescaler Select bitThe prescaler used by Timer0 is also used by the Watchdog Timer.Precautions must be used when switching the prescaler from Timer0 to the Watchdog timer. The Watchdog Timer will reset the PICmicro when it overflows from FFh00h. The datasheet for the PIC16F887 provides an instruction sequence that should be included within the code when switching the prescaler from Timer0 to the Watchdog Timer to avoid generation of an unintended PICmicro reset.PS bits: Prescaler Rate Select bitsThere are 8 prescaler options for the Timer0 module ranging from 1:2 to 1:256 on the PIC16F887. In order to have a 1:1 value the prescaler will need to be assigned to the Watchdog Timer.The prescaler is not readable or writeable but any instruction that writes to the TMR0 register will clear the prescaler.

Operao do Timer 0 T0CON RegisterTMR0ONT08BITT0CST0SEPSAT0PS2:0T0SET0CKIFOSC/41010ProgrammablePrescalerT0CSPSAT0PS2:T0PS0ClockSyncHigh ByteTMR0IFTMR0LTMR0HREADTMR0LWRITETMR0LDATA BUSPrescaler Assignment1= prescaler NOT assigned0= prescaler IS assigned 2006 Microchip Technology Incorporated. All Rights Reserved. Slide n202 PRC11Timer0 is mainly controlled by setting bits within the OPTION register.TOCS: Timer0 Clock Source SelectWhen using an external clock source always refer to the electrical specifications of the datasheet to ensure that timing requirements are met.T0SE: Timer0 Source Edge Select bitThis bit provides the option of selecting either the positive or negative going transition edge of the clock source whichever best suits the designPSA: Prescaler Select bitThe prescaler used by Timer0 is also used by the Watchdog Timer.Precautions must be used when switching the prescaler from Timer0 to the Watchdog timer. The Watchdog Timer will reset the PICmicro when it overflows from FFh00h. The datasheet for the PIC16F887 provides an instruction sequence that should be included within the code when switching the prescaler from Timer0 to the Watchdog Timer to avoid generation of an unintended PICmicro reset.PS bits: Prescaler Rate Select bitsThere are 8 prescaler options for the Timer0 module ranging from 1:2 to 1:256 on the PIC16F887. In order to have a 1:1 value the prescaler will need to be assigned to the Watchdog Timer.The prescaler is not readable or writeable but any instruction that writes to the TMR0 register will clear the prescaler.

Operao do Timer 0 PS2PS1PS0TMR0 RATE0001:20011:40101:80111:161001:321011:641101:1281111:256T0CON RegisterTMR0ONT08BITT0CST0SEPSAT0PS2:0T0SET0CKIFOSC/41010ProgrammablePrescalerT0CSPSAT0PS2:T0PS0ClockSyncHigh ByteTMR0IFTMR0LTMR0HREADTMR0LWRITETMR0LDATA BUSPrescaler Rate Select Bits 2006 Microchip Technology Incorporated. All Rights Reserved. Slide n202 PRC12Timer0 is mainly controlled by setting bits within the OPTION register.TOCS: Timer0 Clock Source SelectWhen using an external clock source always refer to the electrical specifications of the datasheet to ensure that timing requirements are met.T0SE: Timer0 Source Edge Select bitThis bit provides the option of selecting either the positive or negative going transition edge of the clock source whichever best suits the designPSA: Prescaler Select bitThe prescaler used by Timer0 is also used by the Watchdog Timer.Precautions must be used when switching the prescaler from Timer0 to the Watchdog timer. The Watchdog Timer will reset the PICmicro when it overflows from FFh00h. The datasheet for the PIC16F887 provides an instruction sequence that should be included within the code when switching the prescaler from Timer0 to the Watchdog Timer to avoid generation of an unintended PICmicro reset.PS bits: Prescaler Rate Select bitsThere are 8 prescaler options for the Timer0 module ranging from 1:2 to 1:256 on the PIC16F887. In order to have a 1:1 value the prescaler will need to be assigned to the Watchdog Timer.The prescaler is not readable or writeable but any instruction that writes to the TMR0 register will clear the prescaler.

Operao do Timer 0 T0CON RegisterTMR0ONT08BITT0CST0SEPSAT0PS2:0T0SET0CKIFOSC/41010ProgrammablePrescalerT0CSPSAT0PS2:T0PS0ClockSyncHigh ByteTMR0IFTMR0LTMR0HREADTMR0LWRITETMR0LDATA BUSTMR0ON1 = ON 0 = STOPPED 2006 Microchip Technology Incorporated. All Rights Reserved. Slide n202 PRC13Timer0 is mainly controlled by setting bits within the OPTION register.TOCS: Timer0 Clock Source SelectWhen using an external clock source always refer to the electrical specifications of the datasheet to ensure that timing requirements are met.T0SE: Timer0 Source Edge Select bitThis bit provides the option of selecting either the positive or negative going transition edge of the clock source whichever best suits the designPSA: Prescaler Select bitThe prescaler used by Timer0 is also used by the Watchdog Timer.Precautions must be used when switching the prescaler from Timer0 to the Watchdog timer. The Watchdog Timer will reset the PICmicro when it overflows from FFh00h. The datasheet for the PIC16F887 provides an instruction sequence that should be included within the code when switching the prescaler from Timer0 to the Watchdog Timer to avoid generation of an unintended PICmicro reset.PS bits: Prescaler Rate Select bitsThere are 8 prescaler options for the Timer0 module ranging from 1:2 to 1:256 on the PIC16F887. In order to have a 1:1 value the prescaler will need to be assigned to the Watchdog Timer.The prescaler is not readable or writeable but any instruction that writes to the TMR0 register will clear the prescaler.

Timer 1 e Timer 3Temporizadores/Contadores de 16-bitsTemporizador, contador sncrono ou assncronoPode operar a partir de um cristal externoDois registros de escrita/leitura de 8 bitsPrescaler de 1, 2, 4, ou 8Gera interrupo no overflow: 0xFFFF 0x0000 2006 Microchip Technology Incorporated. All Rights Reserved. Slide n202 PRC14Timer 1 e Timer 38-bit Data Bus0101FOSC/4T1OSO/T13CKIT1OSIT1OSCENTMR1CSPrescaler1, 2, 4, 8T1CKPS1: T1CKPS0SyncDetectSleep InputT1SYNCTMR1ONTMR1LTMR1IFClear Timer 1(CCP Special Event Trigger)High ByteTMR1HWRITETMR1LREADTMR1LTimer 1 OscillatorT1CON RegisterRD16T1RUNT1OSCENTMR1CSTCKPS1:0TMR1ONBit 7Bit 0T1SYNC 2006 Microchip Technology Incorporated. All Rights Reserved. Slide n202 PRC15RD16T1RUNT1OSCENTMR1CSTCKPS1:0TMR1ONBit 7Bit 0T1SYNCT1CON / T3CON Register01FOSC/4T1OSO/T13CKIT1OSIT1OSCENTMR1CSPrescaler1, 2, 4, 8T1CKPS1: T1CKPS001SyncDetectSleep InputT1SYNCTMR1ONTMR1LTMR1HTMR1IFClear Timer 1(CCP Special Event Trigger)Timer 1 Oscillator8-bit Data BusTimer 1 e Timer 316-bit Read/Write Mode Enable1 = Read/Write of Timer in one operation0 = Read/Write of Timerin two 8-bit operations 2006 Microchip Technology Incorporated. All Rights Reserved. Slide n202 PRC1616-bit Read/Write Mode Enable1 = Read/Write of Timer in one operation0 = Read/Write of Timerin two 8-bit operationsRD16T1RUNT1OSCENTMR1CSTCKPS1:0TMR1ONBit 7Bit 0T1SYNCT1CON / T3CON RegisterTimer 1 e Timer 38-bit Data Bus0101FOSC/4T1OSO/T13CKIT1OSIT1OSCENTMR1CSPrescaler1, 2, 4, 8T1CKPS1: T1CKPS0SyncDetectSleep InputT1SYNCTMR1ONTMR1LTMR1IFClear Timer 1(CCP Special Event Trigger)High ByteTMR1HWRITETMR1LREADTMR1LTimer 1 Oscillator 2006 Microchip Technology Incorporated. All Rights Reserved. Slide n202 PRC17RD16T1RUNT1OSCENTMR1CSTCKPS1:0TMR1ONBit 7Bit 0T1SYNCT1CON / T3CON RegisterTimer 1 e Timer 38-bit Data Bus0101FOSC/4T1OSO/T13CKIT1OSIT1OSCENTMR1CSPrescaler1, 2, 4, 8T1CKPS1: T1CKPS0SyncDetectSleep InputT1SYNCTMR1ONTMR1LTMR1IFClear Timer 1(CCP Special Event Trigger)High ByteTMR1HWRITETMR1LREADTMR1LTimer 1 OscillatorTimer1 Oscillator Enable1 = T1OSC Enabled0 = T1OSC off 2006 Microchip Technology Incorporated. All Rights Reserved. Slide n202 PRC18RD16T1RUNT1OSCENTMR1CSTCKPS1:0TMR1ONBit 7Bit 0T1SYNCT1CON / T3CON RegisterTimer 1 e Timer 38-bit Data Bus0101FOSC/4T1OSO/T13CKIT1OSIT1OSCENTMR1CSPrescaler1, 2, 4, 8T1CKPS1: T1CKPS0SyncDetectSleep InputT1SYNCTMR1ONTMR1LTMR1IFClear Timer 1(CCP Special Event Trigger)High ByteTMR1HWRITETMR1LREADTMR1LTimer 1 OscillatorTimer1 Clock Source1= External Clock0 = Fosc/4 2006 Microchip Technology Incorporated. All Rights Reserved. Slide n202 PRC19RD16T1RUNT1OSCENTMR1CSTCKPS1:0TMR1ONBit 7Bit 0T1SYNCT1CON / T3CON RegisterTimer 1 e Timer 38-bit Data Bus0101FOSC/4T1OSO/T13CKIT1OSIT1OSCENTMR1CSPrescaler1, 2, 4, 8T1CKPS1: T1CKPS0SyncDetectSleep InputT1SYNCTMR1ONTMR1LTMR1IFClear Timer 1(CCP Special Event Trigger)High ByteTMR1HWRITETMR1LREADTMR1LTimer 1 OscillatorTimer1 Prescale Select11 = 1:810 = 1:401 = 1:200 = 1:1 2006 Microchip Technology Incorporated. All Rights Reserved. Slide n202 PRC20Timer 1 e Timer 38-bit Data Bus0101FOSC/4T1OSO/T13CKIT1OSIT1OSCENTMR1CSPrescaler1, 2, 4, 8T1CKPS1: T1CKPS0SyncDetectSleep InputT1SYNCTMR1ONTMR1LTMR1IFClear Timer 1(CCP Special Event Trigger)High ByteTMR1HWRITETMR1LREADTMR1LTimer 1 OscillatorTimer1 System Clock StatusRead Only Bit1 = Device clock derived from Timer1 OSC0 = Device clock derived from another sourceRD16T1RUNT1OSCENTMR1CSTCKPS1:0TMR1ONBit 7Bit 0T1SYNCT1CON / T3CON Register 2006 Microchip Technology Incorporated. All Rights Reserved. Slide n202 PRC21Timer 1 e Timer 38-bit Data Bus0101FOSC/4T1OSO/T13CKIT1OSIT1OSCENTMR1CSPrescaler1, 2, 4, 8T1CKPS1: T1CKPS0SyncDetectSleep InputT1SYNCTMR1ONTMR1LTMR1IFClear Timer 1(CCP Special Event Trigger)High ByteTMR1HWRITETMR1LREADTMR1LTimer 1 OscillatorTimer1 External Clock SynchronizationBit only used when TMR1CS = 11 = Synchronize external clock input0 = Do not synchronizeRD16T1RUNT1OSCENTMR1CSTCKPS1:0TMR1ONBit 7Bit 0T1SYNCT1CON / T3CON Register 2006 Microchip Technology Incorporated. All Rights Reserved. Slide n202 PRC22Timer 1 e Timer 30101FOSC/4T1OSO/T13CKIT1OSIT1OSCENTMR1CSPrescaler1, 2, 4, 8T1CKPS1: T1CKPS0SyncDetectSleep InputT1SYNCTMR1ONTMR1LTMR1IFClear Timer 1(CCP Special Event Trigger)High ByteTMR1HWRITETMR1LREADTMR1LTimer 1 OscillatorT1CON / T3CON RegisterRD16T1RUNT1OSCENTMR1CSTCKPS1:0TMR1ONBit 7Bit 0T1SYNCTimer On 1 = Enables Timer0 = Stops Timer8-bit Data Bus 2006 Microchip Technology Incorporated. All Rights Reserved. Slide n202 PRC23Lab 3Trabalhando com contadoresV3.2 August 10, 2007 2006 Microchip Technology Incorporated. All Rights Reserved. Slide n202 PRC24Lab 3ObjectivesUpon completion of Lab 3 you should understand:Understand TIMER configuration for counter modeUnderstand TMR1 external crystal modeUnderstand Frequency measurement techniques 2006 Microchip Technology Incorporated. All Rights Reserved. Slide n202 PRC25The goal of lab 1 is to learn how to use external interrupts (INT0) and how to configure and use I/O ports and individual I/O pins.Lab 3Application OverviewImplementar um real time clock (RTC) com um segundo de resoluo e que mostre o tempo no terminal (obs, usando interrupo do timer1).O boto S3 ser usado para ajuste das horas e o boto S2 para o ajuste dos minutos.PIC18F4520S3RB011TXRXTerminal470W0.1mFS2470W4.7kW4.7kWRA4 2006 Microchip Technology Incorporated. All Rights Reserved. Slide n202 PRC26LCD and code to set time is provided. Student will Equao:Interrupt Interval (sec) = ( Prescaler / Clock Frequency ) * (216 preloaded value)

Para o RTC, necessrio uma interrupo por segundo.

Determine o valor inicial para o Timer1 (neste caso, usando um cristal externo de 32,768kHz):1 sec = ( 1 / 32.768 kHz ) * (65536 preloaded value)Preload Value = 32768 = 0x8000Lab 3Calculo do valor inicial do Timer 1 2006 Microchip Technology Incorporated. All Rights Reserved. Slide n202 PRC27Lab 3Main LoopInitialize LCDInitialize I/O PinsDisplay Start MessageSTARTConfigure TIMER1Enable InterruptsClear TMR1IFPreload TIMER1SupportFnFlag=1?NoSupportFnDisplay TimeClear FlagIs S2Pressed?Display MessageSet TimeNoYesYes 2006 Microchip Technology Incorporated. All Rights Reserved. Slide n202 PRC28Lab 3TIMER1 ISRSecs >=60?NoTMR1ISRIncrement SecsHours = 1Increment HoursMins = 00Reload TMR1HNoYesYesIncrement MinsSecs = 00Mins >=60?Hrs >=13?Clear TMR1LFlag = 1RETURNNo 2006 Microchip Technology Incorporated. All Rights Reserved. Slide n202 PRC29Lab 3Resultados esperadosTerminal mostrando a mensagem no formato HH:MM:SS 2006 Microchip Technology Incorporated. All Rights Reserved. Slide n202 PRC30