#include \"msp430x13x.h\" void nop(void) { _NOP(); _NOP(); _NOP(); } void start(void) { SCLEN &= (~(SDA+SCL)); _NOP(); _NOP(); _NOP(); SDAEN |= SDA; SCLEN |= SCL; return; } void stop(void) { SDAEN |= SDA; SCLEN &= (~SCL); SDAEN &= (~SDA); return; } unsigned char ControlByte(int AddressByte) { unsigned char temp; _BIC_SR(0X0001); //CLEAR SR \'C\' temp = (char)(AddressByte>>8); temp <<= 1 ; temp &= 0X0E; temp += Code; return temp; } int shout( unsigned char data ) { unsigned char mask,i,temp; unsigned char Value; Value = data; mask=0X80; for (i=0;i<=7;i++){ temp = (Value & mask); if( temp !=0 ){ SDAEN &= (~SDA); SCLEN &= (~SCL); if( !( SDAIN & SDA ) ){ return 1; } } else { SDAEN |= SDA ; SCLEN &= (~SCL); } mask>>=1; SCLEN |= SCL; } SDAEN &= (~SDA); SCLEN &= (~SCL); if ( !(SDAIN & SDA )){ // return 1; } SCLEN |= SCL; return 0; } // 写 Number 个字节到 E2 中 int WriteBlock(int number,unsigned char *dataadr,unsigned char *e2adr) { unsigned char *p1; int i; unsigned char temp; unsigned int aaa; //unsigned char *temp1; _DINT(); SDADAT &= (~(SCL+SDA)); start(); aaa = (int)(e2adr ); temp = ControlByte(aaa); shout(temp); shout( (int)(e2adr)); p1 = dataadr; for (i=1;i<=8;i++) { shout(*p1); p1++; } stop(); _EINT(); //Write a block use the address and back.. return 0; //if error return 1 right return 0 } // 写N * 8个字节 int WriteN8Byte(int Number) { unsigned char *e2adr,*dataadr; unsigned int i,j; //int WriteBlock(int number,unsigned char *dataadr,unsigned char *e2adr) dataadr = (char*)0x200; e2adr = (char*)0x00; for( i=1;i<=Number;i++){ WriteBlock(8,dataadr,e2adr); dataadr += 8; e2adr += 8; for (j=0;j<=3333;j++); } return 0; } //; Clock out an acknowledge bit (low). //; SCL expected low on entry. Return with SCL, SDA low. void Ack(void) { SDAEN |= SDA; SCLEN &= ~SCL; SCLEN |= SCL; } //; Clock out a negative acknowledge bit (high). //; SCL expected low on entry. Return with SCL low, SDA high.void Nak(void) { SDAEN &= ~SDA; //; NAK bit SCLEN &= ~SCL; //; raise clock SCLEN |= SCL; //; drop clock } //Read 8-bit one-byte data from E2 unsigned char shin(void) { int i; unsigned char temp; unsigned char Mask; SDAEN &= ~SDA; // make SDA an input temp =0; //Read 8-bit data for( i=1;i<=8;i++) { SCLEN &= ~SCL; //SCL = H Mask = 0; if (SDAIN &SDA) Mask = 1; else Mask =0; // Mask = SDAIN & SDA; Mask <<= (8 - i); temp ^= Mask; SCLEN |= SCL; } return temp; } // 24c256 512 Page 64 Byte 14-15bit Word Address!!! //************************************************************************** // Read N bytes into RAM // Parameter description: number the number to be read, dataadr, the address to read in RAM? e2adr, E2 address //****************************************************************** int ReadNByte(int number,unsigned char *dataadr,unsigned char *e2adr) { int i; unsigned char *p; unsigned char temp; unsigned char temp1; _DINT(); SDADAT &= (~(SCL+SDA)); p=dataadr; start(); temp = ControlByte((int)(e2adr )); shout(temp); shout((int)(e2adr )); start(); temp |= 0X01; shout(temp); temp1=0; for (i=1;i<=number;i++){ temp1 = shin(); *p++ = temp1; Ack(); } Nak(); stop(); _EINT(); return 0; }
You Might Like
Recommended ContentMore
Open source project More
Popular Components
Searched by Users
Just Take a LookMore
Trending Downloads
Trending ArticlesMore