Subs and functions in C

Status
Not open for further replies.

be80be

Well-Known Member
Been playing with C for awhile. Its a little bit of a change any way. Whats the right way to write a Sub in C
 
Nice thanks I was thinking more like this C uses functions as both unlike my beloved Basic.

Code:
void send_data(unsigned char data_out)
{
unsigned char i;
i=8;
while (i)
{
data_out = data_out >>1;
i--;
SClock =~ SClock;
}
}
 
A basic sub is just a function that doesn't return anything. So your void send_data(unsigned char data_out) is equivalent to a sub.

Mike.
 
Well I been playing with MikroC and read dang near there whole help file and couldn't fine a thing to point me right. But i think I'm on the right track now
I wrote that piece and complied it and it said that main was a empty sub. Lol it was . Thanks Mike And Sceadwian

Now all i need is to figure how to do a bound In C

Like Send out 0b10101010 out A GPIO.B0 one bit at a time

Code:
int i;
 i=8;
  while (i)   
{
    GPIO.B0 = data>>1;
      i--;
       SClock=~SClock;
  }
 
Last edited:
A for loop will do that for you,
Code:
    for(i=0;i<8;i++){           //for 0 to 7
        GPIO.B0 = data&1;       //send out bit zero
        data>>=1;               //shift data right 1 bit
        SClock=~SClock;         //doesn't this need to be 1 then 0
     }

Edit, what do you mean by Bound?

Mike.
 
Last edited:
Bound just lets you set it to what your sending so you don't have to set 0 to7 it would 0 bound to Data and it's size. Thanks Mike I feel like a real dummy I should of thought about a for loop.

I for loop would work great seeing I can't send more then 8 bits at a time. Thanks agin Mike.
 
You can if you specify a size

Code:
void shiftit( long value, int size)
   {
   while(size--)
      GPIO.B0 = (value >>= 1) & 1;
   }

Sorry if this looks a little compressed, but its only a suggestion.
 
Correct!! This isn't his solution, its only a suggestion I was merely showing the code in a while statement..

It's easy enough to split the line..
 
Dang the joy of a New compiler reserve words I new i wasn't that dumb data is a reserve word. My code worked Mikes code works and now it back to doing something useful. I'll post the whole thing when I get done it's a two wire LCD.
 
That is a pain for sure, the old MikroC allowed data as a variable name, but the new MikroC PRO won't allow data as a variable name.
 
Yep I got the idea i could use data as a variable from your site MrRB.I it worked before but I'm using the 4.80 beta. Funny how simple things seem to get me LOL

I slapped this baby in a nice do loop so it would row along and so far so good
Code:
#define  SClock GPIO.B1
unsigned char Sdata;
unsigned char i;
void main() {
OSCCON = 0x72;
TRISIO = 0b00000000;
CMCON0 = 7;   // no comparator
ANSEL = 0x00;

  do {
       Sdata =0b01010001;

        for(i=0;i<8;i++){           //for 0 to 7
        SClock=0;
        GPIO.B0 = Sdata&1;       //send out bit zero
        Sdata>>=1;               //shift data right 1 bit
        asm nop;
        SClock=1;         //doesn't this need to be 1 then 0
         }
    } while (1);

}

It shifts out nice
 
Yep I got the idea i could use data as a variable from your site MrRB.I it worked before but I'm using the 4.80 beta.
...

Whoops! Thanks for the heads up! I will edit that page and change the variable name "data" to "sdata" or something that will work with the new compiler.
 
Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…