中断接收函数
uint8_t rx_recv(uint8_t byt_e)
{
uint8_t i = 0;
rx.temp = byt_e ;
if((head_info.step == 0) && (byt_e == 0x2C))
{
head_info.cnt = 0;
}
if(head_info.cnt < 4)
{
head_info.head[head_info.cnt++] = byt_e;
}
if(head_info.cnt >= 4)
{
if((head_info.head[0] == 0x2C ) &&
(head_info.head[1] == 0x2F ) &&
(head_info.head[2] == 0x3B ) &&
(head_info.head[3] == 0x27 ))
{
head_info.step = 1;
rx.State = 1 ;
//rx_recv1(byt_e);
}
else
{
head_info.cnt = 0;
}
}
return rx.temp ;
}
uint8_t rx_recv1(uint8_t byt_e)
{
if(rx.W_pos >= 252)
{
rx.W_pos=0;
rx.RecieveFlag = 1;
}
else
{
rx.RecieveFlag = 0;
}
rx.data[rx.W_pos] = byt_e ;
rx.Gettotalnumber += 1 ;
if(rx.W_pos < 255)
{
rx.W_pos++;
}
//= byt_e ;
if(rx.W_pos>=7)
{
if( rx.data[rx.W_pos-1]==0x3b )
{
rx.Getdata_Over_flag = 4 ;
if(rx.data[rx.W_pos-2]==0x2c)
{
rx.Getdata_Over_flag = 3 ;
if(rx.data[rx.W_pos-3]==0x27)
{
rx.Getdata_Over_flag = 2 ;
if(rx.data[rx.W_pos-4]==0x2f)
{
rx.Getdata_Over_flag = 1 ;
rx.RecieveFlag = 1 ;
}
}
}
}
}
}
中断
void __attribute__ ( ( interrupt, no_auto_psv ) ) _U2RXInterrupt( void )
{
uint8_t b_yte;;
int count = 0;
#if 1
b_yte = U2RXREG;
if(rx.State == 1)
{
rx_recv1(b_yte);
}
else
{
rx_recv(b_yte);
}
IFS1bits.U2RXIF = false;
}
写函数
uint8_t Flash_WritePack(uint16_t addresss,uint16_t *data,uint16_t len)
{
uint8_t flag = 0;
uint16_t j,i,page_num,word_num;
flag = FlashErasePage(addresss);
page_num = len/LEN_LEN;
word_num = len%LEN_LEN;
NVMCON = PM_ROW_WRITE;//0x4001;
TBLPAG = 0;
i=0;
while(page_num>0)
{
for(;i<len;i++)
{
__builtin_tblwtl(addresss,(*(data + i)));
addresss +=2;
if((i+1)%LEN_LEN == 0)
{
i += 1;
break;
}
}
__builtin_disi(5); // Disable interrupts for NVM unlock sequence
NVMKEY = 0x55;
NVMKEY = 0xaa;
__builtin_write_NVM(); // initiate write
Nop();
Nop();
//Flash_TmCnt.cnt = 0;
//asm("DISI #0X0000"); //ensure intterupt is closed
while(NVMCONbits.WR)
{
break;
}
if(flag == 1)
{
break;
}
page_num--;
}
if(flag == 0)
{
NVMCON = PM_ROW_WRITE;//0x4001;
TBLPAG = 0;
for(j=0;j<word_num;j++)
{
Flash_WriteWords(addresss,*(data + i));
addresss +=2;
i +=1;
}
}
return flag;
} |