【问题标题】:Why does my programm call the while loop just once ? (Atmel C)为什么我的程序只调用一次while循环? (爱特梅尔 C)
【发布时间】:2017-07-18 16:07:04
【问题描述】:

请帮忙。我的程序只运行一次while循环:(

我不知道问题是什么。我在 python 中也编写了同样的东西,它工作得很好。

我是 C 初学者,我正在使用 Atmel 的 AtMega8 微处理器。

#define F_CPU 8000000UL

#include <avr/io.h>
#include <util/delay.h>

int main(void) {
    DDRD = 0xFF;
    int world[9];
    int nextworld[9];
    //from 1 to 8
    //some random start values  

    world[1] = 1;
    world[2] = 1;
    world[3] = 1;
    world[6] = 1;
    world[7] = 1;
    world[8] = 1;
    for (int i = 0; i < 9; i++) {
        nextworld[i] = world[i];
    }
    int binworld = 0;
    int tiles = 0;
    //=============================
    if (world[1] == 1) {
        binworld = binworld + 128;
    }
    if (world[2] == 1) {
        binworld = binworld + 64;
    }
    if (world[3] == 1) {
        binworld = binworld + 32;
    }
    if (world[4] == 1) {
        binworld = binworld + 16;
    }
    if (world[5] == 1) {
        binworld = binworld + 8;
    }
    if (world[6] == 1) {
        binworld = binworld + 4;
    }
    if (world[7] == 1) {
        binworld = binworld + 2;
    }
    if (world[8] == 1) {
        binworld = binworld + 1;
    }
    PORTD = binworld;
    //================================
    while (1) {
        _delay_ms(100);
        tiles = 0;
        //the live starts

        for (int i = 0; i < 9; i++) {
            tiles = 0;
            for (int xc = -1; xc <= 1; xc++) {
                if (world[i + xc] == 1) {
                    tiles += 1;
                }

                if (world[i] == 1) {
                    tiles = tiles - 1;
                }
            }
            if (world[i] == 1 && tiles == 0) {
                nextworld[i] = 0;
            }
            else if (world[i] == 0 && tiles == 1) {
                nextworld[i] = 1;
            }
            else if (world[i] == 1 && tiles == 2) {
                nextworld[i] = 0;
            }   
        }
        //update old world
        for (int i = 0; i < 9; i++) {
            world[i] = nextworld[i];
        }

        //set null
        binworld = 0;
        //convert
        if (world[1] == 1) {
            binworld = binworld + 128;
        }
        if (world[2] == 1) {
            binworld = binworld + 64;
        }
        if (world[3] == 1) {
            binworld = binworld + 32;
        }
        if (world[4] == 1) {
            binworld = binworld + 16;
        }
        if (world[5] == 1) {
            binworld = binworld + 8;
        }
        if (world[6] == 1) {
            binworld = binworld + 4;
        }
        if (world[7] == 1) {
            binworld = binworld + 2;
        }
        if (world[8] == 1) {
            binworld = binworld + 1;
        }

        PORTD = binworld;
    }
}

【问题讨论】:

  • 调试器......
  • 我认为程序在完成 while 循环之前就退出了(它可能会在命令行上给你一个错误)。你应该在你的问题中发布这些细节。我会首先使用调试器,或者在@500 指出的行之前和之后放置一些打印。
  • 请注意,“只运行一次 while 循环”不是您直接观察到的,它可能是您通过查看 AVR 运行时的输出推断出来的。您对系统的推断远不如您实际观察到的可靠。你应该说出你观察到的,你期望观察到的,以及你的系统是如何连接的。
  • 正确缩进代码并将其修剪为minimal reproducible example也很棒。
  • for (int i=0;i&lt;9;i++) { nextworld[i]=world[i]; } 中的world[0] 的值是多少?

标签: c avr atmega atmel


【解决方案1】:

可能是因为数组索引在第一次迭代时为负数

for (int i=0; i<9;i++)
    {
        tiles = 0;
        for (int xc=-1; xc <= 1; xc++)
        {
            if (world[i+xc]==1) <== HERE, i(0) + xc(-1) == -1

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-28
    • 2015-05-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多