The Silicon Laboratories C 8051 F 020 Enhanced
The Silicon Laboratories C 8051 F 020 Enhanced 8051 Part 2 Timers I ECE/CS-352: Embedded Microcontroller Systems
SFRs for Subsystems • Control registers: – XXXCN (WDTCN, ADC 0 CN, DAC 0 CN, TMR 3 CN) • Configuration registers: – XXXCF (ADC 0 CF, AMX 0 CF, EMI 0 CF) • Data registers for subsystems: – ADC 0 H, ADC 0 L, DAC 0 H, DAC 0 L • Many others. . . ECE/CS-352: Embedded Microcontroller Systems
System Clocks • Internal clock oscillator – default at reset – 2 MHz default frequency – configured by SFR OSCICN • External oscillator installed on board – 22. 1184 MHz – configured by SFR OSCXCN ECE/CS-352: Embedded Microcontroller Systems
Internal Osc Configuration OSCICN Missing clock detector enable Internal Osc. frequency system clock ready select 0 = internal 1 = external (internal by Sysclk default) Default f = 2 MHz T = ? ? Internal Osc. freq. control bits 00 – 2 MHz 01 – 4 MHz 10 – 8 MHz 11 – 16 MHz Internal Osc. enable (enabled by default) ECE/CS-352: Embedded Microcontroller Systems
External Osc. Configuration OSCXCN External Osc. mode bits Crystal Osc. valid flag 00 x: Off. XTAL 1 pin is grounded internally. 010: System Clock from External CMOS Clock on XTAL 1 pin. • External Osc. freq. control bits • Depends on frequency range of operation 011: System Clock from External CMOS Clock on XTAL 1 pin divided by 2. 10 x: RC/C Oscillator Mode with divide by 2 stage. 110: Crystal Oscillator Mode 111: Crystal Oscillator Mode with divide by 2 stage. ECE/CS-352: Embedded Microcontroller Systems
Development Board Oscillator ECE/CS-352: Embedded Microcontroller Systems
To use 22. 1184 MHz external oscillator 110: Crystal Oscillator Mode 111: f > 6. 7 MHz Step 1. Configure the external oscillator. Step 2. Wait at least 1 ms. Step 3. Poll for XTLVLD => ‘ 1’. Step 4. Switch the system clock to the external oscillator. ECE/CS-352: Embedded Microcontroller Systems
Instruction Timing • • C 8051 F 020 – Pipelined architecture Machine cycle = clock cycle Most instructions take 1 or 2 clock cycles Total of 109 distinct instruction types • Summary of instruction timing: ECE/CS-352: Embedded Microcontroller Systems
Delay Loops djnz A, $ clr A ; 2 cycles if no jump, ; 3 cycles if jump ECE/CS-352: Embedded Microcontroller Systems
Sample Configuration • Use 22. 1184 MHz external oscillator Main: mov WDTCN, #0 deh mov WDTCN, #0 adh mov OSCXCN, #67 h clr A djnz A, $ ; disable watchdog timer ; enable external crystal ; oscillator at 22. 1184 MHz ; wait at least 1 ms ; wait ~510 us (255 x 4 x 0. 5 us) = 510 us) ; wait ~510 us osc_wait: ; poll for XTLVLD-->1 mov a, OSCXCN jnb acc. 7, osc_wait orl OSCICN, #08 h ; select external oscillator as ; system clock source ECE/CS-352: Embedded Microcontroller Systems
Timers • Original 8051 has 2 Timers, T 0 and T 1 • Can think of timers as binary counters • Clock is derived from system clock (frequency is configurable) • Timer registers can be read via mov instructions • Events (interrupts or flag bits set) occur when timers overflow • Can be used to create waveforms, measure time intervals, set frequency of events, etc. ECE/CS-352: Embedded Microcontroller Systems
C 8051 F 020 Timers Size of timers can be configured. • Timers can count from arbitrary initial value with the “Auto-reload” feature. • “Capture” allows the timer to be “frozen” when an event occurs. • Timers 2 and 4 can be used to set baud rate for uarts. ECE/CS-352: Embedded Microcontroller Systems
Timer Clock Sources CKCON: Clock Control Register Timer 4 Timer clock selects Timer 0 0 = system clock 12 (for compatibility with original 8051, and for slow stuff) 1 = system clock ECE/CS-352: Embedded Microcontroller Systems
Configuring Timers • TCON and TMOD used to configure Timer 0 and Timer 1 TCON: Timer Control Register Timer 1 Overflow Flag Timer 1 run control 0 = disable Timer 1 1 = enable Timer 1 These deal with external interrupts ECE/CS-352: Embedded Microcontroller Systems
Configuring Timers TMOD: Timer Mode Register Timer 1 Timer 0 GATE 1: 0 = timer enabled with TR 1 only 1 = timer enabled only when TR 1=1 and INT=1 C/T 1: 0 = timer incremented by clock input 1 = counter incremented by edge on external T 1 pin ECE/CS-352: Embedded Microcontroller Systems
Using Timers For Periodic Events Using Polling • Set mode to be timer – use appropriate clock and timer size • In main program – Check for overflow flag (polling) – When overflow flag occurs • Do event • Reset timer overflow flag • Reset timer initial value (unless using auto-reload or initial value of 0 is OK) • Continue checking ECE/CS-352: Embedded Microcontroller Systems
Using Timers For Periodic Events Using Interrupts • Set mode to be timer, use appropriate clock and timer size • Set timer to interrupt on overflow • In Interrupt Service Routine – Do event – Reset interrupt flag – Reset timer to initial value (unless using autoreload) – Return from interrupt ECE/CS-352: Embedded Microcontroller Systems
Using Timers as Counter • Set mode to be counter, with appropriate size (8, 13 or 16 bits) • Initialize counter to zero • Enable counter • Counter can be read and reset by main program. ECE/CS-352: Embedded Microcontroller Systems
Using Timers to Measure Time Using Timers: – Configure timers with mode 0 or 1 (13 or 16 -bit timer) and desired system clock. – In main program, wait for initial event. – Start the timer. – Each time the timer overflows, a register should be incremented using an interrupt service routine. – When the second event occurs, disable the interrupts and the timer in the main program. ECE/CS-352: Embedded Microcontroller Systems
Configuration Tool • Fortunately, there is a tool to help configure all the subsystems in the 8051. • This makes things a lot easier. • To access the tool, go to Silicon Labs – Configuration Tool 2 ECE/CS-352: Embedded Microcontroller Systems
- Slides: 20