Today's the day

向软件大牛炫耀我会焊单片机,向硬件大牛炫耀我会写 Rails,向软硬件大牛炫耀我生物,向软硬件生物大牛炫耀我会折腾期货 -_-bbb

终于搞了个热风枪
初步愿望达成

犯了一系列愚蠢的错误……

galeki posted @ 2009年3月07日 19:42 in 单片机 , 6387 阅读

在个小 CPU 上写个读 EEPROM 的代码,每页 EEPROM 有 256 个字节,循环依次读出来,于是顺手一写:

for( i = 0; i < 256; i++ )
{
     read_eeprom();
     ...

执行之后却没有效果,debug 发现 read_eeprom() 这个函数根本就没有执行,for 循环根本没进去……

愣了半晌,顿悟。因为是一块 8 位的 CPU,所以 256 越界了,被当成 0 了,自然执行不了。

于是立刻顺手一改:

for( i = 0; i <= 255; i++ )
{
     read_eeprom();
     ...

心想这下肯定没问题了~

结果发现写了 256 个数据之后还在写,循环跳不出去了。

愣了两个半晌才醒悟,这次是 i 越界了,8 位的无符号整数数自然永远小于等于 255,汗……

这样的话要咋办捏,难不成要写成:

for ( i = 0; i <= 255; i++ )
{
     read_eeprom();
     if(i == 255)
          break;
     ...

嗯,看上去很郁闷……

最后,终于彻底醒悟:

for( i = 1;  i;  i++ )
{
     read_eeprom();
     ...

 基础啊基础……


Update: 除了发现上面最后一条又写错之外,还是老老实实用 do_while 了:

do{
   read_eeprom();
   i++;
}while(i);

 

 

  • 无匹配
  • 无匹配
Avatar_small
LiYanrui 说:
2009年3月15日 03:42

do {
    read_eeprom ();
} while (++i);

 

 

Avatar_small
LiYanrui 说:
2009年3月15日 03:44

传说 ++i 要比 i++ 效率高,有空可以反汇编看看

Avatar_small
galeki 说:
2009年3月15日 13:38

@LiYanrui: 忘了 ++i 和 i++ 的区别了,没敢用 -_-!

t.k. 说:
2013年5月07日 12:52

哈哈,看得我笑了

Anonymous 说:
2022年9月13日 22:07

Gives you the best website address       I know there alone you'll find how easy it is. Orlando Termite Control

Anonymous 说:
2022年9月13日 22:07

You bear through a awesome vacancy. I sanity definitely quarry it moreover personally suggest to my buddys. I am self-possessed they determination be benefited from this scene. Bed Bug Treatment Tampa

Anonymous 说:
2022年10月13日 13:21

I always enjoy locating a site that provides useful info because I like finding out new things. I wanted to convey that I discovered the information on your site was really entertaining and that I learnt new things. I shall be certain to look out for your future post.. Thank you for the wonderful article I will revisit in the future. attorney conference call

Anonymous 说:
2022年11月26日 20:28

I encourage you to read this text it is fun     described ... ufabet เข้าสู่ระบบ

Anonymous 说:
2022年11月27日 23:39

This is very useful, although it will be important to help simply click that web page link: Terra Hill Price

Anonymous 说:
2022年12月01日 12:20

I invite you to the page where you can read       with interesting information on similar topics. event app

Anonymous 说:
2022年12月15日 22:31

Your blogs further more each else volume is so entertaining further serviceable It appoints me befall retreat encore. I will instantly grab your rss feed to stay informed of any updates. 3vr4m

Anonymous 说:
2022年12月27日 15:22

Beaver says I also have such interest, you can read my profile here:  Tembusu Grand


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter