Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

write the software only using Keil uVision 5 - 4 For this project you will read

ID: 2248989 • Letter: W

Question

write the software only using Keil uVision 5 - 4

For this project you will read the value on a potentiometer using the A/D converter and use that value to adjust the duty cycle that an LED is turned on using PWM. This will allow you to make a dimmable LED. The A/D converter has a reference voltage of 2.5 volts and its input should not exceed this value Likewise the input should not go below zero. Use the potentiometer arrangement show below on the A/D channel zero. The value of R should be either 1K, 5 K, or 10 K. Connect an LED to PWM Channel 0. This arrangement allows the user to select an A/D input voltage from 0 to 2.5 volts. Write a program to accept this input and produce a PWM signal on channel 0 which has a duty cycle proportional to the A/D voltage Your program must be written entirely in C-code. +5V +5V P1.0 ADCe P1.3 PWMO

Explanation / Answer

#include #include #include "LCD.h" extern short AD_last; /* Final AD value will read in interrupt */ extern __irq void TIM3_IRQ_Handler(void); /* value of TIM3 interrupt procedure */ extern __irq void ADC_IRQ_Handler (void); /* ADC interrupt procedure */ char text1[10]; /* Declaration a new value */ void wait (void) { /* for Wait decleration */ int d; d = AD_last; /* to access AD_last value */ if (d != AD_last) /* to confirm that AD interrupt is done */ d = AD_last; /* none interfere with value reading */ d *= 500; d += 50000; /* for measuringanalog value for delay */ /* less value -> for larger delay */ while (d--); /* Only to give some time for LED lights */ AD_last = ADC->DR0 & 0x03FF; /* for the ADC conversion value */ set_cursor(0,0); /* Set the 1st line for display*/ sprintf(text1,"Analogwert %5d",AD_last); lcd_print(text1); /* to show the come back Analog value on the first line of diplay */ } int main (void) { unsigned int i, n; unsigned short AD_old, AD_value; /* ADC Setup */ SCU->GPIOIN[4] |= 0x01; /* P4.0 I/P - mode 0 */ SCU->GPIOOUT[4] &= 0xFFFC; /* P4.0 I/P - mode 0 */ GPIO4->DDR &= 0xFE; /* P4.0 direction -I/P */ SCU->GPIOANA |= 0x0001; /* P4.0 analog mode ON */ ADC->CR |= 0x0002; /* Set POR bit */ for (n = 0; n < 100000; n ++); /* Wait > 1 ms (at 96 MHz) */ ADC->CR &= 0xFFF7; /* Clear STB bit */ for (n = 0; n < 1500; n ++); /* Wait > 15 micro seconds (at 96 MHz) */ ADC->CR |= 0x0400; /* Enable end of conversion interupt*/ ADC->CCR = 0x0003; /* AD Conversion, No WDG on Ch 0 */ SCU->GPIOOUT[7] = 0x5555; /* P7.0..7 output - mode 1 */ GPIO7->DDR = 0xFF; /* P7.0..7 Outputs (LED Data) */ /* LCD Setup */ GPIO8->DDR = 0xFF; /* P8.0..7 Outputs (LCD Data) */ GPIO9->DDR = 0x07; /* P9.0..2 Outputs (LCD Control) */ lcd_init(); lcd_clear(); lcd_print ("Bonjour Yajour"); set_cursor (0, 1); lcd_print (" http://www.keil.com "); for (i = 0; i < 200; i++) wait(); /* Wait for first display */ /* Configure and enable IRQ for A/D Converter (ADC) */ VIC0->VAiR[15] = (unsigned int)ADC_IRQ_Handler; /*To Setup ADC IRQ Hndl addr */ VIC0->VCiR[15] |= 0x20; /* Enable the vector interrupt */ VIC0->VCiR[15] |= 15; /* Specify the interrupt No: */ VIC0->INTER |= (1VCiR[7] |= 0x20; /* Enable the vector interrupt */ VIC0->VCiR[7] |= 7; /* Specify the interrupt number */ VIC0->INTER |= (1CR2 &= 0xFF00; /* Clear prescaler value */ TIM3->CR2 |= 0x000F; /* Setup TIM3 prescaler */ TIM3->CR2 |= 0x2000; /* TIM3 timer overflow intrupt en */ TIM3->CR1 |= 0x8000; /* TIM3 counter enable */ while (1) { /* Loop forever */ for (n = 0x01; n