Microprocessor Chapter 9 :
- Two timers, 0 and 1
- Use as timers or event counters
- Both are 16 bits wide

Timer 0 Registers
- Low byte TL0, high byte TH0
- Accessed like any other register

Timer 1 Registers
- Low byte TL1, high byte TH1
- Accessed in same way as Timer 0

TMOD (timer mode) Register
- Sets operation modes for timer 0 and 1
- Lower 4 bits for Timer 0, upper 4 bits for Timer 1
- Lower 2 bits set timer mode, upper 2 bits specify operation
- M1,M0: Select timer mode.  
	Mode 0: 13 bit timer
	Mode 1: 16 bit timer
	Mode 2: 8 bit timer
- C/T (clock/timer), Decide whether timer is used as delay generator or event counter
- C/T = 0, used as timer for delay generation
- Clock source for time delay is crystal frequency of 8051

Clock source for timer:
- C/T = 0, crystal frequency attached to 8051 is source of clock for timer
- Frequency of timer determines speed at which timer ticks
- Frequency of timer is 1/12th frequency of crystal attached to 8051
- XTAL frequency of 11.0592 allows 8051 to communicate with IBM PC with no errors

GATE:
- Start and stop of timer controlled by software by TR (timer start) bits TR0 and TR1.  
- Achieved with instructions SETB TR1 and CLR TR1 for timer 1,  SETB TR0 and CLR TR0 for timer 0.
- SETB starts timer, CLR stops timer
- These instructions are what start and stop the timer and long as GATE = 0 in TMOD
- Hardware means of starting and stopping timer done by making GATE = 1 in TMOD

Mode 1 Programming:
1. 16 bit timer
2. Started with SETB TRx
3. Timer counts up until it reaches limit of FFFFH.  When rollover back to 0000 occurs, TF (timer flag) set, giving the option of stopping timer
4. For process to be repeated after rollover to 0000, timer must be reloaded with original value

Programming in Mode 1:
1.  Load TMOD value to choose timer 0 or 1 and mode.
2. Load registers TL and TH with initial count values
3. Start timer
4. Continuously monitor timer flag (TF) with "JNB TFx, target" instruction.  End loop when TF becomes high.
5. Stop timer
6. Clear TF flag
7. Go back to step 2

Finding values to load into timer:
- Use crystal frequency of 11.0592 MHz
1. Divide desired time delay by 1.085 uS
2. Perform 65536-n 
3. Convert result to hex
4. Set TL to low bits and TH to bits

Generating a large time delay:
- Size of time delay depends on crystal frequency and timers 16 bit register value
- Largest value of delay made by making TH and TL zero

Mode 0:
- Same as mode1 except there is a 13 it timer instead of 16

Mode 2: 
1. 8 bit timer, loads values 00 to FFH
2. When TH loaded, value copied to TL.  Timer started using SETB TRx
3. Count upward until limit reached, flag set when rolling over from FFH to 00.  
4. TL is reloaded automatically with the value kept in TH when rollover occurs.  Only TF has to be reset.

- Only 8 bits, has auto-reload

Steps to Program in Mode 2:
1. Load TMOD value to choose timer
2. Load TH with initial count value
3. Start timer
4. Monitor TF with "JNB TFx, target" , breaking look when TFx is set
5. Clear TF
6. Go back to Step 4

COUNTER PROGRAMMING: 
- For use as counter, pulse outside 8051 used to increment the TH and TL registers
- All registers and modes are the same though

C/T bit in TMOD register:
- C/T bit chooses source of clock for timer
- When C/T = 1, timer is counter, gets pulses from outside 8051
- Counter increases when pulses fed from pins 14 and 15
- These pins are T0 (timer 0 input) and T1 (Timer one input)
- They are put of port three, 3.4 and 3.5
- We can feed a square wave into the timer to generate second, minute, and hour out of input
- Using interrupts avoids use of "JNB TFx, target" instruction
- This allows microcontroller to perform other operations until TF flag is raised
- TR0 and TR1 belong to TCON register (timer control register)

TCON register: 
- Upper 4 bits of TCON register used to store TF and TR bits for timer 0 and 1
- Lower four bits set interrupt bits
- TCON register is bit-addressable

GATE = 1 in TMOD
- If GATE = 1, start and stop of timer done externally using pins P3.2 and P3.3 for timer 0 and 1

Calculating Delay length using timers:
 - Delay depends on a) crystal frequency b) number of clocks per machine cycle c) C compiler 
- Original 8051 usd 1/12 oscillator frequency as one machine cycle
- Time to execute a 8051 instruction is 1 or 2 machine cyclics 
- Some newer versions have reduced number of clocks per cycle to 4 or 1
- Duration of time to execute an instruction varies, but they all use 1/12th crystal oscillator frequency

Microprocessor Chapter 10: 8051 Serial Port Programming in Assembly and C
- Parallel: 8 or so wires transfer data to device a few feet away
- Serial: Data sent one bit at a time

Basics of Serial Communication:
- Sychronous: Transfers a block of data (characters) at a time, 
- Asynchronous: Trasnfers single byte at a time
- UART: Universal asynchronous Receiver-transmitter, and USART used to transfer data in these modes

Half and Duplex Transmission
- If data can be transmitted and received, it is a duplex transmission.
- SImplex transmissions, computer only sends data
- half duplex: Data transmitted one way at a time
- full duplex: Data transmitted both ways at the same time

Asynchronous Serial Communication and Data Framing
- SInce data received is all 0's and 1's, there has to be protocol on how data is packed, bits per character, start and end of data

Start and Stop Bits: 
- Asynchronous serial data communication used for character oriented transmissions
- Block oriented data uses synchronous
- Asynchronous: Places all data between start and stop bits
- Start is always one bit, always low.  Stop can be one or two bits, always high.
- LSB is sent first
- When there is no transfer, signal is high.  

Serial Port Programming in Assembly
- Baud rate of 8051 must match that of PC's COM port

Baud Rate in the 8051
- Baud rate in 8051 is programmable
- Crystal frequency divided by 12 gives machine cycle frequency
- UART divides machine cycle frequency by 32 before it is used by Timer 1 to set baud rate.
- For 11.0592 MHz crystal frequency, baud rate is 28,800 Hz
- Timer 1 must be programmed in mode 2
- 28,800 is divided by number in TH1 to get baud rate.  

SBUF register
- 8 bits used only for serial communication
- Byte must be placed in SBUF to be transferred and also holds data transferred from RxD line

SCON (Serial Control) Register
- 8 bit register, programs start bit, stop bit, and data bits 
- SM0 and SM1 set mode to frame data.  We only care about mode 1 (SM0=0, SM1=1)
- REN (SCON.4) allows 8051 to receive data from RxD pin when bit is high, we must set it (SETB SCON.4)
- TB8=0
- RB8=0
- TI(transmit interrupt) raises flag when a byte has been transferred and it is ready to send another byte. 
- RI(Receive interrupt) Raised when byte has been received and it is ready to receive another

Programming 8051 to Transfer Data Serially
1) TMOD=20H (Timer 1 in mode 2)
2) TH1 loaded with values specified in table to set baud rate
3) SCON loaded with 50H for serial mode 1
4) TR1 set to 1 to start timer
5) TI cleared by "CLR TI"
6) Character byte to be transferred written to SBUF
7) TI flag monitored with "JNB TI,xx"
8) Go back to step 5 to send next character

Importance of TI Flag
Steps 8051 does to transmit character via TxD:
1) Byte to be transmitted written to SBUF
2) Start bit transferred
3) 8 bit character transferred one bit at a time
4) Stop bit transferred, raises TI flag.
5) Monitoring TI flag prevents overloading SBUF register, if another byte is written info will be lost.  
6) Before SBUF is loaded with another bye, TI flag must be cleared with "CLR TI"

Programming 8051 to Receive Data Serially
1) TMOD=20H, Timer 1 in mode 2
2) TH1 loaded with value from table to set baud rate.
3) SCON loaded with 50H, serial mode 1.  
4) TR1 set to 1 to start timer
5) RI  is cleared w/CLR RI
6) RI flag monitored with JNB RI,xx
7) When RI raised, contents moved to a safe register
8) Go back to step 5 to receive next character. 

Doubling Baud Rate
MOV A, PCON
SETB ACC.7
MOV PCON A

- When SMOD=0, machine cycle frequency is divided by 32 to get baud rate
- When SMOD=1, machine cycle frequency is divided by 16 to get baud rate 
- Baud rate will be doubled for same TH1 values when SMOD is 1