beginers error

Go To Last Post
6 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

hey guys i am new to embedded world and off course to programming,

i have  a arduino uno but i dont use ide (i use avr-gcc, avrdude)

i had a successfull start but everything with the for loop burns me a lot.

when i  manually execute them all work fime

when i use for loop nithing works,i checked my code on with variables(a,b,f,...) in a gcc compiler and the outputs were expected

i know it must be silly one but please do helpme .

here comes the code.

int i;
    DDRB = 0xff;
    char array[]={0x01,0x02,0x04,0x08};
    for(i=0;i<=3;i++)
    {
        PORTB = array[i];
        _delay_ms(100);
    }

 

even when i put these statements inside a  while loop it dosent work.(but loops perfectly with a normal gcc compiler using variables)

This topic has a solution.
Last Edited: Thu. Jul 22, 2021 - 06:56 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

elyts_k wrote:
when i use for loop nithing works,
Tell us more about what that means. What were you expecting? What actually happens?

 

BTW when you build I guess you do an avr-objcopy to create a .hex file from the .elf file? What is the exact command you use. Do you just "-j .text" perhaps? Remember you also need to "-j .data" as well - if you didn't that could well result in your initialisers for array[] not making it to the code image in which case it's very likely to continuously output 0xFF (as the data copied to init array[] will be "unprogrammed" flash that likely contains 0xFF bytes).

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

thanks for reply,

 

my make file:

FILENAME = lee
FREQ = 16000000UL
DEVICE = atmega328p
PORT = /dev/ttyACM0
BOOTLOADER = arduino
COMPILER = avr-gcc
COPY = avr-objcopy
FLASH = avrdude
BAUD = 115200

default: compile copy flash

compile:
    $(COMPILER) -Os -DF_CPU=$(FREQ) -mmcu=$(DEVICE) -c -o $(FILENAME).o $(FILENAME).c
    $(COMPILER) -o $(FILENAME).bin $(FILENAME).o

copy:
    $(COPY) -O ihex -R .eeprom $(FILENAME).bin $(FILENAME).hex

flash:
    $(FLASH) -c $(BOOTLOADER) -p ATMEGA328P -P $(PORT) -b $(BAUD) -U flash:w:$(FILENAME).hex

while(i<5)
    {
        PORTB = (0x01 << i);
        _delay_ms(1000);
        i=i++;  //or  i++;
    }

my code:

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

int main()
{
    int i=0;
    DDRB = 0xff;
    while(1)
    {
        while(i<5)
        {
            PORTB=(0x01<<i);
            _delay_ms(1000);
            i=i++;
        }
    }
    return 0;
}

 

that i have four leds connected to last four pins of portb.

this code is supposed to produce 0b00000001,0b00000010,0b00000100 like wise

but it gets stuck with 0x01

even if  i tried with char array it does the same 

this is not the case with this program all programs where is use while it does the same.

what is wrong in my part

thanks in advance..smiley

This reply has been marked as the solution. 
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

i=i++ causes undefined behavior. Try just using i++ instead.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

yay!!!

that worked thanks a lot..

yes

thaks for suppoarting!!!!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...