Mega32 vs mega328p is SPI setup different?

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

I change my code for ili9163 lcd from MEGA32 which is working fine to MEGA328P arduino nano,i thought it would be very simple -just change the

makefile(WINAVR) to MEGA328P and change the programmer(stk500) to the 328p chip.Then use MOSI RB3(D11) and CLK RB5(D13)....i get no serial data and no CLK pulses.

I also switch on 6 LEDS (B0,B1,B2,B3,B4,B5)  on PORTB for 1 sec then off just to test PORTB.What am i missing.....any sugestions???
 

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

How would we know, you did not show any code?!!!

 

Verify you have set the SS pin to be an output, if left as the default input and left floating, the SPI will switch to slave mode if the pin is low, and stop working as an SPI master!

 

Jim

 

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

Thanks ki0bk ,but did you see that i said the code is working on a MEGA32,WHY does the exact same code compiled for mega328p not work?(No data on MOSI and no CLK pulses).

mega32 DC = RB1..........mega328p DC = RB1

mega32 MOSI = RB5......mega328p MOSI = RB3

mega32 CLK = RB7.........mega328p CLK = RB5

 

I do not use SS and that pin is OUTPUT.

RST is permanent pullup on pcb.

CS is permanent pulled low on pcb.

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

mikedb wrote:
i thought it would be very simple

Famous last words!

 

You really need to go through the two datasheets and verify that everything is the same - or spot the differences.

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

Go on. There is nothing complicated. You just use the appropriate hardware pins for the chip.
This includes the SS pin. The SS pin must be an OUTPUT (whether you use it or not) or the SPI will go haywire.
.
The real mystery is why you choose to hold /CS permanent low when the SPI slave device needs correct timing on the CS pin to ensure that everything works properly.
.
David.

Last Edited: Tue. Jul 18, 2017 - 07:07 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

mikedb wrote:
.i get no serial data and no CLK pulses.
Four of the things that might be:

 

1) You forgot to set MSTR in the 328 code

 

2) You have left SS as a floating/tied input that is going low (and hence kicks SPI from MSTR mode)

 

3) there's an electrical fault and the lines are tied low

 

4) You forgot to detach your ISP programmer and it's holding the SPI lines

 

Or something else?

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

And without seeing the code, it's anyone's guess!

 

 

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

Hallo David.. i back engineered the lcd pins and fixed CS ,RESET (the lcd still worked in the original pcb) so there was 2 less pins to worry about.I am very new to C programming and all this is just part of a learning curve.As i said the code work 100% on  a MEGA32 but the exact same code does not work when build for the MEGA328P.I did mention in the beginning that the MEGA328P is on a ARDUINO NANO pcb.So there is a possibility that the pre loaded bootloader mess up the SPI interface ,i will check that and let you know.

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

On the Arduino Nano, SS pin is PB2 aka D10, this pin must be made an output, if left as an input (floating) it will cause the SPI to switch from master to slave and STOP working!

On the Mega32 chip, SS pin is PB4, so they are not the same, code on the M32 will be setting PB4 to be an output, while the M328 needs PB2 to be made an output, so the code will not work "unchanged"......

 

 

Jim

 

 

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

The bootloader is no problem. SS on a mega32 is PB4. SS on a mega328P is PB2.
Rebuilding for m32, m328p, m128, m2560, ... is nothing more than defining the correct hardware SPI pins.
.
I am a little worried about your choice of 5V boards. The ILI9163 modules are 3.3V. You must use level shifters or series resistors with the Red displays from Ebay.
.
Personally, I always run boards at 3.3V. Life is easier. I have never had a problem with m328p @16MHz and 3.3V.
It works fine for hobbyist use. Likewise running an Xmega @ 60MHz works fine too.
.
David.

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

ki0bk wrote:
And without seeing the code, it's anyone's guess!

Why does OP seem reluctant to post code?

 

And ideally, a small complete test program that exhibits the symptoms.  [note to OP:  Often, the process of creating this uncovers the source of the problem]

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

Hallo David.

I am not using the arduino board on the build in 5v regulator,there is a seperate pin marked as 5v ,i supply 3v3 on that pin and the same 3v3 is the supply to the LCD.

 

I did get the SPI working ,but this is a horror story (i will try to back simulate the problem to see what actually happend).

I use WINAVR ,then open the makefile within WINAVR change the MCU type and build no problem so far but the SPI did not work.

When i comment out the SPCR........ line i get a lot of errors..change back to MEAGA32 I get the same errors and this is the source code that worked for 2 weeks .

I then close WINAVR and restart ...same errors, then i open the MAKEFILE software change the MCU type and save the makefile...build  with no errors and the SPI is working and the LCD is also working.

 

I seems like i was building the code for MEGA32 and then program a MEGA328P,the makefile did no save the changes when opened with WINAVR.

 

 

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

That, my friend, is why most people would use an IDE and not mess around with hand edits in Makefiles!

 

If there's some reason you feel the need to stick with a compiler that is now 8 years out of date then at the very least get a copy of AVR Studio 4 and use it as an IDE for WinAVR. In fact you could even continue to use WinAVR and match it to Atmel Studio 7 but as AS7 comes with a compiler that is 7 years more up to date I would probably go with that anyway.

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

Hi clawson ..yes it feels like you read my mind.The reason why i am using WINAVR is it was the first complete compiler with all the tool chains included that i could find.

 

i tried AVR STUDIO 4 ....no tool chain.(The user manual actually tells you to copy WINAVR tool and libraries to AVR STUDIO  to work).

 

AVR STUDIO 5....no tool chain.

 

AVR STUDIO 6 ....this version was so slow on my pc that i gave up and uninstall it.(you can google this and there is a few people with very fast pc's that also find it slow)

 

When i started of with C programming (all home schooling)a few months back everything was new ....so to get BLINK LED to work was a huge challenge.

For a newbie to start fiddiling with tool chains just to get a led to blink was just asking to much.

With WINAVR i could at least start somewhere and progressed to a point were i could copy sample code modify and get my ili9163 lcd working

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

mikedb wrote:
i tried AVR STUDIO 4 ....no tool chain.(The user manual actually tells you to copy WINAVR tool and libraries to AVR STUDIO to work).
But with As4 you just download AS4 itself and then a separate package with the compiler (which can be WinAVR if you like living in the past or it can be Atmel Toolchain for Windows if you want a modern one). If you use Atmel Toolchain then AS4.19 will just "see" it. if you use WinAVR you need to read a sticky thread at the top of the Studio(AVR) forum that tells you how to get AS4 to "see" WinAVR.
mikedb wrote:
AVR STUDIO 5...
mikedb wrote:
AVR STUDIO 6 ...
5 and 6 were bug ridden development versions on the way to making AS7 which is fairly stable these days. Do not use 5 or 6, ESPECIALLY do not use 5 !!

 

Seems to me your options are:

 

1) AS4 + WinAVR

2) AS4 + Atmel Toolchain

3) AS7 (inc. recent avr-gcc)

4) some other IDE (Eclipse, Netbeans, Code::Blocks, etc)

5) even some other compiler (which probably also has its own IDE)

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

mikedb wrote:
AVR STUDIO 6 ....this version was so slow on my pc that i gave up and uninstall it

TBH, that's an indication that you need a new PC.

 

Or, at least, an upgrade - at least 8MB RAM and a SSD will make a huge difference.

 

AtmelStudio is really not unusual in terms of system requirements these days - you will need the same for anything Eclipse-based.