Sunteți pe pagina 1din 10

EXPERIMENT 6: LED Blinking Basic

PART 1

main:
TRISB = 0x00
While true
PORTB = 0xFF
delay_ms(1000)
PORTB = 0x00
delay_ms(1000)
wend
end.

Question:
1. What is the output of the program stored in the PIC?
2. What will happen if the statement delay_ms(1000) will be removed from the program? Explain
the result.
3. Differentiate TRISB and PORTB.
4. Which LED will be turned ON when the statement PORTB= 0xFF is changed to PORTB = 0x55?
5. What is the purpose of the button in the circuit?
PART 2
Write a BASIC program that will make the first LED blink with an interval of 0.5sec.
main:
TRISB = 0x00
While true
PORTB = 0xFF
delay_ms(500)
PORTB = 0x00
delay_ms(500)
wend
end.
EXPERIMENT 7: Blinking LED

PART 1

void main() {
TRISB = 0x00;
do {
PORTB = 0xFF;
delay_ms(1000);
PORTB = 0x00;
delay_ms(1000);
}
While(1);
}
Question:
1. What is the purpose of the open and close curly braces?
2. What is the function of the statements do and while(1)?
3. Differentiate while statement and do while statement?
4. What statement/s that causes the LEDs to blink?
5. What statement/s should be changed if we want to control only the first four LEDs?
PART 2
Develop and verify a C program that will execute the following repeated sequences
Trigger LEDs at bit position 0,2,4,6 for 1 seconds
Trigger LEDs at bit position 1,3,5,7 for 2 seconds
EXPERIMENT 8: COUNTER

PART 1

dim val1 as byte

main:

trisa = 255

trisb = 0

while true

for val1 = 0 to 255

portb = val1

delay_ms(200)

next val1

wend

end.

Question:

1. What is the output of the program?


2. What is the purpose of the for loop statement in the program?
3. What is the purpose of the keyword dim?
4. Is it possible to pause the output of the circuit? Why or why not?

PART 2

Convert the given basic program to a c program. Verify the new output of the circuit.
EXPERIMENT 9: Counter with Pause Function

PART 1

inti;
void main() {
TRISA = 255;
TRISB = 0;
for (i=0; i<=255; i++){
count:
Portb = I;
delay_ms(250);
if (PORTA.B0 = 1){
goto pause;
}
}
pause:
Portb = I;
If (porta.b0 = 0) {
goto count;
}
}
Question:

1. For what is the statement TRISA = 255;?


2. State the output of the circuit when the state of the switch is open and close?
3. State the purpose of the labels pause: and count:
4. What is the purpose of the if statement in the program?
5. Differentiate Porta.b0 and Porta.
6. Where is the source of the data sent at PortB?

PART 2

Develop and test a program that will display numbers from 255 to 0 and repeat with an interval
delay of .5 seconds.
EXPERIMENT 10: Running Light with Pause

PART 1

Dim start As byte secondpause:


Dim resume As byte do forever
Dim power As byte if porta.0 = 0 then
dir porta in goto second
dir portb out end if
main: gotosecondpause
for start = 1 to 10 loop
portb = 255 running:
if porta.0 = 1 then power = 1
gotofirstpause do forever
end if if power = 256 then
first; portb = 255
wait 200 ms goto running
if porta.0 = 1 then end if
gotosecondpause portb = power
end if wait 200 ms
portb = 0 power = power*2
second; if porta.0 = 1 then
wait 200 ms goto pause1
next start end if
goto running resume:
firstpause: loop
do forever pause1:
if porta.0 = 0 then do forever
goto first if porta.0 = 0 then
end if goto resume
gotofirstpause end if
loop loop

Question:

1. What is the output of the circuit?


2. What is the purpose of the declarations dir porta in and dir portb out?
3. What is the capability of the goto, end if, and do statements in the program?

PART 2
Write and test a program that will display a running light from left to right.
EXPERIMENT 11: Bumping LED
PART 1
Program Written in Basic
main: Program Written in C
TRISB = 0x00 void main() {
PORTA.0 = 1 TRISA0_bit = 1;
While true TRISB = 0x00;
If PORTA.0 = 1 then do{
PORTB =0x81 if( porta.b0 ==1){
delay_ms(100) PORTB = 0x81;
PORTB = 0x42 delay_ms(100);
delay_ms(100) PORTB = 0x42;
PORTB = 0x24 delay_ms(100);
delay_ms(100) PORTB = 0x24;
PORTB = 0x18 delay_ms(100);
delay_ms(100) PORTB = 0x18;
PORTB = 0x24 delay_ms(100);
delay_ms(100) PORTB = 0x24;
PORTB = 0x42 delay_ms(100);
delay_ms(50) PORTB = 0x42;
elseif PORTA.0 = 0 then delay_ms(50);
PORTB = 0xff }
delay_ms(500) else {
PORTB = 0x00
delay_ms(500) PORTB = 0xff;
end if delay_ms(500);
end if PORTB = 0x00;
wend delay_ms(500);
end. }
}
while(1);
}
EXPERIMENT 12: Controlling Stepper Motor
PART 1
unsigned short kp, cnt, oldstate = 0; Lcd_Out(2,1,”Counterclockwise”);
char txt[6]; do
char keypadPort at PORTC; { PORTD = 0b00001000;
sbit LCD_RS at RB1_bit; Delay_ms(200);
sbit LCD_EN at RB0_bit; PORTD = 0b00000100;
sbit LCD_D4 at RB4_bit; Delay_ms(200);
sbit LCD_D5 at RB5_bit; PORTD = 0b00000010;
sbit LCD_D6 at RB6_bit; Delay_ms(200);
sbit LCD_D7 at RB7_bit; PORTD = 0b00000001;
sbit LCD_RS_Direction at TRISB1_bit; Delay_ms(200);
sbit LCD_EN_Direction at TRISB0_bit; }while(kp=50);
sbit LCD_D4_Direction at TRISB4_bit; }
sbit LCD_D5_Direction at TRISB5_bit; void main()
sbit LCD_D6_Direction at TRISB6_bit; {
sbit LCD_D7_Direction at TRISB7_bit; cnt = 0;
void motorclockwise() Keypad_Init();
{ Lcd_Init();
TRISD = 0; Lcd_Cmd(_LCD_CLEAR);
PORTD = 0x0f; Lcd_Cmd(_LCD_CURSOR_OFF);
Lcd_Cmd(_LCD_CLEAR); Lcd_Out(1,1,”Loading…”);
delay_ms(10); Delay_ms(1000);
Lcd_Out(1,1,”Motor rotating”); Lcd_Cmd(_LCD_CLEAR);
Lcd_Out(2,4,”Clockwise”); Dealy_ms(10);
do Lcd_Out(1,1,”1 = CW”);
{ PORTD = 0b00000001; Lcd_Out(2,1,”2 = CCW”);
Delay_ms(200); Do
PORTD = 0b00000010; {
Delay_ms(200); Kp = 0;
PORTD = 0b00000100; Do
Delay_ms(200); {kp=Keypad_Key_Click();
PORTD = 0b00001000; }
Delay_ms(200); While(!kp);
} while(kp = 49); Switch(kp)
} {
void motorcounterclockwise() Case 1: motorclockwise();
{ Case 2: motorcounterclockwise();
TRISD = 0; }
PORTD = 0x0f; } while(1);
Lcd_Cmd(_LCD_CLEAR); }
delay_ms(10);
Lcd_Out(1,1,”Motor rotating”);
Question:
1. What is the purpose of the function kp in the program?
2. What statement/s control the rotation of the stepper motor?
3. What is the purpose of the function cnt?

PART 2
Write and test a program that makes the motor clockwise when key 1 is pressed, rotates
counterclockwise when key 2 is pressed, otherwise the motor does not rotate.

EXPERIMENT 13: Arduino with LEDs


PART 1
void setup() { digitalWrite(4, HIGH);
pinMode( 0, OUTPUT); digitalWrite(5, HIGH);
pinMode( 1, OUTPUT); digitalWrite(6, HIGH);
pinMode( 2, OUTPUT); digitalWrite(7, HIGH);
pinMode( 3, OUTPUT); delay(1000);
pinMode( 4, OUTPUT); digitalWrite(0, LOW);
pinMode( 5, OUTPUT); digitalWrite(1, LOW);
pinMode( 6, OUTPUT); digitalWrite(2, LOW);
pinMode( 7, OUTPUT); digitalWrite(3, LOW);
} digitalWrite(4, LOW);
void loop() { digitalWrite(5, LOW);
digitalWrite(0, HIGH); digitalWrite(6, LOW);
digitalWrite(1, HIGH); digitalWrite(7, LOW);
digitalWrite(2, HIGH); delay(1000);
digitalWrite(3, HIGH); }

Question:
1. What is the purpose of the functions pinMode(), digitalWrite() and delay()?
2. What instruction will be changed if only the first nibble LEDs will blink?
3. Compare the difficulties of controlling LEDs using Arduino and PIC microcontroller.
PART 2
Develop and test a program that will display repeated counting numbers from 0 to 255.
EXPERIMENT 14: Arduino with a Switch and LEDs
PART 1
const int buttonPin =13;
cons tint ledPin = 0;
int buttonState = LOW;
void setup() {
pinMode(ledPin, OUTPUT);
pinMode(buttonPin, INPUT);
}
void loop() {
buttonState = digitalRead(buttonPin);
if (buttonState == HIGH){
digitalWrite(ledPin, HIGH);
delay(100);
digitalWrite(ledPin, LOW);
delay(100);
}
else{
digitalWrite(ledPin, LOW);
}
}

Question:
1. What is the purpose of the variable buttonState in the program?
2. What statement/s controls the operation of the LEDs?
3. What are the statements needed to modify if we wish to control the blinking of the 8 LEDs?

PART 2
Develop and test a program that will cause the 8 LEDs to blink when switch 1 is closed and
switch 2 is open, and the LEDs are off when switch 1 is open and switch 2 is closed, otherwise all LEDs
are off.
EXPERIMENT 15: Arduino with LCD
PART 1
#include <LiquidCrystal.h>
LiquidCrystal led (13,12,11,10,9,8);

int analogin = A0;


int Value = 0;
void setup() {
led.begin(16,2);
led.print(“Vol Reading”);
}
void loop() {
Value = analogRead(analogin);
led.setCursor(0,1);
led.print(Value);
led.print(“Volts”);
delay(100)
led.print(“ “);
delay(1000)
}

Question:
1. Define the function of the 6 pins needed to design the LCD.
2. What is the function of the analogRead() and the digitalRead()?
3. Write the bir-position address of the 32 characters that can be displayed to the LCD.

PART 2
Modify the given circuit and program. Include a switch to read from the analog input if the
status of the switch is closed, otherwise the display to the LCD is your surname.

S-ar putea să vă placă și