EmbDev.net

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


von micro u. (Guest)


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
1
const int freq = F_CPU;
 its exactly the same to
1
const int freq = 3686400;
 isnt it?

thanks for ur help
luk

1
#define F_CPU 3686400   // max 3686400 = 3,6MHz
2
#include <avr/io.h>    
3
#include <util/delay.h> 
4
//#include <stdio.h>
5
#include "lcd-routines.h"
6
#include <math.h>
7
8
const int freq = F_CPU;
9
int temp = 0;
10
int counter = 0;
11
12
int main(void){
13
  
14
  lcd_init();
15
      
16
  for(counter = 7; counter>=0; counter--){
17
    
18
    temp = freq * pow(10, -(counter-1));
19
    
20
    if(counter==7){
21
      if(freq==3686400){
22
        lcd_string("f ok");
23
      } else {
24
        lcd_string("f not okay");
25
      }        
26
      lcd_setcursor( 0, 2 );
27
      lcd_string("F_CPU = ");
28
    }
29
    
30
    for( ; temp>=10; ){
31
      temp = temp%10;
32
    }
33
     
34
    switch(temp){
35
      case 0:  lcd_string("0"); break;
36
      case 1:  lcd_string("1"); break;
37
      case 2:  lcd_string("2"); break;
38
      case 3:  lcd_string("3"); break;
39
      case 4:  lcd_string("4"); break;
40
      case 5:  lcd_string("5"); break;
41
      case 6:  lcd_string("6"); break;
42
      case 7:  lcd_string("7"); break;
43
      case 8:  lcd_string("8"); break;
44
      case 9:  lcd_string("9"); break;
45
    }
46
    
47
  }
48
  
49
   return 0;
50
}

von Peter II (Guest)


Rate this post
0 useful
not useful
Hi,

int is 16bit!

3686400 neet more then 16bit.

von mschoeldgen (Guest)


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.

von micro u. (Guest)


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

von mschoeldgen (Guest)


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.