Documente Academic
Documente Profesional
Documente Cultură
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
main:
trisa = 255
trisb = 0
while true
portb = val1
delay_ms(200)
next val1
wend
end.
Question:
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:
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
Question:
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.
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);
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.