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
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
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 the program in C++ The Rebel food truck has asked you to write a program...
Write the program in C++ The Rebel food truck has asked you to write a program for them to do both inventory and sales for their food wagon. Ben and Dave run the food truck and they already have full time day jobs so they could really use your help. The food truck sells: hamburger, hotdogs, chilli, fries and soda. The truck has spots for 200 hamburger patties, 200 hot dogs, 75 hamburger buns, 75 hot dog buns, 500 ounces...
DSB: Write the code for an m-file (script) to generate a DSB signal. The modulating (message)...
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: use the cosine functions for your signals). In your report you should include the plots for the modulated signal in time...
A Carnot's engine has a low temperature reservoir at 7 °C with efficiency of 40%. It...
A Carnot's engine has a low temperature reservoir at 7 °C with efficiency of 40%. It is desired to increase the efficiency to 50%. By how many degrees should the temperature of the high temperature reservoir be increased?   
write C program to create 4 threads for summing the numbers between 1 and 40 where...
write C program to create 4 threads for summing the numbers between 1 and 40 where the first thread computes the sum between 1 and 10; the second thread computes the sum between 11 and 20; the third thread computes the sum between 21 and 30; and the fourth thread compute the sum between 31 and 40. The output should be similar to the following. The sum from 1 to 10 = 55 The sum from 11 to 20 =...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT