About Students: Name Matric No. Section
About Students: Name Matric No. Section
About Students:
Name Matric No. Section:
1. ARDY SYAZZWAN BIN JIBA CE180078 Instructor’s Name:
2. MUHAMMAD DINIE BIN MAZLAN CE180088 Ir. Ts. Dr. NORFAIZA BINTI FUAD
3. MOHD ZAID IZUDDIN BIN ZULKAPLI CE180133
4. AIMAN BIN ROSLI CE180137
About Experiment:
Title Submission date
Lab 6: Interrupt, Timer and PWM in ARM Cortex-M3 Microcontroller 18/01/2022
(LPC1768)
Pre-Lab /05
C
Questions /15
/20
Lab Activities /15
Lab Assignments /20
P
Observations /10
/50
Conclusion /05
Demonstrations (Understanding) /10
A Ethics (Originality & Lab Submission) /10 /30
Peer Assessment /10
TOTAL MARK Instructor’s Comment Submission Stamp
/100
Peer Assessment (to be filled in by the group leader)
1. List the most significant actions that a CPU takes when it responds to an enabled
interrupt. (3 marks)
the processor can be interrupted for a number of reasons such as a hardware device has
signaled that it has data to process. A hardware device has completed a task. So when an
interrupt enabled, the CPU will have to stop execute the current program. The control then
passes to a special piece of code called an interrupt handler or interrupt service routine.
2. What is the maximum value, in decimal, that a 12-bit and a 24-bit counter can count up
to? (1 mark)
4096
ACTIVITY 1.
LAB ASSIGNMENT 6.1
Modify program:
LAB ASSIGNMENT 2
Activity 2
Assignment 2
#include <LPC17xx.h>
void Init_Wait(void);
int main(void)
Init_Wait();
LPC_GPIO1->FIODIR |=(1<<0);
while (1)
{
LPC_GPIO1->FIOSET |=(1<<0);
wait_us(1);
LPC_GPIO1->FIOCLR |= (1<<0);
wait_us(1);
void Init_Wait(void)
void wait_us(int t)
}
LAB ACTIVITY 6.3
Create PWM signals with different duty cycles on PWM1.1, PWM1.2 and PWM1.3. i.e., PWM1.1
= 20% duty cycle, PWM1.2 = 50% duty cycle, and PWM1.3 = 80% duty cycle. Observe the
outputs on the Logic Analyzer.
The Program:
#include <LPC17xx.h>
void Init_Wait(void);
void wait_ms(int t);
int main(void)
{
Init_Wait();
LPC_PINCON->PINSEL4 |= (21<<0); //Set pin function to (PWM1.1,
P2.0),(PWM1.2,P2.1),(PWM1.3, P2.2)
LPC_PWM1->PCR = (7<<9); //PWM1,PWM2,PWM3 output enabled
LPC_PWM1->PR = 24;//Prescaler 1us resolution
LPC_PWM1->MCR |= (1<<1); //Reset on PWM MR0 & TC if it matches MR0
LPC_PWM1->MR0 = 10000; //set PWM cycle to 10 ms
LPC_PWM1->MR1 = 2000; //Set 20% Duty Cycle to (PWM1.1, P2.0)
LPC_PWM1->MR2 = 5000; //Set 50% Duty Cycle to (PWM1.2, P2.1)
LPC_PWM1->MR3 = 8000; //Set 80% Duty Cycle to (PWM1.3, P2.2)
LPC_PWM1->LER = (1<<3) |(1<<2) |(1<<1) | (1<<0); //latch values in MR3,MR2,MR1 and MR0
LPC_PWM1->TCR = (1<<0) | (1<<3); //enable counters and PWM Mode
while(1)
{
}
}
void Init_Wait(void)
{
LPC_TIM0->CTCR = 0; //timer mode
LPC_TIM0->PR = 24999; //for 1ms resolution
}
void wait_ms(int t)
{
LPC_TIM0->TCR = 1<<1; //reset timer counter
LPC_TIM0->TCR = 1; //enable timer counter
while (LPC_TIM0->TC < t); //wait if conter < t
}
Lab Assignment 6.4:
The Program:
#include <LPC17xx.h>
void Init_Wait(void);
void wait_ms(int t);
int main(void)
{
Init_Wait();
LPC_SC -> PCONP |= (1<<12); //Enable the ADC Peripheral
LPC_ADC -> ADCR = (1<<0); //Select AD0.0 channel
LPC_ADC -> ADCR |= (1<<21); //Power-on the ADC
LPC_ADC -> ADCR |= (1<<8); //Devide PCLK frequency by 2
LPC_PINCON -> PINSEL1 |= (1<<14) ; //select AD0.0 for P0.23
LPC_PINCON->PINSEL4 |= (1<<0); //Set pin function to (PWM1.1, P2.0)
LPC_PWM1->PCR = (1<<9); //PWM1
LPC_PWM1->PR = 24;//Prescaler 1us resolution
LPC_PWM1->MCR |= (1<<1); //Reset on PWM MR0 & TC if it matches MR0
LPC_PWM1->MR0 = 10000; //set PWM cycle to 10 ms
LPC_PWM1->LER = (1<<1) | (1<<0); // MR1 and MR0
LPC_PWM1->TCR = (1<<0) | (1<<3); //enable counters and PWM Mode
int result = 0;
while(1)
{
LPC_ADC -> ADCR |= (1<<24); //Start the conversion
while((LPC_ADC -> ADDR0 & (1u<<31)) == 0)
{} //Wait for conversion to finish
result = ((LPC_ADC -> ADDR0 >> 4) & 0xFFF);
//Shift right by 4 and 12 bit Mask to extract result
if(result == 0) // 0 is Digital Value that Equal to Analog = 0V
{
LPC_PWM1->MR1 = 0; //Set 100% Duty Cycle to (PWM1.1, P2.0)
LPC_PWM1->LER = (1<<1); //latch value in MR1
}
else if (result == 3723) // 3723 is Digital Value that Equal to Analog = 3V
{
LPC_PWM1->MR1 = 10000; //Set 100% Duty Cycle to (PWM1.1, P2.0)
LPC_PWM1->LER = (1<<1); //latch value in MR1
}
else // if Analog Value Between 0V and 3V @ Optional
{
LPC_PWM1->MR1 = 5000; //Set 50% Duty Cycle to (PWM1.1, P2.0)
LPC_PWM1->LER = (1<<1); //latch value in MR1
}
}
}
void Init_Wait(void)
{
LPC_TIM0->CTCR = 0; //timer mode
LPC_TIM0->PR = 24999; //for 1ms resolution
}
Questions
6.1 By referring to the “startup_PLC17xx.s” file, give the interrupt number for Timer0 to
Timer3, and GPIO, respectively. (4 marks)
32 bit
6.2 Explain why in the interrupt handler, the interrupt must be cleared?
(4 marks)
to service the device and stop it from interrupting. Once the handler returns, the CPU
resumes what it was doing before the interrupt occurred.
6.3 Determine the value in the prescale register (PR) if the timer resolution of 5 ms is
required. (4 marks)
5kHz
6.4 Explain the function of the prescale counter (PC) and its relation to the PR. (3 marks)
Prescale register is used to define the resolution of the timer. If the {R is 0 then TC in
incremented every 1 clock cycle of the peripheral clock. The 32-bit is a counter which is
increment to the value stored in PR.
CONCLUSION