Forum Menu




 


Log in Problems?
New User? Sign Up!
AVR Freaks Forum Index

Post new topic   Reply to topic
View previous topic Printable version Log in to check your private messages View next topic
Author Message
nickelis
PostPosted: Jun 09, 2012 - 08:15 PM
Newbie


Joined: Jun 09, 2012
Posts: 4


i am trying to write code to control a bi-color led and a relay for a bypass circuit using an attiny 13a. this is my first program in C so any help or hints would be very welcome. using avr studio 6, haven't gotten a programmer yet but was going for the tinyusb...suggestions on this also needed and welcome.

the code:

*/using attiny13a/*
#include <avr/io.h>
#include <util/delay.h>
#include <time.h>

#define bled PB3
#define rled PB7
#define ryoff PB4
#define ryon PB0
#define switch PB1

void main() { */reset on power up, bypass mode, PORTB I/0 set/*
DDRB = 0b00011101;
switch = 0;
i = 0{
{output_high (PORTB, bled); */flash bled 2x's, then rled on until switch hit/*
delay_ms (250)};
{output_low(PORTB, bled);
delay_ms(250)};
{i = i++
if i >= 2
output_high (PORTB, rled)
goto switch1}
}


switch1 (){ */debounce attempt, start counter at tap, if not tappedfor/*
counter=0; */85msec cycles again, if so, moves to onstate/*
{while output_high (PORTB, switch);
start=counter;
if counter <85;
goto switch1;}
{
elseif counter >=85;
goto onstate;}
}

onstate ():

{{output_low (PORTB, rled); */rled off, bled on, relay on(will have to work this/*
output_high (PORTB, bled); */out a little better../*
output_high (PORTB, ryon);*/relay on/*
delay_ms (15)};
{if output_high (PORTB, switch);
goto switch2};
}

switch2 (): */debounce number 2 for off state.../*
{
counter=0;
{while output_high (PORTB, switch);
start=counter;
if counter <85;
goto switch2;}
{elseif counter >=85;
goto offstate;}
}

offstate (): */self explanatory/*
{
{output_high (PORTB, rled);
output_low (PORTB, bled);
output_high (PORTB, ryoff);
delay_ms (15)}
{if output_high (PORTB, switch);
goto switch1}
}


like i said, this could be missing huge pieces as it's my first attempt...any and all help needed and welcome. thanks in advance
 
 View user's profile Send private message  
Reply with quote Back to top
MBedder
PostPosted: Jun 09, 2012 - 08:47 PM
Raving lunatic


Joined: Nov 02, 2009
Posts: 3239
Location: Zelenograd, Russia


_________________
Warning: Grumpy Old Chuff. Reading this post may severely damage your mental health.
 
 View user's profile Send private message  
Reply with quote Back to top
david.prentice
PostPosted: Jun 09, 2012 - 09:13 PM
10k+ Postman


Joined: Feb 12, 2005
Posts: 16550
Location: Wormshill, England

First off, you need to read your C textbook. Comments should be:
Code:
/* blah-blah */

Then you should make an attempt at formatting the code. e.g. fresh line for each statement. indent each block of statements.

This is not difficult. You will then be able to post some attractive code. You will get more readers this way.

Then you will get plenty of help.

David.
 
 View user's profile Send private message Send e-mail  
Reply with quote Back to top
nickelis
PostPosted: Jun 09, 2012 - 09:34 PM
Newbie


Joined: Jun 09, 2012
Posts: 4


Code:
*/using attiny13a/*
#include <avr/io.h>
#include <util/delay.h>
#include <time.h>

#define bled PB3
#define rled PB7
#define ryoff PB4
#define ryon PB0
#define switch PB1

void maint()  {      /*reset on power up, bypass mode, PORTB I/0 set*/
   DDRB = 0b00011101;
   switch = 0;
   i = 0{

   {output_high (PORTB, bled);  /*flash bled 2x's, then rled on until switch hit*/
   delay_ms (250)};
   {output_low(PORTB, bled);
   delay_ms(250)};
   
   {i = i++
   if i >= 2
      output_high (PORTB, rled)
      goto switch1}
   }


switch1 (){           /*debounce attempt, start counter at tap, if not tappedfor*/

   counter=0;           /*85msec cycles again, if so, moves to onstate*/
      {while output_high (PORTB, switch);
         start=counter;
         if counter <85;
            goto switch1;}
   {
         elseif counter >=85;
            goto onstate;}
}

onstate ():

   {{output_low (PORTB, rled);   /*rled off, bled on, relay on*/
   output_high (PORTB, bled);   /*out a little better..*/
   output_high (PORTB, ryon);   /*relay on*/
      delay_ms (15)};
   {if output_high (PORTB, switch);
      goto switch2};
}

switch2 ():         /*debounce 2 for off state...*/
{
   counter=0;
   {while output_high (PORTB, switch);
      start=counter;
      if counter <85;
         goto switch2;}
      {elseif counter >=85;
         goto offstate;}
}

offstate ():         
{
   {output_high (PORTB, rled);
   output_low (PORTB, bled);
   output_high (PORTB, ryoff);
      delay_ms (15)}
   {if output_high (PORTB, switch);
      goto switch1}
}







 
 View user's profile Send private message  
Reply with quote Back to top
bobgardner
PostPosted: Jun 09, 2012 - 10:42 PM
10k+ Postman


Joined: Sep 04, 2002
Posts: 21396
Location: Orlando Florida

Did that one compile ok?

_________________
Imagecraft compiler user
 
 View user's profile Send private message Send e-mail Visit poster's website 
Reply with quote Back to top
sternst
PostPosted: Jun 09, 2012 - 10:55 PM
Raving lunatic


Joined: Jul 23, 2001
Posts: 2473
Location: Osnabrueck, Germany

bobgardner wrote:
Did that one compile ok?
No way! There are LOTS of syntax errors.

_________________
Stefan Ernst
 
 View user's profile Send private message  
Reply with quote Back to top
bobgardner
PostPosted: Jun 09, 2012 - 10:57 PM
10k+ Postman


Joined: Sep 04, 2002
Posts: 21396
Location: Orlando Florida

Shhh! I'm hunting wabbits!

_________________
Imagecraft compiler user
 
 View user's profile Send private message Send e-mail Visit poster's website 
Reply with quote Back to top
JohanEkdahl
PostPosted: Jun 09, 2012 - 10:58 PM
10k+ Postman


Joined: Mar 27, 2002
Posts: 18761
Location: Lund, Sweden

Quote:

Did that one compile ok?

Not on any standards compliant compiler:
Quote:
Code:
         elseif counter >=85;
 
 View user's profile Send private message Visit poster's website 
Reply with quote Back to top
bobgardner
PostPosted: Jun 09, 2012 - 11:22 PM
10k+ Postman


Joined: Sep 04, 2002
Posts: 21396
Location: Orlando Florida

Shhh! Let Mr Nickelis tell us what his software development chain reports. He might have a full IAR Electronic Workbench setup that none of us know how to run.

_________________
Imagecraft compiler user
 
 View user's profile Send private message Send e-mail Visit poster's website 
Reply with quote Back to top
valusoft
PostPosted: Jun 10, 2012 - 05:17 AM
Raving lunatic


Joined: Jul 02, 2005
Posts: 6039
Location: Melbourne, Australia

Nick,

Without spoiling the fun that others might be eliciting, could I suggest that you consult your C text book to see what it says about the use of {} pairs and where they should be placed.

Don't worry ... we want to help you; spoon feeding doesn't help though.

Cheers,

Ross

_________________
Ross McKenzie
ValuSoft
Melbourne Australia
 
 View user's profile Send private message  
Reply with quote Back to top
nickelis
PostPosted: Jun 10, 2012 - 09:36 PM
Newbie


Joined: Jun 09, 2012
Posts: 4


well, i've used BASIC...thanks for further discouraging an already discouraged attempt at C...gettin back to the books....
 
 View user's profile Send private message  
Reply with quote Back to top
bobgardner
PostPosted: Jun 10, 2012 - 11:05 PM
10k+ Postman


Joined: Sep 04, 2002
Posts: 21396
Location: Orlando Florida

I find that the short chapter called "c in 16 pages" in the imagecraft help file that comes with the compiler is informative. Its a free dl. If you know basic, I think that is a big help. The procedural languages like c, basic, pascal and fortran are all more similar than different. They all have signed and unsigned integers of 1,2 and 4 byte length, they all have subroutines that take parameters and return a value, they all have a for loop, a loop that tests at the top, a loop that tests at the bottom, an if then else statement and a switch statement. Copy the operator page out of the c book. C uses & instead of AND, so you need to either remember it or look it up.

_________________
Imagecraft compiler user


Last edited by bobgardner on Jun 10, 2012 - 11:12 PM; edited 1 time in total
 
 View user's profile Send private message Send e-mail Visit poster's website 
Reply with quote Back to top
js
PostPosted: Jun 10, 2012 - 11:11 PM
10k+ Postman


Joined: Mar 28, 2001
Posts: 20637
Location: Sydney, Australia (Gum trees, Koalas and Kangaroos, No Edelweiss)

Quote:
thanks for further discouraging an already discouraged attempt at C
Don't be discouraged, it took me 20 years++ before I started to use it. Wink

_________________
John Samperi
Ampertronics Pty. Ltd.
www.ampertronics.com.au
* Electronic Design * Custom Products * Contract Assembly
 
 View user's profile Send private message Visit poster's website 
Reply with quote Back to top
js
PostPosted: Jun 10, 2012 - 11:18 PM
10k+ Postman


Joined: Mar 28, 2001
Posts: 20637
Location: Sydney, Australia (Gum trees, Koalas and Kangaroos, No Edelweiss)

From another thread
Quote:
Read the "c in 21 pages" in the imagecraft help file
here
Quote:
the short chapter called "c in 16 pages" in the imagecraft help file
did they cut down the number of pages due to the bad economy or do you get a reduction for good behaviour? Wink

_________________
John Samperi
Ampertronics Pty. Ltd.
www.ampertronics.com.au
* Electronic Design * Custom Products * Contract Assembly
 
 View user's profile Send private message Visit poster's website 
Reply with quote Back to top
zbaird
PostPosted: Jun 10, 2012 - 11:39 PM
Raving lunatic


Joined: Aug 13, 2006
Posts: 6766
Location: Bellingham, WA - USA

Quote:
did they cut down the number of pages

Different optimizer switch.

_________________
Chuck Baird
"It's better to catch the trapeze than test the safety net" -- RPi book
http://www.cbaird.org
 
 View user's profile Send private message Visit poster's website 
Reply with quote Back to top
bobgardner
PostPosted: Jun 11, 2012 - 12:05 AM
10k+ Postman


Joined: Sep 04, 2002
Posts: 21396
Location: Orlando Florida

I've seen a couple of short summary cheat sheets, but that's the one I can usually remember while sitting at the keyboard. I hope you can find it in the help file even though I forgot how many pages it was.

_________________
Imagecraft compiler user
 
 View user's profile Send private message Send e-mail Visit poster's website 
Reply with quote Back to top
larryvc
PostPosted: Jun 11, 2012 - 01:25 AM
Raving lunatic


Joined: Dec 06, 2007
Posts: 2512
Location: Redmond, WA USA

nickelis,

I think you will find this web site very useful:

http://www.cprogramming.com/tutorial.html

_________________
Larry

Those afraid to embrace the future will quickly fade into the past. - larryvc
 
 View user's profile Send private message  
Reply with quote Back to top
nickelis
PostPosted: Jun 11, 2012 - 12:03 PM
Newbie


Joined: Jun 09, 2012
Posts: 4


thank you. I will look into all of these...the help page definitely is informative and the site you posted larry, should help a lot...i'll re-post once i feel i get to someting a little more legible...
 
 View user's profile Send private message  
Reply with quote Back to top
dbrion0606
PostPosted: Jun 12, 2012 - 11:55 AM
Posting Freak


Joined: Jan 07, 2012
Posts: 1318
Location: North of France

Well,there are things I do not understand:
what do you do with the veriables 'start and ' counter (they both seem stuck to 0). Perhaps you should make them evolve a little?


The construction " {while output_high (PORTB, switch); "
might be "while output_high (PORTB, switch){"
(in the fist case, one loops without doing anything unless output_high becomes .. low.

The same thing with " if counter <85;
goto switch1
;" which is likely to become
"if counter <85 goto switch1;"
 
 View user's profile Send private message  
Reply with quote Back to top
clawson
PostPosted: Jun 12, 2012 - 12:00 PM
10k+ Postman


Joined: Jul 18, 2005
Posts: 62952
Location: (using avr-gcc in) Finchingfield, Essex, England

Quote:

which is likely to become
"if counter <85 goto switch1;"

Sorry - you are advocating that a beginner start out using "goto" in C? What is the thinking behind that? Have you heard of the term "structured programming"?

http://en.wikipedia.org/wiki/Structured_programming

_________________
 
 View user's profile Send private message  
Reply with quote Back to top
dbrion0606
PostPosted: Jun 12, 2012 - 12:59 PM
Posting Freak


Joined: Jan 07, 2012
Posts: 1318
Location: North of France

Bonjour, Monsieur Clawson
Yes,
I first read of the term "structured programming" in 1979 (two millenia) .
I also know languages such as R where gotos do not even exist at all (no need to forbid them : people remain happy without goto's)...

But

I had noticed he knew basic and I kow gotos are not forbidden ic C (should be reserved for escaping when an error happens into abyssally deep program structures).

And
one should be indulgent with beginners.... (and even you did not protest whis his goto's)

And
I would be happy to understand what he wants to do, because I am curious and because I am old and silly enough not to understand....
 
 View user's profile Send private message  
Reply with quote Back to top
clawson
PostPosted: Jun 12, 2012 - 01:02 PM
10k+ Postman


Joined: Jul 18, 2005
Posts: 62952
Location: (using avr-gcc in) Finchingfield, Essex, England

Quote:

one should be indulgent with beginners...

Teaching them bad programming practice?

Even BASIC these days is a structured language since the need for line numbers GOTO and GOSUB were removed. There's simply no excuse for bad programming technique in 2012.

(and yes goto has it's place even in C but it's usually for escaping early from a heavily nested situation when a serious error has occurred (like malloc() returned 0) and should not be considered for "normal" programming)

I was formally taught programming 31 years ago at university using Pascal and even back then the idea of using goto was severely frowned upon - hopefully things have progressed in 30 years.

(anyway you were advocating "goto switch1" when it's actually the name of a function not a goto label so the syntax didn't make sense even if the goto did)

_________________
 
 View user's profile Send private message  
Reply with quote Back to top
dbrion0606
PostPosted: Jun 12, 2012 - 01:21 PM
Posting Freak


Joined: Jan 07, 2012
Posts: 1318
Location: North of France

"anyway you were advocating "goto switch1" when it's actually the name of a function not a goto label so the syntax didn't make sense even if the goto did"

was "switch1" the name of a void function in his mind?

(in my mind, I did not read the () and thought of a badly spelled label..., if he had not spoken about basics, maybe I would have read the "()"...)

Therefore, he might happily remove the gotos here if switch is meant as a void function...

and what makes
while(conditionx);
if (conditiony);
except testing the conditions (x y)?

Is that was he was meaning?

But, if one shows too many errors at a time, that might be discouraging! An error after the other is more efficient.

I was not advocating anything, but before structuring
 
 View user's profile Send private message  
Reply with quote Back to top
david.prentice
PostPosted: Jun 12, 2012 - 01:42 PM
10k+ Postman


Joined: Feb 12, 2005
Posts: 16550
Location: Wormshill, England

@nickelis,

Your C syntax is seriously wrong in many places.
I see this as a minor problem that is easily resolved.

However the first job is to lay your program out neatly even in pseudo-code.
1. Indent blocks of code.
2. Put each statement on a fresh line
3. Add explanatory comments that say what you want to do.

Once you have done this, you can translate into C, Pascal, Java or whichever language you choose.
You can lookup the correct syntax for if() or while() in your textbook, and we will help you to get everything right.

Those (1), (2), (3) steps are your responsibility. Get them sorted and translating into C is trivial.

David.
 
 View user's profile Send private message Send e-mail  
Reply with quote Back to top
dbrion0606
PostPosted: Jun 21, 2012 - 02:36 PM
Posting Freak


Joined: Jan 07, 2012
Posts: 1318
Location: North of France

Bonjour Nickelis
I found some code which debounces a push button switch here (http://www.elektor.fr/extra/programmation-en-c-des-microcontroleurs-risc-avr.966219.lynkx, download and unzip AVRprog-VF.zip and have a look at the very well written c source in AVRprog\commut2 :
when the bounce switch is high, it waits 50 ms (you prefer 85 ms) and, if it is confirmed, it waits until it gets low... (in the book, the switch is connected to PORTA, but it doesnot seem that complicated to move it where you want)

Perhaps you should have a variable, let it be called "state", whith two values (this "state" increases each time the button is pressed and released, and if it goes too high, it cames back to its lower value)

state 1 : relay is on, LED A is on, led B is off
state 2 : relay is off, LED A is off, led B is on

BTW: perhaps led A and relay might be put together , and I do not understand why your relay needs two inputs .... This would spare 1 or 2 pins and therefore some hardware complexity

I do not understand hou you initialise it (you make a kind of greeting with a LED blinking?) but your topic is interesting (is already in a book if I do not make any misunderstanding) though it seems simple...
 
 View user's profile Send private message  
Reply with quote Back to top
bobgardner
PostPosted: Jun 21, 2012 - 03:41 PM
10k+ Postman


Joined: Sep 04, 2002
Posts: 21396
Location: Orlando Florida

Before expending too much worry about false button presses due to bouncing, maybe just try to get the program running using no debouncing at all, and just limit the loop time of the program to about 30Hz or 50Hz. (33ms or 20ms per loop, well within the carbon based unit's perception of 'crisp response'). Buttons don't bounce more than a couple of times a couple of ms apart, and a 20 ms loop time might just make the problem moot.

_________________
Imagecraft compiler user
 
 View user's profile Send private message Send e-mail Visit poster's website 
Reply with quote Back to top
js
PostPosted: Jun 21, 2012 - 11:14 PM
10k+ Postman


Joined: Mar 28, 2001
Posts: 20637
Location: Sydney, Australia (Gum trees, Koalas and Kangaroos, No Edelweiss)

Quote:
I do not understand why your relay needs two inputs ....
A latching relay? 1 input for on 1 input for off.

_________________
John Samperi
Ampertronics Pty. Ltd.
www.ampertronics.com.au
* Electronic Design * Custom Products * Contract Assembly
 
 View user's profile Send private message Visit poster's website 
Reply with quote Back to top
dbrion0606
PostPosted: Jun 23, 2012 - 04:27 PM
Posting Freak


Joined: Jan 07, 2012
Posts: 1318
Location: North of France

Bonjour js:
well, I wish to thank you for the idea of latching delays (according to wikipedia, it remains in a given state even if not power supplied).

Bonjour Bobgardner:
well, perhaps giving a function which changes states and one of which parameters is the delay to debounce would be versatile enough.
Such a function would be

Code:
// for syntax checking under cygwin and native gcc (gcc -c or gcc -S)
// should be deleted, of course, with avr-gcc
#define PORTB 1
#include <stdint.h>

#define TIME2DEBOUNCE 85 // ms to see whether the switch is dreaming
#define MAXSTATES 2 // Two states are allowed (1 , 2)

uint8_t changestate(uint8_t state, uint8_t behavior, uint8_t push_button,
      uint8_t debouncetime){
   /* push-button is the portsB'sspin
    *
    * behavior is what one wants to do
    * == 1 -> one waits until key is depressed
    * else -> one goes on, the LEDS and the relay being
    * allowed to change states every
    * 1/10 seconds...
    *
    * state is the present state
    */

   uint8_t aux;
   aux = state;
   if (~input_high(PORTB,push_button))  return (state); // do not change state
   delay_ms(debounce_time);
   if (input_high(PORTB,push_button))
   {
      aux = state + 1;
      if (aux > MAXSTATES) aux=1;
   }
   /*
    * some code to account quickly for state changes could be put there
    */
   if (behavior == 1)
   {
      while (input_high(PORTB,push_button)) ;
        }

   return(aux);
}


Once input_high is defined, perhaps it might work... or there is a better push button "manager"....
 
 View user's profile Send private message  
Reply with quote Back to top
dbrion0606
PostPosted: Jun 23, 2012 - 04:33 PM
Posting Freak


Joined: Jan 07, 2012
Posts: 1318
Location: North of France

@me
s/debounce_time/debouncetime/ and be sorry for posting before syntax checking...
 
 View user's profile Send private message  
Reply with quote Back to top
Display posts from previous:     
Jump to:  
All times are GMT + 1 Hour
Post new topic   Reply to topic
View previous topic Printable version Log in to check your private messages View next topic
Powered by PNphpBB2 © 2003-2006 The PNphpBB Group
Credits