Question

In: Electrical Engineering

7. Write a C program to generate a 16kHz PWM signal that has 40% duty cycle...

7. Write a C program to generate a 16kHz PWM signal that has 40% duty cycle using Timer2 of an ATMega8 microcontroller.  


8. Consider a push button switch SW0 connected to pin INT0 external interrupts of an ATMega8 AVR. 8 LEDS are connected to port PB0-PB7 on the same AVR. These LEDS are connected such a lit LED displays that the output port is on logic level 1. On start-up, TASK1 is executed and continue indefinitely. If the push-button switch SW0 is pressed, TASK1 is paused and TASK2 is carried out after which TASK1 is resumed.

? TASK1- A roll action is performed using the LEDs: The first LED is lit and roll down to the last LED then back to the first LED. This operation is done indefinitely.

? TASK2- ALL the LEDs blink five (5) times.

Solutions

Expert Solution

7 ans) #include "18f4431.h"

#use FAST_IO(C)

#use standard_io(D)

#use delay(clock=20000000)

#fuses NOWDT, HS, NOPROTECT, BROWNOUT, BORV45, NOCPD, LVP, NOFCMEN, NOMCLR

#fuses HPOL_HIGH, LPOL_HIGH, NOPWMPIN

void main ()

{

int a1,a2;

#asm

movlw 0x02 ;1:1 prescale e postscale, continuous up/down counting mode

movwf 0xF7F ;registrador PTCON0

movlw 0x80 ;liga pwm time base e conta pra cima

movwf 0xF7E ;registrador PTCON1

movlw 0x640 ;liga PWMs impares no modo independente

movwf 0xF6F ;registrador PWMCON0

movlw 0x00 ;Configura frequencia em 16kHz

movwf 0xF7A ;registrador PTPERH

movlw 0x64 ;Configura frequencia em 16kHz

movwf 0xF7B ;registrador PTPERL

#endasm

a1=75>>6;

a2=75<<2;

#asm

movf a2,w

movwf 0xF79 ;registrador PDC0L

movf a1,w

movwf 0xF78 ;registrador PDC0H

movf 0xC8

movwf 0xF77 ;registrador PDC1L

movf 0x00

movwf 0xF76 ;registrador PDC1H

movf 0X64

movwf 0xF75 ;registrador PDC2L

movf 0x00

movwf 0xF74 ;registrador PDC2H

#endasm

while (1);

}

in another method

#include

//Define PIC registers __CONFIG(0x3f72);

//Select HS oscillator, Enable (PWRTE,BOREN),

//Disable (CPD,CP,WDTEN,In-circuit Debugger)

#define XTAL 10000

//10Mhz=10000Khz

#define PWM_Freq 1

//1Khz PWM frequency #define TMR2_PRE 16

//Timer2 Prescale #define PR2_Val ((char)((XTAL/(4*TMR2_PRE*PWM_Freq))-1))

//Calculation for Period register PR2 (1Khz)

#define Duty_Cyc PR2_Val*2 unsigned int i;

void PWM_init(void);

void PWM_change(unsigned int);

void DelayMs(unsigned int);

void main(void)

{

PWM_init();

while(1)

{

i=0;

PWM_change(i);

DelayMs(10);

while(i<PR2_Val)

{

i=i+1;

PWM_change(i);

DelayMs(200);

}

}

}

void PWM_init(void)

{

TRISC2=0;

//PWM channel 1 and 2 configured as output TRISC1=0;

PORTC = 0x00;

CCP1CON=0x0c;

//CCP1 and CCP2 are configured for PWM CCP2CON=0x0c;

PR2=PR2_Val;

//Move the PR2 value T2CON=0x03;

//Timer2 Prescale is 16 TMR2=0x00;

TMR2ON=1;

//Turn ON timer2

}

void PWM_change(unsigned int DTY)

//Duty cycle change routine

{

CCPR1L=DTY;

//Value is between 0 to 255 CCPR2L=DTY;

}

void DelayMs(unsigned int Ms)

//Delay Routine

{

int delay_cnst; while(Ms>0)

{

Ms--;

for(delay_cnst = 0;delay_cnst <220;delay_cnst++);

//delay constant for 1Ms @10Mhz

}

}


Related Solutions

Write a code to generate a 5 kHz signal using timer. Vary it's duty cycle. Don't...
Write a code to generate a 5 kHz signal using timer. Vary it's duty cycle. Don't use delay function. using any language
Write a MATLAB program to generate a Gaussian window and apply it to a signal as...
Write a MATLAB program to generate a Gaussian window and apply it to a signal as follows: 1)Generate a time vector from 0 to 1 seconds with an interval of 0.001 second 2)Generate two sinusoid signals with a frequency of 90 Hz and 100 Hz, respectively, and add them together 3)Generate a Gaussian window and apply it to the combined sinusoid signal 4)Plot an overlay of the original signal and the windowed in the same figure 5)Add a legend to...
1.write a program for the msp430 to make led 1 blink at 50 percent duty cycle....
1.write a program for the msp430 to make led 1 blink at 50 percent duty cycle. 2 modify the program to make led 1 blink 5 times, make a long pause, then blink 5 more times then stop. The time delays should be carried out in subroutines
1.write a program for the msp430FR6989 to make led 1 blink at 50 percent duty cycle....
1.write a program for the msp430FR6989 to make led 1 blink at 50 percent duty cycle. 2 modify the program to make led 1 blink 5 times, make a long pause, then blink 5 more times then stop. The time delays should be carried out in subroutines
C++ Visual Studio 2019 Part A : Program Description: Write a program to generate a report...
C++ Visual Studio 2019 Part A : Program Description: Write a program to generate a report based on input received from a text file. Suppose the input text file student_grades.txt contains the student’s Last name , First name, SSN, Test1, Test2, Test3 and Test4. (25%) i.e. Alfalfa   Aloysius   123-45-6789 40.0    90.0   100.0    83.0 Generate the output Report File student_final.txt in the following format : LastName FirstName   SSN Test1   Test2   Test3   Test4 Average FinalGrade i.e. Alfalfa   Aloysius   123-45-6789 40.0    90.0   100.0   ...
a – DSB. Write the code for an m-file (script) to generate a DSB signal. The...
a – DSB. Write the code for an m-file (script) to generate a DSB signal. The modulating (message) signal is a single tone signal with frequency 1kHz and the carrier frequency is 30kHz. Time Vector: 3001 points over range from 0 to 3ms (3 cycles of the modulating signal). Plot your original message signal both in time and its spectrum. (Note: the Matlab examples 6.1 and 6.2 will help, but use the cosine functions for your signals instead of sine...
Write a simple C program to generate a chart of circle properties. Output chart “iteration” is...
Write a simple C program to generate a chart of circle properties. Output chart “iteration” is to be specified by user input. Your chart will include the radius, diameter, circumference, and Area values in a single table, and radius values will range from 0 to 50, floating point values, with three-decimal-place precision. Table should have all column entries right-justified and each column will have a heading above the entries. For example, assuming user enters a incrementation value of 5, the...
Using P18f4580 controller - Write a program in C using Timer0 to generate a square wave...
Using P18f4580 controller - Write a program in C using Timer0 to generate a square wave with a frequency of 1kHz on PORTBRB5? (Assume XTAL = 20 MHz).
write c program to generate 6 numbers to simulate rolling of a dice . use while...
write c program to generate 6 numbers to simulate rolling of a dice . use while loop to run 100 and 10000 times. print out how many times it generates 1, 2,3,4,5,6.
Write a C++ program to generate two random numbers (Rnd1 and Rnd2). These two numbers should...
Write a C++ program to generate two random numbers (Rnd1 and Rnd2). These two numbers should be within a range [100, 500]. If Rnd1 greater than or equals to Rnd2, print out the result of (Rnd1-Rnd2). Otherwise, print out the result of (Rnd2-Rnd1). In all cases, print Rnd1, Rnd2, and the results of subtractions in suitable messages.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT