3.4.1 Hardware State Machine Example - Candy Dispenser
For example, consider a simple Moore State Machine, as shown in Figure 3-3 operating a coin-based candy machine. Figure 3-4 shows a pictorial representation of the state machine and the circuit-level implementation using the Signal Routing Port and CLC peripheral. Hardware State Machines with Signal Routing Ports shows the Signal Routing Port setup.
Tip: In devices where the signal
routing port pin output is available as a PPS output option, the RW0 output can be routed directly
to the physical I/O pin, eliminating the need for the CLC2 peripheral in this example.
Hardware State Machines with Signal Routing Ports
// Input to SRPORT: CLC1 Output (next state) // Output from SRPORT: PPS input to CLC2 (current state) // Note: Initialization routines are not shown void HardwareStateMachine_SRPORT() { // Set RW0 input as CLC1 PORTWIN0 = 0x08; // 0x08 = CLC1 on PIC18F46Q71 // Set RW0 as feedback input CLCIN0 for CLC1 CLCIN0PPS = 0x38; // 0x38 = RW0 on PIC18F46Q71 // Optional: Set RW0 as current state input CLCIN1 for CLC2 // Required only in devices that do not have RW0 routed through PPS output CLCIN1PPS = 0x38; // 0x38 = RW0 on PIC18F46Q71 // Enable flip-flop on RW0 PORTWDFbits.DF0 = 1; // Select clock PORTWCLK = 0x00; // 0x00 = Fosc on PIC18F46Q71 // Initialize state machine to a default state PORTW = 0x01; // Locked state (L=1) // Enable clock to start state machine PORTWCONbits.CLKEN = 1; }