📄 dj485p9k.c.bak
字号:
}
//**************************
// Setup Hardware
//**************************
void Setup(void)
{
// OPTIONREG = 0x84; // pullups,prescailer = TMR0 , rate = 1:32
// OPTION=84; 关一要不然BEEP会一直叫,
// OPTION=04 ;//启用RB0中断
INTCON = 0; // disable global interupts
ADCON1 = 0x07; // Disable ADC
//TRISC = PortCConfig;
// TRISD = PortDConfig;
// TRISD=0X00;
// TRISA=0X00;
// ADCON1=0X07;
// PORTA=0XFF;
// PORTD=0XFF;
// PORTC = 0xFF;
T0IF=0;
T0IE=1;
// INTF=0;
// INTE=1; //允计RB0中断
ConfigureComms(); /* Configure USART for Asyncronous Comms */
// PORTC = 0xF7;
GIE=1;
}
//****************************************************************************
// void Rs485Initialise(void)
//
// Initialise RS485 network driver
//****************************************************************************
void Rs485Initialise(char cAddrHigh, char cAddrLow)
{
cOurAddrHigh = cAddrHigh;
cOurAddrLow = cAddrLow;
cRS485State = PKT_WAIT_START;
// BIT_CLEAR( RS485_CONTROL, OUTPUTS_ON ); // Disable driver
// BIT_SET( PIE1, RCIE ); // Enable Receive Interrupt
OUTPUTS_ON=0;
RCIE=1;
}
//*******************************************************/
//* Configure PIC16F87x USART for communications */
//* */
//* Asynchronous mode */
//* 8 data bits ( For other rates see PIC16F8XX Data ) */
//* 1 stop bits */
//* No Parity */
//* */
//*******************************************************/
//* 通讯模块的设定
void ConfigureComms(void)
{
// BIT_SET( RCSTA, SPEN ); // Enable Serial port
// BIT_CLEAR( RCSTA, RX9 ); // 8 bit receive mode
// BIT_CLEAR( TXSTA, TX9 ); // 8 bit transmit mode
SPEN=1;
RX9=0;//8位接收模块
TX9=0 ;//8位发送模式
// SPBRG = 0; // SPBRG = 1 ( Set Baud rate 115,200 )
// SPBRG = 5; // SPBRG = 5 ( Set Baud rate 38,400 )
// SPBRG = 22; // SPBRG = 22 ( Set Baud rate 9,600 )
// SPBRG = 11; // SPBRG = 11 ( Set Baud rate 19,200 )
// SPBRG = 22; // SPBRG = 22 ( Set Baud rate 9,600 )
// SPBRG = 47; // SPBRG = 47 ( 9600 Baud at 7.3728 Mhx clock )
// SPBRG = 22; // SPBRG = 22 ( 9600 Baud at 3.579545Mhz clock )
SPBRG = 25; // SPBRG = 25 ( 9600 Baud at 4 Mhx clock )
// BIT_SET( TXSTA, BRGH ); // BRGH = 1 ( High speed mode )
// BIT_CLEAR( TXSTA, SYNC ); // Asyncronous mode;
// BIT_SET( TXSTA, TXEN ); // Enable Transmitter
// BIT_SET( RCSTA, CREN ); // Enable continuous receive
// BIT_SET( INTCON, PEIE ); // Enable all Peripheral Interrupts
BRGH=1; //高速模式
SYNC=0; //异步通讯
TXEN=1; //发送使能
CREN=1;
// CREN=0;//先关一下接收使能
PEIE=1;
}
//****************************************************************************
// void Rs485SendChar( char c )
//
// Driver level of RS485 protocol
// Output character on RS485 driver
// // Include line turn around time
//****************************************************************************
void Rs485SendChar( char c )
{
TXREG = c; // Load data to send
while ( !( TXSTA & TRMT_MASK )); // Wait for TX Empty
}
//****************************************************************************
// void Rs485SendPacket( char cAddr, char cCmd, char cLen, char *cData )
//
// Send a packet over the RS485 link
//
// Input: NETWORK_ADDRESS, COMMAND, PAYLOAD_LENGTH, optional DATA
//485发送数据包的格式为
//两个准备字符00+ 开始字符+ 目标地址的高位+目标地址的低位+数据的长度+命令+数据+CRC高位+CRC低位
//
//****************************************************************************
void Rs485SendPacket( char cCmd, char cLen, char *cData )
{
char c, d;
//BIT_CLEAR( PIE1, RCIE ); // Disable Receive Interrupt
RCIE=0;
// BIT_SET( RS485_CONTROL, OUTPUTS_ON ); // Enable driver
OUTPUTS_ON=1;
delay_ms(1); // Line turnarround time
cCalcCrcHigh = 0xff; // Clear CRC
cCalcCrcLow = 0xff;
// Send some NULL preamblesfopr receiving UART
for ( c=0; c < NUM_TX_PREAMBLE; c++ ) Rs485SendChar( 0x00 );
Rs485UpdateCrc( PKT_START );
Rs485SendChar( PKT_START ); // Send packet start character
Rs485UpdateCrc( cOurAddrHigh );
Rs485SendChar( cOurAddrHigh ); // Send address high
Rs485UpdateCrc( cOurAddrLow );
Rs485SendChar( cOurAddrLow ); // Send address low
Rs485UpdateCrc( cLen );
Rs485SendChar( cLen ); // Send length
Rs485UpdateCrc( cCmd );
Rs485SendChar( cCmd ); // Send command
if ( cLen != 0 ) // If payload not empty send data
{
for ( c = 0; c < cLen; c++ )
{
d = cData[c];
Rs485UpdateCrc( d );
}
for ( c = 0; c < cLen; c++ )
{
d = cData[c];
Rs485SendChar( d ); // Send data
}
}
// Rs485SendChar(cCalcCrcHigh);
// Rs485SendChar(cCalcCrcLow);
Rs485SendChar(0X0E);
Rs485SendChar(0X00);
delay_ms(1);
// BIT_CLEAR( RS485_CONTROL, OUTPUTS_ON ); // Disable driver
OUTPUTS_ON=0;
//BIT_SET( PIE1, RCIE ); // Enable Receive Interrupt
RCIE=1;
}
void delay_ms(int kk)
{
int k=330;
for (k;k>0;k--)
;
}
/*************************************************************************
* Example Table Driven CRC16 Routine using 4-bit message chunks
*
* By Ashley Roll
* Digital Nemesis Pty Ltd
* www.digitalnemesis.com
* ash@digitalnemesis.com
*
* The following is an example of implementing a restricted size CRC16
* table lookup. No optimisation as been done so the code is clear and
* easy to understand.
*
* Test Vector: "123456789" (character string, no quotes)
* Generated CRC: 0x29B1
*
* Modified for CCS compiler by J.Winpenny
*************************************************************************/
/*
* CRC16 Lookup tables (High and Low Byte) for 4 bits per iteration.
*/
const char CRC16_LookupHigh[16] = {
0x00, 0x10, 0x20, 0x30, 0x40, 0x50, 0x60, 0x70,
0x81, 0x91, 0xA1, 0xB1, 0xC1, 0xD1, 0xE1, 0xF1
};
const char CRC16_LookupLow[16] = {
0x00, 0x21, 0x42, 0x63, 0x84, 0xA5, 0xC6, 0xE7,
0x08, 0x29, 0x4A, 0x6B, 0x8C, 0xAD, 0xCE, 0xEF
};
/*
* Before each message CRC is generated, the CRC register must be
* initialised by calling this function
*/
void CRC16_Init( void )
{
// Initialise the CRC to 0xFFFF for the CCITT specification
cCalcCrcHigh = 0xFF;
cCalcCrcLow = 0xFF;
}
/*
* Process 4 bits of the message to update the CRC Value.
*
* Note that the data must be in the low nibble of val.
*/
void CRC16_Update4Bits( char val )
{
char t;
// Step one, extract the Most significant 4 bits of the CRC register
t = cCalcCrcHigh >> 4;
// XOR in the Message Data into the extracted bits
t = t ^ val;
// Shift the CRC Register left 4 bits
cCalcCrcHigh = (cCalcCrcHigh << 4) | (cCalcCrcLow >> 4);
cCalcCrcLow = cCalcCrcLow << 4;
// Do the table lookups and XOR the result into the CRC Tables
cCalcCrcHigh = cCalcCrcHigh ^ CRC16_LookupHigh[t];
cCalcCrcLow = cCalcCrcLow ^ CRC16_LookupLow[t];
}
/*
* Process one Message Byte to update the current CRC Value
*/
void Rs485UpdateCrc( char cVal )
{
CRC16_Update4Bits( cVal >> 4 ); // High nibble first
CRC16_Update4Bits( cVal & 0x0F ); // Low nibble
}
//****************************************************************************
// void BinToHexAscii( char c )
//
// Contributed by: Nick De Smith
//
//****************************************************************************
void BinToHexAscii( char c )
{
const char hexMap[17] = "0123456789ABCDEF";
// LcdWrite( hexMap[(c >> 4) & 0xF] );
// LcdWrite( hexMap[(c & 0xF)] );
}
//****************************************************************************
// char PacketForUs(void)
//
// Decide if packet valid and destined for this node.
// Ignore invalid packets and packets for other nodes
//
//****************************************************************************
//#separate
char Rs485Process(void)
{
char cOurPkt, cPktReady;
cOurPkt = FALSE;
cPktReady = FALSE;
//disable_interrupts(GLOBAL);
GIE=0;
if ( cRS485State == PKT_COMPLETE )
{
asm("nop");
if ( ( cNetAddrHigh == cOurAddrHigh )&& // Invalid and destined for this node
( cNetAddrLow == cOurAddrLow ) )
{
cOurPkt = TRUE;
}
else
{
// ClearLine2();
// LcdWrite("traffic"); // Network traffic for other nodes
delay_ms(200);
}
// cRS485State = PostValidatePacket(); // Validate packet CRC 判断数据是否是通过CRC校验
cRS485State = PKT_VALID ;// 数据先不用CRC校验
if ( (cRS485State == PKT_INVALID)||(cRS485State == PKT_VALID) )
{
// Reject invalid packets
if ( cRS485State == PKT_INVALID ) // NAK our invalid packets
{
// ClearLine2();
if ( cError == BAD_CRC ) //
;
//LcdWrite("Bad CRC");
else if ( cError == BAD_LENGTH ) // LcdWrite("Bad length");
delay_ms(200);
//if ( cOurPkt ) Rs485SendPacket( SENSOR_NAK, 0, NULL );
cRS485State = PKT_WAIT_START;
}
else if ( cRS485State == PKT_VALID ) // If packet valid
{ // and destined for this node
if ( cOurPkt ) cPktReady = TRUE;
else cRS485State = PKT_WAIT_START;
}
}
}
//enable_interrupts(GLOBAL);
GIE=1;
return cPktReady;
}
//****************************************************************************
// char PostValidatePacket(void)
//
// Verify the CRC on the last packet received
//
// Check if the CRC is correct
// and return the updated state as the result
//
//检测CRC校验是否是通过了
// 包含判断一下是否是传输回应命令还是传输数据,如果是命令就不加数据CRC如果是包含数据则加数据CRC
//****************************************************************************
char PostValidatePacket(void)
{
char c, d;
CRC16_Init();
Rs485UpdateCrc(PKT_START);
Rs485UpdateCrc(cNetAddrHigh);
Rs485UpdateCrc(cNetAddrLow);
Rs485UpdateCrc(cLenExpected);
Rs485UpdateCrc(cCommand);
if ( PacketHasPayload() ) // If the packet has a payload,
{ // then include the data in the CRC.
for ( c = 0; c < cLenExpected; c++ )
{
d = c485Buf[c];
Rs485UpdateCrc( d );
}
}
// Check if the CRC is correct
// and return the updated state as the result
if ( (cRxCrcHigh == cCalcCrcHigh)&&(cRxCrcLow == cCalcCrcLow) )
{
cRS485State = PKT_VALID;
}
else
{
cError = BAD_CRC;
cRS485State = PKT_INVALID;
// ClearLine2();
BinToHexAscii(cRxCrcHigh );
BinToHexAscii(cRxCrcLow );
// LcdWrite(' ');
BinToHexAscii(cCalcCrcHigh);
BinToHexAscii(cCalcCrcHigh);
delay_ms(255);
delay_ms(255);
delay_ms(255);
delay_ms(255);
}
return cRS485State;
}
//****************************************************************************
// void Rs485GetPacket( char *cCommand, char cLen, char *cData )
//
// Pass packet to main application
//
//****************************************************************************
void Rs485GetPacket( char *cCom, char *cLen, char *cData )
{
char c;
char r;
*cCom = cCommand;
*cLen = cLenExpected;
LKCOUNT=c485Buf[0]; //联网控制器的数量
for ( c=1; c < cLenExpected;c++ )
{
r=c-1;
cData[c] = c485Buf[r];//数据往前缩一位
}
cData[cLenExpected] = 0x00; // Termninate
asm("nop");
}
void delay1ms(uchar count)
{
// int i,j;
uchar i,j;
for(i=0;i<count;i++)
for(j=0;j<130;j++)
;
}
uchar keyprocess() //用于判断按键是否有按下的程序
{
uchar keystate=0 ;
if(runkey==0)
{
asm("nop");
delay1ms;
while(!runkey)
{
keystate=run;
// break;
}
}
if(upkey==0)
{
asm("nop");
delay1ms;
while(!upkey)
{
keystate=up;
}
}
if(downkey==0)
{
asm("nop");
delay1ms;
while(!downkey)
{
keystate=down;
}
}
/*
if(autokey==0)
{
while(!autokey)
{
keystate=autowork;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -