如何给C中的函数赋一个参数

时间:2013-07-11 11:12:27

标签: c

我不知道如何为函数提供参数。我写了一个正文,你可以在下面的程序中看到。任何答案和解释都表示赞赏!

    #include <avr/io.h>
    #include <stdint.h>


    // Ceramic Resonator
    #ifndef F_CPU
    #define F_CPU 3686400 // 4MHz
    #endif

    // UART
    #define UART_BAUD_RATE 9600
    #define UART_BAUD_CALC(UART_BAUD_RATE,F_OSC) ((F_CPU)/((UART_BAUD_RATE)*16L)-1)




    int decode( int rcv[i], ... ){  !!!
        int returnValue;
        if ((rcv[0] == rcv[1]) && (rcv[0] == rcv[2]) && (rcv[1] == rcv[2])){
            returnValue = 0;
            //return UDR0;
        }
        else if   (rcv[1] != rcv[2] && (rcv[0] == rcv[1])){
            returnValue = 1;
            //UDR0 = 01;

        }
        else if  (rcv[1] != rcv[2] && (rcv[0] == rcv[2])){
            returnValue = 2;
            //UDR0 = 02;

        }
        else if  (rcv[0] != rcv[1] && (rcv[1] == rcv[2])){
            returnValue = 3;
            //UDR0 = 03;

        }
        return returnValue;
    }

    int main(void){

        // USART
        UBRR0H =(uint8_t) (UART_BAUD_CALC(UART_BAUD_RATE,F_CPU) >>8);
        UBRR0L =(uint8_t) UART_BAUD_CALC(UART_BAUD_RATE,F_CPU);

        UCSR0B = (1<<RXEN0) | (1<<TXEN0); // enable receiver and transmitter,
        UCSR0C = (3<<UCSZ00); // 8 bit (default: asynchronous, no parity, 1 stop-bit)

        DDRC = (1<<5); // set data direction register bit 5 to one, this means PC5 is configured as output
        PORTC = (1<<5); // set output value of PC5 to High-Level (Source Current, 5V to ground)
        // VARIABLES
        //uint8_t get;
        // PROGRAM

        unsigned char code[3] = {'x','y','z'}; // Here you need to write your code
        unsigned char rcv[3]={'0','0','0'}; // received data

        int i = 0;

        int retVal;

        while(1){
            i = 0;
            for(i=0;i<=2;i++){
                // wait for empty transmit buffer 
                //while (!(UCSR0A & (1<<UDRE0)));
                // wait for data to be received
                while (!(UCSR0A & (1<<RXC0)));
                /* put data into buffer, sends the data*/
                {  
                    code[i]= UDR0  ;
                }

                //while(1) // forever
                //{


                PORTC ^= (1<<5); //this is for LED

                // get received data from buffer
                rcv[i] = code[i];


            }
            retVal = decode(int rcv[i], ... ); !!!
            // wait for empty transmit buffer
            while (!(UCSR0A & (1<<UDRE0)));
            // put data into buffer, sends the data
            /*if ((rcv[0] == rcv[1]) && (rcv[0] == rcv[2]) && (rcv[1] == rcv[2]))*/
            UDR0 = retVal;             
        }

    }

2 个答案:

答案 0 :(得分:1)

我喜欢@Ari的回答,但我觉得我应该补充说,如果你想要的话,可以使用变量参数列表。你可以找到一个很好的教程here,但这可能是你需要的更多工作。

修改 我刚刚注意到// Ceramic resonator评论,所以我猜你是在嵌入式环境中,在这种情况下,我强烈推荐像@Ari建议的简单指针。可能没有在您的体系结构上完全实现变量参数列表。

答案 1 :(得分:0)

你应该把指针指向一个数组,可能是它的大小:

变化:

int decode( int rcv[i], ...)

int decode( unsigned char* rcv)

retVal = decode(int rcv[i], ... ); !!!

retVal = decode(rcv); //rcv is a pointer