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

Here are the detailed functions, which will adjust the LED brightness based on t

ID: 1715840 • Letter: H

Question

Here are the detailed functions, which will adjust the LED brightness based on the pushbutton:

1. For this part, the brightness of the LED should increase from the current brightness when the switch is ON, and decrease when the switch is OFF. In the main() loop, when the pushbutton is depressed, generate a PWM waveform to drive the LED so that the ON time (duty cycle) varies from 0 to 100% in about a second so it is clearly visible, increasing the brightness of the LED on the board until it's 100% ON  and stopping at 100% (fully ON) until the button is released. When the pushbutton is released, the duty cycle should be reduced, from the brightness at the point when the button is released until it is completely OFF (0% duty cycle) if the switch stays OFF (also taking about 1 second to go from full bright ON to OFF). Implement this version using simple delays.

2. Same as 1 above, except use a timer to create the delay. The PERIOD of the output should be very close to 10mS as measured and demonstrated to the lab instructor on the oscilloscope.

Note that the AVR clock frequency on the Xplained Mini board is 16MHz.

Explanation / Answer

1 Answer :-

#ifndef F_CPU

#define F_CPU 16000000UL //clock speed is 16MHz

#endif

#include <avr/io.h>

#include <util/delay.h>

int main(void) //main starts

{

// Globals for PWM

unsigned char pwmCounter = 0;

unsigned char ledActualBrightness = 0;

unsigned char ledTargetBrightness = 0;

unsigned char fadeCounter = 0;

DDRD = DDRD | ( 1<<4) ; //Make pin 4 of port D as a output

DDRC = DDRC & ~(1<<5) ; // Make pin 5 of port C as a input

while (1) //initialize while loop

{

if(PINC & (1<<5) ) //if PIN5 of port C is high

{

if (TMR0IF)

                {

                                // Perform the PWM brightness control

                                if (ledActualBrightness > pwmCounter)

                                                LED0 = 1; else LED0 = 0;

                                pwmCounter++;

                                if (pwmCounter > 19) pwmCounter = 0;

                                // Perform fading control

                                if (ledTargetBrightness >= ledActualBrightness)

                                                ledActualBrightness = ledTargetBrightness;

PORTD = PORTD | ( 1<<4) ; //PIN4 of port D is high

}

else //otherwise

{

PORTD = PORTD & ~( 1<<4) ; //PIN4 of port D will remain low

{

                                                fadeCounter++;

                                                if (fadeCounter == 24)

                                                {

                                                                ledActualBrightness--;

                                                                fadeCounter = 0;

                                                }             

                                }             

// Get ready for the next interrupt

                                TMR0L = 255 - 187;           // Reset the timer0 counter

                                TMR0IF = 0;                        // Clear the timer0 interrupt flag

                }

}

}

}

} // while loop ends

} //main end

2 Answer :-

#define F_CPU 20000000UL

#include <avr/io.h>

#include <util/delay.h>

#include <avr/interrupt.h>

volatile uint8_t total_overflow;

void timer0_init(void)

{

    // Setting the no prescaler

    TCCR0B |= (1 << CS00);

    // Initialize Timer0

    TCNT0 = 0;

    // Initialize the overflow interrupt for TIMER0

    TIMSK0 |= (1 << TOIE0);

    // Enable global interrupts

    sei();

    // Initialize total_overflow variable

    total_overflow =0;

}

// Interrupt service routine

ISR(TIMER0_OVF_vect)

{

    total_overflow++;

}

int main(void){

    DDRB = 0b00000001; // SET PB0 pin as output

    timer0_init();

    while(1)

    {

{

        if(total_overflow >= 1568)

        {

            if(TCNT0 >= 160){

                PORTB ^= (1 << 0);

                TCNT0 = 0;

                total_overflow = 0;

            }

        }

    }

}

}

                       

                               

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote