Go Down

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

yerzhik

I have compiled the following source code on my Arduino UNO.
http://android.serverbox.ch/wp-content/led_pwm.c
(there is an error in the code, you should change the definition of the LED to 13: #define LED 13
The origial article where the source code is referenced :
http://android.serverbox.ch/?p=549

The problem is that the LED doesnt turn on at all. I have changed the code like this:
   
Code: [Select]
for(;;) {
pause = data;

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

}


So that at frist it turns on then turns off, instead of turning off the LED and then turning it off.
After that the program turns on the LED but doesnt turning it OFF.
What is the problem? I even tried to remove the "for" loop and put there delay with fixed delay time - the same problem.
Can somebody help?

pylon

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

Grumpy_Mike

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

Why are you using direct port addressing?

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

yerzhik


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

Why are you using direct port addressing?

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

Grumpy_Mike

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

Code: [Select]
#define LED 5 // LED is on Pin 13 or Pin 5 of Port B

and change both the delays in the loop to:-
Code: [Select]
_delay_us(1000);
And it works

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.

yerzhik


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

Code: [Select]
#define LED 5 // LED is on Pin 13 or Pin 5 of Port B

and change both the delays in the loop to:-
Code: [Select]
_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..

Grumpy_Mike

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

yerzhik


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:

Code: [Select]

#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):

Code: [Select]

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

It stops blinking, it just keep turned ON.

yerzhik


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

Grumpy_Mike

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

Sounds like an assignment or homework.

yerzhik


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

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:-
Quote
but I have changed LED to be 6


So quoting from:-
http://www.arduino.cc/en/Reference/PortManipulation

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


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

yerzhik


Sorry I don't understand that.

Not to worry there is no reason why I should.

However the bit I don't understand is:-
Quote
but I have changed LED to be 6


So quoting from:-
http://www.arduino.cc/en/Reference/PortManipulation

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


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.

PeterH


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

Go Up