【问题标题】:"Refreshing" 8051 Microcontroller :“刷新”8051微控制器:
【发布时间】:2015-03-11 16:50:38
【问题描述】:
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 分钟后,LCD 变得像“asda68!#123646$#$^%&*(dfas”那种垃圾!。

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

【问题讨论】:

  • 为什么每个打印行的开头都有分号? free(count) 不会做任何事情,除非可能会破坏一些内存,因为 count 不是指针。
  • 你已经声明了int count; 那么free(count); 应该做什么?
  • 抱歉,我删除了。
  • 在任何情况下,您都应该使用定时器寄存器(最好使用边沿触发捕获)来测量频率,而不是软件循环。
  • 不应该itoa()带一个字符串参数吗?

标签: c refresh project 8051


【解决方案1】:

你的功能:

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);}
    }
}

【讨论】:

  • 我收到一个错误:非指针类型从“int”类型转换为通用指针
  • 你能再具体一点吗?
  • 对不起。我是新来的,还没有获得网站的所有功能!顺便说一句,这里的人很棒!所以..为什么会出现这个错误。?
  • 哪一行给出了错误?这是上坡;-) 请用黑白拼写出来。
  • 等一下..呵呵...我需要在哪里添加“免费”行命令?
猜你喜欢
  • 2010-10-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多