简体   繁体   English

AVR C编程-全局数组

[英]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. 我正在尝试使用全局数组来存储我知道不会大于255个字节的数据。 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. 但是,当我注释我的日志记录函数“ LogOutput”并使用“ USART0_TX(responseFrame [i])”行直接发送时,它将适当地发送信息。

Here is my USART0_TX function: 这是我的USART0_TX函数:

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? 您确定您的LogOutput函数正在传递有效数据吗?

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. 您可能需要延迟USART0_TX函数。

// 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. 这将使uC能够赶上总线上的数据。 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. 我在传输数据时也遇到了类似的问题,我发现将数据放在UDR上后使用一小段延迟即可解决该问题。

The reason it works in your getData function is because the other two statements act as a delay with respect to your TX function. 之所以在您的getData函数中起作用,是因为其他两个语句相对于TX函数而言是一个延迟。

You will need to include util/delay.h if you haven't already. 如果还没有,则需要包含util / delay.h。 Keep in mind that the max delay is 4294967.295 ms/ F_CPU in MHz 请记住,最大延迟为4294967.295 ms / F_CPU以MHz为单位

Ref: AVR LibC delay.h 参考: AVR LibC delay.h

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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