Go Down

Topic: LED doesnt turn ON (or OFF) (Read 1 time) previous topic - next topic

PaulS

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.

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?

pylon

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).

Techylah

#18
May 22, 2012, 09:04 pm Last Edit: May 22, 2012, 09:12 pm by Techylah Reason: 1
Change
Code: [Select]
PORTB &= (1 << LED);
//  ...
PORTB |= ~(1 << LED);

to

Code: [Select]
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:
Quote
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.

PeterH


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.
I only provide help via the forum - please do not contact me for private consultancy.

Go Up