NerdKits - electronics education for a digital generation

You are not logged in. [log in]

NEW: Learning electronics? Ask your questions on the new Electronics Questions & Answers site hosted by CircuitLab.

Microcontroller Programming » Point to a subroutine in the code for pushbutton switch monitoring

January 07, 2012
by amartinez
amartinez's Avatar

Hello guys and Happy 2012, I'm back. I've been playing with the AVR Dragon after my move and all.

I'm writing a program where lights turn on and off back and forth sort of like a scanner. When I push a button connected to a pin in the 168, I want the timing delay to change making the lights go faster or slower depending on conditional statement in the code.

I use "while" to loop the program but within the code I'd like to consistently monitor the pushbutton routine. As it is, I can change the speed once the program starts the loop again.

I find myself copying and pasting the same conditional statements intervals throughout the code which is horribly inefficient, a better way is to use some sort of pointer to some code like a "goto" statement but I'm finding I'm having problems with that.

It's been a while and I'm trying to ease my way back in to AVR programming.

Thanks for any help on this

January 07, 2012
by treymd
treymd's Avatar

Are you referring to a function?

void myFunction()
{
     reusable code
}

in main:

if (something)
{
    myFunction();
}
January 07, 2012
by amartinez
amartinez's Avatar

Treymd,

I guess I am. As mentioned, I come from the old school of programming where you used goto and all so what you mention may work. I'll look in to it.

Thank you

Al

January 07, 2012
by treymd
treymd's Avatar

I remember using GOTO quite a bit back on my Atari with BASIC. Using gotos in C is frowned upon in many circles, but as far as I know, the ability is still there. Using function calls, however is preferred, as it allows for more readable code. Jumping around with gotos, not so much.

On a side note, it is kind of silly that the spoken rule is to use functions in preference over gotos, when more than likely, the cpu itself uses one of a few jump instructions to accomplish both.

January 09, 2012
by Ralphxyz
Ralphxyz's Avatar

amartinez, I "think" you should be using an interrupt to detect your button push!!

Check out the mcu specsheet for "pin change" interrupt.

How are you doing with the Dragon? got every thing working?

Ralph

January 24, 2012
by amartinez
amartinez's Avatar

Sorry so late. Been busy. Treymd, I am using functions now, I graduated to true C programmer. Now when I compile the code to a hex file I rarely get errors. Makes me smile... ah... but this is usually the time I get kicked in the hind end by some spurious code.

Ralphxyz!!! Hello buddy, I haven't forgotten you. Actually I'm cheating, I'm using the dragon all the time now. I use SPI for programming the 168 (no longer the usb/rs232 and reset button) and the AVR Studio for code, compiling and upload. I don't need a bootloader, that's taken care of by AVR Studio so I buy empty chips. I just have so many projects I want to do it's the best way for me, I'd love to give the guys my business but it's so easy for me to add chips to my monthly order to Digikey or Mouser.

I found the dragon very helpful yet a bit buggy on the debug side. I have got to post some pics of my latest project, it's a timer circuit which sounds a bit rudimentary but it incorporates 2 buttons and several functions. It's being used by a company so I can't reveal the code but I was very pleased with the Dragon's performance with this project.

Next up, oh boy... the LED Marquee project. I purchased one from Nerdkits and I'm nervous about it but I have to learn it. I'm still having problems communicating with hyperterminal, so I think I'm going to incorporate a MAX232 for future projects. It seems that the bootloader the AVR Studio installs is different and the 168 sends data from pins 2 and 3 but I can't figure out what speed or setting as of yet. I also think the bootloader is larger.

Now you're up to date. Ralph, I will post my detailed dragon connection using SPI to a protoboard.

Al

January 29, 2012
by amartinez
amartinez's Avatar

Ralph, and all interested, as promised I posted the AVR Dragon info in the Microcontroller Programming section dated 1/27/2012.

January 29, 2012
by Ralphxyz
Ralphxyz's Avatar

Yeah Al, I saw it.

Hope that will prompt others to get a Dragon.

Though other programmers work as well, I like to go with native Atmel devices.

Ralph

Post a Reply

Please log in to post a reply.

Did you know that inductors try to keep their current constant over short periods of time? Learn more...