//This test program shows how to use flash //and uses the procedure 'write_flash' and // the function 'write_flash' //The flash from 0 to FLASH_SIZE is filled and tested //by numbers. By the end the blinkind of the LED //connected to B7 indicates success and blinking //of all LEDs connected to B0-B7 indicates failure program flash; var i, a, err: char; const FLASH_SIZE = 64; //the memory bank 0 should be selected procedure write_flash( addr, data: char ); begin //Write flash EEADR := addr; EEDATA := data; set_bit( STATUS, 'RP0' ); set_bit( EECON1, 'WREN' ); EECON2 := 0x55; EECON2 := 0xAA; set_bit( EECON1, 'WR' ); while EECON1&2 do nop; clear_bit( EECON1, 'WREN' ); clear_bit( STATUS, 'RP0' ); end; function read_flash( addr: char ): char; begin //Read flash EEADR := addr; set_bit( STATUS, 'RP0' ); set_bit( EECON1, 'RD' ); clear_bit( STATUS, 'RP0' ); asm movf EEDATA, W end; //a short delay procedure delay; var i, j: char; begin i := 0; while i < 250 do begin i := i + 1; j := 0; while j < 200 do j := j + 1; end; end; begin err := 0; disable_interrupt( 'GIE' ); set_bit( STATUS, 'RP0' ); set_tris_b( 0 ); clear_bit( STATUS, 'RP0' ); output_port_b( 0 ); for i := 0 to FLASH_SIZE-1 do begin write_flash( i, i*2 ); a := read_flash( i ); if a <> i*2 then err := 1; end; while 1 do begin if err then begin output_port_b( 0 ); delay; output_port_b( 255 ); end; else begin output_high_port_b( 7 ); delay; output_low_port_b( 7 ); end; delay; end; end.