Half speed - what am I missing?

Go To Last Post
9 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I have problem with my hardware running at half speed.

It is a ATmega88 and it should run with the internal oscullator. Before I changed the fuse bits so it should run at the internal oscillator (this is default so it do not need to be changed) and without the CLKDIV8. Now I changed it so the code changes the CKDIV to DIV0 but now the clock runs at half speed for some reason. I do calibrate the OSCCAL register using an external 32kHz crystal but I havent changed anything there. Also I do not think it i possible to set the clock so low. I have tried to set my UART speed to double speed and if I do so it works so it is more or less exactly half the speed and that makes me think it is some simple setting I have forgot.

I have checked in Studio that the CKDIV is beeing set to 0.

My favorites:
1. My oscilloscope, Yokogawa DLM2024.
2. My soldering iron, Weller WD2M, WMRP+WMRT.
3. JTAGICE3 debugger.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Can you double-check that the calibration is not the cause? Since you have a scope, the easiest method would probably be to enable the CKOUT and see how fast the clock is before and after the calibration.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:

changes the CKDIV to DIV0

Tell more about this. If you set CLKPS0 in CLKPR to 1 with the proper sequence, you will be dividing the system clock by 2.

Lee

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Ok, this is not the error that I believed. The frequency is correct, it is the baud rate that is for some reason halfed. It took even longer time for me to check as I output a signal on a pin and measured the frequency but of course it takes two loops to form high and low cycle and that also creates half the speed.... Embarrasing.

Well, now I have to find out whats wrong in my baud rate setting function but that's another story.

My favorites:
1. My oscilloscope, Yokogawa DLM2024.
2. My soldering iron, Weller WD2M, WMRP+WMRT.
3. JTAGICE3 debugger.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:

...it is the baud rate that is for some reason halfed. It took even longer time for me to check as I output a signal on a pin and measured the frequency but of course it takes two loops to form high and low cycle and that also creates half the speed....

Remember that sending the 'U' character with the U(S)ART gives a nice 1/0 bit pattern. At N,8,1 and continuous transmission it should read nicely on a frequency meter.

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Yes, UBRR0L gets wrong. It sets to 0x17 but should be 0x0B. I must have changed something that I am not aware of ("that shouldn't affect that") because the calculation code that I use is not changed.

This is the code that does the setting:

void uartSetBaudRate(u32 baudrate)
{
	// calculate division factor for requested baud rate, and set it
	unsigned short bauddiv = ((F_CPU+(baudrate*8L))/(baudrate*16L)-1);
	UART_UBRRL = bauddiv;
	#ifdef UART_UBRRH
		UART_UBRRH = bauddiv>>8;
	#endif
}

parameter baudrate is 38400.

My favorites:
1. My oscilloscope, Yokogawa DLM2024.
2. My soldering iron, Weller WD2M, WMRP+WMRT.
3. JTAGICE3 debugger.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:
Equation for Calculating UBRRn Value
UBRRn = fOSC/(16 * BAUD) - 1

from the datasheet.

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Increadible, how can this ever have worked? Maybe I had set the double speed bit before.
Anyway, thanks for the help.

My favorites:
1. My oscilloscope, Yokogawa DLM2024.
2. My soldering iron, Weller WD2M, WMRP+WMRT.
3. JTAGICE3 debugger.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

OK, I finally found the reason, something very unexpected. The #define F_CPU line was corrupted by a file that have been by mistake copied into my library folder. I was beginning to be suspicius after finding that F_CPU had the wrong value and for some reason it was 0. I made another declaration and finally made the compiler to complain about redeclaration. It didn't did that before as it was just including the wrong file.

But what about the baud rate calculation? The calculation by Pascal Stang works just as well as the one in the data sheet. I haven't figured out the formula in either the datasheet or Pascals version but a check ion Excel gives that Pascals version may resulting in 0.5 more so maybe it is some rounding safety. This is the formula:

u16 bauddiv = ((F_CPU+(baudrate*8L))/(baudrate*16L)-1);

And the typical from the data sheet as said before:

F_CPU/(16 * BAUD) - 1;

My favorites:
1. My oscilloscope, Yokogawa DLM2024.
2. My soldering iron, Weller WD2M, WMRP+WMRT.
3. JTAGICE3 debugger.