# Learning/Writing hex code?

#### ke5nnt

Joined Mar 1, 2009
384
I did a search and didn't find much (forum search) so sorry if this has been covered, but...

I've recently become interested in microcontrollers, working on a project now that uses a 16F628A to alternately flash groups of LEDs, and I was wondering, I'd like to learn how to add to or alter the HEX code to change the flash pattern, or better yet, make it so you can push a momentary switch and change between multiple flash patterns. Is this possible? Where's a good place to learn how to do it?

Thank you

#### bertus

Joined Apr 5, 2008
20,657
Hello,

Changing HEX code is difficult.
You have to "translate" what is meant in the HEX code to see what happens.
You have to find out if the HEX part is an OP-code or a variable etc.

Programming microcontrolers like the PIC's can be done in Basic C or assembly.
Here is a page with a C course for pics (and more).
http://www.best-microcontroller-projects.com/index.html

And here one for basic programming of the pic's
http://www.picbasic.nl/indexes_uk.htm

Assembly programming can be done with the MPlab program of microchip itself.

Greetings,
Bertus

#### mik3

Joined Feb 4, 2008
4,843
You have to be very evil to be able to program in HEX

It is really hard and you just make your life very hard.
Use assembly or a high level language to make things simpler.

#### beenthere

Joined Apr 20, 2004
15,819
So I'm only half evil because I used to program in octal?

#### SgtWookie

Joined Jul 17, 2007
22,210
So I'm only half evil because I used to program in octal?
Dec PDP-7/PDP-11?

OP,
If all you have is a HEX file of the original program but you don't want to completely start over, you may be able to find a PIC disassembler for your particular model of uC. Since various PIC uC's have very different architecture, there is no "one" generic PIC disassembler that I'm aware of.

Basically, a disassembler looks at the hex code, and turns it back into assembly language coding. That's only the first step of the battle though, as branch points will have relatively meaningless labels, and it's quite possible for data to be incorrectly displayed (or displayed in a meaningless format), and there will be few (if any) comments in the resulting assembly language text. Disassembly is only the first step of the game; a great deal of analysis follows.

#### ke5nnt

Joined Mar 1, 2009
384
...never said no to a challenge before

#### thatoneguy

Joined Feb 19, 2009
6,359
So I'm only half evil because I used to program in octal?

Reminds me of "Booting" mini computers back then.

For the author of the thread, you may find Assembly easier than Hex, unless your device is a One Time Programmable.. PROMS come to mind, where you can only 'add' to what is burnt by burning more with a high voltage programmer. It's dicey and not very productive compared to simply using a Flash chip. But if you get 1,000 free chips, it's fun to see what you can end up with.

Begin (my personal) Synopsis on Microcontroller Programming for Under $200, closer to$100 or less if you don't want a fancy protoboard.

From here, uC and "Microcontroller" are synonymous in this post.First off: Below, I am attempting to list product options that will get a person going with High Level uC programming for under $100, with a$250 limit, including a $35 PicKit 2 programmer, purchased separately. There may be other compilers out there, but I've not heard of them or tried them. If you simply want to write a program to download it (Any PIC "F" Device, such as 16F877, 16F84). Microchip's assembler and development environment is free from http://www.microchip.com. It will also come with the$35 PicKit2 Programmer. Some sort of programmer is needed, and the price/features on this one are good.

Programming in C or Basic gets you a finished product far faster than assembly in most cases! I was reluctant at first, due to the $400 cash layout for a solid development system (last century), but I have zero regrets! With one chip (often 8 or 18 pin), I have been able to create an application that would have taken several dual 555's, comparators, or, half a bag of discrete logic chips plus a clock! Development Time shrink is nearly an order of magnitude! That is, unless you have a few assistants to design/etch/test each unit, and then glue it together on a single board for your final development. With a uC, components often consisto of 1 IC, power, ground, maybe some output boosting transistors or logic level MOSFETS and a button or two. For LEDs, that is all you need! Also under 10% the cost of a "Basic Stamp ][", and runs much faster than the BS ][ (see below, BS ][ isn't ALL Bad!). Why Buy the$35 PicKit2 when clones can be found for less, Isn't this post about cheap?

Three reasons, all important:

1. Keep Microchip fully aware that hobbyists are still inbound, we don't want everything in Surface Mount, or 100 Quantities because only a couple thousand developers are out there, and haven't gotten the latest programmer anyway.
2. There are cheaper ways, but, I believe most of us like to do things right, rather than wing it to save $10, which you end up spending a few times and getting what was originally wanted. 3. Support and Warranty. I've seen MANY blown USB, Serial, and Parallel ports from homemade or 'no name' factory units. Granted, the huge majority is on the homebuilt side. Microchip is excellent with their warranty, and I've never heard of a programmer killing a port. Sometimes the programmer itself gets toasted from jumping the "wrong programming pins", but that is rare, and just as big of danger for homebrew. Languages I use Boost C from http://www.sourceboost.com They also offer a Basic compiler which they compare to M$ Visual Basic in usage. There are a few other C/BASIC/Pascal compiler/IDE environments out there, ranging from free to thousands of dollars. SourceBoost has multiple level licensing depending on allowed code/memory size, from $4.99 to$150. They also include Novo RTOS, a Semaphore based realtime operating system (ala Win95). Electronika C http://www.mikroe.com/en/compilers is another good C Compiler, free to use with memory limits, $249 to buy , same for their basic. Some vendors offer a BASIC compiler, implemented exactly like a BS ][ stamp as well, no compatibility issues, for prices ranging from$89 (single target IC model) to beyond this article's "budget".

How to get started/understand the syntax?

Every compiler I've tried comes with example source code for a variety of projects, usually more than enough to teach you the quirks of that compiler!

Portability (or lack of)

Many C implementations are NOT portable between compilers! A Program written in Hi-Tech C for a 16F877 will build/run on a 18F2520 under Hi-Tech C, but won't even compile under BoostC or MikroC without tweaks/lots of function renaming, etc. Many of the same syntax/library issues variants of compiled BASIC as well.

uC + Compiler vs. Basic Stamp

A low to mid grade Basic compiler, a PIC Proto board and programmer will cost about the same as 2 Basic Stamp ]['s. But, once your PICs are programmed developed, then out in circuits blinking their lights/running motors/etc, the price per PIC drops like a rock (most are under $5 each, closer to$2-$3). Whereas with Basic Stamps, you need to purchase a whole new Stamp. Basic Stamps are GREAT for interactivity, introduction, and slow scale debugging/checking of sensors/servos/etc. A serial terminal is all that is needed. Don't get me wrong on my view of them, but when the programming cycle will be a few hours, then soldered to the application, never to be tweaked again, the Super-RAD environment of a BS ][ is not the item to look toward. "Crippled" C Compilers??? Not Neccessarily! As far as the device code/data memory limits on C compilers: Most limits are fairly high, and would allow quite a bit of LED sequencing / flashing / etc to be done with the 'free' or 'student' license. Try all of them to see which helps you get what you want done faster. The only one I actually ran into a memory limit on while doing a simple program was BoostC. The$25 registration fixed that, and I later upgraded to their higher license. These limits are to allow hobbyists to do most everything they want with a uC, except sell their product. When you are marketing an item, purchasing the compiler at the commercial price is the least you can do, after all, if it weren't for them, you wouldn't have a product. them to raise prices if the company intends to improve their product.

There is no "best around"!

Remember, because I have had good success with BoostC, doesn't mean it is the best one around for your style. So make sure and look around, and also pay attention to available code on the net, which makes development even faster, pay attention to the compilers used.

Atmel Microcontrollers

These have always had a deep call stack, which is needed for easy C Compiling. All of the development tools, and even programmer schematics are available for free from various websites. Unit cost is a bit higher, but both PIC and Atmel have enough strengths and weaknesses to co-exist without any "Holy Wars" of apocalyptic proportions (yet). I've worked with an ATMega 48s, and was impressed, but already have too much PIC related stuff to keep both in my head.

GNU Compilers for PIC and Atmel

sdcc (Small Device C Compiler) works with both PIC and it's library is growing rapidly. The downside is that it will only work with the 18Fxxxx series PIC Controllers, as were designed to be "C Friendly" with two pointer registers and a deep call stack. the 12F and 16F families do not have those features. Atmel, as stated above, has, so most all Atmel uC's work with GNU compiler tools.

#### mik3

Joined Feb 4, 2008
4,843
So I'm only half evil because I used to program in octal?
Lol.

Really?

How do you find programming at this level (HEX, OCTAL, BINARY)?

#### ke5nnt

Joined Mar 1, 2009
384
thatoneguy,

Thanks for the lengthy and detailed reply. I'll look into all that. Lots to digest.

#### beenthere

Joined Apr 20, 2004
15,819
To mik3 - The computers I first worked on had no compiler or assembler. The only way to program was to set bits in the A register and save each instruction into core memory. The old Univacs were organized as octal machines.

#### mik3

Joined Feb 4, 2008
4,843
To mik3 - The computers I first worked on had no compiler or assembler. The only way to program was to set bits in the A register and save each instruction into core memory. The old Univacs were organized as octal machines.
Yes, that's true because an assembler is a program which converts the assembl language into binary code. At the beginning someone had to make the assembler code from binary numbers.

#### thatoneguy

Joined Feb 19, 2009
6,359
Yes, that's true because an assembler is a program which converts the assembl language into binary code. At the beginning someone had to make the assembler code from binary numbers.
It came in a book. Seriously.

You looked up the opcode, then the operand, then entered the instruction.

It was a load of fun.

#### beenthere

Joined Apr 20, 2004
15,819
Even worse with Univac 1206's - opcode, J, K, and conditional, then 15 bit operand. But only 64 (80 octal) instructions.