The Silicon Laboratories C8051F020 Enhanced 8051 Part 2 Timers I ECE/CS-352: Embedded Microcontroller Systems
ECE/CS-352: Embedded Microcontroller Systems SFRs for Subsystems Control registers: XXXCN (WDTCN, ADC0CN, DAC0CN, TMR3CN) Configuration registers: XXXCF (ADC0CF, AMX0CF, EMI0CF) Data registers for subsystems: ADC0H, ADC0L, DAC0H, DAC0L Many others..... ECE/CS-352: Embedded Microcontroller Systems
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 ready Internal Osc. freq. control bits 00 – 2MHz 01 – 4 MHz 10 – 8 MHz 11 – 16 MHz system clock select 0 = internal 1 = external (internal by default) Default Sysclk f = 2 MHz T = ?? Internal Osc. enable (enabled by default) ECE/CS-352: Embedded Microcontroller Systems
External Osc. Configuration OSCXCN External Osc. freq. control bits Depends on frequency range of operation External Osc. mode bits 00x: Off. XTAL1 pin is grounded internally. 010: System Clock from External CMOS Clock on XTAL1 pin. 011: System Clock from External CMOS Clock on XTAL1 pin divided by 2. 10x: RC/C Oscillator Mode with divide by 2 stage. 110: Crystal Oscillator Mode 111: Crystal Oscillator Mode with divide by 2 stage. Crystal Osc. valid flag 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
ECE/CS-352: Embedded Microcontroller Systems Instruction Timing C8051F020 – 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 clr A djnz A, $ ; 2 cycles if no jump, ; 3 cycles if jump ECE/CS-352: Embedded Microcontroller Systems
ECE/CS-352: Embedded Microcontroller Systems Sample Configuration Use 22.1184 MHz external oscillator Main: mov WDTCN, #0deh ; disable watchdog timer mov WDTCN, #0adh mov OSCXCN, #67h ; enable external crystal ; oscillator at 22.1184MHz clr A ; wait at least 1ms djnz A, $ ; wait ~510us (255 x 4 x 0.5us) = 510 us) djnz A, $ ; wait ~510us osc_wait: ; poll for XTLVLD-->1 mov a, OSCXCN jnb acc.7, osc_wait orl OSCICN, #08h ; select external oscillator as ; system clock source ECE/CS-352: Embedded Microcontroller Systems
ECE/CS-352: Embedded Microcontroller Systems Timers Original 8051 has 2 Timers, T0 and T1 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
ECE/CS-352: Embedded Microcontroller Systems C8051F020 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
ECE/CS-352: Embedded Microcontroller Systems Timer Clock Sources CKCON: Clock Control Register Timer0 Timer clock selects 0 = system clock 12 (for compatibility with original 8051, and for slow stuff) 1 = system clock Timer4 ECE/CS-352: Embedded Microcontroller Systems
ECE/CS-352: Embedded Microcontroller Systems Configuring Timers TCON and TMOD used to configure Timer0 and Timer1 TCON: Timer Control Register These deal with external interrupts Timer 1 Overflow Flag Timer 1 run control 0 = disable Timer 1 1 = enable Timer 1 ECE/CS-352: Embedded Microcontroller Systems
ECE/CS-352: Embedded Microcontroller Systems Configuring Timers TMOD: Timer Mode Register Timer 1 Timer 0 GATE1: 0 = timer enabled with TR1 only 1 = timer enabled only when TR1=1 and \INT=1 C/T1: 0 = timer incremented by clock input 1 = counter incremented by edge on external T1 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 auto-reload) 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 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
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