mikro C if command..teach me pleas...

Status
Not open for further replies.

lockman_akim

Advanced Member level 1
Joined
Jul 12, 2010
Messages
466
Helped
76
Reputation
152
Reaction score
68
Trophy points
1,308
Location
Malaysia
Activity points
3,523
in BASIC if i do

if portb.0 = 0 then
portd.0 = 1
else
portd.0 = 0
end if

so when push switch, led is on
but when try using c,

push switch = off led

im just 2 hours ago learning c..:-D

Code:
void main() {

TRISB = 0xFF;
TRISD = 0x00;
PORTD = 0x00;
 do{
if (PORTB.F0 = 0)
PORTD.F0 = 1;
else
{PORTD.F0 = 0;}
}while (1);
}
 

in c you need == for a comparison... so.. if (PORTB.F0 == 0) { ... etc
a single = means youre trying to assign a value.. such as x = 5;

---------- Post added at 21:12 ---------- Previous post was at 20:29 ----------

and it is true what the other poster had said, some pins might have dual functions, for example if you want to use an input on PORTA it is very likely that the PIN defaults to analogue, so before using it you have to set or clear some register, can't remember, but this makes it a digital input.... you should be ok on PORTB... but if you still can't get it to work look up the datasheet, as most pins do have two or more functions and it might be defaulting to something not usefull
 
Agreed with coffeeCupPepsi, in BASIC the = symbol has two meanings, it can either check if two things are the same or it can assign a value to something. C distinguishes between the two uses, a single = is for assignment, for example a = 10 assigns the value 10 to a just as in BASIC but in a comparing situation use == instead. For example a == 10 asks if 'a' has the value 10. It is a more logical way of doing things and you soon get used to it.

Brian.
 

ok tanx a lot for the xplaination..how can i do the subroutine?
 

yes it work brian..tanx..
and i need to know now is how im going to make subroutine..
(i mean next lesson)
 

for a subroutine
you need to determine if your rutine will return a value or not..also determine if you want to pass parameters to it or not.. lets say you want a rutine to add two numbers

int add(int a, int b) {
return (a + b);
}

you need to declare it above main, that means, functions need to be declared just as variables, but if you create your function above main (or really above the function that will call your new function) then the function is its own declaration...
 
The syntax of what coffeecuppepsi wrote is:

retun-type function name(parameters)
{
operations to perform;
}

In the above example,int is the return type which means that the result of addition is an integer. add is the function name & int a,int b are the two parameters passed to the function. return(a+b); returns the result of a+b which will of integer type value.

You are also advised to declare the function prototype above the main function of the code.
The function prototype will be as follows:
int sum(int a,int b); // return-type function name(parameters);

The above written example is the function definition for the function sum.

Regards,
Jerin. ;-)
 
Hi,
Talking about subroutines, you can break this down into 2: procedures and functions. When you call a procedure, it does some work and then goes back to right after where you had called it. When you call a function, it does some work and returns some value in some data type.
Eg.
Code:
 void turnOnB3(void){
   PORTB.B3 = 1;
}
int sumF(unsigned char a, unsigned char b){
   return (a+b);
}
// Main code here
void main(void){
//Initialization code here
   turnOnB3(); //This does the work of turning on RB3 but doesn not return anything
   x = 5;
   y = 9;
   c = sumF(x,y); //This now calls the function that does the work of adding x and y and then returns the sum and is assigned to variable c
}

void means no data type
void turnOnB3(void) means no return type, nothing to be assigned.
int sumF(unsigned char a, unsigned char b) means a and b are of unsigned char type and the sum that is the return value is of int type

Hope this helps.
Tahmid.
 
Hi,
A short tutorial:
Function Declaration:
Code:
[i]type function_name(parameter-declarator-list);[/i]
The function_name must be a valid identifier. This name is used to call the function.
The type represents the type of function result, and can be any standard or user-defined type. For functions that do not return value, you should use void type. The type can be omitted in global function declarations, and function will assume int type by default.

Function type can also be a pointer. For example, float* means that the function result is a pointer to float. Generic pointer, void* is also allowed.

Function cannot return an array or another function.

Within parentheses, parameter-declarator-list is a list of formal arguments that function takes. These declarators specify the type of each function parameter. The compiler uses this information to check function calls for validity. If the list is empty, function does not take any arguments. Also, if the list is void, function also does not take any arguments; note that this is the only case when void can be used as an argument’s type.

Unlike with variable declaration, each argument in the list needs its own type specifier and a possible qualifier const or volatile.

A function is called with actual arguments placed in the same sequence as their matching formal parameters. Use the function-call operator ():
Code:
[i]function_name(expression_1, ... , expression_n)[/i]

Hope this helps.
Tahmid.
 

dear all friend..tanx a lot for helping in learning C..
i think i can start exploring it by myself now..
but i have last question,

how long can i use mikro c without register it?
hehehe....
 

I think may be 30 days or so. Try to get the full version of it from net...
Regards,
Jerin. ;-)
 

Last time I installed microC (a few years ago...), usage time was unlimited, just code size was limited (4Kbytes object output, if I remember correctly).
I don't know what is their policy today...
 

Hi,
Usage time is unlimited. Code size limited to 2K. Unless you use GLCD, or some huge project or very inefficient coding, you can quite easily use yours.

Hope this helps.
Tahmid.
 

U can try HIGH TECH C compiler also.... with MPLAB

---------- Post added at 11:44 ---------- Previous post was at 11:39 ----------

Why i cant use
for(int i=0;i<100;i++){} in mikroC ?
it is showing some error ...
but the below is working

int i;i=0;
for(i=0;i<100;i++){}
!!!
 

Hi,
In mikroC, when you use the for loop, the data type of the variable being used, in your case, i, has to be declared earlier as either a global or local variable to the main function.

Hope this helps.
Tahmid.
 

For a C program always the variable declaration should be done after the function declaration if the variable is to be used all along the program (i.e as global variable).Else you can declare the variables in a function if the scope of the variable is within that function only (i.e as local variable). Try this code:

int i;
for(i=0;i<1000;i++)
{
//code to be performed
}

If the variable is to be used in a function,

void sum() //sum is the function name & void is the return type.
{
int i;
for(i=0;i<1000;i++)
{
// code to be written
}
}

Hope you understood. If more doubts persists post it here & we'll surely help you out.
Regards,
Jerin. ;-)
 

i want to program in assembler in mikroC, but how can i refer to registers by address? this is not so much a problem for special registers, i can just say clrf portb; but this is a problem for general registers with no name how do i define them or how can i refer to a register by address?? this does not work clrf 0x21;
 


To my knowledge, MikroC is a C compiler used for programming controllers. I don;t know if it can be used for coding in asm too. SFRs can be called by their names itself. There is no need in calling the general purpose registers of the controller as the program is loaded to these general purpose location while programming.
Regards,
Jerin.
 

In a Harvard architecture, the program and register space is in different memory maps, the program is NOT loaded into general purpose locations !

Quote from the MikroC help file:
The mikroC PRO for PIC allows embedding assembly in the source code by means of the asm declaration. The declarations _asm and __asm are also allowed in the mikroC PRO for PIC and have the same meaning.

Brian.
 

Status
Not open for further replies.

Similar threads

Cookies are required to use this site. You must accept them to continue using the site. Learn more…