PIC12c508讀寫(xiě)93LC46 PICC程序
PIC12c508讀寫(xiě)93LC46程序
/************************************************************
*Processer:MicroChipPIC12C508*
*Compiler:Hi-TECHPICC8.00PL2*
*Writer:JasonKuo*
*Description:ItCANread/write93LC46(64x16-bitorganization)*
*************************************************************/
staticvolatileunsignedcharRTCC@0x01;
staticvolatileunsignedcharTMR0@0x01;
staticvolatileunsignedcharPCL@0x02;
staticvolatileunsignedcharSTATUS@0x03;
staticunsignedcharFSR@0x04;
staticvolatileunsignedcharOSCCAL@0x05;
staticvolatileunsignedcharGPIO@0x06;
staticunsignedcharcontrolOPTION@0x00;
staticvolatileunsignedcharcontrolTRIS@0x06;
/*STATUSbits*/
staticbitGPWUF@(unsigned)&STATUS*8+7;
staticbitPA0@(unsigned)&STATUS*8+5;
staticbitTO@(unsigned)&STATUS*8+4;
staticbitPD@(unsigned)&STATUS*8+3;
staticbitZERO@(unsigned)&STATUS*8+2;
staticbitDC@(unsigned)&STATUS*8+1;
staticbitCARRY@(unsigned)&STATUS*8+0;
/*OPTIONbits*/
#defineGPWU(1<<7)
#defineGPPU(1<<6)
#defineT0CS(1<<5)
#defineT0SE(1<<4)
#definePSA(1<<3)
#definePS2(1<<2)
#definePS1(1<<1)
#definePS0(1<<0)
/*OSCCALbits*/
staticvolatilebitCAL3@(unsigned)&OSCCAL*8+7;
staticvolatilebitCAL2@(unsigned)&OSCCAL*8+6;
staticvolatilebitCAL1@(unsigned)&OSCCAL*8+5;
staticvolatilebitCAL0@(unsigned)&OSCCAL*8+4;
staticvolatilebitGP5@(unsigned)&GPIO*8+5;
staticvolatilebitGP4@(unsigned)&GPIO*8+4;
staticvolatilebitGP3@(unsigned)&GPIO*8+3;
staticvolatilebitGP2@(unsigned)&GPIO*8+2;
staticvolatilebitGP1@(unsigned)&GPIO*8+1;
staticvolatilebitGP0@(unsigned)&GPIO*8+0;
#defineCONFIG_ADDR0xFFF
/*codeprotection*/
#defineMCLREN0xFFFF//memoryclearenable
#defineMCLRDIS0xFFEF//memorycleardISAble
/*watchdog*/
#defineWDTEN0xFFFF//watchdogtimerenable
#defineWDTDIS0xFFFB//watchdogtimerdisable
/*codeprotection*/
#definePROTECT0xFFF7//protecttheprogramcode
#defineUNPROTECT0xFFFF//donotprotecttheprogramcode
/*oscconfigurations*/
#defineEXTRC0xFFFF//externalresistor/caPACitor
#defineINTRC0xFFFE//internal
#defineXT0xFFFD//crystal/resonator
#defineLP0xFFFC//lowpowercrystal/resonator
/*93LC46I/OPINdefine*/
#defineCSGP0//ChipSelect
#defineCLKGP1//SerialDataCLOCk
#defineDIGP2//SerialDataInput
#defineDOGP4//SerialDataOutput
voidDelay(unsignedintcounter);
voidPulse(void);
voidStartBit(void);
voidEWEN(void);
voidEWDS(void);
externvoidWrite93LC46(unsignedcharOffset_Addr,unsignedinttx_data);
externunsignedintRead93LC46(unsignedcharOffset_Addr);
voidInitPIC(void);
#defineCLRWDT()asm("clrwdt")
#defineSLEEP()asm("sleep")
#define___mkstr1(x)#x
#define___mkstr(x)___mkstr1(x)
#define__CONFIG(x)asm("tpsectconfig,class=CONFIG,delta=2");
asm("tglobaltconfig_word");
asm("config_word");
asm("tdw"___mkstr(x))
#define__IDLOC(w)asm("tpsectidloc,class=IDLOC,delta=2");
asm("tglobaltidloc_word");
asm("idloc_word");
asm("tirpct__arg,"___mkstr(w));
asm("tdw0&__arg&h");
asm("tendm")
__CONFIG(MCLRDIS&WDTDIS&EXTRC&PROTECT);
/*----------------------------------------------------
Function:Delay
Input:unsignedint(counter)
Output:None
Description:Delayroutine
ifcounter=1delay35us,ifcounter=10delay134us,
ifcounter=100delay1.12ms,
Thesedelayisbaseoninternal4MHz
------------------------------------------------------*/
voidDelay(unsignedintcounter)
{
while(counter>0)counter--;
}
/*----------------------------------------------------
Function:Pulse
Input:None
Output:None
Description:Sendapulse(10)toSerialDataClock(CLK)
------------------------------------------------------*/
voidPulse(void)
{
CLK=1;
Delay(25);
CLK=0;
}
/*----------------------------------------------------
Function:StartBit
Input:None
Output:None
Description:
1.SetChipSelect(CS)=1(high)
2.SetaStartBit(1)toSerialDataInput(DI)
------------------------------------------------------*/
voidStartBit(void)
{
CS=1;
DI=1;
Pulse();
}
/*----------------------------------------------------
Function:EWEN
Input:None
Output:None
Description:ERASE/WRITEEnable
------------------------------------------------------*/
voidEWEN(void)
{
unsignedchari,temp;
StartBit();/*1*/
temp=0x80;/*0011xxxx,(opcode:00,Address:11xxxx)*/
for(i=0;i<8;i++){
if(0x30&temp)
DI=1;
else
DI=0;
Pulse();
temp>>=1;
}
CS=0;
}
/*----------------------------------------------------
Function:EWDS
Input:None
Output:None
Description:ERASE/WRITEDisable
------------------------------------------------------*/
voidEWDS(void)
{
unsignedchari;
StartBit();/*1*/
DI=0;/*0000xxxx,(opcode:00,Address:00xxxx)*/
for(i=0;i<8;i++)
Pulse();
CS=0;
}
/*----------------------------------------------------
Function:Write93LC46
Input:unsignedcharOffsetAddress,unsignedinttx_data
Output:None
Description:Write16bitsdatainto93LC46OffsetAddress
------------------------------------------------------*/
voidWrite93LC46(unsignedcharOffset_Addr,unsignedinttx_data)
{
unsignedcharAddr,i;
unsignedinttemp;
EWEN();
StartBit();/*1*/
Offset_Addr=Offset_Addr&0x3F;/*6bitsaddress*/
Addr=Offset_Addr+0x40;/*01AAAAAA,(opcode:01,address:AAAAAA)*/
temp=0x0080;
for(i=0;i<8;i++){
if(Addr&temp)
DI=1;
else
DI=0;
Pulse();
temp>>=1;
}
temp=0x8000;/*DDDDDDDDDDDDDDDD(16bitsdata)*/
for(i=0;i<16;i++){
if(tx_data&temp)
DI=1;
else
DI=0;
Pulse();
temp>>=1;
}
CS=0;
EWDS();
}
/*----------------------------------------------------
Function:Read93LC46
Input:unsignedcharOffsetAddress
Output:unsignedint
Description:Read16bitsdatafrom93LC46offsetaddress
------------------------------------------------------*/
unsignedintRead93LC46(unsignedcharOffset_Addr)
{
unsignedcharAddr,i,temp;
unsignedintrx_data;
StartBit();/*1*/
Offset_Addr=Offset_Addr&0x3F;/*6bitsaddress*/
Addr=Offset_Addr+0x80;/*10AAAAAA,(opcode:10,address:AAAAAA)*/
temp=0x80;
for(i=0;i<8;i++){
if(Addr&temp)
DI=1;
else
DI=0;
Pulse();
temp>>=1;
}
rx_data=0x0000;/*DDDDDDDDDDDDDDDD(16bitsdata)*/
for(i=0;i<16;i++){
Pulse();
if(DO)
rx_data|=0x0001;
if(i<15)
rx_data<<=1;
}
CS=0;
return(rx_data);
}
voidInitPIC(void)
{
OPTION=(GPWU|GPPU|PS2|PS1|PS0);
TRIS=0x10;
CS=0;
CLK=0;
DI=0;
}
/*Mainroutine*/
voidmain(void)
{
unsignedcharaddr;
unsignedintrx_buf;
InitPIC();
/*Readawordthen+1andwritebackto93LC46*/
for(addr=0;addr<10;addr++)
{
rx_buf=Read93LC46(addr);
rx_buf=rx_buf+1;
Write93LC46(addr,rx_buf);
}
}