Topics Topics Edit Profile Profile Help/Instructions Help Member List Member List  
Search Last 1|3|7 Days Search Search Tree View Tree View  

SPI working in a 18F452 but not in a ...

:: EPE Chat Zone ­:: ­Radio Bygones Message Board :: » EPE Forum Archives 2010 - » Archive through 08 December, 2012 » SPI working in a 18F452 but not in a 18F4520 « Previous Next »

  Thread Last Poster Posts Pages Last Post
  ClosedClosed: New threads not accepted on this page        

Author Message
Top of pagePrevious messageNext messageBottom of page Link to this message

atferrari
Frequent Contributor
Username: atferrari

Post Number: 1283
Registered: 05-2005


Rating: N/A
Votes: 0 (Vote!)

Posted on Monday, 19 November, 2012 - 01:23 am:   Edit Post Delete Post Print Post    Move Post (Moderator/Admin Only)

The settings and routines shown herebelow were used to control an external device that needed only to receive data from a 18F452. It worked at the first try.

After migrating to the 18F4520, I can see that the SPI is not transmitting. SCK and SDO, both remain low all the time.

I am now aware that the 18F4520 was born plagued with bugs in the SPI module but even up to the last (4th) revision no one seems to affect the modality "Master with clock =Fosc/4" which is the one I implemented here.

The migration document show changes and additions but no one affects the MSSP module.

Anyone with hands on experience that could help?

I will appreciate that.
application/octet-streamAbstract of code
Abstract.asm (2.1 k)

Agustín Tomás - Buenos Aires - Argentina
Top of pagePrevious messageNext messageBottom of page Link to this message

bowden_p
Frequent Contributor
Username: bowden_p

Post Number: 146
Registered: 01-2006

Rating: N/A
Votes: 0 (Vote!)

Posted on Monday, 19 November, 2012 - 04:38 pm:   Edit Post Delete Post Print Post    Move Post (Moderator/Admin Only)

Hi Agustin,
I used the SPI interface on a PIC16F876A, and during the initialisation of the module had the SSPEN pin low during writes to the configuration registers, then enabled the module with a
"bsf SSPCON,SSPEN" .
This was mentioned as a good sequence in the datasheet I think, and if changes were requires, to disable the module first with
"bcf SSPCON,SSPEN" .
It worked fine - which surprised and pleased me.
Does this apply to the 18F MSSP module too?
With regards, Paul.
Top of pagePrevious messageNext messageBottom of page Link to this message

bowden_p
Frequent Contributor
Username: bowden_p

Post Number: 147
Registered: 01-2006

Rating: N/A
Votes: 0 (Vote!)

Posted on Monday, 19 November, 2012 - 04:44 pm:   Edit Post Delete Post Print Post    Move Post (Moderator/Admin Only)

Also, I noticed that the transmission time after writing to the SSPBUF was longer than you might expect, as there was a small delay before transmission started!
With regards, Paul.
Top of pagePrevious messageNext messageBottom of page Link to this message

atferrari
Frequent Contributor
Username: atferrari

Post Number: 1284
Registered: 05-2005


Rating: N/A
Votes: 0 (Vote!)

Posted on Monday, 19 November, 2012 - 06:32 pm:   Edit Post Delete Post Print Post    Move Post (Moderator/Admin Only)

Hola Paul,

Gracias for replying.

I tried enabling the module after setting everything, as you did. No joy.

The 8 NOPs are there because I verified with the scope that they are (were!) enough.

Have to rethink this and start afresh. It is true that what worked with the 452 doesn't with the 4520.

Why everything has to chance so much?
Agustín Tomás - Buenos Aires - Argentina
Top of pagePrevious messageNext messageBottom of page Link to this message

bowden_p
Frequent Contributor
Username: bowden_p

Post Number: 149
Registered: 01-2006

Rating: N/A
Votes: 0 (Vote!)

Posted on Monday, 19 November, 2012 - 07:33 pm:   Edit Post Delete Post Print Post    Move Post (Moderator/Admin Only)

Hi Agustin,
Have you tried the Microchip forums? I had a quick look and they appear daunting compared to this one, but you may get some clues there.
A search may cut down the task!
With regards, Paul.
Top of pagePrevious messageNext messageBottom of page Link to this message

atferrari
Frequent Contributor
Username: atferrari

Post Number: 1286
Registered: 05-2005


Rating: N/A
Votes: 0 (Vote!)

Posted on Monday, 19 November, 2012 - 09:01 pm:   Edit Post Delete Post Print Post    Move Post (Moderator/Admin Only)

Yesterday I posted also there but got nothing conclusive. Yours is, at least, positive, not just guessing.

Right now, starting with SPI as it was a new field for me. Will post the outcome.

ˇGracias!
Agustín Tomás - Buenos Aires - Argentina
Top of pagePrevious messageNext messageBottom of page Link to this message

bowden_p
Frequent Contributor
Username: bowden_p

Post Number: 151
Registered: 01-2006

Rating: N/A
Votes: 0 (Vote!)

Posted on Monday, 19 November, 2012 - 10:46 pm:   Edit Post Delete Post Print Post    Move Post (Moderator/Admin Only)

Hi Agustin,
All the best with your trials. Just a thought - is there any effect in declaring the SPI pins as analog rather than digital in function. I don't know this part at all, but I have noticed some pins in the PIC16F series of chips needed odd declarations to get peripheral modules to work.
I can't remember which module it was just now, but could check if you like.
I doubt that this is your problem in reality however.
( When I was developing my SPI driver code I started with a simple loop that just switched the module between master and slave and checked what happened to the pins, then added data input/output etc, switching modes etc. Helped me to understand the module functions and control! I output a pulse to another pin during the looping to sync my scope to the loop. )
With regards, Paul.
Top of pagePrevious messageNext messageBottom of page Link to this message

atferrari
Frequent Contributor
Username: atferrari

Post Number: 1287
Registered: 05-2005


Rating: N/A
Votes: 0 (Vote!)

Posted on Tuesday, 20 November, 2012 - 01:23 am:   Edit Post Delete Post Print Post    Move Post (Moderator/Admin Only)

Hola Paul,

Problem solved.

I started afresh and re read the whole chapter.

I decided to apply the settings and THEN enable the MSSP module. Thanks for reminding me that!

To know when I could start the next byte I implemented the check of the BF flag.

Simple (and effective) as that!

The routine is here.
application/octet-stream
The routine.asm (0.9 k)

BTW, is worth to recognize that my routine for the 18F452 with those horrible NOPs seem to have worked by sheer luck.

Moving ahead again...
Agustín Tomás - Buenos Aires - Argentina
Top of pagePrevious messageNext messageBottom of page Link to this message

bowden_p
Frequent Contributor
Username: bowden_p

Post Number: 152
Registered: 01-2006

Rating: N/A
Votes: 0 (Vote!)

Posted on Tuesday, 20 November, 2012 - 03:30 pm:   Edit Post Delete Post Print Post    Move Post (Moderator/Admin Only)

Hi Agustin,
Glad you now have a working SPI module - as they say "When all else fails - (re-)read the instructions!" I would be the first to admit to have done this! I notice in your Busy flag check that if by chance the Busy doesn't clear - an endless loop happens. A indication I suppose of a comms failure of some sort, but inconvenient if it does occur. It might be advisable to include some sort of an emergency break-out from the loop if this should happen. Perhaps defined by a timer, a simple loop counter, or a watchdog action? This is belt-and-braces, but I personally feel cautious of possible endless loop situations like this. I see you read the BUSY register to clear the Busy flag - I nearly missed that one when writing my code!
In my own SPI code I used the SPI Interrupt and a Timer Interrupt to time transmission. The data to send was put into a table, and the SPI and Timer Interrupts then took the data one byte at a time from the table, sent it and then timed a delay before the next byte. One Interrupt set up the other in a sort of flip-flop action until all bytes were sent. (End of data table.) The last Interrupt action was to set a TX Complete flag.
I surprised myself how well this worked! It means that once transmission is started it completes by itself without any further program interaction, other than an occasional pole of the TX Complete flag, and the processor is free to do other things meanwhile. My data rate was the slowest the SPI could manage, because of the length of the connection to the RX end, and I needed to have other code serviced all the time.
All the best with your SPI project!
With regards, Paul.
Top of pagePrevious messageNext messageBottom of page Link to this message

mikeb
Frequent Contributor
Username: mikeb

Post Number: 758
Registered: 02-2008

Rating: N/A
Votes: 0 (Vote!)

Posted on Tuesday, 20 November, 2012 - 03:53 pm:   Edit Post Delete Post Print Post    Move Post (Moderator/Admin Only)

Well done Agustin.

I notice this is the opposite to enabling a UART where the module needs to be enabled BEFORE anything else - that took me an hour to spot .... grrrrr.
Top of pagePrevious messageNext messageBottom of page Link to this message

mikeb
Frequent Contributor
Username: mikeb

Post Number: 759
Registered: 02-2008

Rating: N/A
Votes: 0 (Vote!)

Posted on Tuesday, 20 November, 2012 - 04:03 pm:   Edit Post Delete Post Print Post    Move Post (Moderator/Admin Only)

Oh yes - and you can always have a look at Micrchips examples in C - that can show what bits they set, when, despite it being a high level approach.
Top of pagePrevious messageNext messageBottom of page Link to this message

atferrari
Frequent Contributor
Username: atferrari

Post Number: 1288
Registered: 05-2005


Rating: N/A
Votes: 0 (Vote!)

Posted on Tuesday, 20 November, 2012 - 09:18 pm:   Edit Post Delete Post Print Post    Move Post (Moderator/Admin Only)

Thanks to all posting here.

Yes, I was thinking of that eventual freezing waiting for a flag but I was so happy that I forgot my own initial concern.

Just few hours ago, something went wrong during my test and first, a MCP4822 was fried (only God knows how) and later a +5V regulator became mute and a zener shorted.

Not Sandy but a desaster anyway.

In testing that rather crowded veroboard I managed to finally burn a TIP 29. I feel I had the right to...

End of the story: I halted this and decided to pay for a decent PCB. I am fed up if assembling things that end looking more precarious than what I expected in the beginning.

And tonight we start with a vessel at 01.00, another at 07.00 and the last at 13.00. And me in all of them!

Not many things to feel happy.
Agustín Tomás - Buenos Aires - Argentina
Top of pagePrevious messageNext messageBottom of page Link to this message

bowden_p
Frequent Contributor
Username: bowden_p

Post Number: 153
Registered: 01-2006

Rating: N/A
Votes: 0 (Vote!)

Posted on Tuesday, 20 November, 2012 - 09:32 pm:   Edit Post Delete Post Print Post    Move Post (Moderator/Admin Only)

Oh dear Agustin! I hope the day job goes better!
All the best, Paul.
Top of pagePrevious messageNext messageBottom of page Link to this message

atferrari
Frequent Contributor
Username: atferrari

Post Number: 1301
Registered: 05-2005


Rating: N/A
Votes: 0 (Vote!)

Posted on Friday, 30 November, 2012 - 11:16 am:   Edit Post Delete Post Print Post    Move Post (Moderator/Admin Only)

Hola Paul,

Yesterday, having just to wait for a vessel to complete, I was thinking of the possibility that the checking of BF could get me falling into and endless loop.

Whether I am in a Master-slave setup or a Master just transmiting, not reading (my case), I realized that, actually, that couldn't happen because who commands Tx and Rx is the micro and no matter what garbage (or no garbage at all) I put at the input, reception will be.

After all it is in the esence of SPI that the Master commands both.

Do you think I am overlooking something?
Agustín Tomás - Buenos Aires - Argentina
Top of pagePrevious messageNext messageBottom of page Link to this message

bowden_p
Frequent Contributor
Username: bowden_p

Post Number: 157
Registered: 01-2006

Rating: N/A
Votes: 0 (Vote!)

Posted on Friday, 30 November, 2012 - 11:55 pm:   Edit Post Delete Post Print Post    Move Post (Moderator/Admin Only)

Hi Agustin,
If there is a chance that something might happen in a program - however unlikely - then it will happen, and more frequently than you would imagine! This is called Sod's Law - or something!
Thus if you write a program with the possibility of an endless loop, even if you believe it "cannot" happen, you can bet that sometime it will!
Perhaps I am being pessimistic, but I always feel uncomfortable if there is this possibility in a program, so I try to avoid this if at all possible.
With regards, Paul.
Top of pagePrevious messageNext messageBottom of page Link to this message

wizard
Frequent Contributor
Username: wizard

Post Number: 85
Registered: 11-2005

Rating: N/A
Votes: 0 (Vote!)

Posted on Saturday, 01 December, 2012 - 12:26 pm:   Edit Post Delete Post Print Post    Move Post (Moderator/Admin Only)

It's called Murphy's Law and it states that:-

"If in any system something can go wrong - it will"

Regards
Top of pagePrevious messageNext messageBottom of page Link to this message

bowden_p
Frequent Contributor
Username: bowden_p

Post Number: 160
Registered: 01-2006

Rating: N/A
Votes: 0 (Vote!)

Posted on Saturday, 01 December, 2012 - 08:12 pm:   Edit Post Delete Post Print Post    Move Post (Moderator/Admin Only)

Hi Wizard,
Thanks for the correct name!
I hope that you don't suffer from it too much!
With regards, Paul.
Top of pagePrevious messageNext messageBottom of page Link to this message

gizo
Frequent Contributor
Username: gizo

Post Number: 96
Registered: 10-2006

Rating: N/A
Votes: 0 (Vote!)

Posted on Sunday, 02 December, 2012 - 12:46 am:   Edit Post Delete Post Print Post    Move Post (Moderator/Admin Only)

And of course there are hundreds of ML codicils. m_Get(frustration) = ( ML->codicil(n) to ML->codicil(n~));

From my experience _some_ are: n = (if it produces smoke... it will happen over Xmas period when all electronic parts shops are closed).
and n++ = (when they are open, they sold the last 4K7 resistor to a guy 5 minutes ago).
and n++ = (they only have 4M7 resistors in stock now).
Top of pagePrevious messageNext messageBottom of page Link to this message

atferrari
Frequent Contributor
Username: atferrari

Post Number: 1302
Registered: 05-2005


Rating: N/A
Votes: 0 (Vote!)

Posted on Sunday, 02 December, 2012 - 01:11 am:   Edit Post Delete Post Print Post    Move Post (Moderator/Admin Only)

Saturday morning, (that is today), if you start to work on that part of the power supply with LM317 and LM337 regulating +/-32V you will find that the tant caps you have got are all for 16V. Hundreds of them!

But once, Murphy helped me:

I work as a surveyor for a big company involved in very big projects around the world. At the time I was in the main plant, discussing the stuffing of several containers. The place was the office in charge of the shipments worldwide, a quite busy place.

There, they have a huge rack full of boxes with plans for all works (and they are MANY).

Suddenly, a project engineer entered asking if they had handy the plans for the project No 529 (yes, I still recall the number but not the name).

Something inspired me to suggest looking at the sole box amongst hundreds that had its label not visible. Effectively, that was the box. The sole thing I could say, Murphy...pure Murphy.
Agustín Tomás - Buenos Aires - Argentina

Administration Administration Log Out Log Out   Previous Page Previous Page Next Page Next Page