Electronic Projects, forums and more.

Go Back   Electronic Circuits Projects Diagrams Free > Electronics Categories > Micro Controllers


Micro Controllers Discuss all aspects of micro controllers - building them, coding them, etc. All controllers are welcome - PIC, BASIC, Z8 Encore!, etc.

Reply
 
LinkBack Thread Tools Display Modes
Old 4th March 2007, 07:37 PM   (permalink)
Default at89c4051 spi code not functioning, any advice?

I have written this code for my mcp42010 digipot. The pot requires spi interface. I modified an i2c code to output sda, scl and cs. I have run my code in a sim. and it looks to be functioning as coded but I an not get the pot to activate. All of the timings with the com require a min 40ns timing. I think I have well exceeded the time min. requirement. there is no max accept for a 10mhz clock speed. I am sure my clock speed is well under 10mhz. I think my prolem is with timing or the order I send commands. here is my code, thanks for any help.

thanks, fiveten
Attached Files
File Type: txt ser_pot.txt (2.0 KB, 5 views)
fiveten is offline  
Old 5th March 2007, 12:43 AM   (permalink)
Default

Sad to say that SPI and I2C are very different animals. They are unrelated protocols and I would not expect you to be able to adapt I2C code to run an SPI peripheral.

RTFDS
Papabravo is offline  
Old 5th March 2007, 05:44 AM   (permalink)
Default

I was using general purpose i/o to send the data, I did change the code to reflect the requirements of spi. It looks to me that I need only three pins. serial in, a clock signal and a chip select. In the timing requirements most were min.40ns signals. I thought because of the crystal I was using (11.059mhz) and 1 mach. cycle = 12 crystal cycles. 1 mach cycle is around 1048ns I was well within the min. with no delays.

Maybe I should be asking if an at89c4051 is a good mc to be impementing an spi interface with? Is software spi not possible.

I was using code for i2c just for a base. I reconfigured it to what I have now. and as I read it, I feel it should work. I am just a noobe, I am learning new things everyday. I appreciate and value your help.

fiveten
fiveten is offline  
Old 5th March 2007, 10:58 AM   (permalink)
Default

OK..Most SPI peripherals require you to send data to them to tell them what is going on. So you would need Data Out(DOUT), Serial Clock(SCLK), Data In(DIN), and Chip Select(CS). You must read the datasheet very carefully to get the clock phase and polarity correct. You can bit bang this protocol but you need to read those datasheets with extraordinary care. Each transaction involves sending a byte and receiving a byte. In many cases the incoming data from the SPI peripheral, is thrown away.
Papabravo is offline  
Old 5th March 2007, 12:03 PM   (permalink)
Default

First of all I agree with Papabravo: Read the datasheet carefully.

Then:
Don't start from another program.
First write down what you need to do and try to convert it into a program. Use the timing diagram to help you on this one.
From what I saw in the datasheet you need to transfer 16 bits in one operation. That can't be that hard .
You need to send the higest bit first, so in my opinion you need to use RLC not RRC like you did.

I wonder how you made this codes:
Code:
potab equ 0dbh    ;enable pot 0 & 1
pota  equ 0d9h    ;enable pot 0
potb  equ 0dah    ;enable pot 1
Are you sure they are correct?
mcs51mc is offline  
Old 10th March 2007, 05:22 PM   (permalink)
Default

These are the pot enable commands. I am pretty sure they are right. I send the pot setting before I send the pot command. So Papabravo, I have to receive the bits from the slave or can I just leave the “so” open? I don’t need to know what it is.

Command byte:

Bit: 7 6 5 4 3 2 1 0
X X C0 C1 X X P1 P0

My Commands:

Write to pot 0 &1 (ab) = 11011011 = 0DBh
Write to pot 0 (a) = 11011001 = 0D9h
Write to pot 1 (b) = 11011010 = 0DAh
fiveten is offline  
Old 12th March 2007, 12:17 PM   (permalink)
Default

In the high nibble you use "11" as don't care.
In the low nibble you use "10" as don't care.
Why is that?
Why not always use "00" or "11"?

Did you read and understand part "5.0 Serial Interface" from the datasheet?
Where did you read that you have to receive bits from the slave? With that device it's a one way communication. You always WRITE data into the MCP42010. There is no command READ data, so don't expect to receive something from the MCP42010.

The "SO" line is for: Daisy chaining multiple devices. If you have only one device, forget about the "SO" line (see part 5.4 of the datasheet).
mcs51mc is offline  
Old 12th March 2007, 12:21 PM   (permalink)
Angry

Quote:
Originally Posted by fiveten
I send the pot setting before I send the pot command.
Again, read the datasheet!!!
Page 18 figure 5.1 What do you see??!!??

Clock 1 to 8: Command byte
Clock 9 to 16: Data byte

High bit first!! You need RLC, not RRC

Good luck
mcs51mc is offline  
Old 13th March 2007, 06:21 AM   (permalink)
Default

I was asking about the "so" because Papabravo mentioned something about having it connected and disregarding the returned data.

As far as the high and low nibble I was just making an easy pattern I could follow in my simulator. I also did not think of it like you mentioned. I should keep it standard.

I see what you are saying about the clock. I was thinking that the datasheet was just showing the 16 clocks and the data shifted in: 15 14 ..............1 0,
I thought I had to shift bit 7 of the command last and bit 0 of the pot setting first. I guess if I read through the datasheet completely I would have understood without wasting your time.

mcs51mc, I do appreciate your help and I thank you for your time.

fiveten
fiveten is offline  
Reply

Bookmarks

Thread Tools
Display Modes



Similar Threads
Title Starter Forum Replies Latest
Need some help with a code provided by ATMEL ikalogic Micro Controllers 1 23rd January 2007 03:46 PM
AT89C4051 assembly code compiler question fiveten Micro Controllers 5 26th March 2006 07:37 PM
Code not working..help Electrix Micro Controllers 15 26th September 2005 12:12 PM
Tough assembly program for the PIC16F84 asmpic Micro Controllers 34 3rd December 2004 07:50 PM
An error in pic16f84a, why? Zener_Diode Micro Controllers 6 11th April 2004 03:55 AM



All times are GMT. The time now is 08:19 AM.


Electronic Circuits  |  Learning Electronics
Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.

eXTReMe Tracker