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.

Atmega2560 PWM bits disable

Status
Not open for further replies.

imranahmed

Advanced Member level 3
Advanced Member level 3
Joined
Dec 4, 2011
Messages
817
Helped
3
Reputation
6
Reaction score
3
Trophy points
1,298
Location
Karachi,Pakistan
Activity points
6,493
Please let me know I am using Atmega2560 program by Arduino IDE not ArduinoMega2560, I run a code successfully all is well, I attached a button on interrupt.
Output is a fan and 2 leds, fan is drive by L293 module. I want L293 connected to two PWM channel and 2 led connected to different PWM channel and I want to stop LEDs and motor when press button. It is not stoping.

Code:
#include <avr/io.h>
#include <avr/interrupt.h>
#include <Arduino.h>

void initPWM()
{
  DDRB |= (1 << 5);  // PORTB5 pin as output for LED 1
  TCCR1A = _BV(COM1A1) | _BV(WGM10); // non-inverting mode, fast pwm with top as 0xFF
  TCCR1B = _BV(CS11); // prescaler set to clk/8

  DDRE |= (1 << 3); // PORTE3 pin as output for motor driver IN1
  TCCR3A = _BV(COM3A1) | _BV(WGM30);   // non-inverting mode, fast pwm with top as 0xFF
  TCCR3B = _BV(CS31);// prescaler set to clk/8

  DDRH |= (1 << 3); // PORTH3 pin as output for motor driver IN2
  TCCR4A = _BV(COM4A1) | _BV(COM4A0) | _BV(WGM40); // inverting mode, fast pwm with top as 0xFF
  TCCR4B = _BV(CS41);   // prescaler set to clk/8

  DDRL |= (1 << 3); // PORTL3 pin as output for LED2
  TCCR5A = _BV(COM5A1) | _BV(COM5A0) | _BV(WGM50); // inverting mode, fast pwm with top as 0xFF
  TCCR5B = _BV(CS51);   // prescaler set to clk/8
}

//Set motor speed and direction with count registers using information obtained by ADC
void changeDutyCycle(int dutycycle) {
  OCR1A = dutycycle;  // set PWM duty cycle for LED 1
  OCR3A = dutycycle;  // set PWM duty cycle for motor driver IN1
  OCR4A = dutycycle;  // set PWM duty cycle for motor driver IN2
  OCR5A = dutycycle;  // set PWM duty cycle for LED 2

}

int main()
{

  initPWM();// initialize PWM
  sei();// initialize Interrupts

  while (1)
  {
    if (flag == 1) {
      changeDutyCycle(128); // read ADC value is 1024/4 = 256 and set motor speed accordingly
    } else {
      changeDutyCycle(0);   // Stop PWM signals    
    }
  }


  return 0;
}
// Establish ISR using external interput on PORTD
ISR (INT0_vect) {
  flag =! flag; // flag with invert command
}
 

Hi,

Code: I don't see pin change interrupt setup.

Nor do we see the schematic. Did you add a debouncing circuit. Your SW is rather sensitive to contact bouncing.

Klaus
 

Hi,

Code: I don't see pin change interrupt setup.

Nor do we see the schematic. Did you add a debouncing circuit. Your SW is rather sensitive to contact bouncing.

Klaus

void initSwitchPD0(); // This is include in main

Code:
void initSwitchPD0()
{
  DDRD &= ~(1 << DDD2);     // Clear the PD0 pin
  // PD0 (INT0 pin) is now an input

  PORTD |= (1 << PORTD0);    // turn On the Pull-up
  // PD0 is now an input with pull-up enabled
  EICRA |= (1 << ISC01);    // set INT0 to trigger on ANY logic change
  EIMSK |= (1 << INT0);     // Turns on INT0

}
 

There is no mention on your code about any button:

I attached a button on interrupt (...) I want to stop LEDs and motor when press button

Code:
// Establish ISR using external interput on PORTD
ISR (INT0_vect) {
  flag =! flag; // flag with invert command
}
Take a time to review your questions.
 

Also you should declare any variable that is modified in a ISR as 'volatile' so that the compiler knows that it could be updated 'somewhere else' and always reads form the variable and not some internally cached copy.
I suggest that you show us all of your code that includes al of the changes you have made since the original post - it can be very confusing when you show snippets of code that don't relate back to the original.
Susan
 
Also you should declare any variable that is modified in a ISR as 'volatile' so that the compiler knows that it could be updated 'somewhere else' and always reads form the variable and not some internally cached copy.
I suggest that you show us all of your code that includes al of the changes you have made since the original post - it can be very confusing when you show snippets of code that don't relate back to the original.
Susan

In attachment there is complete code, I want to read ADC value and change PWM accordingly and a switch is attached for stop motor and 2 LED`s.
LED`s are connected to 2 separate PWM channels and motor connected other 2 PWM channels.
 

Attachments

  • src.rar
    116.5 KB · Views: 118

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top