F32xSPI0EEPROMPolledMode.docx
《F32xSPI0EEPROMPolledMode.docx》由会员分享,可在线阅读,更多相关《F32xSPI0EEPROMPolledMode.docx(17页珍藏版)》请在冰豆网上搜索。
F32xSPI0EEPROMPolledMode
//-----------------------------------------------------------------------------
//F32x_SPI0_EEPROM_Polled_Mode.c
//-----------------------------------------------------------------------------
//Copyright2006SiliconLaboratories,Inc.
//
//
//ProgramDescription:
//
//ThisprogramaccessesaSPIEEPROMusingpolledmodeaccess.The'F32xMCU
//isconfiguredin4-wireSingleMasterMode,andtheEEPROMistheonly
//slavedeviceconnectedtotheSPIbus.Theread/writeoperationsare
//tailoredtoaccessaMicrochip4kBEEPROM25LC320.Therelevanthardware
//connectionsofthe'F32xMCUareshownhere:
//
//P0.0-SPISCK(digitaloutput,push-pull)
//P0.1-SPIMISO(digitalinput,open-drain)
//P0.2-SPIMOSI(digitaloutput,push-pull)
//P0.3-SPINSS(digitaloutput,push-pull)
//P0.4-UARTTXD(digitaloutput,push-pull)
//P0.5-UARTRXD(digitalinput,open-drain)
//P2.2-LED(digitaloutput,push-pull)
//
//
//HowToTest:
//
//Method1:
//1)Downloadthecodetoa'F32xdevicethatisconnectedasabove.
//2)Runthecode.TheLEDwillblinkfastduringthewrite/read/verify
//operations.
//3)Iftheverificationpasses,theLEDwillblinkslowly.Ifitfails,
//theLEDwillbeOFF.
//
//Method2(optional):
//1)Downloadcodetoa'F32xdevicethatisconnectedasabove,and
//alsoconnectedtoaRS232transceiver.
//2)ConnectastraightserialcablefromtheRS232transceivertoaPC.
//3)OnthePC,openHyperTerminal(oranyotherterminalprogram)andconnect
//totheCOMportatand8-N-1
//4)HyperTerminalwillprinttheprogressofthewrite/readoperation,andin
//theendwillprintthetestresultaspassorfail.Additionally,ifthe
//verificationpasses,theLEDwillblinkslowly.Ifitfails,theLEDwill
//beOFF.
//
//
//Target:
C8051F320/1
//Toolchain:
KeilC517.50/KeilEVALC51
//CommandLine:
None
//
//Release1.0
//-InitialRevision(PKC)
//-18MAY2006
//
//-----------------------------------------------------------------------------
//Includes
//-----------------------------------------------------------------------------
#include//SFRdeclarations
#include//printfisdeclaredhere
//-----------------------------------------------------------------------------
//16-bitSFRDefinitionsforthe'F32x
//-----------------------------------------------------------------------------
sfr16TMR2=0xCC;//Timer2lowandhighbytestogether
//-----------------------------------------------------------------------------
//User-definedtypes,structures,unionsetc
//-----------------------------------------------------------------------------
#ifndefBYTE
#defineBYTEunsignedchar
#endif
#ifndefUINT
#defineUINTunsignedint
#endif
//-----------------------------------------------------------------------------
//GlobalConstants
//-----------------------------------------------------------------------------
#defineBAUDRATE115200//BaudrateofUARTinbps
#defineSYSCLK24000000//InternaloscillatorfrequencyinHz
//Microchip25AA320SlaveEEPROMParameters
#defineF_SCK_MAX2000000//MaxSCKfreq(Hz)
#defineT_NSS_DISABLE_MIN500//MinNSSdisabletime(ns)
#defineEEPROM_CAPACITY4096//EEPROMcapacity(bytes)
//EEPROMInstructionSet
#defineEEPROM_CMD_READ0x03//ReadCommand
#defineEEPROM_CMD_WRITE0x02//WriteCommand
#defineEEPROM_CMD_WRDI0x04//ResetWriteEnableLatchCommand
#defineEEPROM_CMD_WREN0x06//SetWriteEnableLatchCommand
#defineEEPROM_CMD_RDSR0x05//ReadStatusRegisterCommand
#defineEEPROM_CMD_WRSR0x01//WriteStatusRegisterCommand
sbitLED=P2^2;//LED='1'meansON
//-----------------------------------------------------------------------------
//FunctionPrototypes
//-----------------------------------------------------------------------------
voidPCA0_Init(void);
voidOSCILLATOR_Init(void);
voidPORT_Init(void);
voidTIMER2_Init(void);
voidUART0_Init(void);
voidSPI0_Init(void);
voidInit_Device(void);
voidDelay_us(BYTEtime_us);
voidDelay_ms(BYTEtime_ms);
voidEEPROM_Write(UINTaddress,BYTEvalue);
BYTEEEPROM_Read(UINTaddress);
//-----------------------------------------------------------------------------
//main()Routine
//-----------------------------------------------------------------------------
voidmain(void)
{
UINTaddress;//EEPROMaddress
BYTEtest_byte;//Usedasatemporaryvariable
Init_Device();//Initializeshardwareperipherals
//ThefollowingcodewilltesttheEEPROMbyperformingwrite/read/verify
//operations.Thefirsttestwillwrite0xFFstotheEEPROM,andthe
//secondtestwillwritetheLSBsoftheEEPROMaddresses.
//FillEEPROMwith0xFF's
LED=1;
printf("Fillingwith0xFF's...\n");
for(address=0;address{
test_byte=0xFF;
EEPROM_Write(address,test_byte);
//PrintstatustoUART0
if((address%16)==0)
{
printf("\nWriting0x%04x:
%02x",address,(UINT)test_byte);
LED=~LED;
}
else
printf("%02x",(UINT)test_byte);
}
//VerifyEEPROMwith0xFF's
printf("\n\nVerifying0xFF's...\n");
for(address=0;address{
test_byte=EEPROM_Read(address);
//PrintstatustoUART0
if((address%16)==0)
{
printf("\nVerifying0x%04x:
%02x",address,(UINT)test_byte);
LED=~LED;
}
else
printf("%02x",(UINT)test_byte);
if(test_byte!
=0xFF)
{
LED=0;
printf("Errorat%u\n",address);
while
(1);//Stophereonerror(fordebugging)
}
}
//FillEEPROMwithLSBofEEPROMaddresses
printf("\n\nFillingwithLSBofEEPROMaddresses...\n");
for(address=0;address{
test_byte=address&0xFF;
EEPROM_Write(address,test_byte);
//PrintstatustoUART0
if((address%16)==0)
{
printf("\nWriting0x%04x:
%02x",address,(UINT)test_byte);
LED=~LED;
}
else
printf("%02x",(UINT)test_byte);
}
//VerifyEEPROMwithLSBofEEPROMaddresses
printf("\n\nVerifyingLSBofEEPROMaddresses...\n");
for(address=0;address{
test_byte=EEPROM_Read(address);
//printstatustoUART0
if((address%16)==0)
{
printf("\nVerifying0x%04x:
%02x",address,(UINT)test_byte);
LED=~LED;
}
else
printf("%02x",(UINT)test_byte);
if(test_byte!
=(address&0xFF))
{
LED=0;
printf("Errorat%u\n",address);
while
(1);//Stophereonerror(fordebugging)
}
}
printf("\n\nVerificationsuccess!
\n");
while
(1)//Loopforever
{
LED=~LED;//FlashLEDwhendone(allverified)
Delay_ms(200);
}
}
//-----------------------------------------------------------------------------
//InitializationSubroutines
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
//PCA0_Init
//-----------------------------------------------------------------------------
//
//ReturnValue:
None
//Parameters:
None
//
//Thisfunctiondisablesthewatchdogtimer.
//
//-----------------------------------------------------------------------------
voidPCA0_Init(void)
{
PCA0MD&=~0x40;
PCA0MD=0x00;
}
//-----------------------------------------------------------------------------
//OSCILLATOR_Init
//-----------------------------------------------------------------------------
//
//ReturnValue:
None
//Parameters:
None
//
//Thisfunctioninitializesthesystemclockat24MHz(intosc->clkmul/2).
//
//-----------------------------------------------------------------------------
voidOSCILLATOR_Init(void)
{
inti=0;
CLKMUL=0x80;
for(i=0;i<20;i++);//Wait5usforinitialization
CLKMUL|=0xC0;
while((CLKMUL&0x20)==0);
CLKSEL=0x02;
OSCICN=0x83;
}
//-----------------------------------------------------------------------------
//PORT_Init
//-----------------------------------------------------------------------------
//
//ReturnValue:
None
//Parameters:
None
//
//ThisfunctionconfiguresthecrossbarandGPIOports.
//
//P0.0-SCK(SPI0),Push-Pull,Digital
//P0.1-MISO(SPI0),Open-Drain,Digital
//P0.2-MOSI(SPI0),Push-Pull,Digital
//P0.3-NSS(SPI0),Push-Pull,Digital
//P0.4-TX0(UART0),Push-Pull,Digital
//P0.5-RX0(UART0),Open-Drain,Digital
//P0.6-Unassigned,Open-Drain,Digital
//P0.7-Unassigned,Open-Drain,Digital
//P1.0-Unassigned,Open-Drain,Digital
//P1.1-Unassigned,Open-Drain,Digital
//P1.2-Unassigned,Open-Drain,Digital
//P1.3-Unassigned,Open-Drain,Digital
//P1.4-Unassigned,Open-Drain,Digital
//P1.5-Unassigned,Open-Drain,Digital
//P1.6-Unassigned,Open-Drain,Digital
//P1.7-Unassigned,Open-Drain,Digital
//P2.0-Skipped,Open-Drain,Digital(SwitchS2onTargetBoard)
//P2.1-Skipped,Open-Drain,Digital(SwitchS3onTargetBoard)
//P2.2-Skipped,Push-Pull,Digital(LEDD4onTargetBoard)
//P2.3-Skipped,Push-Pull,Digital(LEDD2onTargetBoard)
//
//-----------------------------------------------------------------------------
voidPORT_Init(void)
{
P0MDOUT=0x1D;
P2MDOUT=0x0C;
P2SKIP=0x0F;
XBR0=0x03;
XBR1=0x40;
}
//-----------------------------------------------------------------------------
//TIMER2_Init
//-----------------------------------------------------------------------------
//
//ReturnValue:
None
//Parameters:
None
//
//InitializesTimer2tobeclockedbySYSCLKforuseasadelaytimer.
//
//-----------------------------------------------------------------------------
voidTIMER2_Init(void)
{
CKCO