Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

at89c4051 spi code not functioning, any advice?

Status
Not open for further replies.

fiveten

Member
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
 

Attachments

  • ser_pot.txt
    2 KB · Views: 198
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
 
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
 
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.
 
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?
 
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
 
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).
 
fiveten said:
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:)
 
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
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top