⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 parx.c

📁 这是《并行口编程指南》的附盘的源代码 很有参考价值哦
💻 C
📖 第 1 页 / 共 2 页
字号:
	if( !isxdigit(temp2) ) 		{
				printf("\nError in source file. Aborting");
				fclose(fp1);
				return 0;
					}
			ch = chartoi(temp1);
			temp1=ch;
			ch=chartoi(temp2);
			temp2=ch;
			ch = 16*temp1 + temp2;
chk_sum=chk_sum + ch;
/*printf("csum=%x", chk_sum);*/
if(chk_sum !=0)
				{
				printf("\nChecksum Error... Aborting");
				fclose(fp1);
				return 0;
				}

}



return count;

}


int erase_chip(void)
{
unsigned char temp;

/*to erase chip put sequence of P3.3 P3.4 P3.5 P3.7
				1    0    0    0
*/

/*then apply 12V to RST pin*/
/*then pulse P3.2 low for 10 ms*/
/*put RST to 5 V again*/

port_2=port_2 | P33;
port_2=port_2 & ~(P34);
port_2=port_2 & ~(P35);
port_2=port_2 & ~(P37);

/*prepare the control bits of P3.3, P3.4, P3.5, P3.7*/
port_3=port_3 | P33_CON | P34_CON | P35_CON | P37_CON; 


outportb(dport, port_2);

pulse_c2();

outportb(dport, port_3);
pulse_c3();

v12_on(); /*set RST to 12V*/

/*now generate a pulse on P3.2*/

port_2=port_2 & ~(P32);
outportb(dport, port_2);
pulse_c2();



delay(100);

port_2=port_2 | P32;
outportb(dport, port_2);
pulse_c2();


v5_on(); /*put RST to 5 Volts now*/



return 1;

}

int burn_verify_bytes(void)
{
unsigned char temp, low_temp, high_temp;
int program_length;
/*put P3.3 P3.4 P3.5 and P3.7 to 0111*/
/*apply data to the port0*/
/*put RST to 12V*/
/*pulse P3.2 for 2 ms*/
/*RST to 5V*/
/*P3.3, P3.4, P3.5 P3.7 to 0011*/
/*read data back and compare*/


for(program_length=0; program_length<MEMORY; program_length++)
/*for(;;)*/
{
curr_address=program_length;
port_2=port_2 & ~(P33);
port_2=port_2 | P34 | P35 | P37;
outportb(dport, port_2);
pulse_c2();

/*this puts the data into the data latch but the latch is yet to
be enabled*/
port_0=ram[program_length];
outportb(dport, port_0);
pulse_c0();


/*put 12V on the RST pin*/
v12_on();

/*enable the data on the data pins*/
port_1=port_1 | ENB_DATA;
outportb(dport, port_1);
pulse_c1();

delay(1);
/*now generate a pulse on P3.2*/
port_2=port_2 & ~(P32);
outportb(dport, port_2);
pulse_c2();

delay(10);

port_2=port_2 | P32;
outportb(dport, port_2);
pulse_c2();

/*now disable the data latch which sent the programming data*/
port_1=port_1 & ~(ENB_DATA);
outportb(dport, port_1);
pulse_c1();

delay(1);
/*put 5V on the RST pin*/
v5_on();

/*set P3.3 P3.4 P3.5 and P3.7 to read back the data*/
port_2=port_2 & ~(P33) & ~(P34);
port_2=port_2 | P35 | P37;
outportb(dport, port_2);
pulse_c2();

/*read low nibble*/
port_1=port_1 | ENB_LOW;
outportb(dport, port_1);
pulse_c1();
low_temp=inportb(sport);

/*read high nibble*/
port_1=port_1 & ENB_HIGH;
outportb(dport, port_1);
pulse_c1();
high_temp=inportb(sport);

temp= (high_temp & 0xf0) | ( (low_temp >>4) & 0x0f);
temp=temp ^ 0x88;
if(temp != ram[program_length])

{
error_byte=temp;
printf("\nError in program verify at address %X (hex). Aborting...", program_length);
printf("\nProgram data %X, read back data %X\n", ram[program_length], temp);
shutdown();
return 0;
}

/*generate a pulse on XTAL1 to increment the address counter*/
port_2= port_2 | XTAL1;
outportb(dport, port_2);
pulse_c2();

port_2=port_2 & ~(XTAL1);
outportb(dport, port_2);
pulse_c2();

}
return 1;

}

int v0_on(void)  /*apply 0 volts on RST pin*/
{
unsigned char port_val, temp;


port_1=port_1 | PULSE_0; /*PULSE_0 is 0x06*/
outportb(dport, port_1);

/*now generate a low pulse on C1* */
pulse_c1();

return 1;
}

int v5_on(void)  /*apply 5 volts on the RST pin*/
{
unsigned char port_val, temp;

port_1=port_1 | PULSE_0;       /*PULSE_0 is 0000 0110*/
port_1=port_1 & PULSE_5;     /*PULSE_5 is 1111 1101*/
outportb(dport, port_1);

pulse_c1();

return 1;
}

int v12_on(void) /*apply 12 Volts on the RST pin*/
{
unsigned char port_val, temp;

port_1=port_1 | PULSE_0;       /*PULSE_0   is 0000 0110*/
port_1=port_1 & PULSE_12;     /*PULSE_12 is 1111 1001*/
outportb(dport, port_1);
pulse_c1();

return 1;
}

int power_off(void) /*remove power to the ZIF socket and float all pins*/
{
unsigned char port_val, temp;

v0_on(); /* put 0 volts on the RST pin*/

port_1=port_1 & SW_12V_OFF;
port_1=port_1 & SW_5V_OFF;
outportb(dport, port_1); /*switch off power to the ZIF socket*/
pulse_c1();

return 1;
}

int power_on(void) /* apply power and put 0 V on RST and XTAL1 pin*/
		   /*rest all pins float and wait for more than 10 ms*/
		   /*then put RST and P32 to 5V*/
{
unsigned char port_val, temp;

v0_on(); /*put 0V on RST pin*/

port_1=port_1 | SW_12V_ON; /*prepare port_1*/
port_1=port_1 | SW_5V_ON;


port_3=port_3 | XTAL1_CON; /*enable the XTAL1 control pin of port3*/

temp=inportb(cport); /*prepare pulse on c1*/
temp=temp | 0x02;

outportb(dport, port_1); /*output for port_1*/

outportb(cport, temp); /*pulse c1 low and back to high*/
temp=temp & 0xfd;
outportb(cport, temp);

outportb(dport, port_3); /*this puts XTAL1 control to 1 and hence XTAL1 to 0*/
temp=temp | 0x08; /*pulse c3 low and high again*/
outportb(cport, temp);
temp=temp & 0xf7;
outportb(cport, temp);


delay(2);
sleep(1);

/*now put RST and P3.2 to 5 Volts*/

port_2=port_2 | P32;     /*to make P32 high*/
port_3=port_3 | P32_CON; /*to enable P32 to reach the ZIF pin*/

v5_on();                 /*this makes RST 5V*/

outportb(dport, port_2);
pulse_c2();

outportb(dport, port_3);
pulse_c3();

return 1;

}

void shutdown(void)
{
port_3=0;
outportb(dport, port_3);
pulse_c3();

port_1=0;
outportb(dport, port_1);
pulse_c1();

port_2=0;
outportb(dport, port_2);
pulse_c2();

return;
}

main(argc, argv)
int argc;
char *argv[];
{
time_t start, endt;
unsigned long temp;
int byte_value, return_val, total_bytes;

printf("\n\n\n\tAtmel AT89C2051 uC Programmer: Version 1.0\n");
printf("\t------------------------------------------\n");
printf("\t\t    Dhananjay V. Gadre");
printf("\n\t\t       April 1997.\n"); /* 30th April 1997*/

if(argc != 2)
		{printf("\nError... Specify Intelhex source filename. Aborting");
		printf("\nFormat: AtmelP intelhex_sourcefile");
		exit(-1);
		}

if((fp1=fopen(argv[1], "r")) == NULL)
		{printf("\nError...Cannot open source file. Aborting");
		exit(-1);
		}

return_val=initialize(); /*Initialize the printer adapter port*/
if(return_val == 0) {printf("\nLPT1 not available. Aborting...");
fclose(fp1);
exit(-1);
}
printf("\nLPT1 DATA port address = %X (hex)", dport);

power_off();


printf("\nReading Intelhex source file...:");

return_val=fill_buffer();
if(return_val == 0)
		{
		exit(0);
		}
printf("\nIntel hex file %s read successful. Total bytes read =%d", argv[1], return_val);

fclose(fp1);

printf("\n\nPut the target microcontroller in the ZIF socket and press a key\n");

getch();
power_on();
printf("\nErasing the Microcontroller...\n");
erase_chip(); 
printf("\nProgramming and Verifying...\n");

return_val=burn_verify_bytes();

if(return_val == 0)

{printf("\nFailed to program the controller at address: %X (hex)\n", curr_address);
printf("Program value: %X\n", ram[curr_address]);
printf("Verify value: %X\n", error_byte);
exit(-1);
     }

printf("\nMicrocontroller programmed and verified");
power_off();
shutdown();

printf("\nNow remove the controller from the ZIF socket and press a key");
getch();

return 1;


}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -