用HI-TECH C寫的使用PIC12C508讀寫93LC4_第1頁
用HI-TECH C寫的使用PIC12C508讀寫93LC4_第2頁
用HI-TECH C寫的使用PIC12C508讀寫93LC4_第3頁
用HI-TECH C寫的使用PIC12C508讀寫93LC4_第4頁
用HI-TECH C寫的使用PIC12C508讀寫93LC4_第5頁
已閱讀5頁,還剩2頁未讀 繼續免費閱讀

下載本文檔

版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領

文檔簡介

1、用HI-TECH C寫的使用PIC12C508讀寫93LC4    用HI-TECH C寫的使用PIC12C508讀寫93LC46范例程式/*  * Processer : Microchip PIC12C508        * * Compiler : Hi-TECH PICC 8.00 PL2       * * Writer : Jason Kuo   

2、60;       * * Description : It can read/write 93LC46 (64 x 16-bit organization) * */  static volatile unsigned char RTCC 0x01;static volatile unsigned char TMR0 0x01;static volatile unsigned char PCL 0x02;static volatile unsigned char STATUS 0x03;static&#

3、160;         unsigned char FSR 0x04;static volatile unsigned char OSCCAL 0x05;static volatile unsigned char GPIO 0x06;static          unsigned char control OPTION 0x00;static volatile unsigned char control TRIS 0x06

4、;/* STATUS bits */static bit  GPWUF (unsigned)&STATUS*8+7;static bit PA0 (unsigned)&STATUS*8+5;static bit  TO (unsigned)&STATUS*8+4;static bit  PD (unsigned)&STATUS*8+3;static bit  ZERO (unsigned)&STATUS*8+2;static bit DC (unsigned)&STATUS*8+1;static bit C

5、ARRY (unsigned)&STATUS*8+0;/* OPTION bits */#define  GPWU (1<<7)#define  GPPU (1<<6)#define  T0CS (1<<5)#define  T0SE (1<<4)#define  PSA (1<<3)#define  PS2 (1<<2)#define  PS1 (1<<1)#define  PS0 (1<<0)/* 

6、     OSCCAL bits     */static volatile bit     CAL3    (unsigned)&OSCCAL*8+7;static volatile bit     CAL2    (unsigned)&OSCCAL*8+6;static volatile bit     CAL1

7、60;   (unsigned)&OSCCAL*8+5;static volatile bit     CAL0    (unsigned)&OSCCAL*8+4;static volatile bit GP5 (unsigned)&GPIO*8+5;static volatile bit GP4 (unsigned)&GPIO*8+4;static volatile bit GP3 (unsigned)&GPIO*8+3;static volatile bit G

8、P2 (unsigned)&GPIO*8+2;static volatile bit GP1 (unsigned)&GPIO*8+1;static volatile bit GP0 (unsigned)&GPIO*8+0;#define CONFIG_ADDR 0xFFF/* code protection */#define MCLREN  0xFFFF / memory clear enable#define MCLRDIS  0xFFEF / memory clear disable/*watchdog*/#define WDTEN 

9、 0xFFFF / watchdog timer enable#define WDTDIS  0xFFFB / watchdog timer disable/* code protection */#define PROTECT  0xFFF7 / protect the program code#define UNPROTECT 0xFFFF / do not protect the program code/*osc configurations*/#define EXTRC    0xFFFF / external resistor/ca

10、pacitor#define INTRC  0xFFFE / internal#define XT  0xFFFD / crystal/resonator#define LP  0xFFFC / low power crystal/resonator/* 93LC46 I/O pin define */#define CS  GP0  /Chip Select#define CLK GP1  /Serial Data Clock#define DI GP2  /Serial Data Input#define DO GP4&

11、#160; /Serial Data Outputvoid Delay(unsigned int counter);void Pulse(void);void StartBit(void);void EWEN(void);void EWDS(void);extern void Write93LC46(unsigned char Offset_Addr, unsigned int tx_data);  extern unsigned int Read93LC46(unsigned char Offset_Addr);void InitPIC(void);#define CLRWDT()

12、 asm(" clrwdt")#define SLEEP()  asm(" sleep")#define _mkstr1(x) #x#define _mkstr(x) _mkstr1(x)#define _CONFIG(x) asm("tpsect config,class=CONFIG,delta=2");   asm("tglobaltconfig_word");    asm("config_word");    asm

13、("tdw "_mkstr(x)#define _IDLOC(w)       asm("tpsect idloc,class=IDLOC,delta=2");    asm("tglobaltidloc_word");     asm("idloc_word");     asm("tirpct_arg," _mkstr(w);  &#

14、160;  asm("tdw 0&_arg&h");     asm("tendm")_CONFIG(MCLRDIS & WDTDIS & EXTRC & PROTECT);/*-  Function : Delay            Input : unsigned int (counter)     

15、    Output : None            Description : Delay routine         if counter=1  delay 35us , if counter=10 delay 134us,  if counter=100 delay 1.12ms, These delay is base on int

16、ernal 4MHz      -*/      void Delay(unsigned int counter)   while(counter>0) counter-;                          

17、             /*-  Function : Pulse            Input : None         Output : None          

18、  Description : Send a pulse (10) to Serial Data Clock(CLK)        -*/      void Pulse(void)    CLK = 1;    Delay(25);    CLK = 0;/*-  Function : StartBit     &

19、#160;      Input : None         Output : None            Description :    1. Set Chip Select(CS) = 1 (high) 2. Set a Start Bit(1) to Serial Data Input(DI)-*/

20、60;     void StartBit(void)    CS = 1;    DI = 1;    Pulse();/*-  Function : EWEN            Input : None         Output : None &#

21、160;          Description :  ERASE/WRITE Enable -*/      void EWEN(void)    unsigned char i,temp;    StartBit();             

22、     /* 1 */    temp = 0x80;                        /* 0011xxxx ,(opcode:00, Address:11xxxx) */    for(i=0; i<8; i+)   

23、60;     if(0x30 & temp)            DI = 1;        else            DI = 0;        Pulse(); &

24、#160;      temp >>= 1;        CS = 0;/*-  Function : EWDS            Input : None         Output : None      

25、      Description :  ERASE/WRITE Disable -*/      void EWDS(void)    unsigned char i;    StartBit();                  /* 1 */

26、    DI = 0;                       /* 0000xxxx, (opcode:00, Address:00xxxx) */    for(i=0; i<8; i+)        Pulse(); &#

27、160;  CS = 0;/*-  Function : Write93LC46            Input : unsigned char Offset Address, unsigned int tx_data         Output : None           &#

28、160;Description :  Write 16bits data in to 93LC46 Offset Address -*/      void Write93LC46(unsigned char Offset_Addr, unsigned int tx_data)    unsigned char Addr, i;    unsigned int temp;    EWEN();    Sta

29、rtBit();                  /* 1 */    Offset_Addr=Offset_Addr&0x3F; /* 6bits address */    Addr = Offset_Addr + 0x40;          /* 01AAAAA

30、A ,(opcode:01, address:AAAAAA) */    temp = 0x0080;    for(i=0; i<8; i+)         if(Addr & temp)            DI = 1;        else  

31、;          DI = 0;        Pulse();        temp >>= 1;        temp = 0x8000;            &#

32、160;         /* DDDDDDDDDDDDDDDD(16bits data)*/    for(i=0; i<16; i+)         if(tx_data & temp)            DI = 1;     

33、60;  else            DI = 0;        Pulse();        temp >>= 1;        CS = 0;    EWDS();/*-  Function : Read93LC4

34、6            Input : unsigned char Offset Address Output : unsigned int            Description :  Read 16bits data from 93LC46 offset address -*/     

35、 unsigned int Read93LC46(unsigned char Offset_Addr)    unsigned char Addr, i, temp;    unsigned int  rx_data;    StartBit();                   /* 1 */ 

36、0;  Offset_Addr = Offset_Addr&0x3F; /* 6bits address */    Addr = Offset_Addr + 0x80;           /* 10AAAAAA ,(opcode:10, address:AAAAAA) */    temp = 0x80;    for(i=0; i<8; i+)   

37、0;     if(Addr & temp)            DI = 1;        else            DI = 0;        Pulse();  

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯系上傳者。文件的所有權益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
  • 4. 未經權益所有人同意不得將文件中的內容挪作商業或盈利用途。
  • 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
  • 6. 下載文件中如有侵權或不適當內容,請與我們聯系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論