Close or Esc Key

Arduino Projects   |   Raspberry Pi   |   Electronic Circuits   |   AVR   |   PIC   |   8051   |   Electronic Projects

Error in my PWM library

2 posts / 0 new
Last post
S Ganesh
S Ganesh's picture
Offline
Last seen: 2 weeks 6 days ago
Joined: 02/01/2017 - 18:49
Error in my PWM library

Hi I am creating a PWM library in c++ to use in my project. I have started working on it below is part of my code it is not complete yet. I have writing a bit by bit code and building it to find errors and I find some error. So I stopped. Even though I had defined right parameters it is giving error. It should preprocess first #if and assign TCC0A with 0x80. But it skipping #if, #elif and last #else is execuing. Please help me to correct given code.

IDE: Atmel Studio 7.0

MCU: ATmega328p

Code:

/************************************************************
                                     PWM.h file
************************************************************/
 
#ifndef PWM_H_
#define PWM_H_
 
//PWM outputs
#define OC0A 1
#define OC0B 2
 
//PWM modes
#define NINV 0b10000000      //Non-inverting Mode
#define INV  0b11000000      //Inverting Mode
 
#include <avr/io.h>
 
class PWM
    public:
        void Initialize(unsigned char PWMoutputpin,  unsigned char PWMmode)
    {
           TCCR0A = 0x00; TCCR0B = 0x00;
       #if ((PWMoutputpin == OC0A) && (PWMmode == NINV))
             TCCR0A |= NINV;
  
   #elif ((PWMoutputpin == OC0A) && (PWMmode == INV))
          TCCR0A |= INV;
 
    #elif ((PWMoutputpin == OC0B) && (PWMmode == NINV))
            TCCR0A |= (NINV >> 2);
 
    #elif ((PWMoutputpin == OC0B) && (PWMmode == INV))
            TCCR0A |= (INV >> 2);
 
    #else
      #error PWM::Initialize() parameters not defined properly.
  #endif
   }
};
#endif /* PWM_H_ */
/******************************* END **********************/
 
 
/************************************************************
                                      main.cpp file
************************************************************/
#include <avr/io.h>
#include "PWM.h"
 
int main(void)
{
    PWM p;
    p.Initialize(OC0A,NINV);
    return 0;
}
Ashutosh Bhatt
Ashutosh Bhatt's picture
Offline
Last seen: 1 day 19 hours ago
Joined: 21/12/2010 - 15:59
why are you writing PWM_H_   

why are you writing PWM_H_   ?

it should be PWM_H only