controlling motor using pwm

Go To Last Post
4 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

hi guys I am new to AVR world I wanted to know what is the wrong with my code there are no errors in my code but in proteus simulator its still working on 100% speed or full duty cycle any advice?

 

#include <avr/io.h>
#include <avr/interrupt.h>

void Timer0_PWM(unsigned char duty_cycle){

	TCNT0=0;

	OCR0=duty_cycle;

	DDRD |= (1<<3);

	TCCR0 = (1<<WGM00) | (1<<WGM01) | (1<<COM01) | (1<<CS01);

}
int main (void)
{
	Timer0_PWM(64);

	DDRA &= ~(1<<0);
	DDRA &= ~(1<<1);
	DDRA &= ~(1<<2);

	DDRC |= (1<<0);
	DDRC |= (1<<1);

	PORTC &= ~(1<<0);
	PORTC &= ~(1<<1);



	while (1)
	{
		if (PINA & (1<<0))
		{
			PORTC |= (1<<0);
			PORTC &= ~(1<<1);

		}
		else if (PINA & (1<<1))
		{
			PORTC &= ~(1<<0);
			PORTC |= (1<<1);
		}
		else if (PINA & (1<<2))
		{
			PORTC &= ~(1<<0);
			PORTC &= ~(1<<1);
		}

	}
}

 

This topic has a solution.
Last Edited: Mon. Oct 12, 2020 - 10:14 PM
This reply has been marked as the solution. 
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Welcome to AVRFreaks!

 

To get good answers, we need to know which of the 100 or so AVR's your using?  Mega16 perhaps?

The C language lets one add relevant comments using the  /*....*/ or // operators, documenting what is attached to PORTA pin 0, 1, 2 and PORTC 0,1 would help You when you later look at your code, will you remember what is attached to these a few weeks from now? 

 

So assuming your using an Mega16, the fast PWM will output on pin 4 PB3, labled OC0 on the data sheet, this pin must be made an output (all pins default to input) in order to see any output from timer0.

 

jim

 

 

 

(Possum Lodge oath) Quando omni flunkus, moritati.

"I thought growing old would take longer"

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

yes atmega16 

 

it worked thx for ur help heart

Last Edited: Mon. Oct 12, 2020 - 02:48 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Marwan Ashraf wrote:
yes atmega16 it worked thx for ur help

Great, glad it's working, please mark the solution for others that follow.

Jim

 

 

(Possum Lodge oath) Quando omni flunkus, moritati.

"I thought growing old would take longer"