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: 16271
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: 21251
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: 2437
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: 21251
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: 18531
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: 21251
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: 5932
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: 21251
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: 20337
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: 20337
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: 6695
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: 21251
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: 1195
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: 62230
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
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