Discussion in "8051 Discussion Forum" started by    Help    Dec 8, 2008.
Sat Dec 20 2008, 08:40 am
#21
Dear Fren,

Thank for your infor..Ya, it is very good news for me. I already tested using AT89C51RD2 in proteus. It doesn't work. I'm not sure whether to initial the SPI function correct not and the coding. Please can you verify for me? I get reference code from ATMEL.

void SPI_Init(void)
{
	SPCON |= 0x10; /* Master mode */
	P1_1=1; /* enable master */
	SPCON |= 0x02; /* Fclk Periph/128 */
	SPCON &= ~0x08; /* CPOL=0; transmit mode example */
//	SPCON |= 0x04; /* CPHA=1; transmit mode example */
	SPCON &= ~0x04; /* CPHA=0; transmit mode example */
	IEN1 |= 0x04; /* enable spi interrupt */
	SPCON |= 0x40; /* run spi */
	EA=1; /* enable interrupts */
}

void it_SPI(void) interrupt 9 /* interrupt address is 0x004B */
{
	switch( SPSTA ) 
	{
		case 0x80:
			serial_data=SPDAT; 
			transmit_completed=1;
		break;
		case 0x10:
			/* put here for mode fault tasking */
		break;
		case 0x40:
			/* put here for overrun tasking */
		break;
	}
}

unsigned char SpiByte(unsigned char Data)
{
		SPDAT=Data; 
		while(!transmit_completed);
			transmit_completed = 0; 
			SPDAT=0x00; 
		while(!transmit_completed);
			transmit_completed = 0;

	return serial_data; 
}


I just change the SPCON |= 0x04; ro SPCON &= ~0x04;. The coding is it ok?

May i know is it all the AT89Cxx SPI is coding is same? I mean if we using the coding AT89C51RD2 for AT89S8252, AT89S8253, AT89S53 and so on...is it work?

Thank you.
Sun Dec 21 2008, 02:28 am
#22
well almost same i guess.. just go through datasheet, normally its same

you have to be very careful about CPOL and CPHA bits. reason being that these two bits decide at what time your data will be sampled according to selected transition.

and in function SPI Byte.. why are u making register as 0? which u should not do.. was it like that in Atmel's code?
Sun Dec 21 2008, 09:20 am
#23
Dear Fren,

you have to be very careful about CPOL and CPHA bits. reason being that these two bits decide at what time your data will be sampled according to selected transition.
MMC/SD SPI are using CPOL=0, CPHA=0 mode0. I thought once we initialize the uC it will self transition according the mode0 timing diagram to Send or Receive data. So we need not worry the MOSI/MISO and CLK any more rite?

and in function SPI Byte.. why are u making register as 0? which u should not do..
May i know what do you mean making register as 0? Is it because of this the uC can't interface MMC in proteus?

Thank you,..


[ Edited Sun Dec 21 2008, 09:26 am ]
Sun Dec 21 2008, 03:32 pm
#24
I think you did not see page 68 of controller datasheet. Fig 32 and Fig. 33 explains all the four cases with combination of CPOL and CPHA bits.

you can see clearly the state of clock changes when you change the bits.

the selection of clock depends on slave peripheral.

And if you see the MMC Specification document on page 85, you will see the timing diagram. where u can find similarity between clocks. and it seems that CPOL and CPHA bits both should be high to get required clock. try making them 1 and see if it works.
Mon Dec 22 2008, 11:12 pm
#25
Dear fren,

I have tried up but don't know how to make the SPI interrupt routine function work. As i know the CPOL and CPHA is mode0 is when the clock is Hi the MMC/SD would read data and uC would start read from SD.

#include <at89c51xd2.h>
/*-------------------- SPI ---------------------- -----------------*/
Sfr ( SPCON, 0xC3 );
Sfr ( SPSTA, 0xC4 );
Sfr ( SPDAT, 0xC5 );


#include <REG8253.H>
/*------------------------------------------------
P1 (0x90) Bit Registers
------------------------------------------------*/
sbit SS   = 0x94;       /* SPI: SS - Slave port select input */
sbit MOSI = 0x95;       /* SPI: MOSI - Master data output, slave data input */
sbit MISO = 0x96;       /* SPI: MISO - Master data input, slave data output */
sbit SCK  = 0x97;       /* SPI: SCK - Master clock output, slave clock input */
/*------------------------------------------------
SPSR (0xAA) Bit Values - Reset Value = 0000.0000
------------------------------------------------*/
#define WCOL_   0x40    /* SPI Write Collision Flag: 1=Collision */
#define SPIF_   0x80    /* SPI Interrupt Flag */
/*------------------------------------------------
SPCR (0xD5) Bit Values - Reset Value = 0000.01XX
------------------------------------------------*/
#define SPR0_   0x01    /* SPI Clock Rate Select bit 0 */
#define SPR1_   0x02    /* SPI Clock Rate Select bit 1 */
                        /* 00 = Fosc / 4   */
                        /* 01 = Fosc / 16  */
                        /* 10 = Fosc / 64  */
                        /* 11 = Fosc / 128 */

#define CPHA_   0x04    /* SPI Clock Phase */
#define CPOL_   0x08    /* SPI Clock Polarity */
#define MSTR_   0x10    /* SPI Master/Slave Select: 0=Slave, 1=Master */
#define DORD_   0x20    /* SPI Data Order: 0=MSB First, 1=LSB First */
#define SPE_    0x40    /* SPI Enable: 0=Disabled, 1=Enabled */
#define SPIE_   0x80    /* SPI Interrupt Enable: 0=Disabled, 1=Enabled */


From this 2 library i found out there is a lot of different for SPI. I really don't know how to test the result. Does the proteus work for C51RD2? After C51RD2 tested does it run in S8253 also? If i just use it directly. Because my concer is after tested using C51RD2 i don't have chance to do real time experiment due to i don't have this burner and uC at this moment and if i can't find out the result from proteus also. Another concern is S8253 Proteus don't have this S8253.llb so we can't test it there. So, have to wait until the supplier get stock.

But i still intend want to learn how to use it SPI using interrupt. I'm losed again...

Please advice,
Thank you..


[ Edited Mon Dec 22 2008, 11:17 pm ]
Wed Dec 24 2008, 02:34 am
#26
can you build up a simple program for sending spi data with interrupt?
I will tell you where u went wrong.

Create a sample circuit in proteus, Connect SPI debugger on SPI port of controller in proteus. send some data and check if it works.

waiting for response
Wed Dec 24 2008, 10:08 am
#27
Dear Fren,

Ya, i have create a sample cicuit in proteus and c code. Please have a look..
The coding when simulate in Keil it will working fine. the SPI interrupt is functional but once VSM with proteus then the interrupt will no work OK.

Thank for your reply,..

Attachment
Thu Dec 25 2008, 02:38 am
#28
i tried your circuit but i dont see even single byte sent.. and in keil it just waits. seems like SPI not running at all..

one problem i found in circuit is your connections. SCK and DIN is shorted with each other.

I am trying to figure out the problem.. which version of proteus are u using?
Thu Dec 25 2008, 07:39 am
#29
Dear fren,

I'm using Proteus7.2sp6. Ya, in keil it look like waiting but when we just using keil to run debug, it work. The SPI Interrupt is work.

Thank you..

Thu Dec 25 2008, 07:24 pm
#30
the problem is SPI is not getting initialized, pin is just high, even the SS pin is not getting activated.

only way to check is real hardware... well i think you continue with the old program atlest there is success. i willl check this out by the time.

Get Social

Information

Powered by e107 Forum System

Downloads

Comments

carpinteyrowrl
Fri Apr 19 2024, 02:51 pm
DonaldJAX
Fri Apr 19 2024, 01:08 pm
Lewisuhakeply
Thu Apr 18 2024, 06:00 pm
Darrellciz
Thu Apr 18 2024, 11:07 am
Charlessber
Thu Apr 18 2024, 09:29 am
BartonSem
Thu Apr 18 2024, 04:56 am
DonaldKnown
Thu Apr 18 2024, 12:24 am
utaletxcyw
Wed Apr 17 2024, 10:21 am