| Author |
Message |
|
|
Posted: Jun 09, 2012 - 08:15 PM |
|

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 |
|
|
| |
|
|
|
|
|
Posted: Jun 09, 2012 - 08:47 PM |
|

Joined: Nov 02, 2009
Posts: 3239
Location: Zelenograd, Russia
|
|
 |
_________________ Warning: Grumpy Old Chuff. Reading this post may severely damage your mental health.
|
| |
|
|
|
|
|
Posted: Jun 09, 2012 - 09:13 PM |
|

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. |
|
|
| |
|
|
|
|
|
Posted: Jun 09, 2012 - 09:34 PM |
|

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}
}
|
|
|
| |
|
|
|
|
|
Posted: Jun 09, 2012 - 10:42 PM |
|


Joined: Sep 04, 2002
Posts: 21251
Location: Orlando Florida
|
|
| Did that one compile ok? |
_________________ Imagecraft compiler user
|
| |
|
|
|
|
|
Posted: Jun 09, 2012 - 10:55 PM |
|


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
|
| |
|
|
|
|
|
Posted: Jun 09, 2012 - 10:57 PM |
|


Joined: Sep 04, 2002
Posts: 21251
Location: Orlando Florida
|
|
| Shhh! I'm hunting wabbits! |
_________________ Imagecraft compiler user
|
| |
|
|
|
|
|
Posted: Jun 09, 2012 - 10:58 PM |
|


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;
|
|
|
| |
|
|
|
|
|
Posted: Jun 09, 2012 - 11:22 PM |
|


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
|
| |
|
|
|
|
|
Posted: Jun 10, 2012 - 05:17 AM |
|


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
|
| |
|
|
|
|
|
Posted: Jun 10, 2012 - 09:36 PM |
|

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.... |
|
|
| |
|
|
|
|
|
Posted: Jun 10, 2012 - 11:05 PM |
|


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
|
| |
|
|
|
|
|
Posted: Jun 10, 2012 - 11:11 PM |
|


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.  |
_________________ John Samperi
Ampertronics Pty. Ltd.
www.ampertronics.com.au
* Electronic Design * Custom Products * Contract Assembly
|
| |
|
|
|
|
|
Posted: Jun 10, 2012 - 11:18 PM |
|


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?  |
_________________ John Samperi
Ampertronics Pty. Ltd.
www.ampertronics.com.au
* Electronic Design * Custom Products * Contract Assembly
|
| |
|
|
|
|
|
Posted: Jun 10, 2012 - 11:39 PM |
|


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
|
| |
|
|
|
|
|
Posted: Jun 11, 2012 - 12:05 AM |
|


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
|
| |
|
|
|
|
|
Posted: Jun 11, 2012 - 01:25 AM |
|


Joined: Dec 06, 2007
Posts: 2512
Location: Redmond, WA USA
|
|
|
|
|
|
|
Posted: Jun 11, 2012 - 12:03 PM |
|

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... |
|
|
| |
|
|
|
|
|
Posted: Jun 12, 2012 - 11:55 AM |
|


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;" |
|
|
| |
|
|
|
|
|
Posted: Jun 12, 2012 - 12:00 PM |
|


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 |
_________________
|
| |
|
|
|
|
|