开发者

How to call methode in pic 18F4550 with c language (compiler)

update

I'm using pic 18F4550 with microchip v8.63 and C compiler.

I'm trying to make a color sensor. When a led burns a whant to go for example methode red. In OOP is that simple to go to other methods, but how can you do that in C for microchip?

void main(void) 
{ 
    my code here....
    
    // Leds are connected here.
    if(PORTBbits.RB4 == 0) { //red
        LATDbits.LATD0 = 1;     
    }
    else if(PORTBbits.RB5 == 0) { //green
        LATDbits.LATD1 = 1;
    }
    else if(PORTBbits.RB6 == 0) { //blue
        LATDbits.LATD2 = 1;
    }
        // LDR is connected here.
    // 
    if(PORTAbits.RA0 == 1) {
        if(PORTBbits.RB4 == 0) {
            int red = PORTBbits.RB1;    // test.
            colorRed();     
        }
        else if(PORTBbits.RB5 == 0) {
            int green = PORTBbits.RB1;
            colorGreen();
        }
        else if(PORTBbits.RB6 == 0) {
            int blue = PORTBbits.RB1;
            colorBlue();
        }   
    }
}
void colorRed(void) 
{
    LATDbits.LATD0 = 0;
           // other code here

}

void colorGreen(void) 
{
    LATDbits.LATD1 = 0;
}

void colorGreen(void)
{
    LATDbits.LATD2 = 0;
}

These are the errors:

..\code\main.c:56:Warning [2058] call of function without prototype
..\code\main.c:60:Warning [2058] call of function without prototype
..\code\main.c:64:Warning [2058] ca开发者_如何学编程ll of function without prototype
..\code\main.c:69:Error [1109] type mismatch in redeclaration of 'colorRed'
..\code\main.c:74:Error [1109] type mismatch in redeclaration of 'colorGreen'
..\code\main.c:79:Error [1504] redefinition of 'colorGreen'


You simply need to add forward declarations ("prototypes") for the functions before the definition of main.

void colorRed(void);
void colorGreen(void);
void colorBlue(void);

Without these, the compiler assumes a function type of int colorRed(), where int mismatches with void and () mismatches with (void).

Also, as I mentioned in the comments, main should implement an endless loop which checks the chip's inputs and modifies the outputs.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜