Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Time for MICRO-CHANGE!

Status
Not open for further replies.

Peter_wadley

New Member
Hey,

I would really like to move up in the micro department...

I'd like to start using a higher level language now that I feel comfortable with assembly.

Assembly is great; however, I feel it to be too tedious for larger projects.

I would also like to start using a more recent line of PICs...

So,

1) Can you recommend which PIC 18F chips I should try?

2) Can you recommend a higher level language for these new chips? (and provide EZ beginner tutorials)

Thanks and CHEERS
 
Hi Peter,

I'd suggest the 18F2550 USB capable PIC... with a bootloader running you can download code via a USB cable to the PC.

MPLAB's C18 compiler is well and truly good enough for learning purposes.

I'd also suggest looking at the UBW here: **broken link removed** as it has both a bootloader and firmware for experimenting that you can use as a skeleton for your own application.

Sparkfun: www.sparkfun.com also sell the kit and pre-built forms of the UBW.

P.
 
Peter, since you're in Toronto you might want to drop by Creatron and pick up one of my Junebug kits :) shameless plug.
Its got a USB programmer / debugger and a 18F1320 for learning (lights buttons etc...)
 
blueroomelectronics said:
Peter, since you're in Toronto you might want to drop by Creatron and pick up one of my Junebug kits :) shameless plug.
Its got a USB programmer / debugger and a 18F1320 for learning (lights buttons etc...)

Nice site and store blueroom! I will definately swing by sometime! (In Waterloo right now)

After browsing about, I think I will stay with the 16F's for a little while longer... for my purposes they are fine!

However, I cannot believe how great C programming is!!!!

I tried out Proton Lite *TY Gramo*... and was amazed by the simplicity! (although it is more basic than C)

Since I am a student, i am naturally broke... so buying a $100 dollar copy isnt going to happen...

I then downloaded Hi-Tide and MikroC...

I like both, but have choosen MikroC... its great!

Although I am proficent in java and C# the syntax in C is tricky to adapt to!

Please share some opinions if you'd like
 
The Microchip C compiler works with the 18F series PICs. The free version has some code optimizations missing which won't affect the hobbyist much.
 
Good programming is more about style and technique then the language you choose to use.

Several books have been written on writing good code. Arguably the most important thing to keep in mind is readability.

It used to be "the thing" to write C code that was as compact as possible. Anything that made the code shorter was used.

Although terse coding can be fun it should not be used on a regular basis. When it is the code needs to well documented. If straightforward C is not good enough it is far better to use a few lines of well documented in-line assembler.

Avoid attempts to optimize your C code when doing so will make it difficult to understand.

When you are learning the language or new to a compiler leave the optimization to the compiler. What seems more optimized in C source may not result in shorter compiled code.
 
I see,

I guess I will just learn the basics with MikroC for now...

I will begin using C18 with the 18F's when I get some...

Can you guys give a list of good begineer 18F chips?

What would you consider the 877a or 18F? or the 628a?


Also... I was just wondering what happens when you make variables in C?

... is an INT just 4 seperate bytes


... a decimal 16 bytes?


Also.. can you point me to a good place to find the correct syntax for C?

In C#... for loops are created as follows...

for (int i = 0; i < 100; i++)
{
\\do something
}

This doesnt work in C...

What about IF statements? Select Cases? These are all things I cant seem to find on Google.
 
Peter_wadley said:
I guess I will just learn the basics with MikroC for now...
Check the manual of the compiler you're going to use. The **broken link removed** is compiler specific. For example, all the following instructions set RB0:
Code:
PORTB.f0 = 1;           // mikroC
RB0 = 1;                // Hi-Tech PIC C
PORTBbits.RB0 = 1;      // C18
Peter_wadley said:
In C#... for loops are created as follows...
for (int i = 0; i < 100; i++)
{
\\do something
}

This doesnt work in C...
This syntax will work with PICs too.
 
Last edited:
3v0 said:
Good programming is more about style and technique then the language you choose to use.

Several books have been written on writing good code. Arguably the most important thing to keep in mind is readability.

Agree totally here. The *main* advantage of C over PIC assembler is in productivity... you simply cannot write as much code as quickly in assembler.

You can, however, write equally bad code in both :)

P.
 
Ok,

The help file for mikroC has everything I was looking for.

Yes, it seems as though the machine code produced by mikroC is VERY wasteful in terms of memory!!

Just to make an lcd output a couple sentences ate all my 628As ROM!... or RAM...

Can you guys explain the difference between ROM and RAM for micros??

Isn't ROM for storing the actual code that is written? and RAM is...?

Also, why are the 18Fs better suited for C? I have read a bit on the RISC (Reduced Instruction Set Computer) design.
 
Peter_wadley said:
Ok,

The help file for mikroC has everything I was looking for.

Yes, it seems as though the machine code produced by mikroC is VERY wasteful in terms of memory!!

Just to make an lcd output a couple sentences ate all my 628As ROM!... or RAM...

Depends what libraries it loads?, but it will always be pretty wasteful, particularly for a small program. You also should avoid floating point, this requires a large library to be loaded, and is slow and inaccurate (even on a PC) - and is only rarely required.

Can you guys explain the difference between ROM and RAM for micros??

Isn't ROM for storing the actual code that is written? and RAM is...?

Neither term really applies to a PIC, it's been misused, but 'ROM' would be the program memory, and 'RAM' the GPR's.

Also, why are the 18Fs better suited for C? I have read a bit on the RISC (Reduced Instruction Set Computer) design.

Because the increased instruction set is designed to be 'friendly' to a C compiler, so it produces much more efficient code - and as the 18F has a free fully featured C compiler it makes sense to use the 18F series.
 
In general the 18F series has a lot more RAM and ROM too... and as Nigel says, the instruction set is better suited to C than the 16 series.

Finally, the 18F costs just $US3-7 in one-off quantities... seems ridiculously cheap for such ease of use.

P.
 
Peter_wadley said:
Ok,

The help file for mikroC has everything I was looking for.

Yes, it seems as though the machine code produced by mikroC is VERY wasteful in terms of memory!!

Just to make an lcd output a couple sentences ate all my 628As ROM!... or RAM...

I have solved that issue if you want to connect your LCD in 4-bit mode using mikroC, mikroPascal, and mikroBasic. The following program creates an optimized module to insert into your program that has a very small ROM footprint, and leaves plenty of room on your 16F628A:

**broken link removed**
 
Last edited:
Peter_wadley said:
Also, why are the 18Fs better suited for C? I have read a bit on the RISC (Reduced Instruction Set Computer) design.
They're better suited for any language, including assembler.

Besides more RAM/FLASH and a more comprehensive and cohesive instruction set there's direct access to the top-of-stack as well as stack push and pop operations. Very exciting empowering stuff;

Code:
;
;  jump table example (completely 256 byte boundary tolerant)
;
        rcall   Jump            ; WREG = index 0..3
table   goto    Func_0          ; index 0 function
        goto    Func_1          ; index 1 function
        goto    Func_2          ; index 2 function
        goto    Func_3          ; index 3 function
Code:
;
;  Jump function
;
Jump    rlncf   WREG,W          ; WREG *= 2
        addwf   TOSL,F          ; add to return address Lo
        movlw   0               ;
        addwfc  TOSH,F          ; propogate Carry
        addwfc  TOSU,F          ; propogate Carry
        return                  ; return to table + WREG*2
My favorite PutString or PutLCD function;
Code:
;
;  PutString macro usage
;
        PutStr  "Az-El Rotor Maintenance/n/n/r"
        PutStr  "1 - Calibrate Az rotor/n/r"
        PutStr  "2 - Calibrate El rotor/n/r"
        PutStr  "3 - Return to Main Menu/n/r"
;
Code:
;
;  the PutString macro
;
PutStr  macro   pStr
        rcall   PutString       ;
        db      pStr,0          ; null terminated inline string table
        endm
;
Code:
;******************************************************************
;
;  PutString - print in-line string via Stack and TBLPTR
;
;  string must be terminated with a 0 byte and does not need
;  to be word aligned
;
PutString
        movff   TOSL,TBLPTRL    ; copy return address into TBLPTR
        movff   TOSH,TBLPTRH    ;
        clrf    TBLPTRU         ; assume PIC with < 64-KB
PutNext
        tblrd   *+              ; get in-line string character
        movf    TABLAT,W        ; last character (00)?
        bz      PutExit         ; yes, exit, else
        rcall   Put232          ; print character
        bra     PutNext         ; and do another
PutExit
        btfsc   TBLPTRL,0       ; odd address? no, skip, else
        tblrd   *+              ; make it even (fix Program Counter)
        movf    TBLPTRH,W       ; setup new return address
        movwf   TOSH            ; 
        movf    TBLPTRL,W       ;
        movwf   TOSL            ;
        return                  ; to 1st instruction following string table
 
Mike said:
They're better suited for any language, including assembler.

Thats great I was wondering what language to use when I using 18F & DS pics.Its better I can stay with assembler for another 15 years.I didn't have a problem with assembler until now also easy to understand.

I like assembler very much.
 
Peter,

I hope you don't give up on Assembler completely.

While it's hard to beat the built-in high level language library functions, creating your own Assembler functions isn't difficult. And macros can often be used effectively to create very readable and intuitive code.

Take Warren's (wschroeder's) excellent BASIC LCD functions for example. I suspect they're similar to what I use in Assembler;

Code:
        radix   dec
;******************************************************************
;
;  Host program example macro usage
;
Line1   equ     128             ;
Line2   equ     128+64          ;
Line3   equ     128+20          ;
Line4   equ     128+64+20       ;
;
        PutLCD  cmd,0x0C        ; display on, cursor off
        PutLCD  cmd,Line1+10    ; lcd line 1, htab 10
        PutLCD  str,"GMT"       ; print "GMT" string
        PutLCD  cmd,Line2+10    ; lcd line 2, htab 10
        PutLCD  dat,'?'         ; print "?" character
        PutLCD  cmd,0x10        ; cursor shift left
        PutLCD  cmd,0x0F        ; cursor on (blink)
Code:
;--< macro definitions >-------------------------------------------

cmd     equ     1               ; single byte command/control type
dat     equ     2               ; single byte character/data type
dec     equ     3               ; single byte variable 0..9
str     equ     4               ; multi-byte string character type

PutLCD  macro   ptype,pdata
    if ptype == cmd             ; creates 2 instructions
        movlw   pdata           ;
        call    PutCmd          ; low level driver, RS=0
    endif
    if ptype == dat             ; creates 2 instructions
        movlw   pdata           ;
        call    PutDat          ; low level driver, RS=1
    endif
    if ptype == dec             ; creates 3 instructions
        movf    pdata,W         ; Wreg = variable
        iorlw   '0'             ; 0..9 -> ASCII "0..9"
        call    PutDat          ; low level driver, RS=1
    endif
    if ptype == str             ; creates 1 instruction + inline table
        call    PutString       ; (18F version)
        db      pdata,0         ; null terminated inline table
    endif
        endm
 
Last edited:
I understand why those who are already familiar with PIC assembler might want to continue using it, especially if they are not familiar with C. I can also understand that you may be used to trying to squeeze every ounce of performance, or space in RAM/ROM out of a PIC if you've been using the 16 series.

The 18 series are *fast*... much faster than the 16F84 or 628a's most of the current crop of PIC affectionados have been using. The flash ROM has also expanded significantly giving you more room to move. The prices seem so reasonable that it's hard to imagine wanting to continue to use the 16 series except in the most price-sensitive applications.

For me, as an engineer wanting to design his first PIC-based product, using the 18 series and C meant that my productivity was *very* quickly to the point where I was writing useful code, without needing to learn PIC assembler which is quite frankly baroque.

C is C is C... the wrinkles involved in 8-bit vs 16 or 32-bit *are* part of C, so moving from a 6809 to a 68000 to a PIC18, PIC24 etc. is straightforward. In fact moving to the new PIC32 series is *much* easier if you know C... the assembler is so totally different that most PIC assembler fans are going to find the learning curve very steep and therefore simply decide to ignore them.

I have found learning the PIC architecture and I/O capabilities to be a great deal of fun :) Using C I've got a lot further in my design in the time I've spent than I would have had I learned the assembler (and I'm a *very* good assembler programmer.)

So... *please* give some thought to learning C. You're a computer scientist, programmer or engineer, and learning *new* languages, concepts, etc. is part of your vocation or hobby as the case may be.

</rant off>
P.

PS: I re-wrote all of the library routines I used in my prototype... they are fine for a first stab as and as a basis for learning, but near useless for serious applications.
 
Last edited:
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top