gpt4 book ai didi

c - "Refreshing"8051微 Controller :

转载 作者:行者123 更新时间:2023-11-30 17:22:41 25 4
gpt4 key购买 nike

void main(void)
{
unsigned char in_char;
int flag=1,j,i;
int count, d = 0 ;
char s[4]="",p;
ithul();
LCD_res();
init_lcd()
;print_lcd(1,"The Project");//Printing command.
;print_lcd(2,"of Me:");//Printing command.
for(i=0; i<20; i++){delay(1000);delay(1000);delay(1000);}
for(i=0; i<20; i++){delay(1000);delay(1000);delay(1000);}

while(1)
{
count = 0;
loop1 : if(P3_0 == 1){
goto loop1;
}
//Running '1'
loop2 : if(P3_0 == 0){
goto loop2;
}
//Running '0'
while(P3_0 == 1){
count ++;
}
init_lcd()
;print_lcd(1,"Done Counting!");//Printing command.
for(i=0; i<1; i++){delay(1000);delay(1000);delay(1000);}

;print_lcd(2," - L/H...");//Printing command.
for(i=0; i<20; i++){delay(1000);delay(1000);delay(1000);}
for(i=0; i<20; i++){delay(1000);delay(1000);delay(1000);}
for(i=0; i<20; i++){delay(1000);delay(1000);delay(1000);}

;print_lcd(2,itoa(count/200));//Printing command.
for(i=0; i<20; i++){delay(1000);delay(1000);delay(1000);}
for(i=0; i<20; i++){delay(1000);delay(1000);delay(1000);}
}
}

这是我的代码。它的作用是从端口获取频率,读取它并将其打印到 LCD。

它的作品!但大约 2 分钟后,液晶显示屏变得像“asda68!#123646$#$^%&*(dfas”一样的垃圾!。

如何刷新 8051 微 Controller 以免它变得疯狂?我听说过一些关于“新鲜”“免费”“闪存”命令的信息,但我真的不知道它是什么或如何使用它。所以..请帮忙!

最佳答案

你的功能:

char * itoa( int x ) { 
const unsigned int BASE = 10;
unsigned int u = abs( x );
size_t n = 0;
unsigned int tmp = u;
char *s;
do { ++n; }
while ( tmp /= BASE );
n += x < 0;
s = malloc( ( n + 1 ) * sizeof( char ) );
s[n] = 0;
do { s[--n] = u % BASE + '0'; }
while ( u /= BASE );
if ( x < 0 ) s[--n] = '-';
return s;
}

每次调用 itoa() 时,它都会为字符串 s 分配更多内存,一段时间后它会耗尽内存但从不检查返回值来自malloc()。您需要将 free() 放回到程序中。

char *strg;
...
strg = itoa (count / 200);
print_lcd(2,strg);
free(strg);

所以整个代码看起来像这样,看看注释的地方。

void main(void) {
char *strg; // new variable
unsigned char in_char;
int flag=1,j,i;
int count, d = 0 ;
char s[4]="",p;
ithul();
LCD_res();
init_lcd();
print_lcd(1,"The Project");//Printing command.
print_lcd(2,"of Me:");//Printing command.
for(i=0; i<20; i++){delay(1000);delay(1000);delay(1000);}
for(i=0; i<20; i++){delay(1000);delay(1000);delay(1000);}

while(1) {
count = 0;
loop1 : if(P3_0 == 1){
goto loop1;
}
//Running '1'
loop2 : if(P3_0 == 0){
goto loop2;
}
//Running '0'
while(P3_0 == 1){
count ++;
}
init_lcd();
print_lcd(1,"Done Counting!");//Printing command.
for(i=0; i<1; i++){delay(1000);delay(1000);delay(1000);}

print_lcd(2," - L/H...");//Printing command.
for(i=0; i<20; i++){delay(1000);delay(1000);delay(1000);}
for(i=0; i<20; i++){delay(1000);delay(1000);delay(1000);}
for(i=0; i<20; i++){delay(1000);delay(1000);delay(1000);}

strg = itoa (count / 200); // keep the returned pointer
print_lcd(2,strg); // display the "frequency"
free(strg); // free the pointer memory
for(i=0; i<20; i++){delay(1000);delay(1000);delay(1000);}
for(i=0; i<20; i++){delay(1000);delay(1000);delay(1000);}
}
}

关于c - "Refreshing"8051微 Controller :,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27927104/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com