Show Posts
Pages: 1 [2]
16  Using Arduino / Programming Questions / Re: LED doesnt turn ON (or OFF) on: May 21, 2012, 09:29:42 pm
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:
#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:
for(;;) {
pause = data;
PORTB &= (1 << LED);
delay(1000);
PORTB |= ~(1 << LED);
delay(1000);
}
It stops blinking, it just keep turned ON.
17  Using Arduino / Programming Questions / Re: LED doesnt turn ON (or OFF) on: May 21, 2012, 09:24:12 pm
I have no idea why you want to use code like this on an arduino but change this line to:-

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

and change both the delays in the loop to:-
Code:
_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..
18  Using Arduino / Programming Questions / Re: LED doesnt turn ON (or OFF) on: May 21, 2012, 12:10:33 pm
You don't need all the
Code:
for(;;) {
rubbish. Just put the code in the loop() function, that will repeat it for ever.

Why are you using direct port addressing?

Code:
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:
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.
19  Using Arduino / Programming Questions / LED doesnt turn ON (or OFF) on: May 21, 2012, 11:54:55 am
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:
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?
20  Topics / Education and Teaching / Re: Low level documentation on: May 17, 2012, 01:03:03 am
Probably my Arduino UNO goes with built in ATmega8U2.

It is written in the web site about the Arduino UNO, that:
"Serial: 0 (RX) and 1 (TX). Used to receive (RX) and transmit (TX) TTL serial data. These pins are connected to the corresponding pins of the ATmega8U2 USB-to-TTL Serial chip."

Here I am a little bit confused

1. Does the ATmega8U2 function like a USB to Serial chip?
2. Does the RX and TX Pins always work through USB? If no how do I choose the way they work?
For example I have connected the LED between TX and GND pin, and it worked when I was sending some data through it.
Was the connection through USB?
Is there any good documentation explaining it?
3. Can I programm the ATmega8U2 independently of ATmega328P?
21  Topics / Education and Teaching / Re: Low level documentation on: May 17, 2012, 12:22:22 am
Start here:

AVR-LIBC: http://www.nongnu.org/avr-libc/user-manual/modules.html
ATmega328P: http://www.atmel.com/Images/doc8271.pdf

--
The Gadget Shield: accelerometer, RGB LED, IR transmit/receive, speaker, microphone, light sensor, potentiometer, pushbuttons



Almost done with the documentation about ATmega328P.
Thank you for links.
I have another question, I am planning to use USB in order to send/receive data from PC/Smartphone. I have found also that Arduino UNO includes the USB Serial Light Adapter.
Which can be used for the purpose of sending/receiving data from PC via USB connection. But I couldn't find this information in the document above.
Do you know what documentation should I read in order to be able to use the USB Serial Light Adapter (also low level if possible)?
22  Topics / Education and Teaching / Re: Low level documentation on: April 26, 2012, 02:59:59 am
The ATmega328P datasheet does have the answers on the general structure of the CPU, its peripherals, and all the bits in all the registers used to control its operation.

--
The Flexible MIDI Shield: MIDI IN/OUT, stacking headers, your choice of I/O pins

Thank you very much!
23  Topics / Education and Teaching / Re: Low level documentation on: April 25, 2012, 09:52:24 pm
Start here:

AVR-LIBC: http://www.nongnu.org/avr-libc/user-manual/modules.html
ATmega328P: http://www.atmel.com/Images/doc8271.pdf

--
The Gadget Shield: accelerometer, RGB LED, IR transmit/receive, speaker, microphone, light sensor, potentiometer, pushbuttons


Thank you for your answer.
As I understood, this is about the CPU inside the Arduino and about the library. How about the information of the general structure, like, CPU + peripheri + rules to make settings to the peripherial devices, etc?
24  Topics / Education and Teaching / Low level documentation on: April 25, 2012, 08:12:51 pm
Hello, I want to learn the Ardiono from the base. I mean, not just using libraries such as servos etc, but to be able to understand what it does from inside,
and to be able to programming it by myself. Making my own interruption handling from devices connected etc. (Not ASM code of cource)
What should I read? I have seen here the learning foundations or hacking but didnt find the needed information.
Pages: 1 [2]