making a 'C' function
in Propeller 1
Hi all, I’m trying to craft together a function for the following code to put in my library. I tried several methods but each and every one failed. I want the counter to return seconds for a LCD stopwatch I’m making. I know the ‘pause(1000)’ will not give me true seconds but I can deal with that later using some spin code, but for now I’m using C.
So to make this question more clear, I want a function I could call from main like ‘int counter()’ or something like that, that would output seconds. When I put the main body in a function if fails, and so I’m at a loss.
Any help would be most appreciated.
So to make this question more clear, I want a function I could call from main like ‘int counter()’ or something like that, that would output seconds. When I put the main body in a function if fails, and so I’m at a loss.
Any help would be most appreciated.
#include "simpletools.h"
#include "lcd.h"
int main()
{
inithd44780(); //init LCD hd44780 function from my lib
// Main counter
for(int t = 0; t >= -1; t++) //count to ~
{
seconds = t;
char str[80] ;
hours = seconds / 3600;
minutes = seconds / 60;
mins_left = minutes % 60;
secs_left = seconds % 60;
sprintf(str, " Time %d:%d:%d ",hours, mins_left, secs_left );
//above 20 line LCD must have 20 spaces after %d
LCDline(2); //go to line 2 on the LCD function from my lib
for(int i = 0; i < 20; i++) //allow only 20 characters
{
data(); //following this command is data function from my lib
set_outputs (lsbpin,msbpin, (str[i])); //one character at a time from msbit to lsbit
pause(TW); //wait 1 mS
strobe(); //latch the data function from my lib
}
pause(947); //wait 947mS
}
}

Comments
void MyFunction(void); // declare it, so C knows it exists, what it returns, and what arguments it takes void main(void) { MyFunction(); // calling the function we declared } void MyFunction(void) // now actually implement it { Print( "We're here!"); }If the body of MyFunction came before main(), I wouldn't need the forward declaration (that's what that thing is called).
If that isn't what's happening, post the code that's not compiling and someone here will surely be able to point in the right direction.
We call that "box" a "struct".
struct time_t { int hours; int minutes_left; int seconds_left; }; time_t do_stuff () { time_t now = {5, 36, 47}; return now; }void MyFunc( int *hours, int *mins, int *secs) { *hours = 10; // store the result in what the variable points at *mins = 5; *secs = 3; } void main(void) { int hrResult, minResult, secResult; // local variables to store results // pass pointers to the function using & MyFunc( &hrResult, &minResult, &secResult); // values are now in the variables }or
struct MyTime { int hours; int minutes_left; int seconds_left; }; void GetTimeRemaining( MyTime * t ) { t->hours = 5; t->minutes_left = 36; // fill in struct members through the pointer t->seconds_left = 47; return now; } void main(void) { MyTime remain; // local struct to hold result GetTimeRemaining( &remain ); // pass pointer to local struct to function Print( remain.hours ); }