Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

[AVR] Simple C code for led blinking problem

Status
Not open for further replies.

mondo90

Junior Member level 1
Joined
Oct 26, 2014
Messages
19
Helped
1
Reputation
2
Reaction score
1
Trophy points
3
Activity points
151
Hello, I try to complie (using atmel studio 6 ) example C code for led blinking:

Code:
#define F_CPU 1000000UL

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

int
main (void)
{
	DDRB |= _BV(DDB0);
	
	while(1)
	{
		PORTB ^= _BV(PB0);
		_delay_ms(500);
	}
}


I get atmel studio error: Error 1 'DDB0' undeclared (first use in this function)

It's really frustrating when even example code doesn't work ;/ what's wrong ?

PS: for what this line define: #define F_CPU 1000000UL
 

Try this code.


Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#define F_CPU 1000000UL
 
#include <avr/io.h>
#include <util/delay.h>
 
int main (void)
{
    DDRB |= 0x01;
    
    while(1)
    {
        PORTB |= 0x01;
        _delay_ms(500);
        PORTB &= 0xFE;
        _delay_ms(500);
    }
}

 
huh it looks like space between int and main caused the problem o.0 I am strating to doubt if Atmel studio is good IDE.

PS: what this line define: #define F_CPU 1000000UL ??

Thanks.

- - - Updated - - -

One more issue, this is my first time with AVR, I use Atmega8, Atmel studio + avrdude with USBasp programmer. I successfully connected and run it with simple led flasher circuit. But I have no idea how to debug the program. What I mean is I would like to write ADC read circuit from potentiometer and read ADC value in while loop in ATMEL STUDIO or AVRDUDE. The problem is I can't do this, since I flash memory with external tool (avrdude) atmel doesn't know what is going on inside program. How to do this ?

Thanks ;)
 

huh it looks like space between int and main caused the problem

That cannot cause any problem. You have used DDB0 which is not defined in any header file.

F_CPU is reserved by Atmel Studio WinAVR. It is for defining Clock frequency.

#define F_CPU 1000000UL

defines F_CPU as 1000000UL (UL = unsigned long)

F_CPU is an alias for 1 MHz

You need a programmer cum debugger like AVRISP mKII to debug the code inside your chip.

http://www.electroschematics.com/10053/avr-adc/

http://www.avrbeginners.net/architecture/adc/m8_adc_example.html

http://maxembedded.com/2011/06/20/the-adc-of-the-avr/

http://kartikmohta.com/tech/avr/tutorial/
 
Last edited:
Hello, I try to complie (using atmel studio 6 ) example C code for led blinking:

Code:
#define F_CPU 1000000UL

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

int
main (void)
{
	DDRB |= _BV(DDB0);
	
	while(1)
	{
		PORTB ^= _BV(PB0);
		_delay_ms(500);
	}
}


I get atmel studio error: Error 1 'DDB0' undeclared (first use in this function)

It's really frustrating when even example code doesn't work ;/ what's wrong ?

PS: for what this line define: #define F_CPU 1000000UL

Out of curiosity, I compiled the above code listed without error utilizing Atmel Studio v6.1 with a fairly recent compiler suite.

The predefined macro DDB0 is defined in one of the header files included at the top of the code and is standard nomenclature for the Atmel compiler suite used with the Atmel Studio IDE.

As mentioned by other members, the following define:

Code:
[B#define F_CPU 1000000UL

Indicates the system clock frequency for your design and is utilized by the delay routines to generate an accurate delay which is dependent on the current system clock frequency of the microcontroller as implemented in your design.

huh it looks like space between int and main caused the problem o.0 I am strating to doubt if Atmel studio is good IDE.

Actually, such an error would be a compiler issue not the IDE. Typically compilers treats white space which can be composed of spaces, tabs, newlines and comments as a single space. And as I indicated above your original code does compile without error with my current IDE/compiler installation. Perhaps there an issue with your compiler and/or its installation.

I should mention while the compiler does not typically have an issue with white space there are a few instances when it can present an issue for the preprocessor, two common situations are when a string literal is broke up over two or more lines and when it involves a predefined macro expansion.


BigDog
 
hello,
i have similar problem....
this code when i build for Atmega32 is ok,

Code:
#define F_CPU 1000000UL
#include <avr/io.h> /* Defines pins, ports, etc */
#include <util/delay.h> /* Functions to waste time */

int main(void) {
	
	DDRB |= (1 << PB0); 
	
	while (1) {
		PORTB = 0b00000001; /* Turn on first LED bit/pin in PORTB */
		_delay_ms(1000); /* wait */
		PORTB = 0b00000000; /* Turn off all B pins, including LED */
		_delay_ms(1000); /* wait */
		}
		return(0);
	}

when i use this code for ATxmega32E5 i receive this error:

HTML:
error: 'DDRB' undeclared (first use in this function)
		  DDRB |= (1 << PB0); 
error: 'PB0' undeclared (first use in this function)
		  DDRB |= (1 << PB0); 
error: 'PORTB' undeclared (first use in this function)
		   PORTB = 0b00000001;
how can i solve this?
please someone explain
thx in advance
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top