📄 c8051f340-modbus.c
字号:
byteCount = readCount* 2 ;//;
for(i=0;i<byteCount;i++,tempAddr++)
{
tempData=FLASH_ByteRead(tempAddr);
//getRegisterVal(tempAddr,&tempData);
sendBuf[i+3] = tempData;// & 0xff;
}
sendBuf[0] = localAddr;
sendBuf[1] = 3;
sendBuf[2] = byteCount;
byteCount += 3;
crcData = crc_chk(sendBuf,byteCount);
sendBuf[byteCount] = crcData & 0xff;
byteCount++;
sendBuf[byteCount] = crcData >> 8;
sendCount = byteCount + 1;
beginSend();
}
else
{
send_err(0x83,0x03);
}
}//void readRegisters(void)
/*****************************************************************
功能码6 设置单个寄存器
******************************************************************
void presetSingleRegisters(void)
{
u16 addr;
u16 tempAddr;
u16 crcData;
u16 tempData;
// uint8 finsh; //为1时完成 为0时出错
tempData=mod_buf[2];
addr = (tempData<<8) + mod_buf[3];
tempAddr = addr;// & 0xfff;
//addr = mod_buf[3];
//tempAddr = addr & 0xff;
//setCount = (receBuf[4]<<8) + receBuf[5];
//setCount = mod_buf[5];
//byteCount = mod_buf[6];
FLASH_PageErase (tempAddr);
// tempData = (modbuf[i*2+7]<<8) + modbuf[i*2+8];
tempData=mod_buf[4];
// setRegisterVal(tempAddr,tempData);
FLASH_ByteWrite (tempAddr, tempData);
sendBuf[4] = tempData ;
tempData=mod_buf[5];
FLASH_ByteWrite (tempAddr+1, tempData);
sendBuf[5] = tempData;
sendBuf[0] = localAddr;
sendBuf[1] = 6;
sendBuf[2] = addr >> 8;
sendBuf[3] = addr & 0xff;
crcData = crc_chk(sendBuf,6);
sendBuf[6] = crcData & 0xff;
sendBuf[7] = crcData >> 8;
sendCount = 8;
beginSend();
}
/****************************************************************
功能码16,设置多个功能寄存器
******************************************************************/
void presetMultipleRegisters(void)
{
u16 addr;
u16 tempAddr;
u8 byteCount;
u8 setCount;
u16 crcData;
u16 tempData;
// uint8 finsh; //为1时完成 为0时出错
u8 i;
//addr = mod_buf[3];
tempData=mod_buf[2];
addr = (tempData<<8) + mod_buf[3];
tempAddr = addr;// & 0xfff;
//tempAddr = addr & 0xff;
//setCount = (receBuf[4]<<8) + receBuf[5];
setCount = mod_buf[5];
if(setCount<=0x78)
{
byteCount = mod_buf[6];
sendBuf[0] = localAddr;
sendBuf[1] = 16;
sendBuf[2] = addr >> 8;
sendBuf[3] = addr & 0xff;
sendBuf[4] = setCount >> 8;
sendBuf[5] = setCount & 0xff;
crcData = crc_chk(sendBuf,6);
sendBuf[6] = crcData & 0xff;
sendBuf[7] = crcData >> 8;
sendCount = 8;
beginSend();
FLASH_PageErase (0x7c00);
for(i=0;i<byteCount;i++,tempAddr++)
{
// tempData = (modbuf[i*2+7]<<8) + modbuf[i*2+8];
tempData=mod_buf[7+i];
// setRegisterVal(tempAddr,tempData);
FLASH_ByteWrite (tempAddr, tempData);
}
}
else//寄存器数量>120
{
send_err(0x90,0x03);
}
}//void presetMultipleRegisters(void)
/*UART0中断处理*/
void UART0_ISR (void) interrupt 4
{
ES0=0;
if(!TI0)
{
// Receivetimeout=time0;
RI0 = 0;
mod_buf[count_receiveNByte] = SBUF0; //获取数据
count_receiveNByte++; //接收字节数目
// Receivetimeout=350;
if(count_receiveNByte >= MAXNByte) //如果接收满
count_receiveNByte = 0; //接受满 归0
}
TI0 = 0;
ES0=1;
}
//发送子程序
void beginSend(void)
{ u8 i;
ES0=0;
GREEN=0;
RED=1;
PF=1; //485允许发送
for(i=0;i<sendCount;i++)
{
SBUF0 = sendBuf[i];
while(TI0==0);
TI0=0;
}
PF=0; //485允许接收
GREEN=1;
RED=0;
ES0=1;
}
void send_err(u8 err,u8 err_code)//发送错误
{
u16 crc_tmp;
sendBuf[0]=localAddr;
sendBuf[1]=err;
sendBuf[2]=err_code;
crc_tmp=crc_chk(mod_buf,3);
sendBuf[3]=crc_tmp;
sendBuf[4]=crc_tmp>>8;
sendCount=5;
beginSend();
}
/*****************************************************
接收超时判断
调用条件:
((Receivetime_flag=1)&&(count_receiveNByte>0)) 1ms定时时间到
并且 有接收到的字符,调用此模块,判定是否接收超时
*****************************************************
void Receive_timeout(void)
{
Receivetimeout--;
if((Receivetimeout==0)&&(count_receiveNByte>0)) //说明接收超时
{
count_receiveNByte=0; //接受数据指针归零
SCON0|=0x10; //允许UART0接收,REN0=1;
}
}*/
void FLASH_PageErase (u16 addr)
{
bit EA_SAVE = EA; // Preserve EA
char xdata * data pwrite; // FLASH write pointer
EA = 0; // Disable interrupts
// change clock speed to slow, then restore later
VDM0CN = 0x80; // Enable VDD monitor
RSTSRC |= 0x02; // enable VDD monitor as a reset source
FLKEY = 0xA5; // Key Sequence 1
FLKEY = 0xF1; // Key Sequence 2
PSCTL |= 0x03; // PSWE = 1; PSEE = 1
VDM0CN = 0x80; // Enable VDD monitor
RSTSRC |= 0x02; // Enable VDD monitor as a reset source
pwrite = (char xdata *) addr;
VDM0CN = 0x80; // Enable VDD monitor
*pwrite = 0; // Initiate page erase
PSCTL &= ~0x03; // PSWE = 0; PSEE = 0
EA = EA_SAVE; // Restore interrupts
}
/********************************************************************
读寄存器值
********************************************************************/
u8 FLASH_ByteRead (u16 addr)
{
bit EA_SAVE = EA; // Preserve EA
char code * data pread; // FLASH read pointer
unsigned char byte;
EA = 0; // Disable interrupts
pread = (char code *) addr;
byte = *pread; // Read the byte
EA = EA_SAVE; // Restore interrupts
return byte;
}
/************************************************************
Flash写
************************************************************/
void FLASH_ByteWrite (u16 addr, u8 byte)
{
bit EA_SAVE = EA; // Preserve EA
char xdata * data pwrite; // FLASH write pointer
EA = 0; // Disable interrupts
// change clock speed to slow, then restore later
VDM0CN = 0x80; // Enable VDD monitor
RSTSRC |= 0x02; // Enable VDD monitor as a reset source
pwrite = (char xdata *) addr;
PFE0CN &= 0XFE; //设定单字节写入FLASH方式
PSCTL |= 0x01; // PSWE = 1
FLKEY = 0xA5; // Key Sequence 1
FLKEY = 0xF1; // Key Sequence 2
VDM0CN = 0x80; // Enable VDD monitor
RSTSRC |= 0x02; // Enable VDD monitor as a reset source
VDM0CN = 0x80; // Enable VDD monitor
*pwrite = byte; // Write the byte
PSCTL &= ~0x01; // PSWE = 0
EA = EA_SAVE; // Restore interrupts
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -