简体   繁体   English

C数组显示垃圾数据(内存问题?)

[英]C array is displaying garbage data (memory problems?)

I'm making a driver for an 8x8 LED matrix that I'm driving from a computer's parallel port. 我正在为从计算机的并行端口驱动的8x8 LED矩阵制作驱动程序。 It's meant to be a clock, inspired by a design I saw on Tokyoflash. 它的灵感来自我在Tokyoflash上​​看到的设计,它是一个时钟。

Part of the driver is an array of 3*5 number "sprites" that are drawn to the matrix. 驱动程序的一部分是绘制到矩阵的3 * 5数字“精灵”数组。 A coordinate of the matrix is assigned to a coordinate of the sprite and so forth, until the entire sprite is drawn on it. 将矩阵的坐标分配给子画面的坐标,依此类推,直到在其上绘制整个子画面为止。 This process is repeated for the other digit with an offset. 对于具有偏移量的其他数字重复此过程。 I have verified I have drawn the sprites correctly, and that the matrix is blank when it is written to. 我已经验证我正确绘制了精灵,并且矩阵在写入时为空白。 However, when I draw a number on the matrix I get errant 1s at Numpad6 for the left digit, Numpad1 for the right ( Example with the left digit not drawn. ) 但是,当我在矩阵上绘制数字时,在左数字Numpad6处出现错误的1,在右数字Numpad1处出现错误的1( 示例未绘制左数字 )。

I have a week of experience in C and this is baffling me. 我有一个星期的C经验,这让我感到困惑。

Here is the driver in full if you want to compile it yourself. 如果您想自己进行编译,这里是完整的驱动程序。 It is nowhere near finished. 还差一点呢。

//8x8 LED MATRIX DRIVER VER 0.1 APR062009
//CLOCK 
// 
//  01234567
// 0    BXXXXXXH  B: Binary Mode Indicator 
// 1    DXXXXXXM  D: Decimal Mode Indicator
// 2    NNNNNNNN  H: Hour Centric Display
// 3    LLLNNRRR  M: Minute Centric Display
// 4    LNLNNRNR  X: Secondary Information 
// 5    LLLNNRRR  L: Left Digit
// 6    LNLNNRNR  R: Right digit
// 7    LLLNNRRR  N: Not Used

#include <stdio.h>
#include <unistd.h>
//#include <math.h>
#include <time.h>
#include </usr/include/sys/io.h>

#define BASEPORT 0x378

int main()
{
//Increasing array parameters to seems to reduce glitching [best 10 5 3]
int Dig[10][5][3] = {0};    //ALPHANUMERIC ARRAY [NUMBER (0..9)][Y(0..4)][X(0..2)]
int Mat[7][7] = {0};        //[ROW][COL], Top L corner = [0][0]
int Aux1[7] = {0};      //Topmost Row
int Aux2[7] = {0};          //Second to Topmost Row
int Clk;    //Clock
int Wait;   //Delay; meant to eventually replace clock in its current state
int C1;     //Counters
int C2;
int C3;
int L;      //Left Digit
int R;      //Right Digit
//break string left undefined atm

//ioperm (BASEPORT, 3, 1);
//outb(0, BASEPORT);
printf("Now running.\n");

//Set Variables

//3D DIGIT ARRAY [Num][Row][Col] (INITIALIZED BY INSTRUCTIONS)
    //Dig array is meant to be read only once initialized
    //3D arrays are unintuitive to declare so the numbers are 
    //"drawn" instead. 

//Horizontals
    //Some entries in the loop may have the variable in the middle
    //coordinate instead of the 3rd and/or with a +2. This is to 
    //incorporate the incomplete columns some numbers have (eg "2") and 
    //saves coding additional loops. 

for(C1=0; C1<=2; C1++){
Dig[0][0][C1]=1; Dig[0][4][C1]=1;
Dig[2][0][C1]=1; Dig[2][2][C1]=1; Dig[2][4][C1]=1; Dig[2][C1][2]=1; Dig[2][C1+2][0]=1;
Dig[3][0][C1]=1; Dig[3][2][C1]=1; Dig[3][4][C1]=1;
Dig[4][2][C1]=1; Dig[4][C1][0]=1; 
Dig[5][0][C1]=1; Dig[5][2][C1]=1; Dig[5][4][C1]=1; Dig[5][C1][0]=1; Dig[5][C1+2][2]=1;
Dig[6][0][C1]=1; Dig[6][2][C1]=1; Dig[6][4][C1]=1; Dig[6][C1+2][2]=1;
Dig[7][0][C1]=1;
Dig[8][0][C1]=1; Dig[8][2][C1]=1; Dig[8][4][C1]=1;
Dig[9][0][C1]=1; Dig[9][2][C1]=1; Dig[9][4][C1]=1; Dig[9][C1][0]=1;
}

//Verticals 

for(C1=0; C1<=4; C1++){
Dig[0][C1][0]=1; Dig[0][C1][2]=1;
Dig[1][C1][2]=1;
Dig[3][C1][2]=1;
Dig[4][C1][2]=1;
Dig[6][C1][0]=1;
Dig[7][C1][2]=1;
Dig[8][C1][0]=1; Dig[8][C1][2]=1;
Dig[9][C1][2]=1;
    }

Clk=10000;

L=2; //Think about incorporating overflow protection for L,R
R=4;

//Print Left Digit to Matrix @ (3, 0)

for(C1=0; C1<=4; C1++){             //For some reason produces column of 1s at numpad 6
    for(C2=0; C2<=2; C2++){     
    Mat[C1+3][C2]=Dig[L][C1][C2]; 
    printf("%d", Dig[L][C1][C2]);       //Debug
    }
printf(" %d %d %d\n", L, C1, C2); //Debug
}

//Print Right Digit to Matrix @ (3, 5)

for(C1=0; C1<=4; C1++){             //For some reason produces column of 1s at numpad 1
    for(C2=0; C2<=2; C2++){
    Mat[C1+3][C2+5]=Dig[R][C1][C2];
    }
}

//X Test Pattern

//for(C1=0; C1<=7; C1++){
//  Mat[C1][C1]=5;      
//  Mat[7-C1][C1]=5;
//}

usleep(Clk);

    //while(1){

//Breakfree [NOT FUNCTIONAL]

//Break_String=getch(); (Getch is not ANSI, need ncurses)

//if(Break_String != -1){
//  if(Break_String = 27){
//  break;
//  }
//}     

//Terminal Display 

//for(C3=0; C3<=9; C3++){           //Debug Digit array [Successful, numbers draw correctly]
//  for(C2=0; C2<=4; C2++){
//      for(C1=0; C1<=2; C1++){
//          printf("%d", Dig[C3][C2][C1]);
//      }
//  printf("\n");
//  }
//printf("\n");
//usleep(1000000); //Debug
//}

usleep(3000000); //Debug

for(C1=0; C1<=7; C1++){             //Prints to terminal every second, when looping 
    for(C2=0; C2<=7; C2++){
    printf("%d", Mat[C1][C2]);
    }
printf("\n");
}

printf("\n"); 

//Hardware Display

for(C1=0; C1<=29; C1++){                //30 Hz
    for(C3=0; C3<=7; C3++){             //COLUMN
        //printf("%d %d \n", C3, C1);       //Loop Debug
        usleep(1000);
        //CLOCK GROUND TO GO HERE, OUT STATUS
        //for(C2=0; C2<=7; C2++){       //PX
            //outb(Mat[C3][C2], BASEPORT);
        //}
    }
usleep(4*Clk);
}

//} 

//ioperm(BASEPORT, 3, 0);
exit(0);
}

Also, I had to make my Sprite array bounds each one bigger than they should have been to make it work. 另外,我必须使Sprite数组的边界比使该数组工作的边界大。 I figure this is all some some memory snafu but I'm nowhere near that proficient in C to know what to do. 我认为这全都是一些内存问题,但是我对C如此精通,不知道该怎么做。

I would greatly appreciate any help. 我将不胜感激任何帮助。

I need to look through it more but one problem off the bat is that you're driving an 8x8 LED matrix but using a 7x7 matrix to hold the data. 我需要更多地研究它,但是一个问题是,您正在驱动一个8x8 LED矩阵,但使用7x7矩阵来保存数据。 Declare your matrix as: 将矩阵声明为:

int Mat[8][8];

Bryan, I think you are just missing the fundamental understanding of how array indices work in C. Bryan,我想您只是缺少对数组索引如何在C中工作的基本理解。

When you declare 当你声明

int array[N]

you access the elements in a range of 您可以访问以下范围的元素

array[0] ... array[N-1]

which gives you a total of N elements. 总共有N个元素。

For example: 例如:

int array[4]

gives you 给你

array[0]
array[1]
array[2]
array[3]

for a total of 4 elements. 总共4个元素。

When looping over this array, this is the convention that's almost always used: 遍历此数组时,几乎总是使用以下约定:

for(i = 0; i < 4; i++)

I think that this issue is causing multiple problems in your code and if you go back over your arrays after understanding this you'll be able to fix the problems. 我认为这个问题在您的代码中引起了多个问题,如果您在理解了这一点之后又回到了数组上,便可以解决问题。

Bryan, I don't see the problem offhand, but what you're describing sounds like you are having an array indexing issue. Bryan,我认为问题暂时没有解决,但是您所描述的听起来好像有数组索引问题。 (Rule of thumb, any time you think that there's something wrong with the computer causing your errors, you're mistaken.) (经验法则,任何时候只要您认为计算机出了问题而导致错误,您就被误认为是错误的。)

Two places new C programmers run into trouble with this is getting confused by 0 based indices -- an array of size 7 has indices from 0..6 -- and by not realizing that arrays are just laid out on top of one blob of memory, so an array that's [10][5][2] is really just one 100-cell piece of memory. 新C程序员遇到的两个问题是,它们被基于0的索引所迷惑-大小为7的数组具有从0..6开始的索引-并没有意识到数组只是放在一个内存块的顶部,因此[10] [5] [2]数组实际上只是一个100单元的内存。 If you make an indexing mistake, you can put things in what appear to be random places. 如果您犯了索引错误,则可以将内容放在看起来随机的地方。

I'd go through the code and check what's where in smaller steps; 我将遍历代码,并以较小的步骤检查什么地方。 what happens after one initialization, that sort of thing. 初始化之后会发生什么事情。

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

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