Convert WinAVR code to ATMEL Studio code

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

I want run it in my Atmel Studio 7.0 but it is not work always I got errors because the code is for WinAVR. Can anyone help me to make it work in Atmel ?

 

 

 

#include <avr/io.h> 
#define F_CPU 1000000
#include <util/delay.h> 
int main(void)
{
    DDRB = 0xFF;
    DDRD = 0x00;
    
    TCCR1A |=(1<<WGM11)|(1<<COM1A1)|(1<<COM1A0);
    TCCR1B |=(1<<WGM12)|(1<<WGM13)|(1<<CS10);
    ICR1 =19999;
    while(1)
    {
        if (i<500)
        {
            OCR1A = 19999-600;
        }
        i++;
        _delay_ms(1);
        if (bit_is_set(PIND,0))
        {
            PORTB|=(1<<PINB2); 
        }
        if (bit_is_clear(PIND,1)) 
        {
            PORTB&=~(1<<PINB2); 
        }

        if ((i<1000)&&(i>500))
        {
            OCR1A = 19999-2400; 
        }
        if (i==1000)
        {
            i=0;
        }
    } }

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

Welcome to AVRFreaks!

 

So what errors are you getting?  What have you done to trouble shoot the situation?

Are you asking for professional help with the conversion, if so you need to post a request in the marketplace forum with how much your willing to pay for the work.

AVRFreaks are happy to help you solve your problem, but as professionals, we expect to be paid to provide code.

 

Jim

PS: use the "<>" code tags edit menu (next to smileyface/ohm symbols) to display code properly on this forum.

 

Edit: where are the macros "bit_is_set" "bit_is_clear" defined?

 

 

(Possum Lodge oath) Quando omni flunkus, moritati.

"I thought growing old would take longer"

 

Last Edited: Fri. Aug 14, 2020 - 01:01 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

You didn't say which AVR so I just picked ATmega16 at random. When I paste that code into AS7 and try to build it says:

		.././main.c: In function 'main':
D:\test\test\main.c(14,7): error: 'i' undeclared (first use in this function)
		   if (i<500)
		       ^
D:\test\test\main.c(14,7): info: each undeclared identifier is reported only once for each function it appears in

This is not entirely surprising. The code is using a variable called "i" as a counter without having defined it. You have:

        if (i<500)

for example but no sign of "i". If I modify things slightly:

#include <avr/io.h>
#define F_CPU 1000000
#include <util/delay.h>

int main(void)
{
	int i = 0;

	DDRB = 0xFF;
	DDRD = 0x00;

	TCCR1A |=(1<<WGM11)|(1<<COM1A1)|(1<<COM1A0);
	TCCR1B |=(1<<WGM12)|(1<<WGM13)|(1<<CS10);
	ICR1 =19999;
	while(1)
	{
		if (i<500)
		{
			OCR1A = 19999-600;
		}
		i++;
		_delay_ms(1);
		if (bit_is_set(PIND,0))
		{
			PORTB|=(1<<PINB2);
		}
		if (bit_is_clear(PIND,1))
		{
			PORTB&=~(1<<PINB2);
		}

		if ((i<1000)&&(i>500))
		{
			OCR1A = 19999-2400;
		}
		if (i==1000)
		{
			i=0;
		}
	}
}	

then that builds without error for mega16.

 

By the way white space costs nothing but makes code more readable and also don't overuse != when just = would do as well so better is:

int main(void)
{
	int i = 0;

	DDRB = 0xFF;
	DDRD = 0x00;

	TCCR1A = (1 << WGM11) | (1 << COM1A1) | (1 << COM1A0);
	TCCR1B = (1 << WGM12) | (1 << WGM13) | (1 < <CS10);
	ICR1 = 19999;
	while(1)
	{
		if (i < 500)
		{
			OCR1A = 19999-600;
		}
		i++;
		_delay_ms(1);
		if (bit_is_set(PIND,0))
		{
			PORTB |= (1 << PINB2);
		}
		if (bit_is_clear(PIND,1))
		{
			PORTB &= ~(1 << PINB2);
		}

		if ((i < 1000) && (i > 500))
		{
			OCR1A = 19999-2400;
		}
		if (i == 1000)
		{
			i=0;
		}
	}
}

 

Last Edited: Fri. Aug 14, 2020 - 01:23 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0


clawson wrote:

You didn't say which AVR so I just picked ATmega16 at random. When I paste that code into AS7 and try to build it says:

		.././main.c: In function 'main':
D:\test\test\main.c(14,7): error: 'i' undeclared (first use in this function)
		   if (i<500)
		       ^
D:\test\test\main.c(14,7): info: each undeclared identifier is reported only once for each function it appears in

This is not entirely surprising. The code is using a variable called "i" as a counter without having defined it. You have:

        if (i<500)

for example but no sign of "i". If I modify things slightly:

#include <avr/io.h>
#define F_CPU 1000000
#include <util/delay.h>

int main(void)
{
	int i = 0;

	DDRB = 0xFF;
	DDRD = 0x00;

	TCCR1A |=(1<<WGM11)|(1<<COM1A1)|(1<<COM1A0);
	TCCR1B |=(1<<WGM12)|(1<<WGM13)|(1<<CS10);
	ICR1 =19999;
	while(1)
	{
		if (i<500)
		{
			OCR1A = 19999-600;
		}
		i++;
		_delay_ms(1);
		if (bit_is_set(PIND,0))
		{
			PORTB|=(1<<PINB2);
		}
		if (bit_is_clear(PIND,1))
		{
			PORTB&=~(1<<PINB2);
		}

		if ((i<1000)&&(i>500))
		{
			OCR1A = 19999-2400;
		}
		if (i==1000)
		{
			i=0;
		}
	}
}	

then that builds without error for mega16.

 

By the way white space costs nothing but makes code more readable and also don't overuse != when just = would do as well so better is:

int main(void)
{
	int i = 0;

	DDRB = 0xFF;
	DDRD = 0x00;

	TCCR1A = (1 << WGM11) | (1 << COM1A1) | (1 << COM1A0);
	TCCR1B = (1 << WGM12) | (1 << WGM13) | (1 < <CS10);
	ICR1 = 19999;
	while(1)
	{
		if (i < 500)
		{
			OCR1A = 19999-600;
		}
		i++;
		_delay_ms(1);
		if (bit_is_set(PIND,0))
		{
			PORTB |= (1 << PINB2);
		}
		if (bit_is_clear(PIND,1))
		{
			PORTB &= ~(1 << PINB2);
		}

		if ((i < 1000) && (i > 500))
		{
			OCR1A = 19999-2400;
		}
		if (i == 1000)
		{
			i=0;
		}
	}
}

 

 

Thank you very much I try the code you write in the first text box it compile without any errors but when I use that in the second text box I got this errors

 

 

 

 

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

A magic space crept in on line 9 near CS10

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

The way you quote errors is of no help. Make the  build then switch to the tab called Output and copy the complete details of the error.

 

But Kartman spotted it anyway "< <" is supposed to be "<<"

Last Edited: Fri. Aug 14, 2020 - 02:03 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

 

This is output for the first text box code :

 

Build started.
Project "Fire sensing system.cproj" (default targets):
Target "PreBuildEvent" skipped, due to false condition; ('$(PreBuildEvent)'!='') was evaluated as (''!='').
Target "CoreBuild" in file "C:\Program Files\Atmel\Studio\7.0\Vs\Compiler.targets" from project "C:\Users\user\Documents\Atmel Studio\7.0\Fire sensing system\Fire sensing system\Fire sensing system.cproj" (target "Build" depends on it):
    Using "RunCompilerTask" task from assembly "C:\Program Files\Atmel\Studio\7.0\Extensions\Application\AvrGCC.dll".
    Task "RunCompilerTask"
        Shell Utils Path C:\Program Files\Atmel\Studio\7.0\shellUtils
        C:\Program Files\Atmel\Studio\7.0\shellUtils\make.exe all --jobs 2 --output-sync 
        make: Nothing to be done for 'all'.
    Done executing task "RunCompilerTask".
    Using "RunOutputFileVerifyTask" task from assembly "C:\Program Files\Atmel\Studio\7.0\Extensions\Application\AvrGCC.dll".
    Task "RunOutputFileVerifyTask"
                Program Memory Usage     :    202 bytes   2.5 % Full
                Data Memory Usage         :    0 bytes   0.0 % Full
                Warning: Memory Usage estimation may not be accurate if there are sections other than .text sections in ELF file
    Done executing task "RunOutputFileVerifyTask".
Done building target "CoreBuild" in project "Fire sensing system.cproj".
Target "PostBuildEvent" skipped, due to false condition; ('$(PostBuildEvent)' != '') was evaluated as ('' != '').
Target "Build" in file "C:\Program Files\Atmel\Studio\7.0\Vs\Avr.common.targets" from project "C:\Users\user\Documents\Atmel Studio\7.0\Fire sensing system\Fire sensing system\Fire sensing system.cproj" (entry point):
Done building target "Build" in project "Fire sensing system.cproj".
Done building project "Fire sensing system.cproj".

 

 

 

 

 

This is output for the second text box code :

clawson wrote:

clawson wrote:

Project "Test.cproj" (default targets):
Target "PreBuildEvent" skipped, due to false condition; ('$(PreBuildEvent)'!='') was evaluated as (''!='').
Target "CoreBuild" in file "C:\Program Files\Atmel\Studio\7.0\Vs\Compiler.targets" from project "c:\users\user\Documents\Atmel Studio\7.0\Test\Test\Test.cproj" (target "Build" depends on it):
    Using "RunCompilerTask" task from assembly "C:\Program Files\Atmel\Studio\7.0\Extensions\Application\AvrGCC.dll".
    Task "RunCompilerTask"
        Shell Utils Path C:\Program Files\Atmel\Studio\7.0\shellUtils
        C:\Program Files\Atmel\Studio\7.0\shellUtils\make.exe all --jobs 2 --output-sync 
        Building file: .././main.c
        Invoking: AVR/GNU C Compiler : 5.4.0
        "C:\Program Files\Atmel\Studio\7.0\toolchain\avr8\avr8-gnu-toolchain\bin\avr-gcc.exe"  -x c -funsigned-char -funsigned-bitfields -DDEBUG  -I"C:\Program Files\Atmel\Studio\7.0\Packs\atmel\ATmega_DFP\1.3.300\include"  -O1 -ffunction-sections -fdata-sections -fpack-struct -fshort-enums -g2 -Wall -mmcu=at90usb82 -B "C:\Program Files\Atmel\Studio\7.0\Packs\atmel\ATmega_DFP\1.3.300\gcc\dev\at90usb82" -c -std=gnu99 -MD -MP -MF "main.d" -MT"main.d" -MT"main.o"   -o "main.o" ".././main.c" 
c:\users\user\Documents\Atmel Studio\7.0\Test\Test\Debug\Makefile(76,1): error: recipe for target 'main.o' failed
        .././main.c: In function 'main':
c:\users\user\Documents\Atmel Studio\7.0\Test\Test\main.c(13,1): error: expected '=', ',', ';', 'asm' or '__attribute__' before '{' token
         {
         ^
c:\users\user\Documents\Atmel Studio\7.0\Test\Test\main.c(48,1): error: expected '{' at end of input
         }
         ^
        make: *** [main.o] Error 1
    Done executing task "RunCompilerTask" -- FAILED.
Done building target "CoreBuild" in project "Test.cproj" -- FAILED.
Done building project "Test.cproj" -- FAILED.

Build FAILED.
========== Build: 0 succeeded or up-to-date, 1 failed, 0 skipped ==========

 

Last Edited: Fri. Aug 14, 2020 - 03:22 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0
c:\users\user\Documents\Atmel Studio\7.0\Test\Test\main.c(13,1): error: expected '=', ',', ';', 'asm' or '__attribute__' before '{' token

then learn to read an error like this. The (13,1) bit says it detected the error at line 13 column 1 so it's probably objecting to something a bit before then.

 

But anyway, as already pointed out. In my zeal to make the code more readable (by spacing out the operators) I made a typographical errror:

	TCCR1B = (1 << WGM12) | (1 << WGM13) | (1 < <CS10);

is supposed to be:

	TCCR1B = (1 << WGM12) | (1 << WGM13) | (1 << CS10);

(I was adding spaces but in the wrong place!). Rather curiously when I build the code with this error what I see is:

		.././main.c: In function 'main':
D:\test\test\main.c(13,45): error: expected expression before '<' token
		  TCCR1B = (1 << WGM12) | (1 << WGM13) | (1 < <CS10);
		                                             ^

Sure (for me) this is line 13 too but the error is much clearer. Look at that '^' on the last line, it's even pointing to exactly the position in the line where the error was made.

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

I get this :

 

Target "PostBuildEvent" skipped, due to false condition; ('$(PostBuildEvent)' != '') was evaluated as ('' != '').

 

Sorry If I didn't understand some point because I am student and I take this subject in this course but all what I study a simple things how to control led light(on/off),7-segment...,etc

 

This is what I try to do

 

 

 

 

Last Edited: Fri. Aug 14, 2020 - 05:49 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi everybody now when I run it in proteus servo doesn't move and (I got this error <<<< excessive CPU load) 

 

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

KOLOMBOS wrote:
Hi everybody now when I run it in proteus servo doesn't move and (I got this error <<<< excessive CPU load) 

You have a proteus license, so call labcenter support about simulation issues, none of us can help you with that!  You have paid for this support!

 

 

(Possum Lodge oath) Quando omni flunkus, moritati.

"I thought growing old would take longer"

 

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

ki0bk wrote:

KOLOMBOS wrote:
Hi everybody now when I run it in proteus servo doesn't move and (I got this error <<<< excessive CPU load) 

You have a proteus license, so call labcenter support about simulation issues, none of us can help you with that!  You have paid for this support!

 

 

Ok,thank you

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

KOLOMBOS wrote:
By the way white space costs nothing but makes code more readable and also don't overuse != when just = would do as well so better is:
Emphasis added.

I infer |= was meant.

 

BTW I just love constructions like

if(a != b) {
    ...
} else {
    ...
}

 

Iluvatar is the better part of Valar.

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

At least it will not give a warning do you mean ==  or =

 

And I don't think I ever get used to see

if (5 == a)

it's just so wrong!

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

skeeve wrote:

BTW I just love constructions like

if(a != b) {
    ...
} else {
    ...
}

 

Hang your head in shame! Braces should have their own lines for every occurrence in true Allman style (and as mandated by MISRA):

 

if (a != b) 
{
    ...
} 
else 
{
    ...
}

And every subordinate clause should have its own braces - no dangling instructions without wrappers please!

 

Neil

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

barnacle wrote:
Hang your head in shame! Braces should have their own lines for every occurrence in true Allman style (and as mandated by MISRA):
Something else I'd never get used to.

At least it's better than GNU style.

Iluvatar is the better part of Valar.

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

+1

Bigger and harder to read, and nothing added if you use a modern editor. 

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

I prefer the "One True Brace Style" for my personal coding.

However, when a client requests can easily convert to another style using the appropriate style formatting filter.

David

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

frog_jr wrote:
I prefer the "One True Brace Style" for my personal coding.

 

Bah.  You need a compiler to make a C program worth anything.  A book ain't necessary and sufficient, is it?

 

The Whitesmiths style, also sometimes termed Wishart style, was originally used in the documentation for the first commercial C compiler, the Whitesmiths Compiler.

1TBS  indeed:

Whitesmiths, along with Allman, have been the most common bracing styles with equal popularity according to the Jargon File.[5]

I've used that style for so long that I have to struggle a bit reading code in other styles.  Somehow it seems right to have the code section at the same indentation level.

 

lol -- we've just gone through all the discussion at http://www.catb.org/jargon/html/... including, by implication,

Doubtless these issues will continue to be the subject of holy wars.

 

<edit>  I must amend, since I just used 1 TBS yesterday:

 

1 1/2 Cups Water
2 TBS Butter/Shortening/Margarine*
2 Cups Flour
2 Cups Whole Wheat Flour
3 TBS Dry Milk
1 TBS Vital Wheat Gluten
2 TBS Sugar (or Honey)
2 tsp Salt
1 1/2 tsp dry yeast

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.

Last Edited: Tue. Aug 18, 2020 - 12:32 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Whitesmiths Compiler.

I have one of those for the 68HC11, no longer working under new O/S. May still work on my old MSDOS machine.

John Samperi

Ampertronics Pty. Ltd.

www.ampertronics.com.au

* Electronic Design * Custom Products * Contract Assembly