Professional Documents
Culture Documents
Micro Past Test 1 Solutions
Micro Past Test 1 Solutions
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>
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
}