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.

[PIC] 16f628a PWM basic question

Status
Not open for further replies.

alpha91

Full Member level 3
Joined
Sep 23, 2011
Messages
168
Helped
1
Reputation
2
Reaction score
2
Trophy points
1,298
Activity points
2,625
Hi all, i am learning PWM with PIC16f628a now with C language using MikroC compiler.
I encounter ' main function is not defined main function is not defined' error. May i know what is the error means?

below is my code:

void main()
{

TRISB = 0b00000000;
PWM1_Init(5000); // Initialize PWM1
PWM1_Set_Duty(127);
PWM1_Start(); // start PWM1
while (1)

}


is my code correct? i am trying to light up an LED with PWM. I want to light up the LED with slowly increase its brightness and then decrease slowly and off. For example: The LED slowly increase its brightness till max within 5 seconds and slowly decrease within 5 seconds as well.
 
Last edited:

No, you get at least syntax error in last line (";" is missing).
You should include some header files also.
Configuration flags should be defined.
 
No, you get at least syntax error in last line (";" is missing).
You should include some header files also.
Configuration flags should be defined.

hi, i am new to PIC programming, can i know what kind of header file i am missing? i not sure i need to include what header file. i am trying to find header file list to study but i cant get any.
 

As you are using mikroC PRO PIC Compiler you don't have to include any header files. Maybe Compiler is not properly installed or maybe some Compiler file maybe corrupted. Try uninstalling the Compiler and reinstall the Compiler and try. Which version of mikroC PRO PIC are you using ? Your code is correct and it uses mikroC PRO PWMx library functions. In you code the PWM duty is set to 50%. All you have to do is in while(1) loop create two for() loops and use one to increment and another to decrement the PWM duty. In your code you can use values between 0 to 255 for
Code:
PWM1_Set_Duty();
function.
 
As you are using mikroC PRO PIC Compiler you don't have to include any header files. Maybe Compiler is not properly installed or maybe some Compiler file maybe corrupted. Try uninstalling the Compiler and reinstall the Compiler and try. Which version of mikroC PRO PIC are you using ? Your code is correct and it uses mikroC PRO PWMx library functions. In you code the PWM duty is set to 50%. All you have to do is in while(1) loop create two for() loops and use one to increment and another to decrement the PWM duty. In your code you can use values between 0 to 255 for
Code:
PWM1_Set_Duty();
function.


Hi, i am using 6.4.0 version.

void main()
{

TRISB = 0b00000000;
PWM1_Init(5000); // Initialize PWM1
PWM1_Set_Duty(127);
PWM1_Start(); // start PWM1
while (1);

for (
PWM1_Set_Duty(0);

}

is it something like this? i not sure what should i put in the for loop condition.
 

Try this code.


Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
char i = 0;
 
void main()
{
 
TRISB = 0x00;
TRISC = 0x00;
 
PWM1_Init(5000);
PWM1_Set_Duty(127);
PWM1_Start(); 
 
while (1) {
 
    for(i = 0; i < 256; i += 10) {
        PWM1_Set_Duty(i);
        Delay_ms(1000);
    }
 
 
    for(i = 255; i >= 0; i -= 10) {
        PWM1_Set_Duty(i);
        Delay_ms(1000);
    }
}
 
}

 
Try this code.


Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
char i = 0;
 
void main()
{
 
TRISB = 0x00;
TRISC = 0x00;
 
PWM1_Init(5000);
PWM1_Set_Duty(127);
PWM1_Start(); 
 
while (1) {
 
    for(i = 0; i < 256; i += 10) {
        PWM1_Set_Duty(i);
        Delay_ms(1000);
    }
 
 
    for(i = 255; i >= 0; i -= 10) {
        PWM1_Set_Duty(i);
        Delay_ms(1000);
    }
}
 
}


Hi, thank you very much. the code works. but one more thing is the decrease part didnt work as it should. the LED just gone off suddenly. never mind, i will try to by myself. thank you very much for your time and effort. :wink:
 

but one more thing is the decrease part didnt work as it should. the LED just gone off suddenly

This is a usual mistake. The type of your 'i' cycle variable is char (ie, unsigned short, 0 ... 255), so the original FOR cycles will be endless. Try this (or simply use a signed int 'i' cycle variable):


Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
char i; // unsigned variable !!! 
 
void main() {
 
  TRISB = 0x00;
  TRISC = 0x00;
 
  PWM1_Init(5000);
  PWM1_Set_Duty(127);
  PWM1_Start(); 
 
  while (1) {
 
     // for(i = 0 ; i < 255 ; i += 10)  // wrong, infinite loop !!!
     
     for(i = 0 ; i < 250 ; i += 10) {
        PWM1_Set_Duty(i);
        Delay_ms(1000);
     }
 
 
     // for(i = 255 ; i >= 0 ; i -= 10) // wrong, infinite loop !!!
 
     for(i = 250 ; i > 0 ; i -= 10) {
        PWM1_Set_Duty(i);
        Delay_ms(1000);
     }
 
  } // end while
 
} // end main

 
Fixed code.


Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
signed int i = 0;
 
void main()
{
 
TRISB = 0x00;
TRISC = 0x00;
 
PWM1_Init(5000);
PWM1_Set_Duty(127);
PWM1_Start(); 
 
    while (1) {
 
            for(i = 0; i < 256; i += 10) {
                PWM1_Set_Duty(i);
                Delay_ms(1000);
            }
 
 
            for(i = 255; i >= 0; i -= 10) {
                PWM1_Set_Duty(i);
                Delay_ms(1000);
            }
    } 
}

 
This is a usual mistake. The type of your 'i' cycle variable is char (ie, unsigned short, 0 ... 255), so the original FOR cycles will be endless. Try this (or simply use a signed int 'i' cycle variable):

hi, can i know what's the different between signed and unsigned? why it will become infinite loop since the for loop already limit it at 255?

Fixed code.


Hi, thanks for the code. it works now. thank you very much :thumbsup:
 

hi, can i know what's the different between signed and unsigned? why it will become infinite loop since the for loop already limit it at 255?

Because your 1-byte unsigned char type cycle variable i (its value can be only 0...255) is always less than 256 (i<256 is always true) and also always greater or equal to zero (i>=0 is always true too).

For example (pseudo code):

if (i == 250)
then
(i + 10) == 4 // 260-256, overflowed, < 256

- or -

if (i == 5)
then
(i - 10) == 251 // 256-5, 'underflowed', >= 0


But if the variable i is a signed int (a larger, 2-byte variable, can be also negative) then your FOR cycles are working as well.

Hope this helps you.
 
Because your 1-byte unsigned char type cycle variable i (its value can be only 0...255) is always less than 256 (i<256 is always true) and also always greater or equal to zero (i>=0 is always true too).

For example (pseudo code):

if (i == 250)
then
(i + 10) == 4 // 260-256, overflowed, < 256

- or -

if (i == 5)
then
(i - 10) == 251 // 256-5, 'underflowed', >= 0


But if the variable i is a signed int (a larger, 2-byte variable, can be also negative) then your FOR cycles are working as well.

Hope this helps you.

i see..... thank you very much for your explanation ;-)
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top