在PIC单片机上如何做RAM和ROM完整性检查?

demi的头像

RAM测试时有点类似下面的程序(当然你可以修改它来使这个测试更全面些)——这里测试的RAM的数量是特定于PIC16C76的,如果要用于其它单片机型号,你需要查阅数据手册来确定相应型号的存储器范围。

#include

volatile unsigned char INDF @ 0x0; // not defined in pic.h
main()
{

        IRP = 0;                /* select bank 0/1 */

        FSR = 0x20;             /* check bank 0 */
        do {
                INDF = 0x55;
        } while(++FSR != 0x80);

        FSR = 0x20;
        do {
                if(INDF != 0x55)
                        for(;;);
        } while(++FSR != 0x80);

        FSR = 0xA0;                // check bank 1
        do {
                INDF = 0x55;
        } while(++FSR != 0xF0);    // avoid the common area, already checked

        FSR = 0xA0;
        do {
                if(INDF != 0x55)
                        for(;;);
        } while(++FSR != 0xF0);

        IRP = 1;        /* select bank 2/3 */

        FSR = 0x10;     /* bank 2 RAM starts at 110 */
        do {
                INDF = 0x55;
        } while(++FSR != 0x70);  // last address is 170 (common is 170-17F)

        FSR = 0x10;
        do {
                if(INDF != 0x55)
                        for(;;);
        } while(++FSR != 0x70);  

        FSR = 0x90;                 // bank 3 RAM, 190-1EF
        do {
                INDF = 0x55;
        } while(++FSR != 0xF0);

        FSR = 0x90;
        do {
                if(INDF != 0x55)
                        for(;;);
        } while(++FSR != 0xF0);
}

ROM的校验和就要看情况而定了。并不是所有的PIC单片机都能够读它自己的程序存储器的。部分Flash的型号是可以读的,但是PIC16C76是不能够读的,请查看相应单片机的数据手册来确定。

来源:Microchip