本帖最后由 miaohuafeng8 于 2013-9-26 09:59 编辑
#include <pic.h> //使用单片机PIC16F628A
//#define uchar unsigned char
unsigned char hour1;
//#define SOFT_VALUE 250 //(140-1) //0-69
void tmint(void) //PIC16F628A定时器0初始化函数4ms计一次
{
T0CS=0;
PSA=0;
PS2=0;
PS1=1; //1:16 4M晶振16us计一次 250次计时4ms
PS0=1;
//PSA=0;
TMR0=6;
T0IF=0;
T0IE=1;
//PEIE=0;
}
void t1mint() //PIC16F688定时器1初始化函数 总计500ms
{
TMR1H=0x0B;
TMR1L=0xDC;
T1CON=0b00110101; //(FOSC/4) 8分频
PSA=0;
TMR1IF=0;
TMR1IE=1;
PEIE=1;
}
void interrupt ISR(void)
{
if(T0IF&&T0IE) //4M晶振计时4ms
{
T0IF=0;
TMR0=6;
}
if(TMR1IF&&TMR1IE) //4M晶振计时500ms
{
TMR1IF=0;
TMR1H=0x0B;
TMR1L=0xDC;
if(PORTA==0xff)
PORTA=0x00;
if(PORTA==0x00)
PORTA=0xff;
}
}
void IO_init(void)
{
CMCON=0x00;
TRISA=0x00;
TRISB=0x00;
}
unsigned char eeprom_read(unsigned char EEAddr)
{
unsigned char ReEEPROMread;
EEADR = EEAddr;
//EECON1 |= 0b00000001;
//CFGS = 0; //PIC18F中要用到
//GIE = 0;
RD = 1;
//NOP();
hour1 = EEDATA; //EEPROM的读数据
RD = 0;
//GIE = 1;
return hour1 ;
}
void eeprom_write_shuju(unsigned char EEAddr2)
{
EEADR = EEAddr2; //EEPROM的地址
EEDATA = 1; //EEPROM的写数据
//EEPGD = 0;
//CFGS = 0; //PIC18F中要用到
GIE = 0;
WREN = 1;
EECON2 = 0x55; //必须按这样的时序
EECON2 = 0xAA;
WR = 1;
while(WR==1); //等待写操作完成
GIE = 1;
WREN = 0;
}
void vari()
{
hour1=0;
}
main()
{
IO_init();
vari();
tmint();
t1mint();
GIE=1;
eeprom_write_shuju(0);
while(1)
{
}
}
简单程序如上
往EEPRoM地址0写入数据1,却有时能写进去有时写不进去,不知是何原因
使用单片机型号PIC16F628A |