basic LED blinking code

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

<

// ***********************************************************
// Project:
// Author:
// Module description:
// ***********************************************************

#include <avr\io.h>              // Most basic include files
#include <avr\interrupt.h>       // Add the necessary ones
#include <avr\signal.h>          // here
#include <util/delay.h>

// Define here the global static variables
//
int My_global;

// Interrupt handler example for INT0
//
SIGNAL(SIG_INTERRUPT0) {

}

// It is recommended to use this coding style to
// follow better the mixed C-assembly code in the
// Program Memory window
//
void my_function(void) {  // Put the open brace '{' here

   asm("nop");          // Inline assembly example
}

// ***********************************************************
// Main program
//
int main(void) {

DDRB|=0xff;
PORTC=(1<<1)|(1<<2);
//PORTB=(1<<4);
  while(1) {
  if(PINC&(1<<PC1)) {
       PORTB=0X55;
       delay_ms(2000);
       PORTB=0XAA;
       delay_ms(1000);  }

     if(PINC&(1<<PC2)){
       PORTB=0X55;
       delay_ms(2000);
       PORTB=0XAA;
       delay_ms(2000);  }

   }

}
void delay_ms	(int d )
{
//while(1);
}	

 

can someone tell me why  LED is not toggling every 1 and 2 sec?

 

must have tried indefinite times :(

This topic has a solution.
Last Edited: Fri. Oct 12, 2018 - 04:41 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Start by removing all the PINC input stuff. Just do the loop to drive the output:

int main(void) {

  DDRB = 0xff;
  while(1) {
       PORTB=0X55;
       delay_ms(2000);
       PORTB=0XAA;
       delay_ms(1000);
  }
}

So first verify that works then add the complications afterwards.

 

PS I don't see where you set F_CPU??

Last Edited: Wed. Oct 10, 2018 - 01:46 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Get into the habit of specifying path names with a forward slash instead of a back-slash:

#include <avr/io.h>              // Most basic include files
#include <avr/interrupt.h>       // Add the necessary ones
#include <avr/signal.h>          // here
#include <util/delay.h>

See:

https://stackoverflow.com/questions/5790161/is-the-backslash-acceptable-in-c-and-c-include-directives

"Experience is what enables you to recognise a mistake the second time you make it."

"Good judgement comes from experience.  Experience comes from bad judgement."

"Wisdom is always wont to arrive late, and to be a little approximate on first possession."

"When you hear hoofbeats, think horses, not unicorns."

"Fast.  Cheap.  Good.  Pick two."

"Read a lot.  Write a lot."

"We see a lot of arses on handlebars around here." - [J Ekdahl]

 

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

manaswini1317 wrote:
can someone tell me why LED is not toggling every 1 and 2 sec?

Where is my check list of giving all needed information? 

https://www.avrfreaks.net/commen...

Please post a complete test program.  Tell your AVR model and clock speed.  Tell your language, brand of toolchain and version.  Tell compile options, and build results.  Are there any warnings?Show your schematic and describe what is connected.

 

What do you expect to happen?  What >>is<< happening?  How are you testing?

 What do you expect to happen?  What >>is<< happening?  How are you testing?  No lamps at all?  Bulbs explode?   Cops arrive at door?

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.

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

https://www.nongnu.org/avr-libc/...

#define SIGNAL (   vector )  

Introduces an interrupt handler function that runs with global interrupts initially disabled.

This is the same as the ISR macro without optional attributes.

Deprecated:

Do not use SIGNAL() in new code. Use ISR() instead.

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.

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

Hi Clawson,

 

Thanks for the response

I intend to toggle  my led using key as input

I've used key1 and key2

so, when I press key1, the led should toggle with a delay of 1 second

and with key2 it should toggle with a delay of 2 seconds

 

Also, the F_CPU has been configured in the  .prj file

i.e 1MHz

 

Last Edited: Wed. Oct 10, 2018 - 02:54 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Well, we are part of the way there.

theusch wrote:
What do you expect to happen?

manaswini1317 wrote:
when I press key1, the led should toggle with a delay of 1 second and with key2 it should toggle with a delay of 2 seconds

theusch wrote:
What >>is<< happening?

We still don't know what doesn't work...

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.

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

Did any one mention "show schematics/connections"?

Enabling the pullups for the "button" pins implies to me active-low switches.  But the code is looking for a high.

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.

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

Hi,

I was so pissed off with the "not working code" that I simply wanted to know the reasons behind it

I didn't even realize I haven't mentioned any of the details

AVR model :ATMEGA8

clock speed: 1MHz

 language: embedded C

brand of toolchain and version: VMLAB

build results.: successful

  Are there any warnings: No warnings

what is connected. :PORTC(PC1,PC2) are inputs(keys)

desired output:PORTB which is the output needs to toggle every 1 second on pressing key1 and 2seconds on pressing key2

I am working on simulator 

Please let me know if I have missed anything

Kindly do the needful as I am still a learner

I ensure sincere efforts from my side

 all I need is a little push 

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

"  if(PINC&(1<<PC1)) "

if this is what you are talking about, I am trying to tell the compiler that if   button 1(WHICH IS CONNECTED to PORTC.1) is pressed, the pull-up resistor should get activated  which in turn says if (TRUE)

and do the delay job of 1second

Same is the case with Portc.2 for 2 seconds delay

 

what doesn't work:

my LED which should pause for 1 and 2 seconds but once it is into the while loop it is continuously blinking irrespective of the delay I am giving

Last Edited: Wed. Oct 10, 2018 - 03:16 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

manaswini1317 wrote:
Please let me know if I have missed anything

WHAT >>IS<< HAPPENING?

 

Did you see my comment on active-low switches?

 

Did you do Cliff's basic blink test?  Do you see the lamp then?

 

Assuming active-low switches on PC1 and PC2, then in a real application switches/buttons bounce wo you need to address that.  But for your purposes you need to think about each of your if() tests...

 

// Check for active-low on PC1 and only PC1 and not PC2
if ((PINC & 0x06) == 0x04)
    {
        // PC1 pressed and not PC2
    }
    
// Check for active-low on PC2 and only PC2 and not PC1
if ((PINC & 0x06) == 0x02)
    {
        // PC2 pressed and not PC1
    }
    
    

 

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.

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

ok

Now it has come down to a part of what my required output is

The LED toggles on when key1 or 2 pressed

Thanks for that Theusch

but,not with the delay I expected 

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

manaswini1317 wrote:
if this is what you are talking about, I am trying to tell the compiler that if button 1(WHICH IS CONNECTED to PORTC.1) is pressed, the pull-up resistor should get activated which in turn says if (TRUE)

That is nonsense.  Show your connections.  With your code, how do you ever expect the level on that pin be >>not<< a high logic level.

 

You are finally saying a bit about what >>is<< happening.  What does "continually blinking" mean?  Surely a vital piece of information to do a diagnosis is "what is the blink rate".

manaswini1317 wrote:
but,not with the delay I expected

Let me go back to

theusch wrote:
What do you expect to happen? What >>is<< happening?

Again you give only partial information.

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.

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

 

ok..let me put in an algorithm:

step1: set input pins PORTC1,PORTC2

Step2:set output port PORTB

STEP3:if button1 i.e portc1 is pressed(active-low as suggested by you)-->o/p toggle LED with a delay of 1second   1sec-on-- wait for delay(1second)-off---wait for delay(1second)

Else,portc2 is pressed-->o/p:o/p toggle LED with a delay of 2second   2sec-on-- wait for delay(2second)-off---wait for delay(2second) is expected o/p

 

current o/p:

led is in on -state

when key1 is pressed, the led toggles (definitely not with one sec delay) 

same is the case with key2

 

 

You are finally saying a bit about what >>is<< happening.  What does "continually blinking" mean?  Surely a vital piece of information to do a diagnosis is "what is the blink rate".

manaswini1317 wrote:
but,not with the delay I expected

Let me go back to

theusch wrote:
What do you expect to happen? What >>is<< happening?

I am not using << and >> after following your  piece of code :)

and as far as this is concerned 

manaswini1317 wrote:
if this is what you are talking about, I am trying to tell the compiler that if button 1(WHICH IS CONNECTED to PORTC.1) is pressed, the pull-up resistor should get activated which in turn says if (TRUE)

That is nonsense.  Show your connections.  With your code, how do you ever expect the level on that pin be >>not<< a high logic level.

 

I am unable to put it in proper terms 

 

anything else I need to add?

 

Last Edited: Wed. Oct 10, 2018 - 04:32 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

WHAT IS HAPPENING?  Is it that hard to say "I expect the LED to blink every 1.23 seconds, but it is blinking at a rate of 2.34 seconds."  Or 1 second.  Or 10.  Or 100.  Along with the code actually being used, how is anyone supposed to help with a solution if no one knows what the symptoms are?

 

Apparently you know better.  Apparently I am irritating you by asking pertinent questions.  I'm out.

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.

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

@ manaswini1317

 

Please do not edit the code in your OP as it no longer makes sense to what CLiff wrote after, and the posts after and so on.  When oyu make changes, post in your response those changes instead.  This way folks reading the thread for the first time understand what is happening.

 

Jim

If you want a career with a known path - become an undertaker. Dead people don't sue! - Kartman

Why is there a "Highway to Hell" and only a "Stairway to Heaven"? A prediction of the expected traffic load?  - Lee "theusch"

Please Read: Code-of-Conduct

Atmel Studio6.2/AS7, DipTrace, Quartus, MPLAB user

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

Thanks Jim..will follow your suggestion

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

apologies if the tenor of the tone came out wrong

I am just asking if I missed out anything else that might  help you to lead me to a solution

kindly understand that this is my first post in the forum 

never my intention to be rude

 

 

 

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

Are you sure the CPU is operating @ 1MHz? I would do just a simple blink program, without keys, for testing.

 

edit:

Why do you have this function:

void delay_ms	(int d )
{
//while(1);
}	

 

The function defined in util/delay.h has a leading underscore: _delay_ms()

Last Edited: Wed. Oct 10, 2018 - 11:07 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

thanks, much Tangas for leading me to expected o/p