Is it possible to write the OSCCAL register?

Is it possible to write the OSCCAL register from Arduino language?

I want to use atmega168 with internal RC oscillator (outside arduino board) and I want to write to OSCCAL to calibrate the oscillator. (as recommended at http://www.avrfreaks.net/modules/FreaksArticles/files/21/Why%20you%20need%20a%20Clock%20Source.pdf)

Did anybody tried that with Arduino language?

Don't quote me on this, but I'm pretty sure you'd have to change the bootloader to change the clock frequency aswell as some of the arduino core. Can anyone else confirm?

Don’t quote me on this, but I’m pretty sure you’d have to change the bootloader to change the clock frequency aswell as some of the arduino core. Can anyone else confirm?

Sure in http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1163418637

they explain that you must modify arduino settings file to
build.f_cpu=1000000L

but let´s say that I´m using arduino without a bootloader, imagine that I program it directy via ICSP, but using Arduino generated hex files. Is it possible to set the OSCCAL from Arduino language?

I guess that I can modify the bootloader to do it but I wonder if there is any other way.

I'll have to read the datasheet again to check, but if OSCCAL is just another register you should be able to write to it from arduino. It should be no different than writing PORTB = 0xEF to set the portb register to 11101111 as an example. The thing to check in the data sheet is about setting fuses and whether they have anything to do with the internal oscillator calibration.

Thank you, I will try to read the datasheet more in detail as well.

But is good to know that the syntax is supposed to be just OSCCAL=0x3D. I will try that and see if it compiles at least.

By the way, at http://www.avrfreaks.net/modules/FreaksArticles/files/21/Why%20you%20need%20a%20Clock%20Source.pdf they say that the procedure is more or less the following one:

1) use STK500 or similar to do the calibration and get the calibration byte value that you should use. 2) write that value to the eeprom to some known position 3) read that value from your sketch and copy it to OSCCAL,

Do you know if the contents of the eeprom will be erased when loading a new Arduino sketch? or does it only overwrites the data it needs? I was thinking that maybe I can store it at the end of the eeprom.

Anyway, I will try this when I receive my AVR programmer and I will post the result in here.

If you know your calibration value I don't know why you'd have to write it to EEPROM first before you burn your code. I would think you'd be able to hard code it into your sketch and write it directly, unless the OSCCAL register is located in EEPROM, which wouldn't make sense to me.

If you know your calibration value I don't know why you'd have to write it to EEPROM first before you burn your code. I would think you'd be able to hard code it into your sketch and write it directly, unless the OSCCAL register is located in EEPROM, which wouldn't make sense to me.

Of course you can hardcode into the sketch, but if you are going to write the same sketch to 5 arduinos/atmegas then you have to have 5 differentes sketches each one with a different calibration byte hardcoded in it. If you have the calibration byte on the EEPROM (in the same position in every arduino) then you can upload the same sketch to all of them. But you´re right for my hobbyist purposes hardcoding it should be enough.

I didn't think about using it for multiple chips. That would be a perfect reason to use the EEPROM.

Do you know if the contents of the eeprom will be erased when loading a new Arduino sketch?

My understanding is that the contents of the EEPROM are preserved when a new sketch is loaded--it's a command-line configuration option for the avrdude utility used to upload the compiled sketch.

--Phil.

I don't see how lilypad gets away with this. But then again the lilypad pictures show a crystal even though I read the fuse setting in boards.txt to be Internal RC. I'm confused about that.

Anyway, I almost always have to write OSCCAL to get reliable serial. I am going to look into modding the bootloader. I think the right way to do it is to declare a var in the bootloaders own flash program space. Then we can use a regular SPI programmer and AVR Studio to get the correct value for the chip and drop it right into the address of the flash variable.

the newest version of the LilyPad does use an external resonator, but earlier versions didn't. The lilypad bootloader that ships w/ the software is still the internal oscillator version so that people with older boards won't render them useless if they burn them w/ the external oscillator bootloader.

the internal clocks on the AVRs are surprisingly good (much better than PICs in my experience.) You can't do high speed serial communication, but you're fine up to 19200 or so... :-)