# Discuss about PIC Programing Languages

Status
Not open for further replies.

#### jonw0224

##### Full Member level 4
I program in Assembly. How do you guys use higher level languages for time critical applications since you don't know exactly how long the compiled code will take to execute? Here's a simple example:

using a PIC16F84 at 4 Mhz, you want to toggle RB0 every 10uS. In assembly, I may write:

Code:
bsf STATUS, RP0      ;bank 1
movlw B'11111110'
movwf TRISB           ;set RB0 as output
bcf STATUS, RP0      ;bank 0
LOOPFOREVER:
movlw 1
xorwf PORTB, f        ;toggle
goto $+ 1 goto$ + 1
goto \$ + 1
goto LOOPFOREVER ;delay, loop = 10 cycles

How would I write it in PICBasic Pro or C?

What would the size be in terms of general registers used and program words? The assembly code is 0 registers and 10 program words.

#### shafee001

you are right jonw0224 but in complecated applications, say mathematical calculations C is simpler than assemply ..
it is easy yo say
num1=porta;
num2=portb;
result=num1+num2;

shafee

#### jonw0224

##### Full Member level 4
shafee,

I agree with you. I think your example is trivial in assembly as well:

Code:
movf PORTA, w   ;w = PORTA
addwf PORTB, w           ;w = PORTA + PORTB
movwf result                ;result = PORTA + PORTB

Or, if you need to store num1 and num2 for later:

Code:
movf PORTA, w
movwf num1
movf PORTB, w
movwf num2
movwf result

The compiler makes the decision I imagine. The second is a bit harder and it gets harder with each level of complexity in the program. I've always personally been a proponent of MACROS. For example, you can simplify the second as:

Code:
MOVFF macro regA, regB
movf regA, w
movwf regB
endm

movf regA, w
endm

MOVFF PORTA, num1
MOVFF PORTB, num2
movwf result

The macros can be declared in an .inc file (once) and you'll only have to write the four lines. If you wrote more complicated macros you could shorten it to three. Really, I think the benefit to using upper level languages is that the writers of the compiler draw on a set of prewritten code segments to make connecting the dots easier. Libraries are the power of C, PICBasic Pro, etc. over assembly. Many tasks are prewritten, so people don't have to worry about how to write them or how they work. The best compiler will have, among other things, the most extensive library that's easy to understand and use and works across many microcontrollers.

This is only my opinion.

Status
Not open for further replies.

Replies
4
Views
1K
Replies
5
Views
3K
Replies
0
Views
3K
Replies
6
Views
2K
Replies
4
Views
3K
pauloynski
P