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

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

ID: 2291259 • Letter: 7

Question

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.

Explanation / Answer

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

}

}

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