#include"pic.h" #include"pic16f7x.h"
#define CS RC2 /*** PIC16F74 AT93C46 RC4--------DO RC5--------DI RC2--------CS RC3--------SK GND--------GND,ORG VDD--------VCC ***/
void MCU_init() { PORTC=0X2C; TRISC=0XD0; PORTD=0X00; TRISD=0X00; PORTE=0X00; TRISE=0X00; OPTION=0X42;//4MHZ OSC 8分频 TMR0=131; //1ms定时 SSPSTAT=0; SSPCON=0X31; } //93c46读写程序,使用SPI void Delay(void) { asm("nop"); asm("nop"); }
unsigned char OutPut(unsigned char SendData) { unsigned char temp; SSPBUF=SendData; //asm("nop"); //asm("nop"); while(STAT_BF==0) { asm("clrwdt"); } temp=SSPBUF; return(temp); }
void Ewen(void) { unsigned char temp; CS=1; Delay(); temp=0X09; OutPut(temp); temp=EWEN; //0x80; OutPut(temp); Delay(); CS=0; } void Ewds(void) { unsigned char temp; CS=1; Delay(); temp=0x02; OutPut(temp); temp=0x00; OutPut(temp); Delay(); CS=0;
}
void WriteByte(unsigned char WData,unsigned char Adress) { unsigned char wtemp; Ewen(); CS=1; Delay(); wtemp=0x02; OutPut(wtemp); wtemp=Adress|0x80; OutPut(wtemp); OutPut(WData); CS=0; Delay(); CS=1; while(!DI); CS=0; }
unsigned char ReadByte(unsigned char Adress) { unsigned char wrtemp,rtemp; CKP=1; CS=1; Delay(); wrtemp=0x03; OutPut(wrtemp); wrtemp=Adress&0x7f; OutPut(wrtemp); // asm()"nop"; CKP=0; asm("nop"); rtemp=OutPut(wrtemp); Delay(); CS=0; CKP=1; return(rtemp); } /*void WriteBytes(unsigned char *WriteData,unsigned char Number,unsigned char Adress){ unsigned char temp; Ewen(); while(Number!=0){ temp=*WriteData; WriteByte(temp,Adress); asm("nop"); asm("nop"); CS=1; asm("nop"); asm("nop"); while(DI==0){ asm("clrwdt"); } Delay(); CS=0; WriteData++; Adress++; Number--; } }*/
void ReadBytes(unsigned char *ReadData,unsigned char Number,unsigned char Adress) { //unsigned char temp; while(Number!=0) { asm("clrwdt"); *ReadData=ReadByte(Adress); ReadData++; Adress++; Number--; } }
void main(void) { MCU_init(); WriteByte(100,1); temp=0; temp=ReadByte(1); 读出来的是0XFF?? while(1); } |