Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

Q1) a)

1- Timer:
It is commonly used for tasks such as generating accurate time delays, measuring the
frequency of a signal, and controlling the frequency of a PWM (pulse width modulation)
signal. Types of timers, Timer0, Timer1, Timer2, and Timer3 modules, each with different
characteristics and capabilities
2- Interrupt:
Interrupts are used to handle time-critical events or to perform tasks that require immediate
attention. Types of interrupts, such as the Timer Interrupt, ADC Interrupt, and External
Interrupt, each with a specific purpose. Interrupts are used to respond to external events,
such as button presses, sensor readings.
Q1) b)
Dr. Ahmad solution
#include <xc.h>

#define _XTAL_FREQ 4000000

void main (void) {


TRISD = 0;
int x;
for(x=1; x<5*5*4; x++); {
PORTD = 255; //you can use 0xFF or 1 or 0b11111111 or 255 all has the same meaning which is on
__delay_ms(500);
PORTD = 0;
__delay_ms(500);
}
}
Q1) c)
Dr. Ahmad solution
#include <xc.h>
#define _XTAL_FREQ 4000000

void main (void) {


TRISD=0x00;
TRISB=0x00;
int X[5] = {3, 6, 9, 12, 15};
int Y[5];
int Z,P=1;

for (Z=0; Z<5 ; Z++) {


PORTB = X[Z];
__delay_ms(1000);
Y[Z] = X[Z] + P;
PORTD = Y[Z];
__delay_ms(1000);
P++;
}
}
Q1) c)
ChatGPT solution
#include <xc.h>
void main(void) {
// Initialize PORTB and PORTD
TRISB = 0xFF; // Set PORTB as input
TRISD = 0x00; // Set PORTD as output
LATD = 0x00; // Initialize PORTD to 0
while (1) {
// Read the value of PORTB
unsigned char portb_value = PORTB & 0x0F;
// Use a switch statement to set the corresponding value in PORTD
switch (portb_value) {
case 0x03:
LATD = 0x04;
break;
case 0x06:
LATD = 0x08;
break;
case 0x09:
LATD = 0x0C;
break;
case 0x0C:
LATD = 0x10;
break;
case 0x0F:
LATD = 0x14;
}
}
}

Q2) a)
the frequency of the Timer0 module is 4 MHz / 4 = 1 MHz.
TCP = 1/1MHz = 1*10^-6
Max time delay = (255 + 1) * 1*10^-6 = 256 μs
Min time delay = (1 + 1) * 1*10^-6 MHz = 2 μs

Therefore, the largest time delay that can be generated by the PIC18F458 microcontroller
connected to a 4 MHz crystal is 256 μs, and the smallest time delay that can be generated is
2 μs.
Q2) b) condition 1

Written by student
#include <xc.h>
#define _XTAL_FREQ 5120000 // 4 mhz crystal

void Tdelay();
void main(void) {
TRISB = 1;
TRISD = 0;
PORTD=0b00000001; //or you can write 1
Tdelay();
PORTD=0b00000010; //or you can write 2
Tdelay();
PORTD=0b00000100; //or you can write 4
Tdelay();
PORTD=0b00001000; //or you can write 8
Tdelay();
PORTD=0b00010000; //or you can write 16
Tdelay();
PORTD=0b00100000; //or you can write 32
Tdelay();
PORTD=0b01000000; //or you can write 64
Tdelay();
PORTD=0b10000000; //or you can write 128
Tdelay();
PORTD=0b00000000; //or you can write 0
while(1);
return;
}
void Tdelay(){
T0CON= 0B00000111;
TMR0H= 0x2E;
TMR0L= 0xE0;
T0CONbits.TMR0ON = 1; //start timer
while(INTCONbits.TMR0IF==0); //monitor the flag
T0CONbits.TMR0ON=0; //stop timer
INTCONbits.TMR0IF=0; //clear flag
}
Q2) b) condition 2

Written by student
#include <xc.h>
#define _XTAL_FREQ 10230179 // 10.2 mhz crystal

void Tdelay();
void main(void) {
TRISB = 1;
TRISD = 0;
PORTD=128;
Tdelay();
PORTD=64;
Tdelay();
PORTD=32;
Tdelay();
PORTD=16;
Tdelay();
PORTD=8;
Tdelay();
PORTD=4;
Tdelay();
PORTD=2;
Tdelay();
PORTD=1;
Tdelay();
PORTD=0;
while(1);
return;
}
void Tdelay(){
T0CON= 0B00000111;
TMR0H= 0x61;
TMR0L= 0xA8;
T0CONbits.TMR0ON = 1; //start timer
while(INTCONbits.TMR0IF==0); //monitor the flag
T0CONbits.TMR0ON=0; //stop timer
INTCONbits.TMR0IF=0; //clear flag
}

You might also like