Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Need help for PWM generation

Status
Not open for further replies.
AVR Freaks


#include <avr/io.h>
int main()
{
//Set up the PWM signal (page 118-119, ATmega8(L))
//The PWM signal is triggered when Timer/Counter register = Output Compare register. (TCNT2 = OCR2)
//attemtp to make the signal 1MHz
// Clk/(2*Dividor*(1+OCR2))
// 3.6MHz/(2*1*(1+1)) = 1MHz
OCR2 = 0x01; //set OCR2 to overflow at 1
// FOC2 = 0;
// WGM20 = 0; //CTC mode. Reset the timer each overflow of OC2 and toggle the PWM value
// COM21 = 0; //
// COM20 = 1; //Toggle on overflow
// WGM21 = 1; //
// CS22 = 0; //
// CS21 = 0; //
// CS20 = 1; //clk/(no prescaling)
TCCR2= 0x19; //set TCCR2 to the above values (0x00011001b)
DDRB = 0x08; //as of right now, only enable the PWM pin as output
while(1);
return 0;
}
 
check if the micro c itself gives you example program for PWM.. i think you should find it in installation location

---------- Post added at 13:17 ---------- Previous post was at 13:16 ----------

you can run it in micro-c what is the problem with this code.
 

I tried your code in mikroC, but having no output. compiler gives no error.

there are some example built in, but its not clear to me. I'm trying. thanks for your reply.
 

read this program. its working well with int1. tell me how can I use both int0 & int1??

////////////////////////////////////////////////////////////////////////////////////////

int cnt = 0, cnt2 = 0; // Global variable cnt

void interrupt_ISR () org IVT_ADDR_INT1
{ // Interrupt rutine
SREG_I_bit = 0; // Disable Interrupts
cnt++;
PORTD = cnt; // Increment variable cnt
SREG_I_bit = 1; // Enable Interrupts
}




void interrupt_ISR () org IVT_ADDR_INT0 // Look here. how can I call/enable

intrrupt 1?






{ // Interrupt rutine
SREG_I_bit = 0; // Disable Interrupts
cnt2++;
PORTC = cnt; // Increment variable cnt
SREG_I_bit = 2; // Enable Interrupts
}

void main()
{ // Main program

DDRD = 0b00110011; // Set PD3 as input
DDRB = 0xFF;
DDRC = 0xFF; // Set PortB as output

//PORTD = 0x00;
PORTB = 0xFF; // Starting value for PortD
GICR = 0x80; // Set the Interrupts
MCUCR = 0x08; // Configure Interrupt for falling edge on PortD.3

SREG_I_bit = 1; // Enable Interrupts

while(1)
{ // Unending loop
PORTB = 0xFF;
Delay_ms(10);
PORTB = 0x00;
Delay_ms(10);
// Write on PortB value of varibale cnt
}
}
 

Hi,
Try with this code:
Code:
int cnt = 0, cnt2 = 0; // Global variable cnt

void interrupt_INT1() org IVT_ADDR_INT1
{ // Interrupt rutine
cnt++;
PORTD = cnt; // Increment variable cnt
}

void interrupt_INT0() org IVT_ADDR_INT0
{ // Interrupt rutine
cnt2++;
PORTC = cnt; // Increment variable cnt
}

void main()
{ // Main program

DDRD = 0b00110011; // Set PD3 and PD2 as input
DDRB = 0xFF;
DDRC = 0xFF; // Set PortB as output

//PORTD = 0x00;
PORTB = 0xFF; // Starting value for PortB
GICR = 0xC0; // Set the Interrupts - INT0 and INT1
MCUCR = 0x0A; // Configure Interrupt for falling edge on INT0 and INT1

SREG_I_bit = 1; // Enable Interrupts

while(1)
{ // Unending loop
PORTB = 0xFF;
Delay_ms(10);
PORTB = 0x00;
Delay_ms(10);
// Write on PortB value of varibale cnt
}
}
I think you can understand how I did it. I set the values of GICR and MCUCR. If you read the datasheet and understand, then it should be clear. I also think you can understand how I set the interrupts.

Hope this helps.
Tahmid.
 

Hi,
By not working you mean there's no output? Are you simulating it or testing it on dev board?

---------- Post added at 23:31 ---------- Previous post was at 23:24 ----------

Hi,
The problem with the posted code (the PWM one, as posted by ckshivaram), the reason why it does not work in mikroC is, I think, because of this line:
Code:
#include <avr/io.h>
In mikroC, you don't need to include header files like this, in fact, mikroC doesn't even have an avr/io.h file as far as I know. By default, all required headers are loaded and added to the project.

Hope this helps.
Tahmid.
 

the problem was in declaration. Its corrected. now working. but can you say how can I generate pwm signal with varying frequency selected by variable resistor? that means, I want to use a variable resistor in adc0 and the pwm will be varied with the voltage in adc0 pin. but other operation can't be delayed or hampered. I tried but other operation varries with this pwm time delay.
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top