//串口初始化9600,8,N,1
void uart_init(u32 pclk2,u32 bound)
{
float temp;
u16 mantissa;
u16 fraction;
temp=(float)(pclk2*1000000)/(bound*16);
mantissa=temp;
fraction=(temp-mantissa)*16;
mantissa<<=4;
mantissa+=fraction;
RCC->APB2ENR|=1<<2;
RCC->APB2ENR|=1<<14;
GPIOA->CRH&=0XFFFFF00F;
GPIOA->CRH|=0X000008B0;
RCC->APB2RSTR|=1<<14;
RCC->APB2RSTR&=~(1<<14);
USART1->BRR=mantissa;
USART1->CR1|=0X200C;
USART1->CR1|=1<<8;
USART1->CR1|=1<<5;
MY_NVIC_Init(3,3,USART1_IRQn,2);
}
void init_time3(u16 arr,u16 psc)
{
RCC->APB1ENR|=1<<1;
TIM3->ARR=arr; //éè?¨??êy?÷×??ˉ??×°?μ//??o?1ms
TIM3->PSC=psc; //?¤·??μ?÷72,μ?μ?1Mhzμ???êyê±?ó
TIM3->CR1|=1<<4; //?ò????êy
TIM3->DIER|=1<<0;
TIM3->DIER|=1<<6;
MY_NVIC_Init(1,3,TIM3_IRQn,2);//?à??1£?×óó??è??3£?×é2
}
void beginSend(u8* sendPosi,u8 sendCount)
{
//IO1CLR=q2;
TX_EN; //éè?a·¢?í
while(sendCount>0)
{
sendCount--;
USART1_TX(*(sendPosi++));
}
RX_EN;
receCount=0;
}
void readRegisters(void )
{
u8 sendCount;
u16 tempAddr;
u16 crcData;
u16 byteCount;
u8 i;
tempAddr=(receBuf[2]<<8|receBuf[3])*2;
byteCount=(receBuf[4]<<8|receBuf[5])* 2;
sendBuf[0] = localAddr;
sendBuf[1] = 3;
sendBuf[2] = (u8)byteCount;
for(i=0;i<byteCount;i++,tempAddr++)
{
sendBuf[i+3] = syscanshu[tempAddr];
}
byteCount += 3;
crcData = crc16(sendBuf,byteCount);
sendBuf[byteCount] = crcData >> 8;
byteCount++;
sendBuf[byteCount] = crcData & 0xff;
sendCount = byteCount + 1;
beginSend(sendBuf,sendCount); //
}
void presetMultipleRegisters(void)
{
u8 sendCount;
u16 tempAddr;
u8 byteCount;
u16 crcData;
u8 i;
tempAddr = (receBuf[2]<<8|receBuf[3])*2;//ìáè???′??÷??êy
sendBuf[0] = localAddr;
sendBuf[1] = 16;
sendBuf[2] = receBuf[2];
sendBuf[3] = receBuf[3];
sendBuf[4] = receBuf[4];
sendBuf[5] = receBuf[5];
byteCount = receBuf[6];
for(i=0;i<byteCount;i++,tempAddr++)
{
syscanshu[tempAddr]=receBuf[i+7];
}
crcData = crc16(sendBuf,6);
sendBuf[6] = crcData >> 8;
sendBuf[7] = crcData & 0xff;
sendCount = 8;
beginSend(sendBuf,sendCount);
}
/*******************************************************
* ?? 3?£osetRegisterVal()
* 1| ?ü£oéè??μ¥????′??÷
* è??ú2?êy£o?T
* 3??ú2?êy£o?T
********************************************************/
void setRegisterVal(void)
{
u8 sendCount;
u16 tempAddr;
u16 crcData;
tempAddr = (receBuf[2]<<8|receBuf[3])*2;
syscanshu[tempAddr]=receBuf[4];
syscanshu[tempAddr+1]=receBuf[5];
sendBuf[0] = localAddr;
sendBuf[1] = 6;
sendBuf[2] = receBuf[2];
sendBuf[3] = receBuf[3];
sendBuf[4] = receBuf[4];
sendBuf[5] = receBuf[5];
crcData = crc16(sendBuf,6);
sendBuf[6] = crcData >> 8;
sendBuf[7] = crcData & 0xff;
sendCount = 8;
beginSend(sendBuf,sendCount);
}
/*******************************************************
* ?? 3?£ocheckComm0Modbus()
* 1| ?ü£o?ì2a′??úêy?Y
* è??ú2?êy£o?T
* 3??ú2?êy£o?T
********************************************************/
void checkComm0Modbus(void)
{
u16 crcData;
u16 tempData;
if(receBuf[0]==localAddr)
{
if(receBuf[1]==3||receBuf[1]==6)
{
crcData = crc16(receBuf,6);
if(crcData == receBuf[7]+(receBuf[6]<<8))
{//D£?é?yè·
switch(receBuf[1])
{
case 3:LED3=0;readRegisters();break;//?áè?±£3???′??÷(ò????ò?à??)//?áè?±£3???′??÷(ò????ò?à??)
case 6:LED3=1;setRegisterVal();break;//éè??μ¥????′??÷
}
}
}
if(receBuf[1]==16)
{
tempData = (receBuf[4]<<8) + receBuf[5];
tempData = tempData * 2; //êy?Y??êy
tempData += 9;
if(receCount >= tempData)
{// IO1CLR=q2;
crcData = crc16(receBuf,tempData-2);
if(crcData == (receBuf[tempData-2]<<8)+ receBuf[tempData-1])
{
presetMultipleRegisters();
}
}
}
}
}