Pages: 1 [2]   Go Down
Author Topic: LED doesnt turn ON (or OFF)  (Read 1300 times)
0 Members and 1 Guest are viewing this topic.
Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 553
Posts: 46299
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Offline Offline
Newbie
*
Karma: 0
Posts: 24
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged

Switzerland
Offline Offline
Faraday Member
**
Karma: 96
Posts: 4764
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Offline Offline
Full Member
***
Karma: 0
Posts: 162
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Change
Code:
PORTB &= (1 << LED);
//  ...
PORTB |= ~(1 << LED);
to

Code:
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.
« Last Edit: May 22, 2012, 02:12:19 pm by Techylah » Logged

UK
Offline Offline
Shannon Member
****
Karma: 184
Posts: 11196
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

I only provide help via the forum - please do not contact me for private consultancy.

Pages: 1 [2]   Go Up
Jump to: