PDA

View Full Version : SPI interface



malc-c
- 5th January 2011, 14:27
I've been trying to get a couple of addon boards for my EasyPIC5 board running, both of which use SPI. Not having worked with this before, could someone tell me if this is clock specific. I've tried using 4,6,10,12, Mhz crystals, and HS_PLL to x 4 (16,24,40,48 Mhz running speeds) but the examples found on the web (MikroElectronika) just won't play ball with the 18F4520. The only crystal I've not tried is an 8Mhz (32Mhz running speed).... but then the examples imply it should still work with a 10Mhz one ?

I would ask on the Mikrobasic forum, but it seems that as I'm new, each post still goes through the approval stage - so it takes 2 days to post 2 messages - and I know you guys here as really helpful and very knowledgeable :)

mackrackit
- 6th January 2011, 01:12
I do not know much about it but your code/connections might help someone help you.... something like that...

And what are you connecting to?

malc-c
- 6th January 2011, 13:49
Dave,

two modules... the first was serial ethernet

http://www.mikroe.com/img/development-tools/accessory-boards/communication/serial-ethernet/gallery/ethernet_550_1.jpg (http://www.mikroe.com/eng/products/view/14/serial-ethernet-board/)

and then yesterday an MM/SD card reader.

http://www.mikroe.com/img/development-tools/accessory-boards/storage/microsd/gallery/microsd_01.jpg (http://www.mikroe.com/eng/products/view/369/microsd-card-board/)

The latter (when using the downloadable Hex file) should display a message that there is no card inserted when hooked up to via serial to a PC running a serial communicator (such as that contained inside microcode studio. However on power up and with it connected at the correct baud rate, I get several lines of hyroglythics, which on sebsequent re-booting the EP5 board is repeated exactly. However if I change crystals and do the same the characters displayed are different, but again are the same on subsequent re-boots of the EP5. The only difference is that the schematic shows an 8Mhz xtal and I don't have one (well not until I get home as my order has just arrived!) - hence the question about it being sensitive as to what clock frequency these thing run at.

On the serial ethernet - nothing worked so I suspect the card is faulty !

HenrikOlsson
- 6th January 2011, 16:26
Hi Malcolm,
I take it you are loading your PIC with the compiled examplefiles provided by mikroE with the boards, correct? If so I'd say I say it's quite important that you have the correct x-tal speed otherwise all the timing calculations etc will be off. This MIGHT not matter for the actual SPI communications (at least not if running slower) but it WILL for sure when it comes to communicating with the PC over the PICs USART.

Now, I looked at the example files for the microSD card and it looks like it's s written for 8Mhz oscillator and 19200 baud. The comments say 8Mhz HS-PLL so I'm not sure if it's actually 8Mhz or if it's 32Mhz.

If you have a 4Mhz x-tal you MIGHT be able to run it if you set the baudrate in the terminal software to 9600. By the sound of you've already tried that but in case you haven't.... Then of coarse you must set all the DIP switches correctly but I'm sure you have that covered.

I have a PICPLC16 board which have the serial ethernet "module" on board. I'm very interested in what you might come up with, codewise, when it comes to that. It should be doable in PBP but I don't think it's an easy task.

Keep us posted!
/Henrik.

ScaleRobotics
- 6th January 2011, 16:31
I've been trying to get a couple of addon boards for my EasyPIC5 board running, both of which use SPI. Not having worked with this before, could someone tell me if this is clock specific. I've tried using 4,6,10,12, Mhz crystals, and HS_PLL to x 4 (16,24,40,48 Mhz running speeds) but the examples found on the web (MikroElectronika) just won't play ball with the 18F4520. The only crystal I've not tried is an 8Mhz (32Mhz running speed).... but then the examples imply it should still work with a 10Mhz one ?

I would ask on the Mikrobasic forum, but it seems that as I'm new, each post still goes through the approval stage - so it takes 2 days to post 2 messages - and I know you guys here as really helpful and very knowledgeable :)

The instructions on the file say to use a 8mhz crystal. If you are using the hex file, then yes, it would be clock specific for rs-232 serial interface.



' *
' * Project name:
' Mmc_Test (MMC Library Example)
' * Copyright:
' (c) Mikroelektronika, 2010.
' * Revision History:
' 20081016:
' - initial release;
' 20101110:
' - active comments added, minor code and comments modifications;
' * Description:
' MMC library test. Upon flashing, insert a microSD card into the
' module, when you should receive
' the "Init-OK" message. Then, you can experiment with MMC read and write
' functions, and observe the results through the Usart Terminal.
' * Test configuration:
' MCU: PIC18F4520
' http://ww1.microchip.com/downloads/en/DeviceDoc/39631E.pdf
' Dev.Board: EasyPIC6
' http://www.mikroe.com/eng/products/view/297/easypic6-development-system/
' Oscillator: HS-PLL, 8.00000 MHz
' Ext. Modules: microSD CARD board
' http://www.mikroe.com/eng/products/view/369/microsd-card-board/
' ac:microSD_CARD_board
' SW: mikroBasic PRO for PIC
' http://www.mikroe.com/eng/products/view/9/mikrobasic-pro-for-pic/
' * NOTES:
' - Connect microSD Card to PORTC and turn on PIC switches on it (switches 1,4 and 6).
' Put supply select jumper in 5V position.
' - Connect RS232 cable and turn on RS232 switches SW7.1 and SW8.1 (board specific).
' - This is NOT an example for working with FILES on a microSD memory module.
' *

malc-c
- 6th January 2011, 18:25
Guys thanks for the replies.

Dave, you are correct (as always :) ) - the SD card examples work fine when an 8Mhz crystal is used, (just got home to find my order had arrived). I can't test the serial ethernet as the add-on is in the post back to the supplier, but that's where the confusion comes in.

The schematic in the article shows the default 8Mhz extal used with the ENC28J60 chip, but some of the example files state a 8 or 10mHz


target devices :
' * any PIC with integrated SPI and more than 4 Kb ROM memory
' * 32 to 40 MHz clock is recommended to get from 8 to 10 Mhz SPI clock,
' * otherwise PIC should be clocked by ENC clock output due to ENC silicon bug in SPI hardware
' * if you try lower PIC clock speed, don't be surprised if the board hang or miss some requests !
' * tested with PIC16F877A@10Mhz on EasyPIC4 board


On some of the examples I tried re-compiling with the IP settings for my network and the 18F4520 with a 10Mhz and that still didn't work, so I opted to try a 16F877A with 10Mhz as per the notes and loaded the code straight from the example zip file - still no joy - hence the return of the board to the supplier.


I have a PICPLC16 board which have the serial ethernet "module" on board. I'm very interested in what you might come up with, codewise, when it comes to that. It should be doable in PBP but I don't think it's an easy task.

Henrik, to be honest I doubt if I'll be using the serial ethernet board in my current PBP project as it seems far too complicated to do using PBP. I might have to move over to the dark side and either use Mikrobasic or Swordfish, both of which have built in library files for these functions. - have a look at this article http://www.sfcompiler.co.uk/wiki/pmwiki.php?n=SwordfishUser.Ethernet

It appears that the syntax of Swordfish is easy to pick up, but the limitations in the devices it supports compared to MikroBasic or PBP might be it's down fall... this is no doubt reflected in it's cheaper price tag. What it needs is for Darrel to come up with a nice little include file that handles the spi interfacing etc so all that is required is to enter the network settings in the main program, and away you go :) Now if it could upload html to the PIC in the same way as Mikrobasic does and run as a webserver, that would be cool ;)

malc-c
- 8th January 2011, 12:50
Well guys I have some good news.

I returned the original board for replacement, which arrived this morning. I downloaded the article from the ME website and changed the IP settings to reflect that of my home network. Re-compiled the code and loaded into the PIC. Turned off the EP5 and connected the serial ethernet card which was then hooked up to my router. Turned the EP5 back on, got connectivity shown by the LEDS on the card and then tried pinging the IP - Success :)

Opened up a web browser, entered the IP and got the sample page to control an LED on port B - worked a treat. I've also tried this with 8, 10 and 12 mhz crystals - all work fine

So it would seem that the original board was faulty :(

Now I just need to work out how to implement a web server in PBP - My research (google :) ) through up some conversions on posts dating back to 2007 - but most of the links have long since disappeared. - The challenge awaits !

malc-c
- 8th January 2011, 13:28
I may of asked this question before, but us there a way of replicating the html code within PBP.

This is the example from Mikrobasic (apologies to the owners of this forum for listing competitors products)


program home_auto

include "home_auto_utils"

dim myMacAddr as byte[6] ' my MAC address
myIpAddr as byte[4] ' my IP address
gwIpAddr as byte[4] ' gateway (router) IP address
ipMask as byte[4] ' network mask (for example : 255.255.255.0)
dnsIpAddr as byte[4] ' DNS server IP address

main:
ADCON1 = 0x0F ' no analog inputs
CMCON = 0x07 ' turn off comparators

PORTB.0 = 0
TRISB.0 = 0 ' set PORTB.B0 as output (rele control pin)

indexPage =
"<html><head><title>mikroElektronika</title></head><body>"+
"<h3 align=center>MikroElektronika Home Automatization System</h3>"+
"<form name="+Chr(34)+"input"+Chr(34)+" action="+Chr(34)+"/"+Chr(34)+" method="+
Chr(34)+"get"+Chr(34)+"><table align=center width=200 bgcolor=#4974E2 border=2><tr>"+
"<td align=center colspan=2><font size=4 color=white><b>Heat Control</b></font>"+
"</td></tr><tr><td align=center bgcolor=#4974E2><input name="+Chr(34)+"tst1"+
Chr(34)+" width=60 type="+Chr(34)+"submit"+Chr(34)+" value="+Chr(34)+"ON"+
Chr(34)+"></td><td align=center bgcolor=#FFFF00><input name="+Chr(34)+"tst2"+
Chr(34)+" type="+Chr(34)+"submit"+Chr(34)+" value="+Chr(34)+"OFF"+Chr(34)+
"></td></tr></table></form></body></html>"

myMacAddr[0] = 0x00 myMacAddr[1] = 0x14 myMacAddr[2] = 0xA5
myMacAddr[3] = 0x76 myMacAddr[4] = 0x19 myMacAddr[5] = 0x3F

ipMask[0]=255 ipMask[1]=255 ipMask[2]=255 ipMask[3]=0
myIpAddr[0]=192 myIpAddr[1]=168 myIpAddr[2]=0 myIpAddr[3]=120
gwIpAddr[0]=192 gwIpAddr[1]=168 gwIpAddr[2]=0 gwIpAddr[3]=1
dnsIpAddr[0]=192 dnsIpAddr[1]=168 dnsIpAddr[2]=0 dnsIpAddr[3]=1


' starts ENC28J60 with: reset bit on PORTC.F0, CS bit on PORTC.F1,
' my MAC & IP address, full duplex
Spi_Init()
' full duplex, CRC + MAC Unicast + MAC Broadcast filtering
Spi_Ethernet_Init (PORTC, 0, PORTC, 1,
myMacAddr, myIpAddr, Spi_Ethernet_FULLDUPLEX)

' dhcp will not be used here, so use preconfigured addresses
SPI_Ethernet_confNetwork(ipMask, gwIpAddr, dnsIpAddr)

while true ' do forever
SPI_Ethernet_doPacket() ' process incoming Ethernet packets
wend
end.

As you can see the html is contained within quotes (I had to edit this slightly as the forum displayed the page rather than the source code - the complete code is between the HTML tags), but with the heading "indexpage". Can this be replicated in PBP - or is it a case that Mikrobasic is a much higher level language and PBP simply isn't suitable for this sort of functionality ?

mister_e
- 8th January 2011, 18:28
All doable, but I don't think you can access MikroE library to see how they implement their SPI_Ethernet_x routine huh?

I think you could probably port some "open source" routine, say C18, to PBP.

If your time is short, sometimes you may want to stick with a language that already work and learn it rather than reinvent the wheel.

malc-c
- 8th January 2011, 19:36
Steve, nice to see you back :)

Yeah... I must admit I really want to sick with PBP but with other languages already having library files for this sort of thing, then it is tempting to move over to the dark side !

mister_e
- 9th January 2011, 01:06
You can change a car transmission with a flat screwdriver, but it need a bit more work ;) Same goes for compiler. Compilers are just tools, there's nothing bad using more than one, use the one that fits your requirement.

If you feel you may have some requirement for it in the future, I suggest you to port some existing libraries. There's some in C18 on Microchip's website. THere's also some pre-canned solution like ethernet module (Rabbit, Charon, Lantronix etc etc).