Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronic 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.

Discuss about PIC Programing Languages

Status
Not open for further replies.

jonw0224

Full Member level 4
Joined
Nov 22, 2004
Messages
211
Helped
45
Reputation
90
Reaction score
14
Trophy points
1,298
Location
USA
Activity points
2,528
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

Advanced Member level 4
Joined
May 6, 2004
Messages
114
Helped
16
Reputation
32
Reaction score
4
Trophy points
1,298
Location
Egypt
Activity points
1,078
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
Joined
Nov 22, 2004
Messages
211
Helped
45
Reputation
90
Reaction score
14
Trophy points
1,298
Location
USA
Activity points
2,528
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
addwf num1, w
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

ADDFF macro regA, regB, dest
movf regA, w
addwf regB, dest
endm

MOVFF PORTA, num1
MOVFF PORTB, num2
ADDFF num1, num2, w
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.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Top