我用的是HT46R23,這個程序是讀寫EEPRPM 24C16,用的是IIC來做,在用STEP OVER按鍵時讀出的數據是正確。但按下F5(全速)時讀出的數據就不正確了。高手指點。。。
main{ _clrwdt(); writer_eeprom(0xa0,0x00,0x8a,0x05); //連繼寫5個數據,地址00,
read_eeprom(0xa0,0x00,0x07); //read 5 datas ,addr=00 } } //************************************ // writer_eeprom //************************************ void writer_eeprom(unsigned char pag,unsigned char eeprom_addr,unsigned eeprom_data,unsigned char eeprom_cnt) { unsigned char i,data; eeprom_star(); eeprom_send(pag|=write_comm); answer_ack(); eeprom_send(eeprom_addr); answer_ack(); for(i=0;i<eeprom_cnt;i++){ eeprom_send(eeprom_data); answer_ack(); } eeprom_stop(); } //************************************ // read_eeprom //************************************ void read_eeprom(unsigned char pag,unsigned char eeprom_addr,unsigned char eeprom_cnt){ unsigned char i ; eeprom_star(); eeprom_send(pag|=write_comm); answer_ack();
eeprom_send(eeprom_addr); answer_ack(); eeprom_star(); eeprom_send(read_comm); answer_ack(); for(i=0;i<eeprom_cnt;i++){ eeprom_read(); eeprom_congregate=eeprom_da; } eeprom_stop(); } //************************************ // eeprom_read //************************************ void eeprom_read(){ unsigned char i,data; eeprom_da=0; rom_sda_c=1; for(i=0;i<8;i++){ rom_clk=1; _c=0; if(rom_sda)_c=1; _rlc(&eeprom_da); rom_clk=0; } rom_sda_c=0; _nop(); rom_sda=0; rom_clk=1; _nop(); _nop(); rom_clk=0; } //************************************ // eeprom_star //************************************ void eeprom_star(){ rom_clk=1; _nop(); _nop(); rom_sda=1;
_nop(); _nop(); rom_sda=0; _nop(); _nop(); _nop(); rom_clk=0; } //************************************ // eeprom_stop //************************************ void eeprom_stop(){ rom_sda=0; _nop(); _nop(); rom_clk=1; _nop(); _nop(); rom_sda=1; _nop(); _nop(); rom_clk=0; } //************************************ // eeprom_send //************************************ void eeprom_send(unsigned char eeprom_data_buff){ unsigned char i; for(i=0;i<8;i++){ _rlc(&eeprom_data_buff); if(_c)rom_sda=1; else rom_sda=0; rom_clk=1; _nop(); rom_clk=0; } } //************************************ // answer_ack //************************************ void answer_ack(){ rom_sda=0; _nop(); rom_sda_c=1; _nop(); rom_clk=1; wait_ack: if(rom_sda)goto wait_ack; rom_sda_c=0; rom_clk=0; }
|