Recent Changes - Search:

Introduction to Microcontroller Programming

About PICmicro Chips

Clocking Your PICmicro Devices


Flowcode Step By Step

PICmicro Projects


Lab 7 - Decision

1. Introduction

We have already done some kind of simple decision making with loops and connection points, but this is the real thing. The Decision icon is widely known as ‘IF…THEN…ELSE’ – structure and is without any doubt the most used command line in any program in the world. You will experience that programs get more powerful and complex very fast now!

2. Setting up the equipment
3. Hardware settings
EB006 OptionsSettingJumper settings (EB006)Jumper settings (HP488)
Power supplyExternal, 14VJ29: PSUJ29: PSU
PICmicro device16F1937  
Programming methodUSBJ12,13,14: USBJ20: USB
Clocking methodXTALSW2: XTALS2: XTAL
R/C clock speed   
Xtal frequency19 660 800 Hz  
LVP Jumper selectionI/O PortJ11,16,17: I/O PortJ15,16,18: I/O
Port A E-block   
Port B E-blockLED board  
Port C E-blockSwitch board  
Port D E-block   
Port E E-block   
4. Flowcode and download settings
Build > Project Options... > General Options Build > Project Options... > Configure
Clock speed19 660 800 Hz
Simulation speed10
Watchdog timerOff
5. Software learning objectives

Input, output, binary code training, Decisions, If then else structures, making use of temporary memory.

6. Hardware learning objectives

LED’s, logic output levels, Multiprogrammer basics.

7. Instructions

Construct the system shown from E-blocks.

In the course navigate to the ‘Flowcode step-by-step’ and review the section on Decisions (step 7) and 'Goto' Connection Point (step 8) . The descriptions of the Multiprogrammer board, the switch board and the led board are in the E-blocks section.

Make use of the 'Help' menu and function in Flowcode to get the info you need.

Exercises in these labs look quite complex the first time you read them. Take your time to divide the big task into smaller sub-tasks and work them out first. Only then, can you start putting it all together. This is the only correct way to solve these kinds of complex problems, in software or in other technical areas.

8. Labs
Letter Meaning
L Lab x
B Basic complexity
I Intermediate complexity
E Expert complexity
Make a program in Flowcode that ‘inverses’ the relation between the switches and the LEDs. If SW0 is pressed, then LED7 lights up, If SW1 is pressed, then LED6 lights up, and so on...
Remember how you made an 8-bit counter using loops. Make an 8 bit counter that counts up till 255 and back down from 255 to 0 and keeps on doing this, using Decision icons instead of Loop icons.
Use the Decision icon and write a program that counts up (to a variable called ‘count’) when SW0 is pressed and counts down when SW1 is pressed. The status of the variable ‘count’ should be shown on the LEDs of PORT B. Save this program as [L7-B3-G], download it to the PIC.
Use the Decision icon to write a program that counts up when SW0 is pressed and counts down when SW1 is pressed. The difference with the program above should be that this program uses a 16-bit counter. Use 2 sets of LEDs, one on port C and one on port D, that represent the 16 bits. The switches should be connected to port B. Make only use of normal 8-bit variables in this program. Save this program as [L7-B4-G], download it to the PIC.
Make a program that makes all the LEDs of Port B flash (or blink) at a frequency of 1Hz (This means that the total period, on-off, is...). If SW0 is pressed, then the LEDs should blink faster, if SW1 is pressed, then the LEDs should blink slower. Download this program to the PIC and test it. Can you point out some kind of relevancy between the blinking frequency and the reaction time to the switch that is pushed. You will learn how this is fixed in a better way in the chapter about external interrupts. Save this program as [L7-I5-G] and ask your teacher to come and grade this. Prepare yourself for questions about frequency, period and the changing reaction time to the switch.
Make a program so that if SW0 is pressed once, then all LEDs of port B light up. If the same SW0 is pressed again, then all LEDs should turn off again. You'll need some kind of memory to solve this problem. Download this program to the PIC and test it.
In a certain car, there are 2 lights to light the interior. The first (here LED0) is in the front of the car, the second (here LED1) is in the back of the car. There are 5 switches that control these 2 lights: SW0-SW3 are the switches that indicate if one of the 4 doors is opened or not. SW4 indicates if the trunk is opened or not. Let's suppose that the switches are closed if the doors are open. This makes it easier for us to simulate with our 'push to make' switches. When one of the 4 car doors is opened, both lights need to light up. When the trunk is opened, only the trunk-light needs to light up. Download this program to the PIC and test it. Save this program as [L7-B7]
Same program [L7-B7], but now when the last door closes, the lights should stay on for 5 more seconds. Download this program to the PIC and test it.
This car has external-lights too and they are all controlled by a few switches on the steering wheel. SW0 starts the left direction indicator (LED0). This will blink with 250msec intervals until SW0 is released. The same goes for SW1, that controls the right direction indicator (LED1). SW2 controls the brake lights (LED2-LED3). The brake-lights will light up as long as SW2 is pressed. SW3 controls the Head lights (LED4-LED5). One push of SW3 will activate these lights. They will stay activated until SW3 is pushed again. The same story for the Fog-lights (LED6-LED7) which are controlled by SW4. Do not attempt to write this program all at once. Divide it into small sub-problems and try to solve them first before you put them all together. Download this program to the PIC and test it. Hint- to make writing your program easier use the labelling feature of Flowcode to label switches and LEDs.
This is an exercise that counts how many sheep are in both paddocks at all times. The maximum can not exceed 6 and is always shown in binary form on the LEDs of port B. The 4LSB’s are for the number of sheep in the left paddock and 4MSB’s are for the number of sheep in the right paddock. SW0 can be used to simulate a '1' for Sensor 1 and SW1 can be used to simulate Sensor 2. Be aware of sheep that turn back before they are in the other paddock. This is why we need 2 sensors. A sheep is longer than the distance between the 2 sensors. Don't fall asleep! Download this program to the PIC and test it.

Print - Search - Login

Page last modified on May 14, 2013, at 02:57 PM