c - 将参数传递给另一个 .c 文件

标签 c atmel

如果其他人发布了相同的问题,我已经环顾了一段时间,但没有找到适合我的答案。

这是我的问题: 当我将 3 个参数(位于 main 中)传递给另一个 .c 文件时,信息会被删除。如果我将函数保留在 main 中(不将参数传递给另一个 .c 文件),则程序可以正常工作。

我在这里缺少什么?

此外,我正在使用微 Controller ,但我确信问题出在我的代码中。

主要:

#include <avr/io.h>
#include <stdbool.h>
#include <stdio.h>

#include "glcd.h"
#include "fonts/Liberation_Sans15x21_Numbers.h"


//functions used:
void glcd_print_clock(void);
void time_clock(void);
void display_clock_text(uint8_t x, uint8_t y, uint8_t z);


int main(void)
{
    glcd_init();

    while(1)
    {
        time_clock();
    }

}

//
void time_clock(void)
{
    uint8_t sec, min, hr;
    //char str_time[8] = "";

    for(hr=1; hr<13; hr++)
    {
        for(min=0; min<60; min++)
        {
            for(sec=0; sec<60; sec++)
            {

                display_clock_text(hr,min,sec);

                /*
                //clears buffer
                glcd_clear_buffer();
                glcd_set_font(Liberation_Sans15x21_Numbers,15,21,46,57);
                //sends hr, min, and sec to string
                sprintf(str_time,"%02d:%02d:%02d",hr,min,sec);
                //x-y coordinate of text
                glcd_draw_string_xy(12,5,str_time);
                //displays text
                glcd_write();
                _delay_ms(1000);
                */

            }
        }
    }
}

其他 .c 文件(display_text):

#include "glcd.h"
#include "fonts/Liberation_Sans15x21_Numbers.h"
#include <stdio.h>

#define Y_AXIS  5
#define X_AXIS  12

void display_clock_text(uint8_t x, uint8_t y, uint8_t z);


/*  Gets the hour, minutes and seconds from main.
    Then displays the information on the LCD
*/
void display_clock_text(uint8_t x, uint8_t y, uint8_t z)
{
    char str_time[8] = "";

    //clears buffer
    glcd_clear_buffer();

    //selects font to be used
    glcd_set_font(Liberation_Sans15x21_Numbers,15,21,46,57);

    //sends hr, min, and sec to string
    sprintf(str_time,"%02d:%02d:%02d", x, y, z);

    //x-y coordinate of text
    glcd_draw_string_xy(X_AXIS,Y_AXIS,str_time);

    //displays text
    glcd_write();

    _delay_ms(1000);
}

最佳答案

char str_time[8] = ""; 不正确。它应该是 char str_time[9] = "";。这是因为 sprintf(str_time,"%02d:%02d:%02d", x, y, z); 由于以 0 结尾,因此需要 9 个字节。

另外,一般来说,一个好主意是:

snprintf(str_time, sizeof(str_time), "%02d:%02d:%02d", x, y, z);

这可能会也可能不会导致您遇到的不良行为,但它肯定需要修复。

关于c - 将参数传递给另一个 .c 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33947106/

相关文章:

c - 对 AVR 进行编程以解释 Arduino 旋转编码器模块的输入时出现问题

c - 已定义但未用于整个项目的变量

C奇怪的int数组指针

c - 我不能使用 sprintf()

asp.net - 如何在VS2012下构建SplendidCRM 2.1?

iphone - 有没有办法从 iPhone 上的标准输入读取,无论是硬件还是模拟器?

c - 在gdb 下调试汇编代码|平台linux

c - Atmel 处理器已复位,但 MCUSR 中未指示复位源

arduino - 有没有办法处理 AVR/Arduino 微 Controller 中的堆内存碎片?

assembly - 这个功能需要多长时间?