LED doesnt turn ON (or OFF)

PORTB &= (1 << LED);

This does not work for definitions of LED greater than 7, so you have to connect your LED to another pin or change the code to use the standard digitalWrite() calls.

You don't need all the for(;;) { rubbish. Just put the code in the loop() function, that will repeat it for ever.

Why are you using direct port addressing?

PORTB &= (1 << LED);

Will set a bit in PORTB that is a 1 shifted to the left 13 times. As this is only an 8 bit register this does nothing for you.

Why did you not post all the code so we can see if you have set the data direction register in the setup?

Grumpy_Mike:
You don't need all the for(;;) { rubbish. Just put the code in the loop() function, that will repeat it for ever.

Why are you using direct port addressing?

PORTB &= (1 << LED);

Will set a bit in PORTB that is a 1 shifted to the left 13 times. As this is only an 8 bit register this does nothing for you.

Why did you not post all the code so we can see if you have set the data direction register in the setup?

Thanks for reply.
I have posted an URL from where you can download the source code in one file.
The thing is that I tried to change the main to loop, but it stopped compiling, saying that there are some errors with __vector_18.

I have tried changing the code like this:

PORTB &= B00100000;
for(i = 0; i < pause; i++)
	_delay_us(10);
PORTB |= B00100000;
for(i = 0; i < 255-pause; i++)
	_delay_us(10);

The same problem, it turns on but doesnt turn off.

I have no idea why you want to use code like this on an arduino but change this line to:-

#define LED 5 // LED is on Pin 13 or Pin 5 of Port B

and change both the delays in the loop to:-

_delay_us(1000);

And it works

Show us the whole code you are using because you have changed some things (the definition of the LED which was fine in the original code but wrong defined to 13).

This code is not intended for the Arduino IDE, it's AVR C code to be compiled directly by the gcc-avr.

Grumpy_Mike:
I have no idea why you want to use code like this on an arduino but change this line to:-

#define LED 5 // LED is on Pin 13 or Pin 5 of Port B

and change both the delays in the loop to:-

_delay_us(1000);

And it works

In my case it didnt worked, but I have changed LED to be 6 and _delay_us(10000)
Then it worked..

but I have changed LED to be 6

So have you got an external LED then?

Why are you using code that is writing in such a machine code way? There are much easier ways to achieve the same effect. As a learning exercise that code is useless.

pylon:
Show us the whole code you are using because you have changed some things (the definition of the LED which was fine in the original code but wrong defined to 13).

This code is not intended for the Arduino IDE, it's AVR C code to be compiled directly by the gcc-avr.

Here is my latest code:

#include <stdio.h>
#include <avr/io.h>
#include <avr/interrupt.h>
#include <util/delay.h>

#define LED 6 // LED is on Pin 13 or Pin 5 of Port B
/*
 * UART-Initialization from www.mikrocontroller.net
 * Hint: They are awesome! :-)
 */

#ifndef F_CPU
#warning "F_CPU was not defined, defining it now as 16000000"
#define F_CPU 16000000UL
#endif

#define BAUD 9600UL      // baud rate
// Calculations
#define UBRR_VAL ((F_CPU+BAUD*8)/(BAUD*16)-1)   // smart rounding
#define BAUD_REAL (F_CPU/(16*(UBRR_VAL+1)))     // real baud rate
#define BAUD_ERROR ((BAUD_REAL*1000)/BAUD) // error in parts per mill, 1000 = no error
#if ((BAUD_ERROR<990) || (BAUD_ERROR>1010))
#error Error in baud rate greater than 1%!
#endif

void uart_init(void) {
	UBRR0H = UBRR_VAL >> 8;
	UBRR0L = UBRR_VAL & 0xFF;

	UCSR0C = (0 << UMSEL01) | (0 << UMSEL00) | (1 << UCSZ01) | (1 << UCSZ00); // asynchron 8N1
	UCSR0B |= (1 << RXEN0); // enable UART RX
	UCSR0B |= (1 << TXEN0); // enable UART TX
	UCSR0B |= (1 << RXCIE0); //interrupt enable
}

/* Receive symbol, not necessary for this example, using interrupt instead*/
uint8_t uart_getc(void) {
	while (!(UCSR0A & (1 << RXC0)))
		// wait until symbol is ready
		;
	return UDR0; // return symbol
}

uint8_t uart_putc(unsigned char data) {
	/* Wait for empty transmit buffer */
	while (!(UCSR0A & (1 << UDRE0)))
		;
	/* Put data into buffer, sends the data */
	UDR0 = data;
	return 0;
}


void initIO(void) {
	DDRD |= (1 << DDD3);
	DDRB = 0xff; //all out
}


volatile uint8_t data = 10;

int main(void) {
	initIO();
	uart_init();
	sei();

	uint8_t i = 0;
	volatile uint8_t pause;
	for(;;) {
		pause = data;
		PORTB &= (1 << LED);
		for(i = 0; i < pause; i++)
			_delay_us(10000);
		PORTB |= ~(1 << LED);
		for(i = 0; i < pause; i++)
			_delay_us(10000);
	}
	return 0; // never reached
}

ISR(USART_RX_vect) {//attention to the name and argument here, won't work otherwise
	data = UDR0;//UDR0 needs to be read
}

And it is blinking.

But if I change the _delay_us function to delay(1000):

for(;;) {
		pause = data;
		PORTB &= (1 << LED);
		delay(1000);
		PORTB |= ~(1 << LED);
		delay(1000);
	}

It stops blinking, it just keep turned ON.

Grumpy_Mike:

but I have changed LED to be 6

So have you got an external LED then?

Why are you using code that is writing in such a machine code way? There are much easier ways to achieve the same effect. As a learning exercise that code is useless.

No, I am using the LED which is the Arduino UNO's LED, and it was blinking.
This code is what I actually have, and I need to rewrite it to use an easier way.

This code is what I actually have, and I need to rewrite it to use an easier way.

Sounds like an assignment or homework.

Grumpy_Mike:

This code is what I actually have, and I need to rewrite it to use an easier way.

Sounds like an assignment or homework.

I mean I dont have another code so that I can chose want code to use, written in an easier way or harder way.
I do not need to rewrite it, I mean, I will have to rewrite it to get a usual form (easier way) using setup and loop functions.

Sorry I don't understand that.

Not to worry there is no reason why I should.

However the bit I don't understand is:-

but I have changed LED to be 6

So quoting from:-

PORTB maps to Arduino digital pins 8 to 13 The two high bits (6 & 7) map to the crystal pins and are not usable

So I don't see how 6 could possibly work, you are targeting the wrong bit.

Grumpy_Mike:
Sorry I don't understand that.

Not to worry there is no reason why I should.

However the bit I don't understand is:-

but I have changed LED to be 6

So quoting from:-
Arduino Reference - Arduino Reference

PORTB maps to Arduino digital pins 8 to 13 The two high bits (6 & 7) map to the crystal pins and are not usable

So I don't see how 6 could possibly work, you are targeting the wrong bit.

LED is blinking but after sending some values as 4 or 22 to it it doesnt turn OFF but if I send 1 it blinks very often.
I also cant understand what is wrong but it seems not working as intendent if I set LED to 6,
if I set to 5 it doesn work at all.

yerzhik:
What is the problem?

What you are trying to do is trivially simple and could be done in a couple of lines of code using the Arduino libraries.

The approach you're using attacks the hardware directly, which is much harder to implement. Why don't you simply do it the easy way?

If you insist on doing it the hard way, start with explaining how pin 5 or 6 belongs to PORTB which is pins 8 to 13. Then, explain how manipulating any value in PORTB is going to affect any pins outside the range 8 to 13.

PeterH:

yerzhik:
What is the problem?

What you are trying to do is trivially simple and could be done in a couple of lines of code using the Arduino libraries.

The approach you're using attacks the hardware directly, which is much harder to implement. Why don't you simply do it the easy way?

You were right. I have already made it by changing the SerialEvent example.
But I was wondering why that strange things happened.
And if you can do this thing in a hard way - it means you understand the arduino quite well.
By the way the thing which is done in that code using the hard way, regarding the USB communication, what should be read from the datasheet in order to understand it? There are several chapters but I dont know which one exactly to read: the SPI, USART or USART in SPI mode?

The communication to the USB is handled by the USART on most Arduinos. Converting the serial signals to USB chunks is done by a separate chip (FTDI for older Arduinos and most clones, an ATmega16U2 on the UNO and Mega2560).

Change

PORTB &= (1 << LED);
//  ...
PORTB |= ~(1 << LED);

to

PORTB &= (1 << [b](LED-1)[/b]);
// ...
PORTB |= ~(1 << [b](LED-1)[/b]);

The pins to bits relationship is [ x x 13 12 11 10 9 8 ], with 8 being in the lsb position.

To create the mask for pin 8 ( which is the least significant bit, called "b0" ) requires 0 shifts, not 1.
To create the mask for pin 13 (which is b5) , you need to
take 1 and shift it left 5 times, not 6.

1 << (LED-1) gives you #B00100000, which is what you want.
1 << 6 results in #B01000000, which is unusable. See Grumpy_MIke's post:

PORTB maps to Arduino digital pins 8 to 13 The two high bits (6 & 7) map to the crystal pins and are not usable

Also, I hope you are aware that delay(1000) is a one second delay, whereas your delay_us(10000) is 10 ms or 1/100th second.

yerzhik:
I was wondering why that strange things happened.

Probably because of the numerous faults in your implementation of the hard approach, which others have already pointed out.