Thread: using a single button input for multiple PWM Outputs

  1. #1
    Registered User
    Join Date
    Oct 2020
    Posts
    2

    using a single button input for multiple PWM Outputs

    i am having some trouble setting up this code. i need to use an external interrupt to output 4 different PWM values and each button press must loop the PWM until the button is pressed again, but after setup i have no idea what to do next. any ideas?

    Code:
    /*
     * PWMdutycycle.c
     *
     * Created: 23/10/2020 12:39:40
     * Author : camog
     */ 
    
    #include <avr/io.h>
    #include <avr/interrupt.h>
    #include "avr/iom328p.h"
    
    #define OCR0A_0
    #define OCR0A_1
    #define OCR0A_2
    #define OCR0A_3
    
    void T0Interrupt1();
    void T0interrupt2();
    void T0interrupt3();
    void T0interrupt4();
    
    int main(void)
    {
    	DDRD | =  1<<PORTD6; /* OCR0A output port */
    	PORTD &= ~ 1 << PORTD6; /* set OCR0A to 0 */
    	
    	DDRB &= ~ 1 << PORTD2;
    	EIMSK = ( 1 << INT0 );
    	EICRA |= ( 1 << ISC00, | 1 << ISC01 ); 
    	
    	TCCR0A | = ( 1<< WGM00, | 1 << WGM01, | 1<< COM0A1, | 1<< COM0A0); /* fast PWM mode, inverting mode set*/
    	TCCR0B | = 1 << CS00; /* clk no prescaling */
    	OCR0A_0 = 0;
    	OCR0A_1 = 0x3F; /*duty cycle=63, for 25% for inverting mode*/
    	OCR0A_2 = 0x9F;/*duty cycle=159, for 62.5%*/
    	OCR0A_3 = 0xDF; /*duty cycle =223 for 87.5%*/
    	
    	sei();
    	
        
        while (1);
    }
    ISR (INT0_vect)
     {
    	 if (PIND2 == 1 )
    	 {
    		 OCR0A = OCR0A_1; 
    	 }
    	 else  (PIND2 = 0)
    	 {
    		 OCR0A = 0;
    	 }
     }
    Attached Files Attached Files
    Last edited by co1223; 10-28-2020 at 05:44 AM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,306
    So is it like this?

    - press (and release) button, PWM pattern 1 starts.
    - press (and release) button, PWM pattern 1 stops.
    - press (and release) button, PWM pattern 2 starts.
    - press (and release) button, PWM pattern 2 stops.

    and so on.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Oct 2020
    Posts
    2
    its like,

    -press (and release) button, pwm pattern 1 starts
    -press (and release) button, pwm pattern 2 starts and 1 stops
    -press (and release) button, pwm pattern 3 starts and 2 stops
    -press (and release) button, pwm pattern 4 starts and no pattern should occur

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,306
    Is your ISR attached to your button or your PWM pin?

    I'm guessing you have something like this to test on (for example).
    AVR Development Kit - Tutorials

    It would be really useful if you posted the URL to the actual kit you're using, so we can see how things are configured etc.

    A useful pre-cursor step would be to just have a single button, a single LED, and make the LED light in response to the button presses.

    It's a highly iterative process, with small steps and lots of testing.
    Because when things go wrong, it's hard to get any information at all out of the system.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    May 2009
    Posts
    3,922
    Code:
    else  (PIND2 = 0)
    This line should raise an warning or maybe an error!

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. multiple condition for single variable in single line
    By merafiq in forum C Programming
    Replies: 2
    Last Post: 03-13-2016, 05:26 PM
  2. Processing outputs of multiple inputs
    By dotnet13 in forum C Programming
    Replies: 5
    Last Post: 11-27-2013, 02:32 AM
  3. 3 input to print 3 outputs at the same time
    By jason007thomas in forum C++ Programming
    Replies: 4
    Last Post: 09-08-2010, 05:48 AM
  4. segmentation: function called with multiple outputs?
    By bertazoid in forum C Programming
    Replies: 11
    Last Post: 10-27-2008, 12:44 PM
  5. Single API for multiple CGI
    By Unregistered in forum Linux Programming
    Replies: 1
    Last Post: 10-09-2001, 06:31 AM

Tags for this Thread