Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

[PIC] Assembly Code inside MicroC program

Status
Not open for further replies.

jerryd

Member level 2
Member level 2
Joined
Feb 4, 2013
Messages
48
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,286
Activity points
1,709
eadboard,

Windows, MPLAB X IDE v6.05, XC8 v2.41

I have tried many internet suggestions but haven't been successful at including some assembly language inside a MicroC program.

I have tried selecting the pic-as the compiler but no luck.

As a one time assembly coder I would like to learn how to do this.

Any suggestions of examples?
jerryd
 

MikroC or XC8? Both tools support embedding assembly code in C source.
The mikroC PRO for PIC allows embedding assembly in the source code by means of the asm declaration.
With XC8, you can write pure assembler code projects.
 

edaboard,
I have made some headway, thanks to hints here, but the following creates an "error: (800) undefined symbol" on line 11. Without line 11 it compiles and loads.

1 /* File: Asm.c */
2
3 #include <xc.h>
4 #include "config.h"
5
6 unsigned int x;
7
8 void main(void) {
9 x = 1;
10 asm("movlw 0xAA");
11 asm("movlw x");
12 return;
13 }

Also I would like to use:
asm {
....
....
}
instead of having to use asm("xxxx x"); on every line but XC8 doesn't like that.

jerryd
 

I don't know XC8 but I do know assembler, "movlw x" on line 11 is not a valid assembly instruction because "x" has not been defined. you should try asm("x EQU 1); on line 9
 

I suggest that you read the User Guide for the XC8 compiler
It shows that there that the 'C' variable name is pre-pended with an underscore character so you need to access the 'C' "x" variable as "_x" in the assembler.
There is a lot of other useful information in teh user gudie.
Susan
 

pjmelect,
Your suggestion that I try asm("x EQU 1"); worked. It seems that is a way to initiate a variable. I just don't know what kind of variable it is. This turns out to be the tip I needed to get going.

I'm sure I'll be back with more questions.

Thanks,
jerryd
 

edaboard,
I am able to include inline assembly code in my main.c file using asm("....."); but can't can't find how to use inline blocks of assembly code.

I've tried:

asm()
{
}

_asm
_endasm

__asm
__endasm

asm
{
}

#asm
#andasm

but all yield a syntax error?

Any suggestions?
jerryd
 

Again the User Guide shows how you can have a block of assembler code - put it in as a function in a file with the .s (or .S depending on how you want the file assembled) file type.
The user guide only shows one way to put in-line assembler code in a .c file and therefore trying other ways won't work (as you have found out).
Aa the song says "Just 'cos that's how you want it doesn't mean it will be so"
Susan
 

Aussie Susan,
Thanks for the tip about using _var. Of course it worked.

About the inline assembly. I created a .s file and added it to my project. I put an empty function in it and it assembles. I can add simple commands like movlw 1 to the function but if I try to address hardware it complains. I tried to #include my config.h file but the assembler didn't like that. Still it's progress.

I'm just beginning to find my way around the User Guide. I think I complied to section 4.12.1 in the User Guide accept I couldn't find an appropriate PSECT in section 4.15.1.

Here's the function:
#include <xc.inc>
GLOBAL _match

_match:
movlb 0
movlw 1;
//movwf PORTC
return

Next problem will be how to call it from the C file.

Thanks,
jerryd
 

edaboard,
I have my program working with the assembly code in this separate .s file.

#include <xc.inc>
GLOBAL _function
PSECT text0,class=CODE,reloc=2

_function:
movlw 0b00000011
BANKSEL (PORTC)
movwf PORTC
return

The only problem is when I run the assembler I get:
warning: (1522) RAM access bit operand not specified, assuming access-bank

Any suggestions?
jerryd
 

BANKSEL (PORTC) is not a valid assembly command, I don't know what you are trying to do (nor does the assembler)
 

Then look at the next line of the example where it uses the 'BANKMASK' instruction.
These only PIC MCUs had a banked memory arrangement and you needed to set the 'bank' selection bits for many of the variable memory locations and SFRs. (Some were mapped across all banks so you could access them from anywhere in the address space.)
I can't see anywhere where you have said which MCU you are using but the data sheet for it will show the memory layout and explain the BANKSEL operation.
By the way, I'm not sure why you want to use assembler. The XC8 compiler takes care of all of the issues you have had if you wrote in C. The only time I can see a value in using assembler is when you need to guarantee the order of instructions (i.e. in any 'unlock' sequence) or when you have already discovered that there is a performance issue with a specific section of your code.
Susan
 

Aussie Susan,
Thanks for your replies.

The MCU is a pic18f2550 because it has exactly the number of ports and interrupts I needed.

I have written this program in MicroC and MicroBasic while teaching my grandson about coding.

I started out writing pseudo assembly in Silicon Valley a long time ago and just wanted the exercise.

Do you have any idea about the warning I'm getting on the movwf PORTC instruction?

jerryd
 

Look at Section 5.3 of the datasheet for that MCU and you can see the memory bank system that it uses.
As for the warning, look at Section 26 for a summary of the instructions. In Table 26-2 you will see that the 'movwf' instruction takes 2 arguments, the 'f' register that is the destination, and the 'a' value hat is explained in Table 26-1.
Susan
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top