Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
OBJECTIVES:
MATERIALS:
PROCEDURES:
Note: Please observe safety precautions and proper handling while conducting the laboratory experiment. This
experiment is to be. performed under
1. Wire the given circuit below. Make sure that all connections are correctly placed. This is also the same
circuit you have used in Laboratory Exercise 3. Check the functionality of your circuit by loading the
code you developed also in Laboratory Exercise 3. Either the count up or count down code will do.
2. As an additional requirement, wire the given circuit below. This circuit will serve as our input to the
microcontroller. The output is taken across the capacitor and must be connected to the PIN 9 of the
microcontroller. Since the supply is a DC voltage, at steady state, the capacitor is an open circuit when
the switch is untouched or open. This charges to 3.3V through the resistor R1. Now, when the switch is
pressed, the capacitor discharges to the ground thus the output now is equal to 0V. In these given
scenarios, the input-output relationship of the simple RC circuit is given by the table below.
4. We are now ready to connect our microcontroller to the outside world. If the above circuit diagram has
been wired correctly, open your ACEduino328 IDE and type in the code below.
/*
Blink
Turns on an LED on for one second, then off for one second, repeatedly.
char inPin = 1;
void count0()
{
digitalWrite(3,HIGH);
digitalWrite(1,HIGH);
digitalWrite(2,LOW);
digitalWrite(4,LOW);
digitalWrite(5,LOW);
digitalWrite(6,LOW);
digitalWrite(7,LOW);
digitalWrite(8,LOW);
}
void count1()
{
digitalWrite(3,HIGH);
digitalWrite(1,HIGH);
digitalWrite(2,LOW);
digitalWrite(4,HIGH);
digitalWrite(5,HIGH);
digitalWrite(6,LOW);
digitalWrite(7,HIGH);
digitalWrite(8,HIGH);
}
void count2()
{
digitalWrite(3,HIGH);
digitalWrite(1,LOW);
digitalWrite(2,HIGH);
digitalWrite(4,LOW);
digitalWrite(5,LOW);
digitalWrite(6,LOW);
digitalWrite(7,LOW);
digitalWrite(8,HIGH);
}
void count3()
{
digitalWrite(3,HIGH);
digitalWrite(1,LOW);
digitalWrite(2,LOW);
digitalWrite(4,LOW);
digitalWrite(5,HIGH);
digitalWrite(6,LOW);
digitalWrite(7,LOW);
digitalWrite(8,HIGH);
}
void count4()
{
digitalWrite(3,HIGH);
digitalWrite(1,LOW);
digitalWrite(2,LOW);
digitalWrite(4,HIGH);
digitalWrite(5,HIGH);
digitalWrite(6,LOW);
digitalWrite(7,HIGH);
digitalWrite(8,LOW);
}
void count5()
{
digitalWrite(3,HIGH);
digitalWrite(1,LOW);
digitalWrite(2,LOW);
digitalWrite(4,LOW);
digitalWrite(5,HIGH);
digitalWrite(6,HIGH);
digitalWrite(7,LOW);
digitalWrite(8,LOW);
}
void count6()
{
digitalWrite(3,HIGH);
digitalWrite(1,LOW);
digitalWrite(2,LOW);
digitalWrite(4,LOW);
digitalWrite(5,LOW);
digitalWrite(6,HIGH);
digitalWrite(7,LOW);
digitalWrite(8,LOW);
}
void count7()
{
digitalWrite(3,HIGH);
digitalWrite(1,HIGH);
digitalWrite(2,LOW);
digitalWrite(4,HIGH);
digitalWrite(5,HIGH);
digitalWrite(6,LOW);
digitalWrite(7,LOW);
digitalWrite(8,HIGH);
}void count8()
{
digitalWrite(3,HIGH);
digitalWrite(1,LOW);
digitalWrite(2,LOW);
digitalWrite(4,LOW);
digitalWrite(5,LOW);
digitalWrite(6,LOW);
digitalWrite(7,LOW);
digitalWrite(8,LOW);
}void count9()
{
digitalWrite(3,HIGH);
digitalWrite(1,LOW);
digitalWrite(2,LOW);
digitalWrite(4,LOW);
digitalWrite(5,HIGH);
digitalWrite(6,LOW);
digitalWrite(7,LOW);
digitalWrite(8,LOW);
}
void setup() {
// initialize the digital pins 1 - 7 as an output.
pinMode(1, OUTPUT); // 7 segement
pinMode(2, OUTPUT); // 7 segement
pinMode(3, OUTPUT); // common anode supply
pinMode(4, OUTPUT); // 7 segement
pinMode(5, OUTPUT); // 7 segement
pinMode(6, OUTPUT); // 7 segement
pinMode(7, OUTPUT); // 7 segement
pinMode(8, OUTPUT); // 7 segement
pinMode(9, INPUT); // for switch
}
// |--7--|
// 8 6
// |--1--|
// 5 2
// |--4--|
//
//
//
void loop() {
count0();
inPin = digitalRead(9);
while (inPin == 1)
{
inPin = digitalRead(9);
}
delay(100);
count1();
inPin = digitalRead(9);
while (inPin == 1)
{
inPin = digitalRead(9);
}
delay(100);
count2();
inPin = digitalRead(9);
while (inPin == 1)
{
inPin = digitalRead(9);
}
delay(100);
count3();
inPin = digitalRead(9);
while (inPin == 1)
{
inPin = digitalRead(9);
}
delay(100);
count4();
inPin = digitalRead(9);
while (inPin == 1)
{
inPin = digitalRead(9);
}
delay(100);
count5();
inPin = digitalRead(9);
while (inPin == 1)
{
inPin = digitalRead(9);
}
delay(100);
count6();
inPin = digitalRead(9);
while (inPin == 1)
{
inPin = digitalRead(9);
}
delay(100);
count7();
inPin = digitalRead(9);
while (inPin == 1)
{
inPin = digitalRead(9);
}
delay(100);
count8();
inPin = digitalRead(9);
while (inPin == 1)
{
inPin = digitalRead(9);
}
delay(100);
count9();
inPin = digitalRead(9);
while (inPin == 1)
{
inPin = digitalRead(9);
}
5. Such code is an example of brute force countup or countdown. Let us now analyze this part of the
code here:
inPin = digitalRead(9);
while (inPin == 1)
{
inPin = digitalRead(9);
}
delay(100);
The variableis inPininitialized at the very start of the storage of the status of our switch and it is in unpressed.
Line 1 takes in the current status of the switch. Remember that the switch was placed at Pin 9 of the
microcontroller. Also, take a look at the setup() function where at the last line, you will see this:
6. Verify the code, upload it and run. Notice if the above statements are happening to your hardware.
One press of a switch means one change at a time.
7. You might have noticed that developing the code for this microcontroller is taking too long. Let us
make code development easy and fast. But first, in
..\hardware\tools\avr\avr\include\avr
8. This folder contains header files that will make our code development fast. Copy the whole folder and paste in
the Arduino 11.0.folder0 shouldfolderlooklike.the figureYourbelow, Arduinowiththe highlighted folder the one
that we have copied.
9. After doing such, copy the code below, verify, upload and run it.
/*
Blink
Turns on an LED on for one second, then off for one second, repeatedly.
char inPin = 1;
char digitsD[] = {0x0a, 0xba, 0x0c, 0x28, 0xb8, 0x68, 0x48, 0x3a, 0x08,
0x28}; char digitsB[] = {0x00, 0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x01,
0x00, 0x00}; int z = 0;
void setup() {
// Pins 0-7 belong to Port D //
Pins 8-13 belong to Port B
DDRD = 0xFF; // initialized as OUTPUT
DDRB = 0x01; // B0 is an OUTPUT, the rest are INPUTS
}
// |--7--|
// 8 6
// |--1--|
// 5 2
// |--4--|
//
//
//
void loop() {
for(z = 0;z<10; z++)
{
PORTD = digitsD[z];
PORTB = digitsB[z];
delay(1000);
}
/*
for(z = 0;z<10; z++)
{
PORTD = digitsD[z];
PORTB = digitsB[z];
inPin = PINB;
inPin = inPin & 0x02;
while(inPin == 2)
{
inPin = PINB;
inPin = inPin & 0x02;
}
delay(1000);
}
*/
}
10. What did you notice? This code allows the 7-segment to count up.
11. Try to uncomment the code in bold format and comment the first for loop statement. Verify, upload
and run the code. The count up will only commence once the switched is pressed.
ACTIVITIES
1. Using the same setup, develop a program that will make the 7-segment LED display count from 9 0
repeatedly. Use function prototypes.
2. Using the same setup, develop a program that will make the 7-segment LED display count from 9 0
repeatedly when the switch is pressed. Use function prototypes.
3. Using the same setup, develop a program that will make the 7-segment LED display initially count up
from 0 9 and then will count down once the switch is pressed. Use function prototypes.
4. Using the same setup, add two more input switches. The functionalities of the three switches are:
If the count has stopped, pressing the 1st pushbutton will increase the digit display by one. If the
2nd pushbutton is pressed, then the countup resumes.
CONCLUSION: