Society of Robots - Robot Forum

Software => Software => Topic started by: karlis on June 20, 2008, 03:13:44 PM

Title: flashing led, port problem
Post by: karlis on June 20, 2008, 03:13:44 PM
Hi, I am semi-new to this, so please dont shout on my mistakes  8)

I bought AVR ISP from some Bulgarian, he allso sended *.hex to test if everything is working well, it realy did
When i asked him to give source code, just for learning, he ansvered that he dosnt have it >.<

anyhow now i`m trying to do it myself, found some tuts and slowly puting all to gather, i`m stuck at declaring ports, here is the code, and screenshot with error's could some one explane bugs ?  ???
I`m using atmega16 with external 4mhz clock, all electronic part is perfect, beleave me or not :P

Quote
/*********************************************
* vim: set sw=8 ts=8 si :
* Author: Guido Socher, Copyright: GPL
* This program is to test the led connected to
* PD5.
* See http://linuxfocus.org/English/March2002/
* for details.
* Chip type           : AT90S4433
* Clock frequency     : 4,000000 MHz
*********************************************/
#include "mega16.h"

void delay_ms(unsigned short ms)
/* delay for a minimum of <ms> */
/* with a 4Mhz crystal, the resolution is 1 ms */
{
   unsigned short outer1, outer2;
        outer1 = 200;

       while (outer1) {
      outer2 = 1000;
      while (outer2) {
         while ( ms ) ms--;
         outer2--;
      }
      outer1--;
   }
}

int main(void)
{
   /* enable  PD5 as output */
   sbi(DDRD,PD5);
   while (1) {
      /* led on, pin=0 */
      cbi(PORTD,PD5);
      delay_ms(500);
      /* set output to 5V, LED off */
      sbi(PORTD,PD5);
      delay_ms(500);
   }
}


i know that in coment area there is other chip, but since its not so important i leaved it so, just added my chips i/o
Title: Re: flashing led, port problem
Post by: bens on June 20, 2008, 03:34:43 PM
Delete #include "mega16.h" and replace it with #include <avr/io.h>.  You will need to have WinAVR installed if you don't already.

The bugs are coming from the fact that the program doesn't know what things like PORTD are.  These defines and macros will be included in your program if you include avr/io.h.

Also, you should use a much better delay_ms(), such as:

#define F_CPU 8000000UL  // 8 MHz  (replace this number with your AVR's clock speed)
#include <util/delay.h>
void delay_ms(unsigned int time_ms)
{
  while (time_ms--)
    _delay_ms(1);
}

- Ben
Title: Re: flashing led, port problem
Post by: karlis on June 20, 2008, 03:50:42 PM
Yes i have installed winavr
some problems sorted out some new occour
like
Quote
#ifndef __OPTIMIZE__
# warning "Compiler optimizations disabled; functions from <util/delay.h> won't work as designed"
#endif
it showed as warning,after adding delay.h - what should i change there ? (its in delay.h) should i delete that #warning ?

is this correct if i`m using atmega16mHz MCU ?
Quote
#ifndef F_CPU
/* prevent compiler error by supplying a default */
#define F_CPU 16000000UL
#endif
or should i remove "UL" since my atmega does not have any letters at end of the name of it ? its simple Atmega16

what are "sbi" and "cbi" ?
still have errors with them
Quote
E:\root\robots\led\ledx\ledx\default/../ledx.c:34: undefined reference to `sbi'
E:\root\robots\led\ledx\ledx\default/../ledx.c:37: undefined reference to `cbi'

last thing - i dont understand what for are these lines for
Quote
void delay_ms(unsigned short ms)
/* delay for a minimum of <ms> */
/* with a 4Mhz crystal, the resolution is 1 ms */
{
   unsigned short outer1, outer2;
        outer1 = 200;

       while (outer1) {
      outer2 = 1000;
      while (outer2) {
         while ( ms ) ms--;
         outer2--;
      }
      outer1--;
   }
}
Title: Re: flashing led, port problem
Post by: bens on June 20, 2008, 04:57:03 PM
Are you using AVR Studio?

Quote
it showed as warning,after adding delay.h - what should i change there ? (its in delay.h) should i delete that #warning ?

Don't change the contents of WinAVR header files.  Just build with compiler optimization set and this warning will go away.

Quote
is this correct if i`m using atmega16mHz MCU ?
or should i remove "UL" since my atmega does not have any letters at end of the name of it ? its simple Atmega16
The 16 in ATmega16 doesn't necessarily stand for its clock speed.  You know your setup better than I do.  Are you using an external resonator or crystal?  If so, what frequency?  If not, look at the datasheet for your device to see what its internal oscillator is running at.  The "UL" just casts the number as an unsigned long.  You should not delete those.

Quote
what are "sbi" and "cbi" ?
still have errors with them

I guess I don't know where they're defined.  You can just add to the top of your file the following:

#define sbi(a, b)  (a |= (1 << b))
#define cbi(a, b)  (a &= ~(1 << b))

Quote
E:\root\robots\led\ledx\ledx\default/../ledx.c:34: undefined reference to `sbi'
E:\root\robots\led\ledx\ledx\default/../ledx.c:37: undefined reference to `cbi'

Quote
last thing - i dont understand what for are these lines for

This is the delay function I suggested you replace with my version.  It's up to you if you really want to replace it or leave it as is.
Title: Re: flashing led, port problem
Post by: karlis on June 21, 2008, 02:10:11 AM
thakns for definitions, just compiled and uploaded hex and every thing is working.. i`m keep experimenting with difrent pins dogather, just to make led show, for now ^_^

ps could drop some page with .c syntax ? ??? i`m looking for counter so it could count PD4 4x flashed then PD5 flashes twice and so on, changing leds, i have learned algorithms in school (last year) still got notebook where is every thing writen, and dont think that it will be huge problem to understand :)

Thanks Ben

Karlis