Programming AVR fuse bits – oscillator settings

I guess many of you were confused when programming AVR fuse bits. I get many newbie questions like “I programmed Atmega8 but it doesn’t work”. Then my standard answer is: “Did you touch configuration bits?” and if yes, then I am almost 90% sure that he did it wrong. Most of them understands wrongly that programmed fuse bit in configuration should be left unchecked and opposite. Let’s take a look at widely used programming software – PonyProg.

First thing you do before programming chip is set configuration bits (Atmega8 in picture):

image001.jpg

The first attention should be paid to clock sources. There are Four bits controlling Atmega8 clock sources: CKSEL0, CKSEL1, CKSEL2, CKSEL3.

As you may already know, Atmega8 can have 4 different clock sources:

  • External Crystal or Resonator;
  • External Low Frequency Crystal;
  • External RC Oscillator or Calibrated Internal RC Oscillator;
  • External Clock source.

In the table you see adequate CKSEL0..3 settings

image003.jpg

As you may noticed, for most of them there are ranges of settings left. For example External Crystal or Resonator

External Crystal or Resonator may be set from 1010 to 1111 (Note that ‘0’ programmed while ‘1’ means unprogrammed). These ranges are left for user to select microcontroller startup times to stabilize oscillator performance before first instruction.

If you connect external Crystal oscillator or resonator to XTAL1 and XTAL2

image005.jpg

There comes another Fuse bit CKOPT. This bit selects two different modes of oscillator amplifier. If CKOPT is programmed (unchecked), then Oscillator oscillates a full rail-to-rail output. This is handy when environment is noisy or you use XTAL2 to drive another microcontroller or buffer. But in other hand this takes more power. If CKOPT is unprogrammed (checked), than the swing is smaller and power consumption is less as well. Typically if resonator is up to 8MHz then leave CKOPT (checked=unprogrammed) and programmed (unchecked) above 8MHz.

In table you see selectable crystal oscillator modes:
image007.jpg

CKSEL0, SUT0 and SUT1 bits in this case are used to select startup times of microcontroller – how many clock cycles to wait before execution first command. These settings are necessary to ensure stability of ceramic resonators and crystals.

image009.jpg

Note 1 .  These are fast startup times – not suitable for crystals;

Note 2.  Most used with ceramic resonators to ensure stable startup. This is OK to use with crystals as well, but device is not operating at maximum frequency.

So if you want stable system startup, it is better to leave CKSEL0, SUT0 and SUT1 programmed (unchecked) what means about 65ms startup time.

Low frequency oscillator

There is only one bit set for CKSEL0, CKSEL1, CKSEL2, CKSEL3 – 1001 (unchecked, checked, checked, unchecked) For Low frequency oscillator. Low frequency crystal is typical clock crystal where frequency is 32768kHz. The main options for this mode are setting crystal startup times with SUT0 and SUT1 fuse bits.

image011.jpg

Note 1. Less stable startup.

External RC oscillator

The frequency in this mode is calculated by formula f=1/(3RC) while C should be at least 22pF. If CKOPT is programmed (unchecked), then Internal capacitor 36pF between XTAL1 and GND is enabled, then there is only resistor needed.

image015.jpg.. image013.jpg

And again the table of operating modes:

image017.jpg
And startup times:
image019.jpg

Note 1. don’t use this setting when close to the maximum frequencies.

Calibrated internal RC oscillator

Calibrated internal RC oscillator is pretty often used, because there is no need any external components and it can provide fixed 1, 2, 4 and 8 MHz clock. This mode is selected by programming fuse bits in range 0001 to 0100.Leave CKOPT bit unprogrammed for this mode (checked).

Operating modes:

image021.jpg

Note 1. by default this option is selected for all Atmega8

And start up option table:
image023.jpg

Note 1.  by default this option is selected for all Atmega8

About internal RC oscillator calibration read in device datasheet.

External Clock

image025.jpg

External clock – this is the main mistake the newbie’s are making when starting their projects. To enable external clock source you have to leave CKSEL0, CKSEL1, CKSEL2, CKSEL3 bits unprogrammed – this means checked. People make mistake checking them and thinking that they are enabling external crystal resonator and then they are wondering why it is not working.

And again there is a external clock source startup table controlled by SUT0 and SUT1:

image027.jpg

If you uncheck CKOPT then you will enable the capacitor between XTAL1 and GND.

Examples on how to program Atmega8 clock sources correctly

  • 8MHz Crystal with slowly rising power:
image029.jpg
  • 2MHz ceramic resonator fast raising power:

image031.jpg

  • 32768kHz resonator stable frequency at startup:
image033.jpg
  • External RC 4MHz oscillator with internal C enabled fast rising power:
image035.jpg
  • If you want to drive maximum from your Atmega: then select 16MHz fast rising power:
image037.jpg

You may find all this information in datasheets. But like always – people forget to read them.

If there is any questions or concerns don’t hesitate to ask.

97 Comments:

  1. Please explain to me about setting ATMega16 internal oscillator fuse bit. I found a problem with my Mega16, i try to the ADC in Mega16 using 12 MHz Crystal(i do nothing on fuse bit) and that works (no problem), that means my M16 works (not dead). But when i test the m16 serial usart connect to my comm port pc (i observe the data from M16 usart with hyper terminal), that is not works, i try using cystal 12 Mhz, 4 MHz, 8 Mhz and also change the baud rate from 19200, 9600, 2400, and that is still remain NOT WROKS, why ..??
    please help me..

  2. If you didn’t touch fuse bits your ATmega16 works with internal 1MHz frequency. ADC works with this too, just sampling freq is lower.
    What about USART if you just exchange crystals, but fuses indicates that AVR works with internal freq generator, then it shouldn’t work.
    If you want Atmega work with external crystal, in PonyProg uncheck all CKSEL3..0 bits.

  3. i unintentionally programmed the fuses on an ATtiny2313 to work on External clock, and off course not working, and i am confused, i can find any article on how to recover the first setting, or change it to anything else, plz help

  4. I suggest reading life-giving-to-atmega8 article, where I was trying to do the same with Atmega8’s. Just fact is that they were really burned 😉

  5. I have read about fuse bits in the datasheet for the ATMega8, and have read this article as well, but I use avrdude (on Linux) and I am still confused.

    It seems that there are hfuse and lfuse (something to do with high byte and low byte of fuses), but I can’t figure out where the different bits are placed in these bytes?

    And how am I going to set the bits using a 12 MHz crystal? The sheet I have been reading only specifies up to 8MHz?

    Kindest reagrds

  6. I would suggest reading this forum post.
    AVR Fuse Bits
    Might this will clear things out.

  7. Brilliant – thank you!

    Kindest regards

  8. Hello everyone!

    I am trying to hook my Mega8515 to run from external 4Mhz crystal, with 2×27 pF capacitors.
    Even if i set the fuses to run from internal 4Mhz, or external crystal, when I try to read program memory, PonyProg reads the first ~400-500 bytes O.K., but after that it gets garbage (I’m not talking about 0xFFs, i am talking about random data).
    If I remove the capacitors and the crystal it works O.K…. what could be wrong? Is my crystal blown?

  9. I programmed one Atmega8 with AVR ISP programmer ( bulit without any buffer ) and it worked fine with right fuse bits configs.
    later I had to program another one and I set fuse bits wrong and now second atmega8 is unreadable and nor write-erase operation can be prepared…
    according to the article “life-giving-to-atmega8 ” do you mean I have to put it to trash ? or is there any way to reprogram fuse bits with some “stand-alone AVR (non ISP) programmer” ?
    there is some avr programmer in markets that they are expensive and stand-alone which I guess they might be able to reprogram fuse bits and eeprom area.
    I hope you guide me

  10. I guess you unprogrammed all CKSEL fuse bits, what means clocked by external clock source. One way to restore Atmega is to connect a clock generator to your Atmega and try to reprogram fuses with ISP. As generator can be used another AVR with simple program like this:
    int main(void)
    {
    DDRB = 0b11111111;
    DDRD= 0b1111111;
    TCCR0A = (1<
    If this doesn't work - I suggest not to waste time and get new one 😉

  11. thanks a lot but I asked about stand alone parallel avr programmers which seems to be able to reprogram fuse bits and more vs serial device programmers …
    I can’t find any schematic for parallel programmers.
    can you tell me is there any programmer to do that job? and how to make my own etc… ?
    I know so many COM or LPT programmer and softwares like ponyprog which they are NOT able to work with fuse bits well but I still don’t know which are able to do it.

  12. You may try AVR serial and parallel High-Voltage programmer. I didn’t try this one, so I cannot help much on that for now.

  13. what about fuses for 11,0592MHz crystal oscillator (mega32)?
    i can’t find them in datasheet 🙁 up to 8MHz only)

  14. Nik, You are asking about fuse settings for 11,0592MHz for Atmega32?

  15. After I programmed my first mega16’s bits to 0 (ALL of them) I blamed the home-made programmer for not being able to access the chip in PonyProg. So I bought another chip and again, not minding the documentation, I unchecked all the possible bits in the window. What should I attach to the XTAL1 and XTAL2 pins to get my chips back running?

    Best regards

  16. Hi scienceprog,
    I don’t understand;
    “1” means unprogrammed(checked) and “0” means programmed(unchecked)?
    I think that “1” means unprogrammed (unchecked, not checked!).
    Regards.

  17. Help needed… I made the biggest nOOb mistake ever, great! I fused the Atmega16 for running on external clock and not external crystal. My question, can I just add a clock signal to the XTAL1 without removing the crystal (all SMD components)? Maybe I can ground the XTAL2 and then add the clock to XTAL1? My circuit looks like this: http://www.idt.mdh.se/rc/sumo/pcb/Schema.pdf

    Which frequency should I add to the XTAL1?

  18. I don’t understand;
    “1″ means unprogrammed(checked) and “0″ means programmed(unchecked)?
    I think that “1″ means unprogrammed (unchecked, not checked!).

    This explanation is according to ponyprog. Check-box checked means programmed(bit=0). Unchecked box means unprogrammed(bit=1). AVR fuses are programmed when bits have values = 0. Look at first picture of article where fuse setting window is. I understand this causes some confusion – sometimes for me two 🙂

  19. To Real nOOb,
    So you checked in ponyprog all CKSEL fuse bits set to 0? I think this should be enough to apply like 1MHz to XTAL1 lead. Frequency can be generated by another AVR or another function generator. I think lower and of course higher frequencies should work well. First thing you will have to do uncheck these CKSEL check-boxes.

  20. Ok, that did it! I just added a 1Mhz square wave to the XTAL1 and then unchecked the CKSEL boxes, thx for the help.

  21. Hi scienceprog,
    My name is Gerardo and I’m using de PonyProg2000 v2.06f BETA version to program ATmega16 Atmel family microcontrollers. I have a problem when I try to change
    the Security and Configuration Bits default value. When I change a bit value of this register (to change the frequency oscillation for example) the programmer doesn’t work any more. It’s always giving me de following error message “Device missing or unknown device -24” after
    the action, and the programmer doesn’t recognised the microcontroller any more. I tried to solve the problem following the FAQ in the help topics but it doesn’t work.
    What can I do to solve this ugly problem?
    Thanks for your reply

  22. How did you change oscillation settings? Maybe you selected external clock source – common problem.

  23. For people who bricked there Atmega, here a little tuto: “How to recover the fuses bits”

    http://www.larsen-b.com/Article/260.html

  24. From the above article you state that a fuse bit is programmed when unchecked. Looking at the picture from PonyProg that is wrong. It sure confuses me.

    Cut from the article:
    … If CKOPT is programmed (unchecked), then Oscillator oscillates a full rail-to-rail output. …

  25. i tried to use atmega16, external clock 11.059MHz
    all fuse bits are unchecked
    ponyprog can read/ write my device
    ..but, when i try to do something like PORTC=0xFF
    i only measure something like 0.5V
    can’t figure out what i did wrong

  26. Hi, after set fuse bits, my atmega16 still working but i cant write nor read its fuse/code. Was an mistake of my or another thing? tnx.

  27. Hi,
    I want to drive an external sensor using the clock from AVR 32 .But the output I am getting from the clock buffer is a sine wave 🙁 I have programmed the fuse bits as 1111 and CKOPT=0 .My freq is 13.56 M.
    Any help will be really appreciated.

    Thanx!

  28. Do you mean some dedicated MCU pin or XTAL1. Maybe you are wrong with measurements – how do you know it is a sine? Fuse settings seems to be ok.

  29. Hi
    Thanx for replying. I am using 7404 IC (NOT gate) as a buffer. I have attached the XTAL2 to this IC , and the inverted ouput that I am getting is more of a sine wave to me.
    I tried using 74C14 CMOS hex inverter but it gives a triangular wave as output 🙁
    I have tried this with both XTAL1 and XTAL2.

    Thanx!
    Aditi

  30. If you measure wave shape with oscilloscope try to change probe attenuation to like x10. I bet signal shape is OK, only you cant see it right because of probe bandwidth limitations.

  31. Hi ,
    I am interfacing a CMOS image sensor with a ATmega 32 .I have pulled the lines high. I have also checked the CMOS sensor,It is working fine.I have followed the exact code as given in AVR datasheet.Only he registers in the sensors are not getting updated.Is thr sumthing I am missing ? How can I find out where the mistake lies ?

  32. Hello!
    I need to know if I am interfacing a 5 V Atmega 32 with a peripheral like say an SRAM working at 3.3V , then do I need to scale down all my data lines to 3.3 V as well ? If I do not , the muc will drive high lines to 5V while my SRAM’s rating is only 3.3V , so will that blow it up ?

  33. You should read SRAM datasheet to find out if it tolerates 5V signal. If it tolerates 5V input, then there should not be a problem.
    If not, then you should scale dowt the signal from AVR to SRAM. You can use a transistor key powered from 3.3V or 3.3V buffer IC. You can go more rude by using a simple resistor voltage divider.

  34. how to change the baud rate of programming Atmega16 using ponyprog. I am using SPI dongle. But I want to change baud rate to a value less than 100bits/sec.
    Is it possible? how? what is the minimum baud rate that can be achieved?

  35. Got an Atmega32 and programed it with ispprog.
    I unchecked all Cksel boxes.
    I understand this is opposite to ponyprog?
    The case is i get now unknown device, i suppose in this config i have set to ext clock source?

  36. i want to configure atmega32 for 4MHZ crystal
    plz can you help ?

  37. Hi,

    I would like the fuse settings for a ATmega32 to be used with a 16MHz crystal. Could you please help me?

  38. Brilliant, you explained everything clearly. Thanks for the help.

  39. Hi Scienceprog
    I´m a beginner in Atmel mcu programming and I need help with the atmega 32.
    I programmed the atmega32 but the fuses bits are wrong.Before the first procedure programming(at-prog), the fuse bits became wrong and the miso pin is always in three state and I had never stabished communication again to fix the fuse bits.thank you very much

  40. Hi!

    I have a ATmega32 that is connected to 16MHz crystal. It was working until I changed the CKSEL setting from “Ext. Crystal/Resonator High Freq; Start-up time 16K + 64 ms” to “Int. RC Osc. 8MHz; Start-up time: 6CK + 64ms” in AVR Studio using my ISP… Now it dosent respond at all anymore… what can I do?

  41. Hi!
    I have already tried on 2 Atmega32 controllers by setting bits. But the need is I have 8MHz crystal and working with Atmega32. What are the configuration of the bits? Could you please help me?

  42. If you are using ponyprog, just uncheck all CKSEL checkboxes.

  43. My ATtiny13 locked, lockbit is in level 3
    Can I unlock it without deleting the program inside?

  44. I had some atmega8 devices (and some attiny2313’s) that I messed up. No matter what I did, I could not recover them. I did not want to order some more until I needed some other parts (to save the shipping cost).

    I did find a good programmer to fix every single one of my avr’s here:

    http://www.microcontrollerprog.com

  45. So far i’ve been running my MC at 1 Mhz internal Oscillator.

    How can i directly run my ATmega32 using 2 22pF capacitors and a 16 Mhz crystal ?
    Or is there some extra settings required ?
    What is this fuse bits ? Is it necessary to operate at 16 Mhz with a crystal oscillator ?
    Please help.

  46. Yes you can run it at 16MHz provided the speed of the chip will support it. (i.e ATMEGA32-16, can be clocked as high as 16MHz, ATMEGA32L-8 can be clocked as high as 8MHz)

    Look at page 25 of the datasheet. For more help, you can also look here:
    http://www.engbedded.com/fusecalc/

    You didn’t mention what software you are using to program the chip.

    When you program the fuses for a 16MHz external crystal, after resetting the atmega, the atmega may not respond in your ISP software unless you have an external crystal installed on the programming circuit.

    Beware, you can lock yourself out really easily!
    That is why I got the fuse restore programmer in the link above.

  47. i have tried to program my atmega16 chip with all fuse bits set to 1 unknowingly though my board works on internal crystal(clk source).is this a reason for the device not being detected by programmer software?

  48. hi
    i selected some wrong fusebits and my atmega32 got locked , it’s not responding to command . can you suggest a solution plzz

  49. Could you please let me know the low byte fuse value and high byte fuse value for ATMega32 for 16MHz crystall?

  50. To sanjay:

    Hi , I think your problem will be solved if you just connect a crystal to your MC and then test whether or not you can connect to it ?

    I hope this may help you.

  51. Pingback: Blinking the leds

  52. HEY, CAN YOU PLS CORRECT THE CONFUSION ABOUT THE programmed(0) = unchecked confusion. Many seem to have become more confused

    The PonyProg screenshot and the datasheet says:
    UNPROGRAMMED (1) = UNCHECKED
    PROGRAMMED (0) = CHECKED

  53. ya,it was so intersting,but i did not get clarified with my dout,actually i am using a 16Mhz external oscillator in atmega16,i do not know how to set fuse bit in avrdude.so can u help me out give me some sample programe and some idea.

  54. Hi, I programmed an ATmega8 with ponyprog with the following fuses:
    BODLEVEL-0
    BODEN-0
    SUT1-1
    SUT0-1
    CKSEL3-1
    CKSEL2-1
    CKSEL1-1
    CKSEL0-1

    RSTDISBL-1
    WDTON-1
    SPIEN-0
    CKOPT-0
    EESAVE-1
    BOOTSZ1-0
    BOOTSZ0-0
    BOOTRST-1

    My clock is a quartz 16MHz with 22p caps.
    When i read the micro ponyprog says it is locked
    and when i read the fuses, all of them are set to 0(set).

    Pls tell me what is the problem.Thanks

  55. I just need to know that can i change my external crystal oscillator clock frequency once i set it to say 8mhz. I just change the clock source and rise time settings and leave untouched other bits. Will my controller be locked to 8Mhz and the code inside or can i reprogram it with other clock freq?

  56. Dragos, your fuses seems to be OK, maybe your chip is locked preventing from accessing memories. Try to erase chip first, then try reading.

    Pavan, yes you can if you change in to same type of oscillator. let say if you used 8MHz crystal and you changed to 6MHz you microcontroller will work normally. As far as I tried 4 and 16MHz crystals work fine without changing any fuse settings.

  57. sir,
    please can i get the fuse bit settings for the ATmega32 working on 12MHz.

  58. Hi,

    I would like the fuse settings for a ATmega8 to be used with a 12MHz crystal. Could you please help me?
    There is no setting for fuse bits to 12MHz in Atmega8 datasheet

  59. hey,
    first ty for such a great post i really appreciate your work….
    i wish to program fuse bits for atmega32 for 16MHz crystall osci.
    should i uncheck(i.e program) cksel3..0= ‘0’(unchecked) right???? plz reply asap….
    ty

  60. also is thr any othr settings i need to perform????

  61. in ATmega8, is PB6 and PB7 can be used for I/O pin, if I used internal oscillator mode (8 MHz)?
    is there any additional circuit if I use that internal oscillator?
    thanks sir

  62. visit http://www.connectgeeks.com it is given much better there about avr tutorials visit

  63. Pingback: Programming Arduino with Arduino « Wire Jungle

  64. nice tutorial…

  65. I’m a newbie. I’m working with an ATmega32 and have the eeprom & hex files written & verified, but am told I also need to set the high fuse to 0xCF & low fuse to 0xFF. (4MHz crystal) How do I accomplish that with Ponyprog?

    Thanks.

  66. Hi everyone,
    I’m a newbie. I use atmega32, AVR Studio 4 and kit STK500, I want set Fuse bit for ext crystal (8Mhz). But in Avr Studio 4 have many selection for set Fuse bit. Pls help me, I want know detail selection. Thank you very much. (You can sent mail to votuananhs@gmail.com)

  67. I am using ATmega32 and write hex by using ponyprog.
    please help me how to setting bit fuse configure for use 12MHz external crystal oscillator

  68. Hi,
    I’m a bit confused about change fuse bits in Ponyprog. I need program a ATmega 8 with HFUSE=0xc9 LFUSE=0xef.
    May I change de fuse bits to follow the project instructions with the options in the window “confguration and security bits” of Ponyprog?

    Thank you in advance.

  69. Pingback: Paul & Sabrina's cheap DIY 144v motor controller - Page 423 - Fuel Economy, Hypermiling, EcoModding News and Forum - EcoModder.com

  70. Hi
    programmer is stk200/300
    fuse bites id for external low frequency crystal
    but for program eeror =chip is damaged
    plase help for crystal

  71. Pingback: Problem with AVR microcontroller programming

  72. Hello,
    I hav Atmega 32 with external 16Mhz crystal oscl.Connected to PC via MAX232,DB9 conn. M using Hyperterminal 2 check the respond but uC not responding.
    Have Programmed it using WinAVR.Have set the baudrate correctly,1Stop bit,Non parity,8bit data frame.
    But still no response 🙁
    Do i have to fiddle my fuse setting?If yes then which??
    Help Appreciated. 🙂

  73. Hi
    I use Atiny2313 configurated for external clock source with no prescaler.
    It work fine but XTAL2 pin cant bee used as I/O pin PA1 (is all time output with low level !).
    I’m wrong ?
    Data sheets is not precisely with this.
    Please help !

  74. i want to use crystal of 16MHz in atmega controller can you tell me how
    wil the fuse bit look like?
    what does hfuse and lfuse mean?

  75. When i m running atmega8 for long time on timer its hanging using inernal oscillator, does using external crystal will help do i use 12mhz or 8mhz?
    n plz do reply its settings also

  76. I intend to use adc and usart part of atmega32. I have already programmed the same without changing any fuse bits and it works perfectly fine so i suppose its using internal 1Mhz clock, but using this i am able to get only 4800 baud maximum and i want to have 9600 baud. So i need to use internal 4Mhz clock atleast. Please suggest me necessary changes i need to make in code and elsewhere. I am using avrstudio for coding and usb programmer for programming. I have interfaced lm35 through adc and it is working so will changing the clock speed affect that? Please reply as soon as possible. Thanking you

  77. if i want to program a new AVR controller using USB_ISP programmer,what should i do with the fuse bits and how can i do that..

  78. Hi!
    I programmed one Atmega16A-PU using the Arduino software and a USBasp Programmer.
    I previously defined the board using the fuse calculator utility from http://www.engbedded.com/fusecalc

    My atmega16A board definition is this

    atmega16-8.name=Atmega16 (internal 8MHz clock)

    atmega16-8.upload.protocol=stk500v2
    atmega16-8.upload.maximum_size=14336
    atmega16-8.upload.speed=57600

    atmega16-8.bootloader.low_fuses=0xE4
    atmega16-8.bootloader.high_fuses=0x99
    atmega16-8.bootloader.extended_fuses=0xFF
    atmega16-8.bootloader.unlock_bits=0x3F
    atmega16-8.bootloader.lock_bits=0x0F

    atmega16-8.build.mcu=atmega16
    atmega16-8.build.f_cpu=8000000L
    atmega16-8.build.core=arduino:arduino
    atmega16-8.build.variant=mega16

    I succesfuly programed the blink example, but the Atmega16A is running at 1Mhz, so the led blinks every 8 seconds.
    The chip came without a bootloader
    I want to use the chip for MIDI controlling, but its not working, I suposse because the timing is incorrect

    Could you help me to solve this, please?

    Thanks in advance!

    Enrique

  79. hi scienceprog

    I am using atmega2561 to communicate between optocoupler and RS232 using USART communication.i am getting junk value at hyper terminal and getting warning like data is not storing in UDR0 buffer while transmission.I assigned correct baud rate value and fuse bits as prog said.then also am not getting correct output.

    here am using 8MHz frequency and baud rate 9600
    if you have solution for this please help me.

  80. Your low fuse settings should be like 0X7F and high 0XD9

    Are you sure that your baud settings are set correctly? Check
    http://www.embedds.com/programming-avr-usart-with-avr-gcc-part-1/
    how it’s done. Please share your code chunk to to see if there are anywhere else a problem.

  81. Thanks for your reply,

    this is the code i wrote to just transmit the data.
    here LOW fuse = 0x42 & HIGH fuse = 0x99,am not getting how to set these values.

    #include “avr/interrupt.h”
    #include “avr/io.h”
    #include “util/delay.h”

    #define FOSC 8000000
    #define BAUD 9600
    //#define baud (FOSC/(16*BAUD))-1
    #define baud 51

    void configure_usart(unsigned char baudrate);
    unsigned char usart_putchar(void);
    unsigned char usart_tx(unsigned char c);
    unsigned char usart_receive(unsigned char r1_output);
    unsigned char DataTx[11] = {‘G’,’A’,’T’,’I’,’M’,’T’,’E’,’C’,’H’};

    void configure_usart(unsigned char baudrate)
    {
    UBRR0H = (unsigned char)(baudrate>>8);
    UBRR0L = (unsigned char)baudrate;
    UCSR0C |= (1 << UCSZ00) | (1 << UCSZ01);
    UCSR0B |= (1 << RXEN0) | (1 << TXEN0);
    UCSR0B |= (1 << RXCIE0);
    }

    unsigned char usart_putchar(void)
    {
    unsigned char i;
    unsigned char txdata;

    for(i = 0; i <= 9; i++)
    {
    txdata = usart_tx(DataTx[i]);
    unsigned char temp = txdata;
    }
    }
    unsigned char usart_tx(unsigned char c)
    {
    //unsigned char buffer;
    while (!( UCSR0A & (1<<UDRE0)));
    //buffer = c;
    UDR0 = c;
    return UDR0;
    }

    /*unsigned char USART_Receive(unsigned char DataTx)
    {
    while ( !(UCSR0A & (1<<RXC0)) );
    DataTx = UDR0;
    return DataTx;
    }*/

    int main(void)
    {
    DDRC = 0x00;
    DDRD = 0x00;
    DDRE = 0xFF;
    DDRB = 0x00;

    configure_usart(baud);
    while(1)
    {
    if (~PINC & (1 << PINC6 ))
    {
    usart_putchar();
    }
    }
    }

  82. By the looks of it, your AVR is working at default 1MHz frequency. But your code is calculated for 8MHz. Thisis why you are seeing garbage on terminal screen.

    Define your baud rate to be 6 (actually it is 5.5) it should work:
    #define baud 6

    Or program fuse bits to get your desired frequency.
    you can calculate fuse values here:
    http://www.engbedded.com/fusecalc/

  83. yeah i fixed baud to 6,but same thing is happening prog…please send code for 8 MHz frequency.

  84. Actually this is not recommended to run USART at 1MHz and on internal clock source. You should reprogram fuses to use at least 8MHz (unprogram 8 divider).
    Another trick you can try in this situation is to double USART transmission speed by settin U2X0 bit in UCSR0A register and doubling UBRR0 value. Try editing code place to this:

    #define baud 12

    void configure_usart(unsigned char baudrate)
    {
    UBRR0H = (unsigned char)(baudrate>>8);
    UBRR0L = (unsigned char)baudrate;
    UCSR0A |= (1 << U2X0);//double speed tx UCSR0C |= (1 << UCSZ00) | (1 << UCSZ01); UCSR0B |= (1 << RXEN0) | (1 << TXEN0); UCSR0B |= (1 << RXCIE0); }This sould reduce error on transmission line.

  85. Again am getting warning as “Reading from UDR0 while no data is received”.

  86. You are getting this error because your try to read UDR0 after you transmit.
    Change usart_tx function to this:
    void usart_tx(unsigned char c)
    {
    while (!( UCSR0A & (1 << UDRE0))); UDR0 = c; } //

  87. thanku, i got output bt string is transmitting in endless loop prog….and why baud is 12?

  88. baud was doubled because transmission speed was double by setting U2X0 bit in UCSR0A register.

    The endless loop because there something wrong with condition line:
    if (~PINC & (1 << PINC6 )) line it depends on what are you checkingthere.

  89. Here am transmitting data by connecting channel to pinc6 so to enable pin i wrote that line…if its not correct then how to do that pin enable?

  90. Try:
    if (PINC & (1 << PINC6 ))

  91. this code is only for single channel..like this i have to connect 16 channels to transmit data for different pins by putting pin enable condition in if statement .

  92. if (PINC & (1 << PINC6 )).
    nothing is displaying on terminal for this statement.

  93. baud 12 is for 8MHz frequency?

  94. You should figure out PINC testing method by yourself as it depends on what you expect 0 or 1 as true.
    Baud 12 is for 1MHz clock. And d because USAR is in double transmission speed. If you would use 8MHz clock, then double speed wouldn’t be required and your baud could be 51 as was before.
    This is basic info that you can find in avr datasheet. I would consider starting forum (http://www.scienceprog.com/forum/) thread as these comments are getting clogged and out of topic.

Leave a Reply

Your email address will not be published. Required fields are marked *