AVR Studio - ATmega8 - display f_cpu on lcd

Guest #3027410
Rate this post
• ▲ 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
}
Guest #3027421
Rate this post
• ▲ 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.
Guest #3027430
Rate this post
• ▲ 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

Note: the original post is older than 6 months. Please don't ask any new questions in this thread, but start a new one.

Formatting
  • [c]C-Code[/c]
  • [code]code in other languages, ASCII drawings[/code]
  • [math]formula (LaTeX syntax)[/math]
  • [[Title]] – link to an article
  • Link to another post: right-click its post ID (#8078628), copy the address, paste it into the text

More about formatting

Post long source code as an attachment, not in the text.

Already registered? Log in

Not shown to anyone. We send you a link; your post appears once you confirm it there.

By submitting you create a free account and accept the terms of use.