upc eetac_1


Project P10: Let's program a μC in C like a FSM


6-bit Johnson counter

1. Specifications

- Implement a 6-bit Johnson counter using a microcontroller chip. The program is organised mimicking a FSM  and interrupt-driven to attend edge-triggered inputs from pushbuttons or other external digital signals.

Well, as you see, it looks that simply once again we are revisiting the same circuits from Chapter 2. However, it is not only so, because in the end, comprehending correctly the basic behaviour of such simple systems will give you a great advantage when considering larger and far more complex systems. This P10 is like the key to open the door of professional embedded microcontroller applications (data logging, signal acquisition and distribution, motors, LED lamps control, robotics, etc.).


Fig 1. Circuit to be designed using interrupts to detect the external CLK edge transitions (visio).

- 6-bit reversible Johnson counter (UD_L signal from a switch)

- Count enable (CE signal from a switch)

- Start/stop (ST_SP) pushbutton to trigger the operation and to stop it once the sequence has ended. The circuit is sensitive to the falling edge of this signal.

- The counter advances synchronously on the falling edge of an external CLK input of 2.5 Hz.

- TC12 (terminal count) is an squared signal which is high in half of the states and low in the other half.

Follow the discussion in class (1), (2) in class to fully understand the specifications and be able to infer a plan.

Learning materials and tutorials:

Study carefully these tutorials and run them in Proteus to see how the system works and can be programmed taken advantage of all your previous knowledge.

- BCD counter_1digit (PIC18F4520). Plan X: current_state is an state enumeration (for design phases #1 and #2)

- Counter_mod256 (PIC18F4520). Plan Y: current_state is an arithmetic vector variable, not an state enumeration.

- This a tutorial example (PIC16F877A) on the use of the Port B and external INT interrupts. Class discussion (1), (2). This is the adaptation when using this demonstration board.

- On the design of the 74HCT4017 chip (5-bit Johnson /one-hot counter) (PIC16F877A) .

This is a list of some more introductory topics on microcontrollers. Pay attention to the idea of external interrupt and how they are handle by the μC in hardware (INT0IF) and by our application in software (Var_CLK_Flag).


2. Planning

Some initial notes on a feasible plan that, as you see, mixes hardware and software. 

1) Project locations and file names. Place the project folder in your Samba drive L:


2) Organise the hardware. Draw your circuit in sheets of paper and discuss where to connect: Reset (CD), CLK oscillator, digital I/O and push-buttons. A good idea is to connect inputs and outputs in the free pins of a development board like the PICDEM2 Plus, in case you would like to prototype the application.

3) Organise the C code using a FSM-like program flowchart: Init_system(), output_logic(), state_logic(), ISR() (interrupt service routine). Design the hardware-dependent functions read_inputs(), write_outputs(). Define the RAM internal variables that will allow the FSM sequencing using convenient flags, like CLK_flag and ST_Flag.  Class notes: (1), (2), (3).

Fig. 2. General interrupt-driven FSM-style program organisation (visio).

4) Plan a sequence for building and debugging the application: as is P9 the idea is "plan & develop & test" step by step enhancing the initial state diagram with a new features at a time. For instance: 1) solve a basic up Johnson counter with an Start/Stop button; 2) add the up/down switch; etc. 


3. Development

1) Draw the schematic of the application in Proteus copying and adapting an example or tutorial.

Fig. 3. The counter as captured in Proteus.

2) Run the microcontroller's IDE to develop and compile the C code copying and adapting an example from a tutorial. Do it section by section according to your plan, testing if it works before adding new code.


4. Testing 

 Run the Proteus simulator. Do it in step by step mode while watching variables and placing break points, specially to follow the interrupt flags.


Fig. 4.  The circuit in "run" mode while monitoring the variables in the "watch" window.


5. Report

Your class participation is required adopting an active attitude; it is highly recommended you engage in the project discussions, rising questions and giving answers.

Project report starting with the template sheets of paper, scanned figures, file listings, docx , pptx, or any other resources.  

Remember that at this point in the course, you'll have to be able to to explain any section of your project individually or in group using sketches, diagrams, and other engineering tools. 


6. Prototyping

You're invited to download the application to a given training board an verify that it works as expected and the same as in the simulator.


Other similar projects on sequential circuits

Books, web pages, etc.

- 16-key matrix encoder and many other uC applications can be solved using similar approaches .

- Exams, questions, problems and projects

Other materials of interest

Hundreds of books, thousands of web pages, videos, webinars, slides, etc. on microcontrollers.