Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronic 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.

Register Log in

Problem with PWM programming in mikroC pro

Status
Not open for further replies.

Cheetos

Member level 3
Joined
May 26, 2011
Messages
57
Helped
1
Reputation
2
Reaction score
1
Trophy points
1,288
Activity points
1,700
I'm having a problem with mikroC PRO
i am using PIC16F877a and 8MHz xtal
This is my code and i always get Undeclared identifier on all PWM related codes.. please help me

void InitMain() {
PORTB = 0; // set PORTB to 0
TRISB = 0xF0; // designate PORTB pins as output
PORTC = 0; // set PORTC to 0
TRISC = 0xFF; // designate PORTC pins as output
Pwm1_Init(5000); // Initialize PWM1 module at 5KHz
PWM2_Init(5000); // Initialize PWM2 module at 5KHz

}

void main(){
InitMain();
PWM1_start();
PWM2_start();

while (1) {

if(PORTB==0x80)
{
PORTB=0x01;
}

if(PORTB==0x40)
{
PORTB=0x00;
Pwm1_Change_Duty(30);
}

if(PORTB==0x20)
{
PORTB=0x00;
Pwm1_Change_Duty(15);
}

if(PORTB==0x10)
{
PORTB=0x00;
Pwm1_Change_Duty(0);
}

}
}
 

ckshivaram

Advanced Member level 5
Joined
Apr 21, 2008
Messages
5,070
Helped
2,146
Reputation
4,298
Reaction score
2,087
Trophy points
1,393
Location
villingen (Germany) / Bangalore
Activity points
30,097
post the complete code with the header files used in program and the list of errors you get as snapshot or copy paste so that we can see you.....
 

Cheetos

Member level 3
Joined
May 26, 2011
Messages
57
Helped
1
Reputation
2
Reaction score
1
Trophy points
1,288
Activity points
1,700
Thank you for your reply. That's all of my codes, i have no header files. my program is just a simple PWM..

These are my errors :(

 

ckshivaram

Advanced Member level 5
Joined
Apr 21, 2008
Messages
5,070
Helped
2,146
Reputation
4,298
Reaction score
2,087
Trophy points
1,393
Location
villingen (Germany) / Bangalore
Activity points
30,097
http://www.micro-examples.com/public...alculator.html

Code:
/*
 * this example smoothly blinks LEDs on RC1 and RC2 alternatvely
 * using PIC CCP module configured as PWM output
 *
 * source code example for mikroC
 * feel free to use this code at your own risks
 *
 * target : PIC16F877A, 8 Mhz crystal
 * HS clock, no watchdog.
 *
 * easyPIC4 settings :
 *      LEDs on PORTC enabled
 *
 * Author : Bruno Gavand, September 2007
 * see more details on http://www.micro-examples.com/
 *
 *******************************************************************************
 */

void main()
        {
        unsigned char   dc ;

        TRISC = 0 ;                     // set PORTC as output
        PORTC = 0 ;                     // clear PORTC

        /*
         * configure CCP module as 4000 Hz PWM output
         */
        PR2 = 0b01111100 ;
        T2CON = 0b00000101 ;
        CCP1CON = 0b00001100 ;
        CCP2CON = 0b00111100 ;

        for(;;)                         // forever
                {
                /*
                 * PWM resolution is 10 bits
                 * don't use last 2 less significant bits CCPxCON,
                 * so only CCPRxL have to be touched to change duty cycle
                 */
                for(dc = 0 ; dc < 128 ; dc++)
                        {
                        CCPR1L = dc ;
                        CCPR2L = 128 - dc ;
                        Delay_ms(10) ;
                        }
                for(dc = 127 ; dc > 0 ; dc--)
                        {
                        CCPR1L = dc ;
                        CCPR2L = 128 - dc ;
                        Delay_ms(10) ;
                        }
                }
        }
 

Tahmid

Advanced Member level 5
Joined
Jun 17, 2008
Messages
4,758
Helped
1,791
Reputation
3,574
Reaction score
1,650
Trophy points
1,393
Location
Silicon Valley, California, USA (from Dhaka, Bangl
Activity points
30,545
Hi,
Change your code to:
Code:
void InitMain() {
PORTB = 0; // set PORTB to 0
TRISB = 0xF0; // designate PORTB pins as output
PORTC = 0; // set PORTC to 0
TRISC = 0xFF; // designate PORTC pins as output
Pwm1_Init(5000); // Initialize PWM1 module at 5KHz
PWM2_Init(5000); // Initialize PWM2 module at 5KHz

}

void main(){
InitMain();
PWM1_start();
PWM2_start();

while (1) {

if(PORTB==0x80)
{
PORTB=0x01;
}

if(PORTB==0x40)
{
PORTB=0x00;
Pwm1_Set_Duty(30); ///// Difference here ----------------
}

if(PORTB==0x20)
{
PORTB=0x00;
Pwm1_Set_Duty(15); ///// Difference here ----------------
}

if(PORTB==0x10)
{
PORTB=0x00;
Pwm1_Set_Duty(0); ///// Difference here ----------------
}

}
}
The difference is pretty straightforward. The problem was that in the old mikroC compilers, you changed the duty cycle by writing:
Code:
Pwm1_Change_Duty
However after the newer "mikroC PRO for PIC" series was released, it was changed to:
Code:
Pwm1_Set_Duty
If you run your code on the old compilers (don't remember which version, but not mikroC PRO for PIC), it will compile fine.
Try the code I posted. It compiles just fine.

You can view all the library functions by selecting "Help" from the Help menu or simply by pressing F1! :smile:

Hope this helps.
Tahmid.

---------- Post added at 00:41 ---------- Previous post was at 00:35 ----------

post the complete code with the header files used in program
Hi,
In mikroC, you do not need to declare header files separately. By default, all "header and library files" are loaded. You can choose to set which "header files" to load by clicking on the Library Manager and then selecting from the list:



:smile:

Hope this helps.
Tahmid.
 

Status
Not open for further replies.
Toggle Sidebar

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Top