EmbDev.net

Forum: µC & Digital Electronics AVR Studio - ATmega8 - display f_cpu on lcd


Author: micro u. (Guest)
Posted on:

Rate this post
0 useful
not useful
hi
all this program should do is do print 2 lines:
1.) either "f ok" or "f not ok"
2.) f_cpu = 3686400


but all i get is:
1.) f not ok
2.) f_cpu = 0016384

i noticed that the factor between those 2 numbers is 225,.. no clue why!


i dont really get it.
#define should replace in the pre-prossessor every "F_CPU" with 
"3686400" right?

so if i write
const int freq = F_CPU;
 its exactly the same to
const int freq = 3686400;
 isnt it?

thanks for ur help
luk


#define F_CPU 3686400   // max 3686400 = 3,6MHz
#include <avr/io.h>    
#include <util/delay.h> 
//#include <stdio.h>
#include "lcd-routines.h"
#include <math.h>

const int freq = F_CPU;
int temp = 0;
int counter = 0;

int main(void){
  
  lcd_init();
      
  for(counter = 7; counter>=0; counter--){
    
    temp = freq * pow(10, -(counter-1));
    
    if(counter==7){
      if(freq==3686400){
        lcd_string("f ok");
      } else {
        lcd_string("f not okay");
      }        
      lcd_setcursor( 0, 2 );
      lcd_string("F_CPU = ");
    }
    
    for( ; temp>=10; ){
      temp = temp%10;
    }
     
    switch(temp){
      case 0:  lcd_string("0"); break;
      case 1:  lcd_string("1"); break;
      case 2:  lcd_string("2"); break;
      case 3:  lcd_string("3"); break;
      case 4:  lcd_string("4"); break;
      case 5:  lcd_string("5"); break;
      case 6:  lcd_string("6"); break;
      case 7:  lcd_string("7"); break;
      case 8:  lcd_string("8"); break;
      case 9:  lcd_string("9"); break;
    }
    
  }
  
   return 0;
}

Author: Peter II (Guest)
Posted on:

Rate this post
0 useful
not useful
Hi,

int is 16bit!

3686400 neet more then 16bit.

Author: mschoeldgen (Guest)
Posted on:

Rate this post
0 useful
not useful
Even if your int really is a 16-bit int, it would never do to store 
3686400 in it.
You'll have to declare your variable a 'long' and then store F_CPU in 
it. The result is a truncation of 3686400 = 0x384000 to the lower 2 
bytes resulting in 0x4000 = 16384.

Author: micro u. (Guest)
Posted on:

Rate this post
0 useful
not useful
yes??

i thought that int is 16-32 bit, so it is -2^31 to 2^31-1, which is much 
bigger than 3.6MHz

anyway, i check it. thanks for your help

Author: mschoeldgen (Guest)
Posted on:

Rate this post
0 useful
not useful
Well, your posted results clearly show that your int is a 16bit int. Its 
one of the reasons why we tend to use explicit varibales these days, as 
int differs from one architecture to the next.
If you use e.g. 'uint32_t' the problem never arises as you know its a 
32 bit number.

Reply

Entering an e-mail address is optional. If you want to receive reply notifications by e-mail, please log in.

Rules — please read before posting

  • Post long source code as attachment, not in the text
  • Posting advertisements is forbidden.

Formatting options

  • [c]C code[/c]
  • [avrasm]AVR assembler code[/avrasm]
  • [code]code in other languages, ASCII drawings[/code]
  • [math]formula (LaTeX syntax)[/math]




Bild automatisch verkleinern, falls nötig
Note: the original post is older than 6 months. Please don't ask any new questions in this thread, but start a new one.