Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Problem with pwm code in Mikroc

Status
Not open for further replies.

00100

New Member
I want to use pwm in pic16f877a
when I wrote this code :




unsigned short i;

void main() {
PORTC = 00; // Set PORTC to $FF
TRISC = 0; // PORTC is output
Pwm_Init(5000); // Initialize PWM module

Pwm_Start(); // Start PWM

while (1) { // Endless loop
for(i=0;i<=255;i++)

{ Pwm1_Change_Duty(i);

delay_ms(10); }

} }


there are some errors which tells that the compiler could not identify the expressions
the messages are:
Undeclared identifier 'Pwm_Init' in expression pwm.c
Undeclared identifier 'Pwm_Start' in expression pwm.c
Undeclared identifier 'Pwm1_Change_Duty' in expression pwm.c
Finished (with errors): 01 Jan 2010, 04:43:12 pwm.mcppi


How can I solve this problem ?
Thanks in advance
 
You don't use C much? In beginner terms;

Those 3 things are functions, and the compiler can not find them.

The simplest way is to put those functions above main (before main) in the C code.

Also, please make your { brackets neater and use indenting. To display your code properly on this forum you use "code tags" like this;
(code)
your code goes here
(/code)

and you must use SQUARE brackets for the code tags. :)
 
The three functions are from the PWM library.
**broken link removed**

I do not use MikroC but there is supposed to be a lib manager GUI which is used in placed of

#include <PWM.h>

Maybe the above line will work anyway ?
Code:
#include <PWM.h>

unsigned short i;

void main() {
    PORTC = 00; // Set PORTC to $FF
    TRISC = 0; // PORTC is output 
    Pwm_Init(5000); // Initialize PWM module
 
Last edited:
The embarrassing problem that I copied the example code from the Mikroc manual itself and pasted it as it is.
 
The embarrassing problem that I copied the example code from the Mikroc manual itself and pasted it as it is.
Examples are written with a particular PIC (and compiler version) in mind. You often need to modify example code for your particular processor.

Maybe this will work, though I have not tried it:
Code:
unsigned short i;

void main() {
    PORTC = 00; // Set PORTC to $FF
    TRISC = 0; // PORTC is output
    PWM1_Init(5000); // Initialize PWM module

    PWM1_Start(); // Start PWM

    while (1) { // Endless loop
        for(i=0;i<=255;i++){
            PWM1_Set_Duty(i);
            delay_ms(10);
        }
    }
}
 
Last edited:
Are we still here

there are some errors which tells that the compiler could not identify the expressions
the messages are:
Undeclared identifier 'Pwm_Init' in expression pwm.c
Undeclared identifier 'Pwm_Start' in expression pwm.c
Undeclared identifier 'Pwm1_Change_Duty' in expression pwm.c
Finished (with errors): 01 Jan 2010, 04:43:12 pwm.mcppi


How can I solve this problem ?
Thanks in advance
 
The three functions are from the PWM library.
...

Thanks for the correction 3v0. :) That was my mistake I don't use the compilers inbuilt PWM library functions, it's easy enough and faster to do that manually on PICs.

MikroC PRO requires the compiler libraries to be manually ticked in the libraries window, I mainly use the older MikroC which detects which libraries you are using automatically.
 
Examples are written with a particular PIC (and compiler version) in mind. You often need to modify example code for your particular processor.

Maybe this will work, though I have not tried it:
Code:
unsigned short i;

void main() {
    PORTC = 00; // Set PORTC to $FF
    TRISC = 0; // PORTC is output
    PWM1_Init(5000); // Initialize PWM module

    PWM1_Start(); // Start PWM

    while (1) { // Endless loop
        for(i=0;i<=255;i++){
            PWM1_Set_Duty(i);
            delay_ms(10);
        }
    }
}

thank u hexreader for ur good noticing to the error
that was (pwm) instead of (pwm1)
 
0100 it is obvious that the compiler does not know it should be using the PWM lib. We talked about that.
u r right 3v0 the first problem was that the compiler did not know it should be using the PWM lib.
I followed the steps u had adviced me but the error messages did not differ and that make me thinking that it was not the right way to solve the solution
now every thing goes without problem
thanks to all.
 
MR RB
what is the use of these instructions in mikroc :
{ANSEL = 0; },{ANSELH = 0;}
and why the compiler cannot identify them
I tried them with different pics with no use .
microchip use them in the example codes in it's manual for pwm explanation
 
Some pics don't have ANSEL registers... The pic16f877a adc's are selected in the ADCON0... The ANSEL are used when there are oodles of channels to select from.

The compiler uses the specific header file for that chip... that's why its undefined
 
Last edited:
I used the pic refered by microchip
and also I have tried this for many more advanced pics like 18 family and the compiler also did not recognize then
what pic do u think that it have ansel register ?
 
Check the PIC datasheet, it's a good idea when writing code to also have the datahseet (Adobe) open in another window. Then you can flip back and forth between the code compiler and the datasheet to check things. This is especially important if you don't know that PIC real well, or you are struggling with simple things like ANSEL etc.

ANSEL and ANSELH are not in every PIC but when they are used they are explained in the ADC "analogue to digital converter" chapter.

It sounds like you are learning that PIC and also learning with the compiler, so it helps to work slowly and read up on the PIC hardware in the datasheet as you work.

You are doing good with a difficult challenge, keep it up! :)
 
Status
Not open for further replies.

New Articles From Microcontroller Tips

Back
Top