📄 parx.c
字号:
/* parx.c */
/*
Atmel AT89C2051 Programmer
Dhananjay V. Gadre
*/
#include<stdio.h>
#include<conio.h>
#include<dos.h>
#include<process.h>
#include<time.h>
#include<alloc.h>
#include<ctype.h>
#include<stdlib.h>
#include<string.h>
/*port addresses of the parallel adapter*/
unsigned int dport, sport, cport;
/*these ports control data to the uC, voltage to the ZIF socket,*/
/*control the tri-state buffers respectively*/
unsigned char port_0, port_1, port_2, port_3, error_byte;
#define MEMORY 2048 /*last address of the target controller memory*/
/*int MEMORY=2048;*/
/*the Intelhex file has lines of code. each line begins with a : */
/*next is number of bytes in the line*/
#define LL 1 /*offset in the hex file where the line length is stored*/
#define ADDR 3 /*offset in the hex file where the destination address is stored*/
#define ZEROS 7
#define CODE_ST 9 /*offset of the beginning of the code*/
/* status port */
#define pin_11 0x80
#define pin_10 0x40
#define pin_12 0x20
#define pin_13 0x10
#define pin_15 0x08
/* control port */
#define pin_1 0x01
#define pin_14 0x02
#define pin_16 0x04
#define pin_17 0x08
/*define to be used with port_1*/
#define ENB_DATA 0X80 /*OR this*/
#define ENB_LOW 0X20 /*OR this*/
#define ENB_HIGH 0xdf /*1101 1111, AND this*/
#define SW_12V_ON 0x08 /*OR this*/
#define SW_5V_ON 0x10 /*OR this*/
#define SW_12V_OFF 0xF7 /*AND this*/
#define SW_5V_OFF 0xEF /*AND this*/
#define PULSE_0 0x06 /* OR this 0000 0110 */
#define PULSE_5 0xFD /* AND this 1111 1101 */
#define PULSE_12 0xF9 /* AND this 1111 1001 */
/*define to be used with port_2*/
#define XTAL1 0x80
#define P32 0x40
#define P33 0x20
#define P34 0x10
#define P35 0x08
#define P37 0x04
/*defines to be used with port_3*/
#define XTAL1_CON 0x80
#define P32_CON 0x40
#define P33_CON 0x20
#define P34_CON 0x10
#define P35_CON 0x08
#define P37_CON 0x04
/*local global variables*/
unsigned char ram[2100];
unsigned int curr_address;
FILE *fp1;
/* local routines */
int initialze(void); /* initialzes the external hardware */
int fill_buffer(void); /*read the intelhex format file & fill up the internal buffer */
int chk_programmer(void); /*check if the programmer is connected and if +12V is ON*/
int erase_chip(void);
int burn_verify_bytes(void);
int v0_on(void); /*apply 0volts on RST pin*/
int v5_on(void); /*apply 5volts onm the RST pin*/
int v12_on(void); /*apply 12V on the RST pin*/
int power_off(void); /*remove power to the ZIF socket and float all pins*/
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*/
void shutdown(void); /*routine to disable everything
and to shutdown power so that the chip can be removed*/
/*routines to generate pulse on each of the 4 control port pins*/
void pulse_c0(void);
void pulse_c1(void);
void pulse_c2(void);
void pulse_c3(void);
void pulse_c0(void)
{
unsigned char temp;
temp=inportb(cport);
temp=temp & 0xfe;
outportb(cport, temp);
delay(1);
temp=temp | 0x01;
outportb(cport, temp);
delay(1);
}
void pulse_c1(void)
{
unsigned char temp;
temp=inportb(cport);
temp=temp | 0x02;
outportb(cport, temp);
delay(1);
temp=temp & 0xfd;
outportb(cport, temp);
delay(1);
}
void pulse_c2(void)
{
unsigned char temp;
temp=inportb(cport);
temp=temp & 0xfb;
outportb(cport, temp);
delay(1);
temp=temp | 0x04;
outportb(cport, temp);
delay(1);
}
void pulse_c3(void)
{
unsigned char temp;
temp=inportb(cport);
temp=temp | 0x08;
outportb(cport, temp);
delay(1);
temp=temp & 0xf7;
outportb(cport, temp);
delay(1);
}
char chartoi(char val)
{
unsigned char temp;
temp = toupper(val);
if(temp>0x39) {temp = temp -0x37;}
else {temp=temp-0x30;}
return temp;
}
int initialize(void)
{
dport = peek(0x40, 8);
sport=dport+1;
cport=dport+2;
if(dport ==0) return 0;
outportb(dport, 0);
outportb(cport, 0x05); /*all cport outputs high, except C0*/
outportb(cport, 0x0a); /*all cport pins are low, except C0*/
outportb(cport, 0x05); /*all cport outputs high, except C0*/
port_0=0;
port_1=0;
port_2=0;
port_3=0;
return 1;
}
int fill_buffer(void) /*read the intelhex format file & fill up the
internal buffer */
{
unsigned char ch, temp4, temp1, temp2, temp3;
unsigned char chk_sum=0, buf[600], num[10];
unsigned int line_length, address, line_temp, tempx, count=0;
count=0;
while(!feof(fp1) )
{
chk_sum=0;
/* check if start of line = ':' */
fgets(buf, 600, fp1);
tempx=strlen(buf);
/*printf("\n\nString length=%d\n", tempx);*/
/*printf("\n\n%s", buf);*/
if( buf[0] != ':') {printf("\nError... Source file not in Intelhex format. Aborting");
fclose(fp1);
return 0;
}
/* convert the next 2 characters to a byte which equals line length */
temp1=buf[LL];
temp2=buf[LL+1];
if( !isxdigit(temp1) ) {
printf("\nError in source file. Aborting");
fclose(fp1);
return 0;
}
if( !isxdigit(temp2) ) {
printf("\nError in source file. Aborting");
fclose(fp1);
return 0;
}
temp4 = chartoi(temp1);
chk_sum=chk_sum + 16*temp4;
line_length=(unsigned int)temp4;
temp4=chartoi(temp2);
chk_sum=chk_sum + temp4;
line_length = 16*line_length + (unsigned int)temp4;
/*printf("Entries=%d ", line_length);*/
if(line_length ==0) {
return count;
}
temp1=buf[ADDR];
temp2=buf[ADDR+1];
temp3=buf[ADDR+2];
temp4=buf[ADDR+3];
if( !isxdigit(temp1) ) {
printf("\nError in source file. Aborting");
fclose(fp1);
return 0;
}
if( !isxdigit(temp2) ) {
printf("\nError in source file. Aborting");
fclose(fp1);
return 0;
}
if( !isxdigit(temp3) ) {
printf("\nError in source file. Aborting");
fclose(fp1);
return 0;
}
if( !isxdigit(temp4) ) {
printf("\nError in source file. Aborting");
fclose(fp1);
return 0;
}
ch = chartoi(temp1);
temp1=ch;
ch = chartoi(temp2);
temp2=ch;
chk_sum = chk_sum + 16*temp1 + temp2;
ch = chartoi(temp3);
temp3=ch;
ch = chartoi(temp4);
temp4=ch;
chk_sum = chk_sum + 16*temp3 + temp4;
address = 0x1000 * (unsigned int)temp1 + 0x100 * (unsigned int)temp2 + 0x10*(unsigned int)temp3 + (unsigned int)temp4;
/*printf("Start Address=%x hex, %x\n", address, MEMORY);*/
if( address > MEMORY )
{
printf("\nError in source file. Bad address. Aborting");
fclose(fp1);
return 0;
}
/*check for the next byte. It has to be 00 **/
temp1=buf[ZEROS];
temp2=buf[ZEROS+1];
if( !isxdigit(temp1) ) {
printf("\nError in source file. Aborting");
fclose(fp1);
return 0;
}
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;
if(ch != 0)
{
printf("\nError... Source file not in Intelhex format. Aborting");
fclose(fp1);
return 0;
}
/* now read bytes from the file & put it in buffer*/
for(line_temp=0; line_temp<line_length; line_temp++)
{
temp1=buf[2*line_temp+CODE_ST];
temp2=buf[2*line_temp+CODE_ST+1];
if( !isxdigit(temp1) ) {
printf("\nError in source file. Aborting");
fclose(fp1);
return 0;
}
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;
if(address > MEMORY)
{
printf("\nError in source file. Bad address. Aborting");
fclose(fp1);
return 0;
}
/* printf("%X ",ch);*/
ram[address]=ch;
address++;
count++;
}
/*get the next byte. this is the chksum */
temp1=buf[2*line_length+CODE_ST];
temp2=buf[2*line_length+CODE_ST+1];
if( !isxdigit(temp1) ) {
printf("\nError in source file. Aborting");
fclose(fp1);
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -