0
votes

I had written a code on atmel studio for blinking a led on pin 13. After uploading the code with xloader mega's builtin led was blinking. I uploaded fade code on my mega and the builtin led was blinking instead of led. What should i do? I am using arduino mega 2560.

int main(void)
{
    DDRB=0b00000000;  
    while (1)   
    {
     PORTB=0b10000000;
     _delay_ms(1000);
     PORTB=0b00000000;
    }
}
3
There's nothing in this cryptic code that does any fading. Tip: use the macros and names defined for your part. The binary constants are hard to read and debug.TomServo

3 Answers

0
votes

What you should do? Read the manual.

Please refer to https://ww1.microchip.com/downloads/en/devicedoc/atmel-2549-8-bit-avr-microcontroller-atmega640-1280-1281-2560-2561_datasheet.pdf

Chapter 13.2.

The DDxn bit in the DDRx Register selects the direction of this pin. If DDxn is written logic one, Pxn is configured as an output pin. If DDxn is written logic zero, Pxn is configured as an input pin.

Working with registers doesn't make sense if you don't know what they do.

DDRB=0b00000000;     

Gives you inputs only.

0
votes

why would you use Arduino and try to program it without its conventional macros and functions?

If you are trying to blink an led or make it breath then use the Arduino IDE and its built-in functions analogWrite() to generate a pwm pulse for your led or any led on suitable pins which support analogwrite(). You shouldn't try to do any direct modifications on registers if you have no suitable knowledge, because your risk destroying your development kit and maybe burning some other stuff around. Please use your kit's schematics to spot the pins which support analogwrite() and then use the code in examples.

That way you will achieve your goal faster and without any issues.

0
votes

TL/DR: you have to set 7th bit in DDRB to one.

In AVR ports are configured by bits in two registers: DDRx and PORTx.

When the corresponding bit in the DDRx register is set to one, the port is configured as output. And the corresponding bit in the PORTx register chooses which electrical level is output on the pin. If it is 0 then internal MOSFET shorts the pin to "ground" lane, and sinks current from external source. When the bit of the PORTx is one, then the pin is connected to "VCC", sourcing big amount of current enough to lit up a LED.

But if the pin is connected to something, what consumes too much of current, or the pin is shorted to GND or VCC (let's say you have a button connected and pressed), then output MOSFETS might be overloaded and damaged.

If the bit in DDRx is set to zero, then the pin is configured as input. If the corresponding bit in the PORTx is zero, then the pin has no internal connection to power lines, it is called "Hi-impedance" state, or Tri-state. It does not source or sink any current. So, if no external source of current is connected, then pin level is floating, influenced by electrical interference. Logical level is not detectable and can change occasionally. If you want to connect, for example, a button (between the pin and GND), then logical level will be defined only when button is pressed. When it is released, the logical level will be undefined.

But! If the bit in the PORTx is set to one, then internal MOSFET connects the pin thru a resistor (about 35 kOhm) to VCC line. This make the pin to source a little amount of current, setting its logical level to high. Therefore, if a button is connected, when it is released, then pin will have defined high level. This is called "pull-up resistor". When button is pressed, it will not short and damage the MCU, because current flowing thru the button is limited by the resistor, but the logical level will be defined low.

What if instead of button you have a LED connected to the pin? Very small amount of current will flow thru the LED, makes it barely glow.

Read more in the datasheet (chapter 13. I/O-Ports)