Atmega328p vs Atmega32

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

I migrated my Atmega328p projet to an Atmega32A device, becuause it has a JTAG interface, and easier to debug problems. With the Mega328p I was handling an SD card with FatFS successful. I copied the source code and made the necessary adjusments (SPI pinout definition) in the FatFs library, but regarding the datasheet the SPI registers and settings are the same, I thought it is enough. I'm using the sample code from ffsample.zip:

int main(void)
{
	UINT bw;

	FRESULT fr;
	FATFS fs;
	FIL fil;

	UART_Init(9600);
    LCDInit(LS_NONE);

    LCDClear();
    LCDWriteStringXY(0, 0, "Welcome");

    _delay_ms(1000); 

    // SDCard write test
	f_mount(&fs, "", 0);		/* Give a work area to the default drive */

	fr = f_open(&fil, "newfile.txt", FA_WRITE | FA_CREATE_ALWAYS);	/* Create a file */
	LCDWriteIntXY(0, 1, fr, 1);

	if (fr == FR_OK)
	{
		f_write(&fil, "It works!\r\n", 11, &bw);	/* Write data to the file */
		LCDWriteIntXY(2, 1, bw, 2);

		fr = f_close(&fil);
		LCDWriteIntXY(5, 1, fr, 1);

		if (fr == FR_OK && bw == 11)
		{
			LCDClear();
			LCDWriteStringXY(0, 0, "Written");
		}
	}

    while(1){}
}

 

adjustments in mmc_avr.c:

/* Port controls  (Platform dependent) */
#define SS		    (1<<PORTB4)
#define MOSI	    (1<<PORTB5)
#define MISO	    (1<<PORTB6)
#define SCK		    (1<<PORTB7)
#define CS_LOW()	PORTB &= ~SS			/* CS=low */
#define	CS_HIGH()	PORTB |= SS			/* CS=high */

#define	FCLK_SLOW()	SPCR = 0x52		/* Set slow clock (F_CPU / 64) */
#define	FCLK_FAST()	SPCR = 0x50		/* Set fast clock (F_CPU / 4) */

static
void power_on (void)
{
	DDRB  |= SS | SCK | MOSI;
	SPCR = 0x52;			/* Enable SPI function in mode 0 */
	SPSR = 0x01;			/* SPI 2x mode */
}

static
void power_off (void)
{
    /* Disable SPI function */
	SPCR = 0;
    /* Set SCK/MOSI/CS as hi-z, INS#/WP as pull-up */
	DDRB  &= ~(SS | SCK | MOSI);
}

With the Mega328p I was using Atmel Studio 7, but with the JTAG-ICE debugger from ebay, I'm using AVR Studio 4.17. I tried to compile the Atmega32 code in Atmel Studio 7 too, but the result was annoying. 
The newfile.txt sometimes created, sometimes not, and sometimes a bunch of unreadable files also created with a strange-character name.

Do you know any major differnce between the 2 CPU which makes difference?

This topic has a solution.
Last Edited: Mon. Oct 21, 2019 - 08:58 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

With the Mega328p I was using Atmel Studio 7

With which debugger?

I migrated my Atmega328p projet to an Atmega32A device, becuause it has a JTAG interface,

So does the Mega324 family (pin compatible up to 128K) but you need a better debugger like an Atmel ICE.

John Samperi

Ampertronics Pty. Ltd.

www.ampertronics.com.au

* Electronic Design * Custom Products * Contract Assembly

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

For the Mega328 i have no debugger, just an ISP programmer

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

debugWIRE via USB UART | AVR Freaks though one of the operator's interfaces is the AVR GDB client instead of Atmel Studio.

 

"Dare to be naïve." - Buckminster Fuller

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

yoman91 wrote:
Do you know any major differnce between the 2 CPU which makes difference?

 

What F_CPU are you using?  the 32A tops out at 16MHz where the 328P is 20MHz.

Are you using the correct Fuse settings for the XTAL?

 

What supply voltage is used?

The 328P uses 2.5ma at 5V and 16MHz.

The 32A uses 14ma at 5V and 16MHz.

Are the bypass caps adequate for the higher current spikes?

 

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

Yes, the Chinese JTAGICE-1 clone works fine with a mega32 in AS4.18
It can connect directly to mega32 board JTAG header with a regular 10 way ribbon (that comes with the dongle).
It can even supply power to the target.
.
Of course you are limited to AS4 and very OLD mega16,32,64,128, ... and a few others.
.
Another solution is to buy a Microchip SNAP. You have to make your own adapter or cable.
You can debug all your chips. Even modern ones.
And use AS7.0.2389 (tick one Option box)
.
David.

Last Edited: Mon. Oct 21, 2019 - 06:38 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

The frequency is 8MHz, and the fuses set to work with external capacitor. I power it from a power bank, directly, not via the JTAG or USBAsp. 

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


yoman91 wrote:
I migrated my Atmega328p projet to an Atmega32A device, becuause it has a JTAG interface, and easier to debug problems.

Eh??

 

ATmega328P also has on-chip debug!

 

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Yes, but the OP does not own a debugWIRE-capable debugger. He does seem to have a Chinese JTAGICE-1 clone.
.
Since he uses external power at all times there is no advantage in using power from a USBASP or JTAGICE-1 ribbon.
He can use any regular programmer or debugger that only senses VCC.
I still reckon that a SNAP or PICKIT4 would be a wise investment. A full ATMEL-ICE is expensive but provides a 2.54mm JTAG adapter.
.
David.

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

OK, but I forget to emphasize one thing. I only have the chinese JTAG-ICE device, for one-wire debug applications I don't have interface. To keep it simple, please forget about debugging in deep level. One thing I think it is important, that the code, which is working on ATMEGA328p and it should work on MEGA32A as well, it isn't.

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

Do you have access to any kind of hardware diagnostic tool like a DSO or a Logic Analyser? More than anything it would be interesting to look at the SPI signals to the SD/MMC in both the 328 working case and the 32 failing case. 

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

Unfortunately I dont't have any of these device. 

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

You can run FatFS on mega32 or mega328P.   You have the same amount of SRAM and Flash memory but more PORT pins

 

SPI works exactly the same but on slightly different pins.

Input signals like Card-Detect will just go to regular PORT pins.

 

So it just comes down to configuring the mmc_avr.c file

 

You appear to have a 16x2 LCD.   This requires port pins too.

When you move from mega328P to mega32 you must ensure different pins to SD.

 

FatFS often uses a Timer for checking external signals like Card-Detect.

Timers are more primitive on the mega32

SFRs obviously have different addresses in memory but if your C code uses official SFR names and BIT_NAMEs the Compiler looks after this.

 

David.

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

You grabbed the main point of it, that's why I'm confused. 16x2 LCD is working, since readable characters appear on it, and more or less the SD card too, because some data are written on it, but nothing openable/readable. (that is the main problem). I'm using official names like SFR, SPCR, etc..., so it is the task of the compiler to deal with it. One thing, I don't use the card detected signal neither on Mega328p nor on Mega32A, because the SDcard module has no such a feature. 

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

yoman91 wrote:
the fuses set to work with external capacitor.

What does that mean - external XTAL?

 

yoman91 wrote:
I power it from a power bank

What voltage?

What capacitors do you have on the board near the 32A?

Normally, you should have a 4.7uF to 10uF cap on the board,

and a 0.1uF cap mounted right next to each Vcc pin and the AVcc pin. 

Also, all GND pins should be connected to ground.

 

I'm asking this because "The newfile.txt sometimes created, sometimes not,

and sometimes a bunch of unreadable files also created" indicates that it works

sporadically.  This could be caused by hardware that's on the margin.

 

 

 

 

 

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

For the 2 panels I'm using 8MHz external crystal with two ceramic capacitor. 

 

Clock fuse setting for Atmega328p: 

Ext. Crystal Osc.; Frequency 3.0-8.0 MHz; Start-up time PWRDWN/RESET: 16K CK/14 CK + 65 ms;  [CKSEL=1101 SUT=11]

Clock fuse setting for Atmega32A:

Ext. Crystal/Resonator Medium Freq.; Start-up time: 16K CK + 64 ms; [CKSEL=1101 SUT=11]

 

Because the 8Mhz is the border of the above settings, I also tried these combination:

Mega328p:

Ext. Crystal Osc.; Frequency 8.0-    MHz; Start-up time PWRDWN/RESET: 16K CK/14 CK + 65 ms;  [CKSEL=1111 SUT=11]

Mega32A:

Ext. Crystal/Resonator High Freq.; Start-up time: 16K CK + 64 ms; [CKSEL=1111 SUT=11]

 

The development board is from ebay: 

https://www.ebay.com/itm/ATMEGA16-ATmega32-ISP-Minimum-System-Board-AVR-Minimum-System-Development-Board/201622043605?hash=item2ef19c3bd5:g:bRoAAOSwRgJXhSjf

https://www.ebay.com/itm/1pcs-ATMEGA8-ATMEGA48-ATMEGA88-Development-Board-AVR-DIY-Kit/183903735398?hash=item2ad1844e66:g:OdwAAOSwnopdP-VK

 

Regarding the power suppy: It is an USB cable plugged in a 5V power bank, and the VCC and GND connected directly to the port without regulators and capacitors.

 

 

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

I assumed that external capacitor  was a typo for external XTAL

 

A PowerBank seems to be a Lithium battery that can be charged by a USB cable.   And have outlet "USB" power sockets that you use for your Phone, Laptop, ...

 

I suggest that you measure the actual voltage that you find on the PowerBank outlets.

A well designed "PowerBank" should provide a well regulated 5V outlet.

 

David.

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

Just a reminder that SD/MMC are 3.3V - hope there's some translation logic somewhere if 5V is being input as Vcc to the AVR !!

This reply has been marked as the solution. 
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

You must definitely have 3.3V for the SD card VCC pin.

The logic signals should be 3.3V

 

In practice the SD signals "seem" to be input voltage tolerant.

I would not make a commercial product that uses 5V logic.

 

David.

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

OMG. I saw an AMS1117-3.3 regulator on the board, and some resistors, I thought it is there to protect the MISO, MOSI, SCK signals to from the 5V. I grabbed my multimeter and I figured out, that the data lines are connected directly to the SD card, without series resistor. As final conclusion, lowering the power source to 3.3V solved the problem.