c - 二维阵列的一部分的无限循环工作
问题描述
当我在 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;
} }
解决方案
你有你的第一个 for 循环,你使用 variabele 'i'。在该 for 循环中,您有 2 个也使用变量“i”的 for 循环。查看 j++,您应该在第一个 for 循环中使用变量“j”。
推荐阅读
- sql-server - 如何在 SQL Server 中的有子句中使用多个案例
- python - 使用 Python/Flask 在 uWSGI/nginx 部署中运行背景 while 循环
- fish - 比较鱼中的命令替换字符串不为空
- reactjs - 在输入元素中键入内容时,React 如何知道将光标放在哪里?
- visual-studio-code - 实时 Sass 编译器
- python - vs code,尝试安装包时出错
- php - 我添加 1 个月后的 strtotime 值错误
- r - 用所需数据填充 NA'S
- angular - 对 ngContent 的内容应用 CdkVirtualScroll
- angular - Angular 11 Universal 和 Bootstrap 5 Toast 不起作用 - 新的引导程序 TS2304:找不到名称“引导程序”,被粉碎