Hex code intended for atmega168, can it be burned on atmega328?

Go To Last Post
17 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I am new to this forum and have no experience with AVR chips other than I have done some Arduino programming.  I was given some hex code for an electronic project.  No source code is available because the author is deceased.  Electronic project and the code, I am told, worked very well.  The code was written for an atmega168 using the Bascom basic compiler.  The fuse settings are factory default ie. burning hex code makes no changes to the fuse settings, and it uses the internal clock osc.  I am planning on using the arduino as an ISP to burn atmega168 using AVRDUDE.

 

My question is . . . is it possible to  burn an alternate device the ATMEGA328 instead, using the same HEX code??  The atmega328 chips are more readily available and as I understand, has double the flash and ram size.

 

As I have stated, I new to this forum and AVR, so don't beat me up, if this seems to be a dumb question.

 

Your advice is appreciated.

 

Thanks

 

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

How do the two AVRs' data sheets compare in terms of register addresses?

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Welcome to the Forum.

 

OK, interesting question.

 

My initial answer was going to be no, it won't work.

 

BUT, it appears that both the Registers are the same, and the Interrupt Vector Table is the same, (168 to 328).

Additionally, it doesn't sound like the program will be loading a bootloader section, (which would fail, different addresses).

 

The DIP pinouts are the same.

 

So, will the compiler just load the program into the larger memory space, oblivious to the fact that it isn't using 1/2 the chip?

 

So, it MIGHT work.

 

Others might chime in: "No way, you forgot ...".

We'll see.

 

Although I appreciate Steve's approach of leading the OP to find their own solution, I'm not sure this is an "easy" question for a beginner to AVR's to sort out.

 

If you have a socketed PCB go ahead and give it a try.

You won't hurt the 328 chip.

 

JC

 

Last Edited: Mon. Nov 17, 2014 - 12:02 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

The extra flash and RAM don't have any value without the source, but the register addresses are the same.  (Both devices are on the same data sheet.)

 

In consideration of others, please RTFM!

Last Edited: Mon. Nov 17, 2014 - 12:04 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I'm going to go ahead and say yes, it will work. 

 

Direct hex code transference between AVR devices fails when:

-- The original code is too big for the new device.  >  Not when a 16K program goes into a 32K device.  Same with SRAM and EEPROM.

-- The peripheral I/O registers are in different locations on the new device.  > Mega168 and m328 have the same I/O structures.  Mega8 to Mega88

    would have a problem because Mega8 uses all IN and OUT instructions for peripherals while Mega88 uses LDS/STS for some and In/OUT for others.

-- The IRQ vector tables are different.  > M328 and m168 have the same vector tables.

-- The IRQ vectors are different sizes.  > M328 and m168 both use 16-bit vectors.  A program for Mega88 going to M328 would have this problem.

-- Original device uses instructions not found on new AVR.  >M328 and M168 have same instruction set.   A M168 program going into a Tiny1634 might fail if the M168 program uses any of MUL instructions that are not found on the Tiny1634.

-- The original code is for a bootloader that is installed at the end of the Flash space.   Look at the hex file with a text editor and a hex-file guide from the web.  If it starts at flash addr:0000, then it's not a bootloader.

-- Original code references PCINTs not found on new device, or GPIORs.  Going from a Mega88 to a Mega8 could have this problem.

-- Original code references input or output ports or timers not found on new device.

 

I'm missing something.  I always do.  Please write a message with what I'm overlooking so that future readers will have a complete list of device transfer issues.

 

Last Edited: Mon. Nov 17, 2014 - 12:46 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I'm going to go ahead and say yes, it will work. 

+1

 

the 328 simply has "more of stuff" than the 168. But what's common is in the same place. So code built for 168 will run in a 328 but the converse is not true (because the 328 code will be setting the stack pointer to a place where the 168 doesn't have RAM).

 

While 48/88 only have RJMP/RCALL but no real JMP/CALL I think it's the case that 48/88 code would also trade up to a 168 or 328 OK too. (but again, not backwards, this time for more than just RAM, like those missing opcodes).

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

clawson wrote:
While 48/88 only have RJMP/RCALL but no real JMP/CALL I think it's the case that 48/88 code would also trade up to a 168 or 328 OK too. (but again, not backwards, this time for more than just RAM, like those missing opcodes).

88->168 won't work because the vector table entries have different sizes and are therefore in different places.

Iluvatar is the better part of Valar.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

skeeve wrote:
88->168 won't work because the vector table entries have different sizes and are therefore in different places.
And RJMP/RCALL won't wrap at the 8 KB boundary.

"Experience is what enables you to recognise a mistake the second time you make it."

"Good judgement comes from experience.  Experience comes from bad judgement."

"Wisdom is always wont to arrive late, and to be a little approximate on first possession."

"When you hear hoofbeats, think horses, not unicorns."

"Fast.  Cheap.  Good.  Pick two."

"We see a lot of arses on handlebars around here." - [J Ekdahl]

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

My question is . . . is it possible to  burn an alternate device the ATMEGA328 instead, using the same HEX code??

Y'all that responded missed the obvious answer--of course you can burn the alternate device.  With a '168 image, or e.g a Tiny841 image.

 

But will it >>run<<?!? devil

 

Simonetta gave the analysis I would do.  But a puzzling reason for the change of chips...

 

The atmega328 chips are more readily available and as I understand, has double the flash and ram size.

 

Last things first:  If this is just a reprogramming of additional boards with the same HEX, then what difference does double the SRAM/flash make?

 

Re availability: ???  Where, in corner shops?  Perhaps.  Perceived because of Arduino?  Perhaps.  (Remember when Arduino took off and there was a >>shortage<< of '328 for common people?)

 

At distributors?  Let's see what Octopart says.  Pick a package; I'll choose TQFP...  Well, if one was looking for 50000 off-the-shelf of a particular flavour, then perhaps.  But otherwise US distributors generally have thousands of the TQFP flavours of '168 in stock.

 

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hello theusch

 

Thanks for your comments to my post.  The increased memory of the '328 is of no consequence, as the hex file will fit on a '168.  I don't require the extra memory.

I am simply trying to find out what the possibility is of using the same hex file with a '328 ie. burning the device and (inferred) having the burned device function as it was intended to work on a '168.

 

I am using PU (dip) devices on this project. 

 

thanks again

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hello  to all

 

Thanks for your thoughtful responses to my question.

If I've correctly interpreted all that has been said, I think my hope of success with programming a'328 programming with hex code for a '168, has gotten a boost.  I think I will try it.

 

The matter of relative jumps and calls ...I'm still trying to digest.  I have no idea how the BASCOM compiler handled jumps and calls, but it did compile for '168 that has 16K, therefore had to deal with relative addressing and wraps at 8k boundaries.

Incidentally, my Project is a radio frequency antenna. In order to progress this project I'll have to get the '168 burned first and get the electronics working.   Once that is out of the way and I am sure it is working, I'll try burning a '328 for the circuit.

 

My thanks to all, you have been very helpful.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Although it will run I don't believe you will be able to program it due to the device signatures being different? 

ATmega168P      1e940b
ATmega168       1e9406
ATmega328P      1e950f
ATmega328       1e9514

 

 

Happy Trails,

Mike

JaxCoder.com

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I am not sure what a mega168 would do with an RJMP to non-existent memory.     It would be easy enough to just try it with debugWIRE or Simulator.

 

I would be very surprised if any HLL compiler would attempt an RJMP to non-existent memory.

 

A mega88 is designed to 'wrap'.    i.e. that is how you access all 8kB without a JMP instruction.

 

David.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Device signature isn't in the .hex file. So seems to me you can burn a .hex as long as it'll fit. If registers and vectors and so on aren't the the same, the code will go berzerk.

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Incidentally, my Project is a radio frequency antenna.

 

Can you tell us a little bit more?

 

Is the project an antenna impedance (VSWR) tuner?

Does the project aim the antenna at a given lat/long coordinate?

Is the project a small RF transmitter?

 

I'm not quite sure what the project actually does?

 

JC 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hello DocJC

 

When you write code, if you leave out a word, the program might not work.  Sorry, I left a word.  The project is an RF Antenna Analyzer.

This equipment does  give VSWR as you guessed, as well as rf resistance, reactance and sign of the reactance.  It can do a frequency  sweeps of antenna characteristics at the frequencies of interest.  The output is normally via LCD character display, but can also feed the serial data  to a PC for graphic plots and Smith Chart views.  The analyzer is somewhat unique, it uses uses a PLL and frequency mixers ( multiplication of rf sine waves) to produce in phase and quadrature values a voltage and current from which the Impedance can be calculated.

 

The project was the inspiration and creation of a Radio Amateur( now deceased) from the Netherlands.

 

I hope this explanation will provide some compensation for the word that I omitted in previous post.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Wow.  It sounds like a great project!

 

Feel free to post a photo of it when you have it working!

 

JC

KD8HKD