PIC18 question....

Status
Not open for further replies.

Gibson486

Lifer
Aug 9, 2000
18,378
2
0
So, I am working on this project using a PIC18 chip. I am going through all these tutorials and I see that everything is coded in C. However, when you look at the tech sheet, they do not reference C, they just reference the assembly code. I am familiar with how C gets compiled into assembly which gets compiled into hex, but I am loosing anything by just coding it in C? I want to learn as much as possible on this project, but it all seems to easy. I mean, I have to implement USB communications on a board, but Microchip gives you a framework USB (and even I2C), so it's like they are doing the work for me and I am not doing much at all...
 

Modelworks

Lifer
Feb 22, 2007
16,240
7
76
I have worked with pic chips for years and while the tutorials might be coded in C there are plenty of pic users that rely on assembly because sometimes in the embedded field it is quicker to do things that way. You also sometimes will need to use assembly in the C program you are writing to do something the C compiler can't depending on which one you are using. If you want to learn as much as possible then don't use the frameworks microchip give you, write your own. The information needed is in the data sheets or the programming guides. Knowing assembly can help you a lot when something isn't performing correctly after compile, often it is an compiler error and these are pretty common. Or you will see from reading the compilers output that it isn't doing things the best way that they could be done.

You don't have to write the code in assembly but you should be able to read the compiler output and understand what it is doing to be able to diagnose problems. Things like using the debugger require some ASM knowledge to get a grasp of how the registers and memory locations are processing the C code you entered.

Sometimes assembly is a lot quicker. For example to open a USART port in C can take several lines of code, sometimes using macros. In assembly it is just two lines because I can write to the registers directly.
 
May 11, 2008
22,551
1,471
126
Perhaps these forums can help out with your USB frame work issue :

http://www.microchip.com/forums/Default.aspx?

http://forum.sparkfun.com/viewforum.php?f=4

You have to do a little searching and traversing through the threads, though.

I2C is easy to do. Also in assembly.

http://www.nxp.com/documents/user_manual/UM10204.pdf
I recommend to only create a single I2C bus master driver because that is very easy to do.
One master (your PIC18 controller)and multiple slaves (for example a MCP23008 and a PCF8591).

Microchip assembly is very easy to learn. But not so long it seemed it was difficult to find affordable compilers that can generate efficient assembly code for the pic. The microchip ide and simulator is free and works really well. And it allows third party c compiler plug in.
 
Last edited:

CycloWizard

Lifer
Sep 10, 2001
12,348
1
81
I think Modelworks hit this on the head. You can write everything directly in assembly if you want, but the whole point of these controllers is that you can write a higher-level language, then use their nice compiler to worry about the details. If you're just doing it to learn the details, there's nothing that says you have to use all of the available tools though. You're always free to write your own. In the end it depends on whether you're interested in an application or in the development itself. All of those I2C libraries are very handy for someone like me who just wants to use the controller quickly to perform a specific task.
 
May 11, 2008
22,551
1,471
126
OP, I just checked for you, microchip has an update of the mplab c compiler released.

http://www.microchip.com/stellent/idcplg?IdcService=SS_GET_PAGE&nodeId=1406&dDocName=en010014


If you want the free LITE version, this is your link.
Especially for academic purposes :

http://www.microchip.com/stellent/idcplg?IdcService=SS_GET_PAGE&nodeId=1406&dDocName=en536656
It is limited of course, but maybe very well sufficient enough for your needs. Usually it means you do not get the fully optimized libraries. But if you want to make your own library, you do not need to.

108138-1.png

Features
Key features of the MPLAB C Compiler for Academic Use (LITE) include:
ANSI-compliant
Free, fully functional, with all libraries and source code as commercial versions
Integrated with MPLAB IDE for easy-to-use project management and source-level debugging
Generates relocatable object modules for enhanced code reuse
Fully compatible with object modules generated with MPASM Assembler, allowing complete freedom to mix assembly and C in a single project
Interrupt code can be written in C or assembly
Flexible memory models take advantage of small memory applications and the storage of data in SFR space
Strong support for inline assembly when total control is absolutely necessary
Super-efficient code generator engine with multi-level optimization
Extensive library support, including standard C, math, DSP and peripheral libraries
Additional software libraries and application development tools are available from Microchip’s web site
GNU-based, open source code (except PIC18)


If you can do it in C, use the c compiler, code is a lot easier to read when you are on a time schedule compared to assembly (Unless somebody starts to write c code in the cryptic style). Assembly on the other hand can be more useful as well as modelworks pointed out.

But learn to understand and read assembly as well. Because sometimes the compiler creates code to the best ability and that is sometimes just not enough. Learn about :
  • The assembler.
  • The preprocessor.
  • The linker.
  • The compiler.
If you understand the functions of these programs, and you are able to use that knowledge, you can do tricks other less experienced people will claim to be impossible. The only limit is really lack of knowledge and lack of creativity here. All though sometimes, it is just impossible.

For those interested :
My colleague for example made me aware of zlib. A free software library for compression routines. And can also be adapted for embedded use...
http://zlib.net/
 
Last edited:

Gibson486

Lifer
Aug 9, 2000
18,378
2
0
Thanks....

So, I am starting to code in C first, then i am just going to see how it compiles to assembly while using the debug feature.

The guy I am working with bought me the PIC18 starter kit. It's alright, but I am just confused because it does not really say what is going on. I go into their demo files, but it doe snot really explain everything. Since the board is feature packed, there is alot of stuff I could not care less about, but I have to sit there and read the code anyways.

At the moment, I am just confused about the boot loader. How am I supposed to code it? Is it already done and I just code my embedded program or do I actually have to code the boot loader also?

The way the board does it out of the box is that it gets everything from a preloaded SD card. However, I will not have an SD card when I write the code for the real device. I am trying to do this ASAP because I am on the eval version of the compiler/MPLAB IDE, and once it expires, you lose the optimizations and the notes say that the programs that the compiler will make will be too big once the optimizations are gone.

What I plan on doing is using this board and having it grab info from the SD card and then displaying it on the PC. I am pretending that the SD card is going to be a dumb FPGA, so I am just preparing the SPI (will not be I2C) communications for it so I am prepared when we move this code to the real project.

thanks!
 

PsiStar

Golden Member
Dec 21, 2005
1,184
0
76
This is not really a contribution, but more of a question for my own edification. I have not programmed for several years, but I definitely enjoyed the object oriented structure of C++ versus plain old C.

My C++ experience was all about device drivers & embedded processors for machine control. Because of C++ my code was easier to understand than what I could write in C. I want to use the phrase "self documenting" but that is entirely dependent upon who else has to read the code ... as is the case of documentation in general.

In the day, Borland C++ was the best thing to use as it provided its optimized assembly for my review. The useful sections were delineated by the comments in the C++ so then I could modify the assembly if necessary to use the best of the cpu. In other words, instructions that the compiler did not know about and in sections that mattered. This saved a lot of over all development time. I miss those days!!!!:( Coding is so much fun especially when interfacing with the real world.

I should add that my attitude is that I never wanted to own forever what I wrote. I always approached my projects at designing myself out of the job and hoping that the next project would be available when I needed it.
 

Modelworks

Lifer
Feb 22, 2007
16,240
7
76
At the moment, I am just confused about the boot loader. How am I supposed to code it? Is it already done and I just code my embedded program or do I actually have to code the boot loader also?

That board uses the PIC18F46J50 so the very first thing I would do is get the datasheet for that chip off the MC website. The board you are using has the bootloader already installed but the chips themselves when purchased new do not have anything installed. Normal process is program the chip with the bootloader, you can use the MC one or create your own. Then once the loader is installed you can use it to upload your own code for the application you need. Look on the MC page for the chip and there should be app notes below the listing that detail things like the bootloader .

The way the board does it out of the box is that it gets everything from a preloaded SD card. However, I will not have an SD card when I write the code for the real device. I am trying to do this ASAP because I am on the eval version of the compiler/MPLAB IDE, and once it expires, you lose the optimizations and the notes say that the programs that the compiler will make will be too big once the optimizations are gone.

If your final project does not have an SD card then do not use one now because changing the code later from SD to some other spi device is fairly major. Do you have to use this specific pic chip or can you use another variant ? There are chips with more memory available.

What I plan on doing is using this board and having it grab info from the SD card and then displaying it on the PC. I am pretending that the SD card is going to be a dumb FPGA, so I am just preparing the SPI (will not be I2C) communications for it so I am prepared when we move this code to the real project.

Using an SD card now really isn't a good idea if the final project doesn't need it. Interfacing to the SD card relies on adding code to handle file systems and lots of SPI routines that interfacing straight SPI to another chip will not use.

The kit you have has the programmer via usb on board and that isn't a good way to do pic development. It is convenient but doesn't allow the full development options like your own custom bootloader. If you erase the bootloader on the board and your new bootloader fails then you have a bricked board . What you need is a pickit3 programmer, it interfaces directly with the chip and takes full control of it. You can set hardware breakpoints, see memory values in real time, watch registers, and flash bootloaders or code over and over no matter how corrupt the chip data may become. You don't even need the bootloader at all with the pickit. If you can get rid of the need for the bootloader that is more memory you have for your own programs.
 

Gibson486

Lifer
Aug 9, 2000
18,378
2
0
Thanks for the insight! I guess I will just use the board to learn my way around coding the chip and so forth. The one thing I hate about this board is that it has all this stuff I do not care about. I do not care about the touch pad and all that. I kind of wish we just got a chip and built around it. It seems like I am trying to learn to much at one time with this starter kit.
 

CycloWizard

Lifer
Sep 10, 2001
12,348
1
81
Thanks for the insight! I guess I will just use the board to learn my way around coding the chip and so forth. The one thing I hate about this board is that it has all this stuff I do not care about. I do not care about the touch pad and all that. I kind of wish we just got a chip and built around it. It seems like I am trying to learn to much at one time with this starter kit.
That's been my experience with MPUs in general - I never use more than 5% of the features of one for a given system. Sifting through to find the features you need usually takes some doing, but once you learn the basics of one system, you can easily use it for future projects as well. This is true for Arduino, TI's MSP series, PIC, and others I'm sure.
 
Status
Not open for further replies.