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] AVR code not working on Atmel Studio 6

Status
Not open for further replies.

LeoStar

Member level 2
Joined
Nov 19, 2014
Messages
50
Helped
0
Reputation
0
Reaction score
0
Trophy points
6
Activity points
500
Hey there champions,
I am back with another jug of problems.
I have just started working on AVR Atmega16 microcontroller.
And I am using Atmel Studio 6.
I am very well familiar with the programming of PIC microcontroller on Microchip.
And i find it quite easy to build.
But i tried to program AVR in the same way of building logic as that of PIC and I it didn't work.
Here is the code that I tried.

PURPOSE OF CODE:
To on an LED with a Button for ten seconds

Here is the group of instructions that I wrote considering it complete code:
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
Code:
#include<avr/io.h>
#include<util/delay.h>

#define SW1 PA0
#define LED1 PA1

void main()
{
DDRA=0xFE;
PORTA=0x01;
LED1<<0;
while(1)
{
if(sW1==0)
{
LED1<<1;
_delay_ms(10000);
}
else
{
LED1<<0;
}
}
}
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
When I tried it on proteus, nothing happens no matter how many times i press the button.
Plus there is an error of internal exception on proteus and it crashes.
Any help would be greatly appreciated. It is urgent for me.
So please someone look for it.
Eagerly waiting for the reply.
Thanks in advance.
 
Last edited by a moderator:

Hi, LeoStar

Code:
#define SW1 PA0  //pin must be input (PINA.0 in CVAVR)
#define LED1 PA1 

void main()
{
DDRA=0xFE;
PORTA=0x01;
LED1<<0;   //no effect
while(1)
{
if(sW1==0)    //must be SW
{
LED1<<1;     //LED1=1; or LED1=1<<LED1
_delay_ms(10000);
}
else
{
LED1<<0;     //LED1=0; or LED1=0<<LED1
}
}
}
 

See if ADC has to be disabled to make the pins digital IO. For input PINA0 has to be used.

I don't use Atmel Studio much. I use mikroC PRO AVR Compiler. Here I am attaching a working project. You can test it in hardware. It is compiled for 4 MHz external oscillator.

I have tested this in Proteus and it works fine. You can download and install demo version of mikroC PRO AVR Compiler and open the .mcpav file.
 

Attachments

  • LED and Switch.rar
    66.7 KB · Views: 65
Last edited:

Thank you so much for replying guys.
DeepOne i tried your way and got the following error:

Error 3 lvalue required as left operand of assignment

for the following line:
Code:
LED1=1<<LED1;

Please tell me how to resolve this.
And the pin is already configured as input pin(DDRA=0xFE;).
And Milan thanks for advicing regarding the ADC, I will disable them and try it once again.

And I don't want to quit working on Atmel Studio 6 as it is new for me and adds variety to my software list.
I will find some other way of programming but wont quit working on it.
I will set it as my last resort to work with MIKRO C PRO AVR.
Thanks a lot for your precious time guys.
Looking forward to hear from you soon again.
 
Last edited by a moderator:

LED1=1<<LED1; //this equation is work in CVAVR, i think in #define LED1 PA1 - PA1 must be PORTA.1 or something equal
pin is already configured as input pin //of course, but it is necessary to address it as input register (PINA.0 in CVAVR)
// ADC is already disabled after power on reset, according to datasheet
 

seems construction like PORTA.1 or PINA.0 is not work in Atmel Studio. So you may use equation like PORTA |= 1<<PA1, if(PINA&PA0){do something}, etc. or manually organize access to separate bits. For example:
Code:
typedef unsigned int uint8_t;
typedef struct Bits_t
{
uint8_t Bit0 :1;
uint8_t Bit1 :1;
uint8_t Bit2 :1;
uint8_t Bit3 :1;
uint8_t Bit4 :1;
uint8_t Bit5 :1;
uint8_t Bit6 :1;
uint8_t Bit7 :1;
}Bits;

#define PortaBits (*((volatile Bits*)&PORTA))
#define PinaBits (*((volatile Bits*)&PINA))
#define LED1 PortaBits.Bit1
#define SW1 PinaBits.Bit0

void main()
{
DDRA=0xFE;
PORTA=0x01;

 while(1)
  {

	if(SW1==0)
	{
	LED1=1;
	_delay_ms(10000);
	}
	else
	{
	LED1=0;
	}
  }
}
 

Hello everyone,
Long time no see.
I have started working on MIKRO C PRO FOR AVR. I use it for pic as well and I consider it the best among the compilers.
Here is the code that i am trying on it. It gets compiled without errors but when i try it on proteus the pin that is connected to led is low and the pin with the switch is high as well but when I push the button it doesnt work.
Here is the code:
*******************
sbit SW1 at PinA.B0;
sbit LED at PORTa.B1;
void main()
{
DDRA=0xFE;
PORTA.B0=1;
if(!SW1)
{
LED=1;
delay_ms(5000);
}
else
LED=0;
}
+++++++++++++++++++++++++++
And the attachment shows that the led doesn't glow even if the switch is pressed.

Any help will be appreciated.

And whenever I try it out on proteus there is an error and proteus crashes. I am attaching a snapshot of the error as well. PROTEUS ERROR.jpgAVR.jpg
 
Last edited:

Try this project. It toggles LED on button press and release.



Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
sbit SW at PINA.B0;
sbit LED at PORTA.B1;
 
void main() {
 
    DDRA = 0xFE;
    PORTA = 0x00;
    
    while(1) {
        if(!SW) {
           Delay_ms(50);
           if(!SW);
              LED = 1;
        }
        else if(SW) {
             Delay_ms(50);
             if(SW)
               LED = 0;
        }
    }
}



broken link removed
 

Attachments

  • Switch and LED.rar
    86.7 KB · Views: 66
  • Switch and LED.png
    Switch and LED.png
    23.4 KB · Views: 120
Last edited by a moderator:

Thank you for your reply Milan, my code worked on proteus as well and now I have learned programming for AVR as well. As i told before I was very comfortable working with MICRO C so I made it to work.
Thanks a lot for your precious time.
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top