problem write to pic16f1509 program memory

Status
Not open for further replies.

kidteam

Junior Member level 3
Joined
Dec 22, 2010
Messages
27
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Visit site
Activity points
1,420
sorry i am bad in English
i try modify program memory pic16f1509
to read, erase program memory work fine.
but when i write to it
i alway encounter error at first address
code
Code:
#include <htc.h>
__CONFIG(FOSC_XT&WDTE_OFF&PWRTE_OFF&MCLRE_OFF&CP_OFF&BOREN_OFF&CLKOUTEN_OFF&IESO_OFF&FCMEN_OFF);
__CONFIG(WRT_OFF&STVREN_OFF&LPBOR_OFF&LVP_OFF);
#define _XTAL_FREQ 3686400

#define KT_BOOT_START 0x1e00

void Send(unsigned char d);
void Init_UART(void);
unsigned char Get(void);
unsigned char s2h(unsigned char d);//chuyen tu ascii sang so hexa
void init_timer0(void);//dung de dem gio
void KT_Write(void);//ghi vao vung nho flash

unsigned char count,cnt;//cnt de dem szBuff, count de dem gio
unsigned char szBuff[64];//vung dem chua du lieu

void main() {
	char c,bOk;
	cnt=0;
	init_timer0();
	Init_UART();
	//sau khi reset thi se cho 15s
	//neu sau 15s ma nhan duoc 'K' thi dua vao mode bootload
	//gui lai 'Y'
	//neu khong thi gui lai 'N'
	bOk=0;
	Send('T');//bao hieu cho PC biet la da san sang
	while(1) {
		//cho 15s
		while(!TMR0IF) {
			if(RCIF) {//co ky tu nhan duoc
				c=Get();
				if(c=='K') {//neu nhan duoc K la dua vao mode bootloader
					bOk=1;
					break;
				}
			}
		}
		if(bOk)
			break;
		TMR0IF=0;
		++count;
		if(count==200)//het gio
			break;
	}
	if(bOk) {
		Send('Y');
	} else {
		Send('N');
	}
	//dang gui cho gui xong ky tu ket thuc da
	if(!bOk) {
		
		//chay chuong trinh cu
		__delay_ms(10);
		#asm
			goto _redirect
		#endasm
	}
	//thuc thi Bootloader
	while(1) {
		//lan luot lay chuong trinh moi va ghi vao bo nho
		
		c=Get();
		//===================================================
		//nhan du lieu moi va ghi vao bo nho
		//:-)
		switch(c) {
			case 'R'://bat dau dong du lieu
				cnt=0;
				break;
			case 'W'://ra lenh ghi vao flash
				KT_Write();
				break;
			default://tang bien dem va luu vao du lieu nhan duoc
				szBuff[cnt]=c;
				++cnt;
				break;
		}
	}
}


void Init_UART(void) {
	ANSELB=0;
	TRISB=0;
	ANSELA=ANSELC=0;
	TRISA=TRISC=0;
	TRISB7=0;
	TRISB5=1;
	//khoi tao EUART truyen
	//cho phep truyen
	TX9=0;//truyen 8 bit
	BRGH=0;
	BRG16=0;
	SPBRGL=5;
	SPBRGH=0;
	//9600
	TXEN=1;
	CREN=1;
	SYNC=0;
	SPEN=1;
	//
	SPEN=0;
	SPEN=1;
	//co the rut gon lai chi bang it lenh gan hon
	
}

unsigned char Get(void) {
	while(!RCIF);
	return RCREG;
}

void Send(unsigned char d) {
	while(!TXIF);
	TXREG=d;
}

void redirect(void) @ KT_BOOT_START { //voi pic16f1509 phai dam bao 1 vung nho lon hon 32W
	//nen ta phai ghi dummy vao vung nho start nay// ton bo nho 24 W vo ich
#asm
	PCLATH equ 0Ah
	PCL equ 02h
	
	; this must be 4 word in length (including return)
	movlw 0
	movwf PCLATH
	movwf PCL
	nop
	movlw 0
	movwf PCLATH
	movwf PCL
	nop
	movlw 0
	movwf PCLATH
	movwf PCL
	nop
	movlw 0
	movwf PCLATH
	movwf PCL
	nop
	movlw 0
	movwf PCLATH
	movwf PCL
	nop
	movlw 0
	movwf PCLATH
	movwf PCL
	nop
	movlw 0
	movwf PCLATH
	movwf PCL
	nop
	movlw 0
	movwf PCLATH
	movwf PCL
	nop
#endasm
}

unsigned char s2h(unsigned char d) {
	if(d>='A')
		return (d-'A'+10);
	return (d-'0');
}

void init_timer0(void) {
	T0CS=0;
	PSA=0;
	PS0=PS1=PS2=1;//1:256
	count=0;
	TMR0IF=0;
}

void KT_Write(void) {
	//ghi vung dem vao flash
	//cho nhan tiep dia chi can ghi
	unsigned char addrhh, addrll,addrhl, addrlh, addrh, addrl,i;
	addrhh=Get();
	addrhl=Get();
	addrlh=Get();
	addrll=Get();
	GIE=0;
	CFGS=0;//ghi vao flash khong ghi eeprom, config
	addrh=((s2h(addrhh))<<4)|(s2h(addrhl));
	PMADRH=addrh;
	addrl=((s2h(addrlh))<<4)|(s2h(addrll));
	PMADRL=addrl;
	if((0==addrh)&&(0==addrl)) {//page dau tien chua lenh nhay toi ham main cua chuong trinh moi
		PMADRL=0;//vung redirect
		PMADRH=0X1E;//ghi vao vung redirect
	}
	
	FREE=1;//lenh xoa
	WREN=1;
	PMCON2=0x55;
	PMCON2=0xaa;
	WR=1;
	#asm
		nop
		nop
	#endasm
	WREN=0;
	
	//xoa xong moi ghi du lieu vao
	PMADRH=addrh;
	PMADRL=addrl;
	if((0==addrh)&&(0==addrl)) {//page dau tien chua lenh nhay toi ham main cua chuong trinh moi
		PMADRL=0;//vung redirect
		PMADRH=0X1E;//ghi vao vung redirect
	}
	LWLO=1;//ghi vao latch

	FREE=0;//lenh ghi
	
	WREN=1;
	for(i=0;i<32;++i) {
		//Send(szBuff[0]);
		//Send(szBuff[1]);
		PMADRL=i;
		PMDATH=i;//s2h(0);//du lieu se ghi
		PMDATL=i;//s2h(1);
		//while(WR);
		PMCON2=0x55;
		PMCON2=0xaa;
		WR=1;
		#asm
			nop
			nop
		#endasm
		
	}
	/*
	PMADRL=0;
	PMDATH=0;//s2h(0);//du lieu se ghi
		PMDATL=0;//s2h(1);
		//while(WR);
		PMCON2=0x55;
		PMCON2=0xaa;
		WR=1;
		#asm
			nop
			nop
		#endasm
	//bat dau ghi vao flash
	*/
	PMADRH=addrh;
	PMADRL=addrl;
	if((0==addrh)&&(0==addrl)) {//page dau tien chua lenh nhay toi ham main cua chuong trinh moi
		PMADRL=0;//vung redirect
		PMADRH=0X1E;//ghi vao vung redirect
	}
	LWLO=0;
	PMCON2=0x55;
	PMCON2=0xaa;
	WR=1;
	#asm
		nop
		nop
	#endasm
	WREN=0;
	//gui lai thong bao cho PC biet la da ghi xong
	Send('O');
}
but code
Code:
#include <htc.h>
__CONFIG(FOSC_XT&WDTE_OFF&PWRTE_OFF&MCLRE_OFF&CP_OFF&BOREN_OFF&CLKOUTEN_OFF&IESO_OFF&FCMEN_OFF);
__CONFIG(WRT_OFF&STVREN_OFF&LPBOR_OFF&LVP_OFF);
#define _XTAL_FREQ 3686400

#define KT_BOOT_START 0x1e00

void Send(unsigned char d);
void Init_UART(void);
unsigned char Get(void);
unsigned char s2h(unsigned char d);//chuyen tu ascii sang so hexa
void init_timer0(void);//dung de dem gio
void KT_Write(void);//ghi vao vung nho flash

unsigned char count,cnt;//cnt de dem szBuff, count de dem gio
unsigned char szBuff[64];//vung dem chua du lieu

void main() {
	char c,bOk;
	cnt=0;
	init_timer0();
	Init_UART();
	//sau khi reset thi se cho 15s
	//neu sau 15s ma nhan duoc 'K' thi dua vao mode bootload
	//gui lai 'Y'
	//neu khong thi gui lai 'N'
	bOk=0;
	Send('T');//bao hieu cho PC biet la da san sang
	while(1) {
		//cho 15s
		while(!TMR0IF) {
			if(RCIF) {//co ky tu nhan duoc
				c=Get();
				if(c=='K') {//neu nhan duoc K la dua vao mode bootloader
					bOk=1;
					break;
				}
			}
		}
		if(bOk)
			break;
		TMR0IF=0;
		++count;
		if(count==200)//het gio
			break;
	}
	if(bOk) {
		Send('Y');
	} else {
		Send('N');
	}
	//dang gui cho gui xong ky tu ket thuc da
	if(!bOk) {
		
		//chay chuong trinh cu
		__delay_ms(10);
		#asm
			goto _redirect
		#endasm
	}
	//thuc thi Bootloader
	while(1) {
		//lan luot lay chuong trinh moi va ghi vao bo nho
		
		c=Get();
		//===================================================
		//nhan du lieu moi va ghi vao bo nho
		//:-)
		switch(c) {
			case 'R'://bat dau dong du lieu
				cnt=0;
				break;
			case 'W'://ra lenh ghi vao flash
				KT_Write();
				break;
			default://tang bien dem va luu vao du lieu nhan duoc
				szBuff[cnt]=c;
				++cnt;
				break;
		}
	}
}


void Init_UART(void) {
	ANSELB=0;
	TRISB=0;
	ANSELA=ANSELC=0;
	TRISA=TRISC=0;
	TRISB7=0;
	TRISB5=1;
	//khoi tao EUART truyen
	//cho phep truyen
	TX9=0;//truyen 8 bit
	BRGH=0;
	BRG16=0;
	SPBRGL=5;
	SPBRGH=0;
	//9600
	TXEN=1;
	CREN=1;
	SYNC=0;
	SPEN=1;
	//
	SPEN=0;
	SPEN=1;
	//co the rut gon lai chi bang it lenh gan hon
	
}

unsigned char Get(void) {
	while(!RCIF);
	return RCREG;
}

void Send(unsigned char d) {
	while(!TXIF);
	TXREG=d;
}

void redirect(void) @ KT_BOOT_START { //voi pic16f1509 phai dam bao 1 vung nho lon hon 32W
	//nen ta phai ghi dummy vao vung nho start nay// ton bo nho 24 W vo ich
#asm
	PCLATH equ 0Ah
	PCL equ 02h
	
	; this must be 4 word in length (including return)
	movlw 0
	movwf PCLATH
	movwf PCL
	nop
	movlw 0
	movwf PCLATH
	movwf PCL
	nop
	movlw 0
	movwf PCLATH
	movwf PCL
	nop
	movlw 0
	movwf PCLATH
	movwf PCL
	nop
	movlw 0
	movwf PCLATH
	movwf PCL
	nop
	movlw 0
	movwf PCLATH
	movwf PCL
	nop
	movlw 0
	movwf PCLATH
	movwf PCL
	nop
	movlw 0
	movwf PCLATH
	movwf PCL
	nop
#endasm
}

unsigned char s2h(unsigned char d) {
	if(d>='A')
		return (d-'A'+10);
	return (d-'0');
}

void init_timer0(void) {
	T0CS=0;
	PSA=0;
	PS0=PS1=PS2=1;//1:256
	count=0;
	TMR0IF=0;
}

void KT_Write(void) {
	//ghi vung dem vao flash
	//cho nhan tiep dia chi can ghi
	unsigned char addrhh, addrll,addrhl, addrlh, addrh, addrl,i;
	addrhh=Get();
	addrhl=Get();
	addrlh=Get();
	addrll=Get();
	GIE=0;
	CFGS=0;//ghi vao flash khong ghi eeprom, config
	addrh=((s2h(addrhh))<<4)|(s2h(addrhl));
	PMADRH=addrh;
	addrl=((s2h(addrlh))<<4)|(s2h(addrll));
	PMADRL=addrl;
	if((0==addrh)&&(0==addrl)) {//page dau tien chua lenh nhay toi ham main cua chuong trinh moi
		PMADRL=0;//vung redirect
		PMADRH=0X1E;//ghi vao vung redirect
	}
	
	FREE=1;//lenh xoa
	WREN=1;
	PMCON2=0x55;
	PMCON2=0xaa;
	WR=1;
	#asm
		nop
		nop
	#endasm
	WREN=0;
	
	//xoa xong moi ghi du lieu vao
	PMADRH=addrh;
	PMADRL=addrl;
	if((0==addrh)&&(0==addrl)) {//page dau tien chua lenh nhay toi ham main cua chuong trinh moi
		PMADRL=0;//vung redirect
		PMADRH=0X1E;//ghi vao vung redirect
	}
	LWLO=1;//ghi vao latch

	FREE=0;//lenh ghi
	
	WREN=1;
	for(i=1;i<32;++i) {
		//Send(szBuff[0]);
		//Send(szBuff[1]);
		PMADRL=i;
		PMDATH=i;//s2h(0);//du lieu se ghi
		PMDATL=i;//s2h(1);
		//while(WR);
		PMCON2=0x55;
		PMCON2=0xaa;
		WR=1;
		#asm
			nop
			nop
		#endasm
		
	}
	
	PMADRL=0;
	PMDATH=0;//s2h(0);//du lieu se ghi
		PMDATL=0;//s2h(1);
		//while(WR);
		PMCON2=0x55;
		PMCON2=0xaa;
		WR=1;
		#asm
			nop
			nop
		#endasm
	//bat dau ghi vao flash
	
	PMADRH=addrh;
	PMADRL=addrl;
	if((0==addrh)&&(0==addrl)) {//page dau tien chua lenh nhay toi ham main cua chuong trinh moi
		PMADRL=0;//vung redirect
		PMADRH=0X1E;//ghi vao vung redirect
	}
	LWLO=0;
	PMCON2=0x55;
	PMCON2=0xaa;
	WR=1;
	#asm
		nop
		nop
	#endasm
	WREN=0;
	//gui lai thong bao cho PC biet la da ghi xong
	Send('O');
}
is work fine
please tell me why?

**broken link removed**
**broken link removed**
 
Last edited:

Status
Not open for further replies.

Similar threads

Cookies are required to use this site. You must accept them to continue using the site. Learn more…