-4

I don't know how to give a parameters to a function. I wrote a body, as you can see in my program below. Any answers and explanations appreciated!

    #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;             
        }

    }
4

2 回答 2

1

I like @Ari's answer but I feel like I should add that you can use variable parameter lists if that's what you want. You can find a good tutorial here, but this would probably be more work that you require.

Edit: I just noticed the // Ceramic resonator comment so I'm guessing you're in an Embedded environment in which case I would strongly recommend a simple pointer like @Ari suggested. The variable parameter list might not be fully implemented on your architecture.

于 2013-07-11T11:50:46.587 回答
0

You should put pointer to an array and maybe size of it:

change:

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

to

int decode( unsigned char* rcv)

and

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

to

retVal = decode(rcv); //rcv is a pointer
于 2013-07-11T11:17:16.247 回答