Recent Changes - Search:

Introduction to Microcontroller Programming

About PICmicro Chips

Clocking Your PICmicro Devices

E-Blocks

Flowcode Step By Step

PICmicro Projects

Labs

Port B Interrupts

<^< Set up the Equipment | Course Index | Setting up the LCD >^>

There are two more common interrupts that you should understand: Port B bit 0, and Port B change.

The Port B bit 0 interrupt will trigger a given macro every time the RB0 line on the PICmicro device goes to a logic 1 or 5V. The Port B change interrupt triggers every time there is a change of state on the highest 4 bits of Port B: bits RB4, 5, 6, 7. The Port B bit 0 interrupt is straight forward enough, but the Port B change is a little more difficult to handle.

Here we will set up part of a Hi-Fi system that has 4 preset radio channels - channels 1 to 4. Each one is selected by the use of one of 4 push-to-make switches connected to Port B. The idea behind our system will be that every time a switch is pressed the Port B change interrupt will trigger a macro that alters the channel number.

Follow the instructions below to set up the program.

  1. Start a new flowchart with a 16F1937 device.
  2. Add an Interrupt icon to the program
  3. Add a Loop to the program. Don't put anything in the loop yet. To start with the loop will just keep the program going: the real work will be done by the Interrupt routine we will make.
  4. Add 4 push to make switches connected to pins B4, 5, 6, 7. Label them '1' to '4'
  5. Add two new Byte variables: 'channel' and 'switches'
  6. Add a new macro called 'Chandet'
  7. Assemble the program shown to the right in the 'Chandet' macro.
  8. Double-click on the Interrupt icon in the main program. In the 'Interrupt on:' box select 'PORT'. In the 'Will call macro:' box select 'Chandet' so that it will call the macro called 'Chandet'.
  9. Select 'Build' then click 'Project Options' (Build > Project Options) and set simulation speed to '1000'. The actual value is not important: a setting other than 'As fast as possible' will allow you to see the values of the variables in the system as your program simulates.
  10. Run the program, and check that the variable 'channel' is assigned a channel number on the press of a switch.

Here is an explanation of how the 'Chandet' macro works:

The first icon assigns the value of the top 4 bits of Port B to the variable switches. Use masking here to get the state of only the top 4 bits. The difficulty (or advantage) of the Port B change interrupt is that it triggers on a change of status in Port B. That means that it triggers when the switch is pressed, and also when it is released. Because of this you need to filter out all switch releases: we want to detect only switch presses. The first Decision box detects a value of 0 for the variable switches. If a 0 is detected the connection point diverts the program to the end of the Interrupt macro.

The next Decision box detects a value of 0x10 (16 decimal). This corresponds to the switch on RB4 being pressed. If RB4 is pressed then we assign the variable 'channel' to have a value of 1.

The next Decision box detects a value of 0x20 (32 decimal). This corresponds to the switch on RB5 being pressed. If RB5 is pressed then we assign the variable 'channel' to have a value of 2.

You should now be able to work out how the complete program works.

<^< Set up the Equipment | Course index | Setting up the LCD >^>

Print - Search - Login

Page last modified on May 03, 2013, at 02:39 PM