Using Assembly in C

Thread Starter

johnny_sjs

Joined Aug 3, 2010
10
Hello Friends,
I am Johnny,
I am using mplab, hi-tech PICC and pic16f877a microcontroller.

Suppose in my C Code I have a variable x, I assigned it some value say 5,
now I want to output this value on PORTB, for that I thought of 2 methods:

  1. C Code: code for dec to bcd
  2. ASM Code: assembly code and then use hex values and simply equate portb to x(I have some experience with 8051 asm programming).
I want to know which one is better and how I can use assembly in C (like format or something)
Thank You,
 

debjit625

Joined Apr 17, 2010
790
Here is a way to do that...
Rich (BB code):
void main()
{
  PORTB = 0x00; //Clear PORTB
  TRISB = 0x00; //Config PORTB as digital output
  int x = 5;
  PORTB = x;
}
Good Luck
 

Thread Starter

johnny_sjs

Joined Aug 3, 2010
10
#include <htc.h>

//device configuration bit
//__CONFIG(0xFFBA); //LVP=ON; RB3 can't be used as an I/O PIN
__CONFIG(0xFF3A); //LVP=OFF; RB3 can be used as an I/O PIN

//define
#define OUTPUT 0
#define INPUT 1


//main function
//================================================================
void main(void)
{
TRISB=OUTPUT;

while(1)
{
#asm
CLRF PORTB
#endasm

//short delay
for(unsigned int i=0;i<30000;i+=1);

#asm
MOVLW 0xff
MOVWF PORTB
#endasm

//short delay
for(unsigned int i=0;i<30000;i+=1);
}
}
compilation was successful, but while building its giving this error:
Error [800] C:\Users\Johnny\AppData\Local\Temp\s61k.; 286. undefined symbol "PORTB"

Please Help,
 

debjit625

Joined Apr 17, 2010
790
Your definition for INPUT is not proper.
Rich (BB code):
#define INPUT 1
Which means in binary "00000001" and that will not make all the pins of a port digital input.

If you are using in-line assembly in HI TECH C and want to access local variables and SFR ,just use an underscore before the name of the identifier,by this way the compiler will not conflict between assembly and C identifiers under the same source code.

Here is the way to do it...
Rich (BB code):
#include <htc.h>
//device configuration bit
//__CONFIG(0xFFBA); //LVP=ON; RB3 can't be used as an I/O PIN
__CONFIG(0xFF3A); //LVP=OFF; RB3 can be used as an I/O PIN
//define
#define OUTPUT 0x00
#define INPUT 0xFF 
 
//main function
//================================================== ==============
void main(void)
{
TRISB=OUTPUT;
while(1)
{
#asm
CLRF _PORTB
#endasm
//short delay
for(unsigned int i=0;i<30000;i+=1);
#asm
MOVLW 0xff
MOVWF _PORTB
#endasm
//short delay
for(unsigned int i=0;i<30000;i+=1);
}
}
Good Luck
 

Thread Starter

johnny_sjs

Joined Aug 3, 2010
10
thank you very much man!
it worked !
and for the INPUT one also.
I wonder how my other programs were working though!
Thanks again.
 

blah2222

Joined May 3, 2010
582
Cool, so if you want to add in-line Assembly code into your C18 code you just have to add the #asm and #endasm tags before and after, respectively? No other pragma or config controls?
 

debjit625

Joined Apr 17, 2010
790
Cool, so if you want to add in-line Assembly code into your C18 code you just have to add the #asm and #endasm tags before and after, respectively? No other pragma or config controls?
Their are two formats in HI TECH C to add in-line assembly in your C source code file.
First one already discussed above using directives "#asm and #endasm",anyway here is how again
Rich (BB code):
void main()
{
int i = 0;
//other C codes...
 
//Inline assembly starts
#asm
MOVLW 0x00
NOP
MOVLW 0xFF
#endasm
//end of inline assembly
 
//other C codes...
}
The above method can't be used inside any C constructs such as while,if,do,for etc.
To use inline assembly inside C constructs use this one...
Using statement "asm()"
Rich (BB code):
void main()
{
int i = 0;
//other C codes...
 
//Inline assembly statements
asm("MOVLW 0x00");
asm("NOP");
asm("MOVLW 0xFF");
 
//other C codes...
}
johnny_sjs said:
I wonder how my other programs were working though!
Actually if you look at the source code of your current program,then you will notice that it will work anyway, because you are not using the "INPUT" directive you are only using "OUTPUT" directives which you declared as 0 and in binary it is also zero i.e.. "00000000".

Good Luck
 
Top