Microchip Technology DV164139 Data Sheet

Page of 54
© 2009 Microchip Technology Inc.
DS41356B-page 23
FIGURE 2-5:
STATE DIAGRAM FOR KEYBOARD()
Referring to the state diagram, the individual states perform these general tasks:
1.
STATE 0: this is a delay state used to ensure that the hold capacitor on the ADC 
peripheral has sufficient time to charge before a conversion is initiated. 
2.
STATE 1: this state begins the conversion process by setting the GO_DONE bit 
in the ADCON0 control register. 
3.
STATE 2: checks for a completed conversion (GO_DONE = 0) before allowing 
the state machine to move to the next state. 
4.
STATE 3: this final state reads the ADC result register, converts the result to a 
value between 4 (‘a’) and 29 (‘z’), loads a HID buffer and transmits the resulting 
data along the USB to the Host PC for output to an opened .txt document. 
The main point the user should take away from this lab is this: since the USB Frame-
work is a multitasking environment, no blocking code should be used. Therefore, as 
shown in this lab, state machines will become the norm in many applications. The 
keyboard()
 state machine is called each time through the main loop. If the condition 
that changes the current state to the next state is not met, the state machine simply 
returns from the function without changing states. The next time through the main 
loop, the condition is once again checked. If the state condition has been met, the 
state is changed to the next sequential state. The state variable is declared as a type 
static 
as this will allow the current value in the state variable to remain after a return 
from the keyboard().
STATE
0
STATE
1
STATE
2
STATE
3
delaycounter < 9000
delaycounter > 9000
Start ADC Conversion
(Set the GO_DONE bit
While GO_DONE = 1
(ADC Conversion in Progress)
GO_DONE = 0 (Cleared in Hardware)
• Read ADC result register
• Load HID buffer
• Transmit HID buffer contents along USB