programming microchip pic family: floating leds … · operators per bits enable execution of...

13
PROGRAMMING MICROCHIP PIC FAMILY: FLOATING LEDs BLINKING EXERCISE BASIC INFORMATION ABOUT REMOTE EXPERIMENT Module title FLOATING LEDs BLINKING Authors of remote experiment Dr Aleksandar Peulić, associate professor, Dr Vanja Luković, assistant professor Dr Radojka Krneta, associate professor Želјko Jovanović, assistant Đorđe Damnjanović, assistant Location of remote experiment Faculty of technical science Cacak, laboratory 216/E-Lab WEB address of experiment http://lirex.ftn.kg.ac.rs/ Educational field Computer science and Programming Teacher courses in secondary schools where experiment is applicable Computer hardware, Programming Target group - pupils Computer technician, third and fourth class Target group - students Undergraduate study program in Electrical and Computer Engineering Master study programs in Computer Engineering and Remote Control Teaching unit Hardware programming Level of difficulty Moderately complex Task of the exercise Programming microcontroller ports so that the LEDs turn on and off at specific time intervals Outcomes Students should: master the architecture of microcontroller master the software tool for programming microcontroller

Upload: others

Post on 25-Jan-2020

7 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: PROGRAMMING MICROCHIP PIC FAMILY: FLOATING LEDs … · Operators per bits enable execution of operations on the level of bits inside of integer data. Such operations are common in

PROGRAMMING MICROCHIP PIC FAMILY: FLOATING LEDs BLINKING EXERCISE

BASIC INFORMATION ABOUT REMOTE EXPERIMENT

Module title FLOATING LEDs BLINKING

Authors of remote experiment

Dr Aleksandar Peulić, associate professor, Dr Vanja Luković, assistant professor

Dr Radojka Krneta, associate professor Želјko Jovanović, assistant Đorđe Damnjanović, assistant

Location of remote experiment

Faculty of technical science Cacak, laboratory 216/E-Lab

WEB address of experiment http://lirex.ftn.kg.ac.rs/

Educational field Computer science and Programming

Teacher courses in secondary schools where experiment is applicable

Computer hardware, Programming

Target group - pupils Computer technician, third and fourth class

Target group - students

Undergraduate study program in Electrical and Computer Engineering Master study programs in Computer Engineering and Remote Control

Teaching unit Hardware programming

Level of difficulty Moderately complex

Task of the exercise Programming microcontroller ports so that the LEDs turn on and off at specific time intervals

Outcomes Students should:

• master the architecture of microcontroller • master the software tool for programming microcontroller

Page 2: PROGRAMMING MICROCHIP PIC FAMILY: FLOATING LEDs … · Operators per bits enable execution of operations on the level of bits inside of integer data. Such operations are common in

ACTIVITIES BEFORE THE EXPERIMENT

Prerequisites for performing exercises:

• Study key features of MikroElektronika BigPIC5 • Study development environment of mikroC PRO for PIC software • Study operators per bits in C programming language

Necessary preparation activities for the execution of the experiment:

• Install MikroC PRO for PIC software on user’s PC • Install .Net Framework 4.5 on user’s PC • Install CeyeClon Viewer on user’s PC • Contact administrator for getting access key on: [email protected]

THEORETICAL BACKGROUND

Main components of MikroElektronika BigPIC5

Figure 1. Main components of MikroElektronika BigPIC5

MikroElektronika BigPIC5 allows students and engineers to easily test and explore abilities of PIC microcontroller. Additionally, it allows PIC microcontroller to be interfaced with external circuits and broad range of peripheral devices.

MikroElektronika BigPIC5 is connected to PC using USB cable (4 in Fig. 1). Before establishing connection it is necessary to install PICFlash software on PC, to enable a program to be transferred from PC to

Page 3: PROGRAMMING MICROCHIP PIC FAMILY: FLOATING LEDs … · Operators per bits enable execution of operations on the level of bits inside of integer data. Such operations are common in

microcontroller. The main components of MikroElektronika BigPIC5 for the experiment are as the following (Fig. 1):

- External power supply 8V – 16V AC/DC - 1 - Selectable external and USB power supply – 2 - Power on/off switch - 3 - USB connector - 4 - Microcontroller - 6 - LEDs – 13 (Slika 3) - Switch group SW5 allows all LEDs on PORTA, PORTB, PORTC, PORTD, PORTE, PORTF, PORTG, PORTH

i PORTJ to be connected/disconnected from the microcontroller – 14 (Fig. 2)

Figure 2. SW5 switch group

Figure 3. On BigPIC5 board LEDs

Page 4: PROGRAMMING MICROCHIP PIC FAMILY: FLOATING LEDs … · Operators per bits enable execution of operations on the level of bits inside of integer data. Such operations are common in

BigPIC5 has 67 LEDs (Fig. 3) arranged in nine groups and connected to microcontroller: PORTA, PORTB, PORTC, PORTD, PORTE, PORTF, PORTG, PORTH and PORTJ. With the exception of PORTA and PORTG, each group consists of eight LEDs and can be enabled/disabled using switches of the switch group SW5. PORTA and PORTG have 6 and 5 LEDs, respectively. When enabled, LEDs displays the states of corresponding microcontroller pin. Other ways the LEDs are always off, no matter what the port state is, since no current can flaw through them.

Figure 4 illustrates the connection between PORTD pins and the corresponding LEDs. A resistor is serially connected to LEDs in order to limit current through them.

Figure 4. LEDs schematic

MikroC PRO developing environment for programming PIC microcontrollers

MikroC for PIC is a very powerful development tool for programming PIC microcontrollers. It is designed to maximally facilitate developer’s job in applications development. PIC microcontrollers are the most popular 8-bit microcontrollers which are used in wide range of applications. On the other hand, the C programming language, as one of the most widely used programming languages, natural choice for microcontroller programming. MikroC PRO for PIC provides a very efficient and advanced IDE (Integrated Development Environment), ANSI compliant compiler, a wide range built in libraries, comprehensive documentation and numerous examples ready for use. MikroC PRO for PIC allows easy and fast application development. MikroC PRO for PIC environment consists of (Fig. 5):

Page 5: PROGRAMMING MICROCHIP PIC FAMILY: FLOATING LEDs … · Operators per bits enable execution of operations on the level of bits inside of integer data. Such operations are common in

• 01- Main toolbar • 02-Code explorer • 03-Projects settings • 04-Messages • 05-Code Editor • 06-Image preview • 07-Project manager • 08-Library manager

MikroC PRO for PIC organizes applications into projects consisting of a single project file (with .mcppi extension) and one or more source files (files with .c extension). The MikroC PRO for PIC compiler allows you to menage several projects at a time. Source files can be compiled only if it is a part of the project. A project file contains:

• Project name and optional description; • Target microcontroller in use; • Microcontroller clock; • List of the project source files; • Binary files (.mci) and • Other files

Figure 5. MikroC PRO for PIC environment

Page 6: PROGRAMMING MICROCHIP PIC FAMILY: FLOATING LEDs … · Operators per bits enable execution of operations on the level of bits inside of integer data. Such operations are common in

Operators per Bits

Operators per bits enable execution of operations on the level of bits inside of integer data. Such operations are common in symbolic machine languages, but as standard operators in high-level programming languages were first introduced in C language. Operands and the results are of integer type. There are two groups of operators per bits. The first group consists of operators to perform logical operations while the second consists of operators for shifting.

x y x&y x y x|y x y x^y x ~x

0 0 0 0 0 0 0 0 0 0 1

0 1 0 0 1 1 0 1 1 1 0

1 0 0 1 0 1 1 0 1

1 1 1 1 1 1 1 1 0

Figure 6. Table of values of logical operators per bits

Operators for performing logical operations per bits can be binary and unary (Figure 1). Binary logical operators per bits are logical AND (&), logical OR (|) and logical EXOR (^). Unary logical operator per bits is the prefix logical NOT (~) operatror, which complements the bits of its operand. Figure 6 shows a table of values of logical operators per bits for operands x and y.

Operators for shifting are: shift to the left (<<), shift to the right (>>). Operators for shifting perform shifting of value of the left operand for as binary places to the left or to the right which is the value of the right operand. Right operand must be a positive integer. The result is undefined if the right operand is negative or is greater than the number of bits in the left operand.

In the case of shifting to the left, freed bits at the right end of unsigned operand are filled with zeros, while in the case of signed operand they are filled with the leftmost bit of operands, encoding its sign. In this way, shifting for n positions to right is equivalent to multiplying by 2n.

In the case of shifting to the right, freed bits at the left end unsigned operand are filled with zeros, while in the case of signated operand they are filled with the leftmost bit of operands, encoding its sign. In this way, shifting for n positions to left is equivalent to division by 2n.

For all binary operators per bits there are adequate complex operators for allocation of values, such as: & =, | =, ^ =, << =, >> =. Like other operators for allocation of values, these operators are grouped from right to left.

Page 7: PROGRAMMING MICROCHIP PIC FAMILY: FLOATING LEDs … · Operators per bits enable execution of operations on the level of bits inside of integer data. Such operations are common in

ACTIVITIES DURING THE EXPERIMENT

• Create LED blinking application for given task in the MikroC PRO for PIC, according to manual written below

• Login to Lirex platform over Ceyeclon viewer • Create a folder with the student's name and surname in the existing "LED BLINKING" folder • Copy Notepad file containing source file code realized in MikroC PRO for PIC project to the personal

student's folder • Open MikroC PRO for PIC and edit existing source file in LED BLINKING project by replacing it with

students Notepad file contest • Run Build and Build + Program commands (Fig. 13)

Creating an application project in MikroC PRO for PIC

The new project is created by choosing New Project option from the cascading menu New File (Fig. 5). This command starts a wizard that takes us through the four steps during which we create our project (Fig. 7).

Figure 7. Creating a new project in the software tool mikroC PRO for PIC

In the first step, we select the name of the project, the path to the project, used microcontroller and microcontroller clock (Fig. 8).

Page 8: PROGRAMMING MICROCHIP PIC FAMILY: FLOATING LEDs … · Operators per bits enable execution of operations on the level of bits inside of integer data. Such operations are common in

Figure 8. Defining the name and location of the project, microcontroller and microcontroller clock in the software tool mikroC PRO for PIC

In the next step we add existing source files that need to be included in our project (Fig. 9).

Figure 9. Additing existing source files in project in the software tool mikroC PRO for PIC

Page 9: PROGRAMMING MICROCHIP PIC FAMILY: FLOATING LEDs … · Operators per bits enable execution of operations on the level of bits inside of integer data. Such operations are common in

Figure 10. Inclusion of the required libraries in the project in the software tool mikroC PRO for PIC

Figure 11. Optional field for additional setting of microcontroller configurations bits

In the third step, we determine libraries that we will include in the project (Fig. 10). We are offered two options, the first (Include All) is suitable for beginners. More advanced users can choose the second option (Include None), and then with the help of the Library Manager to select the desired library.

Page 10: PROGRAMMING MICROCHIP PIC FAMILY: FLOATING LEDs … · Operators per bits enable execution of operations on the level of bits inside of integer data. Such operations are common in

In the last step we can chose option to open Edit Project window in which we can further configure our project (Fig.11). Click Finish button to create a project. New source file is created and it contains the void main() function (Fig. 12).

Slika 12. Glavni source fajl sa već unetom metodom void main()

In the Project Manager we can see the created project. Using the menu command Project, we can create a new project, delete the existing project, add and delete source files from the project. After writing a program, it is necessary to translate it to the machine language, a language that is understandable to the microcontroller. This is done by the command Build from the Build menu, after that HEX file (Fig. 14) is created and by issuing the command Program from the same menu the file is loaded into microcontroller memory (Fig. 13). If there were anomalies during the execution of the program, it is necessary to debug a program.

Figure 13. Building the project and creating HEX file which can be loaded in microcontroller

Page 11: PROGRAMMING MICROCHIP PIC FAMILY: FLOATING LEDs … · Operators per bits enable execution of operations on the level of bits inside of integer data. Such operations are common in

Figure 14. Listing of projects files after building is done

FLOATING LEDs BLINKING

In this software solution (Fig. 15), all input ports B, C and D are firstly set to zero with starting value (0000 00002), i.e. all LEDs are turned off (Figure 7). Thereafter, in the first iteration of the cycle, i.e. for counter = 0, logical operations per bits OR (|) is applied between all output ports and the number 110 = 0000 00012. As a result output ports are set to 0000 000112, which is manifested by turning on the first diode in range. After that 100 ms pause is made. Thereafter, for counter = 1, the number 110 = 0000 00012 is shifted 1 bit to the left, generating the number 0000 00102. Further, logical operations per bits OR (|) is applied between the generated number and all output ports. As a result output ports are set to 0000 00112, which is manifested by turning on the second diode in range. After that 100 ms pause is again made. Thereafter, for counter = 2, the number 110 = 0000 00012 is shifted 2 bits to the left, generating the number 0000 01002. Further, logical operations per bits OR (|) is applied between the generated number and last value of all output ports, which was 0000 00112. As a result output ports are set to 0000 01112, which is manifested by turning on the third diode in range. After that 100 ms pause is again made. It is logical to conclude that at the end in the last iteration of cycle i.e. for counter = 8 all ports will be set to 1111 11112. This would be manifested by turning on the last eighth diode in range. In the described way, the first part of the program gives the impression of a floating LED’s turning on with pauses of 100ms.

Page 12: PROGRAMMING MICROCHIP PIC FAMILY: FLOATING LEDs … · Operators per bits enable execution of operations on the level of bits inside of integer data. Such operations are common in

Figure 15. Software application Floating LEDs blinking created in the softvere tool MikroC PRO for PIC

In the second part of the program (Fig. 15) all output ports are gradually reset as follows. Thereafter, in the first iteration of the cycle, i.e. for counter = 0, logical operations per bits NOT (~) over the number 110 = 0000 00012 is applied, resulting the number 1111 11102. Further, logical operations per bits AND (&) is applied between the generated number and last value of all output ports, which was 1111 11112. As a result output ports are set to 1111 11102, which is manifested by turning off the first diode in the range. After that 100 ms pause is made. Thereafter, for counter = 1 the number 110 = 0000 00012 is shifted 1 bit to the left, generating the number 0000 00102, over witch logical operations per bits NOT (~) is applied, resulting the number 1111 11012. Further, logical operations per bits AND (&) is applied between the generated number and last value of all output ports, which was 1111 11102. As a result output ports are set to 1111 11002, which is manifested by turning off the second diode in the range. After that 100 ms pause is again made. It is logical to conclude that at the end in the last iteration of cycle i.e. for counter = 8 all ports will be reset to 0000 00002. This would be manifested by turning off the last eighth diode in range. In the described way, the second part of the program gives the impression of floating LEDs turning off with pauses of 100ms.

Page 13: PROGRAMMING MICROCHIP PIC FAMILY: FLOATING LEDs … · Operators per bits enable execution of operations on the level of bits inside of integer data. Such operations are common in

ACTIVITIES AFTER THE EXPERIMENT

• Students should check result of their work by using the camera, which can be started by application vmcam, which is located on the desktop of workstation.

• Record the running program effect in the form of video files, and save the corresponding program code and send it to: [email protected] or [email protected]

Tasks to exercise in order to improve knowledge:

Task 1. Write a program that will implement traveling lights on two by two LEDs with a pause of 2s. After ignition of all the LEDs, it is necessary to realize traveling LEDs extinguishing two by two in reverse order with a pause of 2s.

Task 2. Write a program that will implement traveling lights on two by two LEDs with a pause of 3s. After ignition of all the LEDs, it is necessary to realize traveling LEDs extinguishing two by two in reverse order with a pause of 3s.

Task 3. Write a program that will implement traveling lights on / off two by two LEDs with a pause of 2s.

Task 4. Write a program that will implement traveling lights on / off every even position LEDs with a pause of 2s.

Task 5. Write a program that will implement traveling lights on / off every odd position LEDs with a pause of 2s.

Task 6. Write a program that will implement traveling lights on / off four by four LEDs with a pause of 5s.

Task 7. Write a program that will implement traveling lights on / off of every third LEDs with a pause of 3s.

Task 8. Write a program that will implement traveling lights on / off of every fifth LEDs with a pause of 3s.