首页 > 解决方案 > 二维阵列的一部分的无限循环工作

问题描述

当我在 Xcode 中编译它printf并为每个字母的破折号和点运行语句时,我的代码可以工作。但是,当我将代码放入 TI Code Composer Studio 时,代码可以正常工作,但它只会在我的微控制器上闪烁“Go Knights”的第一个字母“G”。

我知道它与“while”循环有关,但我不确定它是什么。我已经移动了所有东西,打开/关闭了闪光灯,关闭了我的计数器,将我的变量从全局变量更改为局部变量,检查语法是否有任何缺失,删除了 if 语句==-1,但没有任何效果

#include <msp430g2553.h> 
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

unsigned int i = 0;
unsigned int j=0;
int row = 0;

void main(void)`
{

char str [] = "Go Knights";
int morse_array [36][6] = { //array for A-Z, 0-9 in morse code
    {0,1,-1},      //'.' = 0 and '-' = 1 and all else = -1
    {1,0,0,0,-1},
    {1,0,1,0,-1},
    {1,0,0,-1},
    {0,-1},
    {0,0,1,0,-1},
    {1,1,0,-1},
    {0,0,0,0,-1},
    {0,0,-1},
    {0,1,1,1,-1},
    {1,0,1,-1},
    {0,1,0,0,-1},
    {1,1,-1},
    {1,0,-1},
    {1,1,1,-1},
    {0,1,1,0,-1},
    {1,1,0,1,-1},
    {0,1,0,-1},
    {0,0,0,-1},
    {1,-1},
    {0,0,1,-1},
    {0,0,0,1,-1},
    {0,1,1,-1},
    {1,0,0,1,-1},
    {1,0,1,1,-1},
    {1,1,0,0,-1},
    {1,1,1,1,1,-1},
    {0,1,1,1,1,-1},
    {0,0,1,1,1,-1},
    {0,0,0,1,1,-1},
    {0,0,0,0,1,-1},
    {0,0,0,0,0,-1},
    {1,0,0,0,0,-1},
    {1,1,0,0,0,-1},
    {1,1,1,0,0,-1},
    {1,1,1,1,0,-1}
};


WDTCTL = WDTPW | WDTHOLD;    // stop watchdog timer
P1DIR |= 0x41;         //set the direction register for LED1 and LED2
P1OUT &= 0xBE;         //initialize LED1 and LED2 to off*/

for(;;){               //empty for loop is an infinite loop

    for (i=0; i<strlen(str); i++) {
        if(str[i] >= 'a' && str[i] <= 'z'){
            row = (str[i]- 'a');
        }
        else if(str[i] >= 'A' && str[i] <= 'Z'){
            row = (str[i]- 'A');
        }
        else if(str[i] >= '0' && str[i] <= '9'){
            row = (str[i] - '0');
        }
        else{
            row = -1;
        }

        j= 0;

        while(morse_array[row][j]!= -1){
            // DOT

            if(row == -1){
                for(i=0; i<140000; i++);
                break;
            }
            if(morse_array[row][j] == 0) {
                P1OUT ^= 0x40;
                for (i=0; i <25000; i++);
                P1OUT &= 0x00;
                for (i=0; i <30000;i++);
            }

            //Dash
            else if(morse_array[row][j] == 1) {
                P1OUT ^= 0x40;
                for (i=0; i<60000; i++);
                P1OUT &= 0x00;
                for (i=0; i<30000; i++);
            }

            j++;
        }
    }
    P1OUT ^= 0x01;
    for (i=0; i<30000; i++);
    P1OUT ^= 0x01;

} }

标签: cloopsmicrocontroller

解决方案


你有你的第一个 for 循环,你使用 variabele 'i'。在该 for 循环中,您有 2 个也使用变量“i”的 for 循环。查看 j++,您应该在第一个 for 循环中使用变量“j”。


推荐阅读