给ICC C编程软件自带函数库搞晕!好垃圾的写EEPROM子程序! 写EEPROM子程序 自带函数库: int EEPROMwrite( int location, unsigned char byte) { unsigned char oldSREG;
EEAR = location;
EEDR = byte;
oldSREG = SREG; SREG &= ~0x80; // disable interrupt, 这里看出在整个写过程中中断都是禁止的,EEPROM的编程时间是8448个周期。在这么长时间内禁止中断,什么中断实时处理都废了。 EECR |= 0x04; // Set MASTER WRITE enable EECR |= 0x02; // Set WRITE strobe while (EECR & 0x02); // Wait until write is done
SREG = oldSREG; return 0; // return Success. // Could be expanded so that // the routine checks that the address // is within the range of the chip. }
修改后的程序: int EEPROMwrite( int location, unsigned char byte) {
EEAR = location; EEDR = byte;
CLI(); EECR |= (1<<EEMWE); // Set MASTER WRITE enable EECR |= (1<<EEWE); // Set MASTER WRITE enable SEI(); while (EECR & (1<<EEWE)); // Wait until write is done
return 0; // return Success. // Could be expanded so that // the routine checks that the address // is within the range of the chip. } |