简体   繁体   中英

AVR C Programming - Global Arrays

I'm trying to use a global array to store data that I know won't be greater than 255 bytes. But when I try to transmit my data using the array, nothing seems to transmit. What am I doing wrong?

char responseFrame[255];

int main {
    ...
    while(1){
        getData();
    }

};
void getData(void) {
    int responseLen = USART1_RX();
    // put data in the response frame
    for (int i = 0; i < responseLen; i++){
        recv_data = USART1_RX();
        responseFrame[i]=recv_data;
        //USART0_TX(responseFrame[i]);
    }

    LogOutput(responseFrame, responseLen);
}

void LogOutput(char *msg, int size) {

    int i;
    for (i = 0; i < size; i++) {
        USART0_TX(msg[i]);
    }
}

However, when I comment the my logging function "LogOutput" and use a straight transmit using the line "USART0_TX(responseFrame[i])", it appropriately transmit the information.

Here is my USART0_TX function:

void USART0_TX(uint8_t myData) {
    // Wait if a byte is being transmitted
    while( !(UCSR0A & (1<<UDRE0)) );
    // Transmit data
    UDR0 = myData;
};

Are you sure your LogOutput function is being passed valid data?

This should work

unsigned char* data = responseFrame;
LogOutput(data,255);

Another thought:

Are you running the compiler with any optimizations? Try turning them off to see if the problem goes away.

Also, you may want to consider marking the global volatile.

volatile char data[255];

You may need to put a delay in your USART0_TX function.

// Wait if a byte is being transmitted
while( !(UCSR0A & (1<<UDRE0)) );
// Transmit data
UDR0 = myData;
_delay_ms(250);

This will allow the uC to catch up with the data that gets put on the bus. I've run into a similar issue with transmitting data and I found that using a short delay after putting the data on the UDR solved the problem.

The reason it works in your getData function is because the other two statements act as a delay with respect to your TX function.

You will need to include util/delay.h if you haven't already. Keep in mind that the max delay is 4294967.295 ms/ F_CPU in MHz

Ref: AVR LibC delay.h

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM