⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 tstrs485.lst

📁 基于PIC16F873构成485接口的C源码
💻 LST
📖 第 1 页 / 共 5 页
字号:
....................       // The number is a decimal number 
....................       if (base == 10) 
....................       { 
....................          while (c >= '0' && c <= '9') 
....................          { 
....................             result = 10*result + (c - '0'); 
....................             c = s[index++]; 
....................          } 
....................       } 
....................       else if (base == 16)    // The number is a hexa number 
....................       { 
....................          while (c = TOUPPER(c), (c >= '0' && c <= '9') || (c >= 'A' && c<='F')) 
....................          { 
....................             if (c >= '0' && c <= '9') 
....................                result = (result << 4) + (c - '0'); 
....................             else 
....................                result = (result << 4) + (c - 'A' + 10); 
....................  
....................             c = s[index++]; 
....................          } 
....................       } 
....................    } 
....................  
....................    if (sign == 1 && base == 10) 
....................        result = -result; 
....................  
....................    return(result); 
.................... } 
....................  
....................  
....................  
.................... signed long atol(char *s) 
.................... { 
....................    signed long result; 
....................    int sign, base, index; 
....................    char c; 
....................  
....................    index = 0; 
....................    sign = 0; 
....................    base = 10; 
....................    result = 0; 
....................  
....................    do 
....................       c = s[index++]; 
....................    while ((c < '0' || c>'9') && c != '+' && c != '-'); 
....................  
....................    // increase index if either positive or negative sign is detected 
....................    if (c == '-') 
....................    { 
....................       sign = 1;         // Set the sign to negative 
....................       c = s[index++]; 
....................    } 
....................    else if (c == '+') 
....................    { 
....................       c = s[index++]; 
....................    } 
....................  
....................    if (c >= '0' && c <= '9') 
....................    { 
....................       if (c == '0' && (s[index] == 'x' || s[index] == 'X')) 
....................       { 
....................          base = 16; 
....................          index++; 
....................          c = s[index++]; 
....................       } 
....................  
....................       // The number is a decimal number 
....................       if (base == 10) 
....................       { 
....................          while (c >= '0' && c <= '9') 
....................          { 
....................             result = 10*result + (c - '0'); 
....................             c = s[index++]; 
....................          } 
....................       } 
....................       else if (base == 16)    // The number is a hexa number 
....................       { 
....................          while (c = TOUPPER(c), (c >= '0' && c <= '9') || (c >= 'A' && c <='F')) 
....................          { 
....................             if (c >= '0' && c <= '9') 
....................                result = (result << 4) + (c - '0'); 
....................             else 
....................                result = (result << 4) + (c - 'A' + 10); 
....................  
....................             c = s[index++]; 
....................          } 
....................       } 
....................    } 
....................  
....................    if (base == 10 && sign == 1) 
....................       result = -result; 
....................  
....................    return(result); 
.................... } 
....................  
.................... signed int32 mult_with10(int32 num) 
.................... { 
....................    return ( (num << 1) + (num << 3) ); 
.................... } 
....................  
.................... signed int32 atoi32(char *s) 
.................... { 
....................    signed int32 result; 
....................    int sign, base, index; 
....................    char c; 
....................  
....................    index = 0; 
....................    sign = 0; 
....................    base = 10; 
....................    result = 0; 
....................  
....................    do 
....................       c = s[index++]; 
....................    while ((c < '0' || c>'9') && c != '+' && c != '-'); 
....................  
....................    // increase index if either positive or negative sign is detected 
....................    if (c == '-') 
....................    { 
....................       sign = 1;         // Set the sign to negative 
....................       c = s[index++]; 
....................    } 
....................    else if (c == '+') 
....................    { 
....................       c = s[index++]; 
....................    } 
....................  
....................    if (c >= '0' && c <= '9') 
....................    { 
....................       if (c == '0' && (s[index] == 'x' || s[index] == 'X')) 
....................       { 
....................          base = 16; 
....................          index++; 
....................          c = s[index++]; 
....................       } 
....................  
....................       // The number is a decimal number 
....................       if (base == 10) 
....................       { 
....................          while (c >= '0' && c <= '9') { 
....................             result = (result << 1) + (result << 3);  // result *= 10; 
....................             result += (c - '0'); 
....................             c = s[index++]; 
....................          } 
....................       } 
....................       else if (base == 16)    // The number is a hexa number 
....................       { 
....................          while (c = TOUPPER(c), (c >= '0' && c <= '9') || (c >= 'A' && c <='F')) 
....................          { 
....................             if (c >= '0' && c <= '9') 
....................                result = (result << 4) + (c - '0'); 
....................             else 
....................                result = (result << 4) + (c - 'A' + 10); 
....................  
....................             c = s[index++]; 
....................          } 
....................       } 
....................    } 
....................  
....................    if (base == 10 && sign == 1) 
....................       result = -result; 
....................  
....................    return(result); 
.................... } 
....................  
....................  
.................... #endif 
.................... 
.................... #include <Rs485.c>    // RS485 packet protocol driver 
....................  //************************************************************************** 
.................... // Rs495.c 
.................... //                      RS485 network protocol 
.................... // 
.................... // Version 1.0 beta 
.................... // 
.................... // Processor: PIC16F87x 
.................... // 
.................... // Network packet protocol: 
.................... // 
.................... // STX ( 0x02 ) 
.................... // NET ADDRESS HIGH   
.................... // NET ADDRESS LOW 
.................... // PAYLOAD LENGTH 
.................... // COMMAND 
.................... // DATA ( Optional ) 
.................... // CRC HIGH 
.................... // CRC LOW 
.................... // 
.................... // 
.................... // 
.................... // 
.................... // 
.................... // By: J.Winpenny 
.................... // j.winpenny@ukonline.co.uk 
.................... // 
.................... //************************************************************************** 
.................... #include <Rs485.h> 
....................  #separate 
.................... char Rs485Process(void); 
.................... void Rs485Initialise(char cAddrHigh, char cAddrLow); 
.................... #separate 
.................... char Rs485Decode( void ); 
.................... void Rs485UpdateCrc(char cVal ); 
.................... void CRC16_Init( void ); 
.................... void CRC16_Update4Bits( char val ); 
.................... void Rs485SendPacket( char cCmd, char cLen, char *cData ); 
.................... void Rs485GetPacket( char *cCom, char *cLen, char *cData ); 
.................... void Rs485SendChar( char c ); 
.................... char PostValidatePacket(void); 
.................... char PacketHasPayload(void); 
.................... void BinToHexAscii( char c ); 
....................  
....................  
.................... #define NULL 0 
....................  
.................... // Configurables 
.................... #define RS485_CONTROL PORTC 
.................... #define OUTPUTS_ON 5 
.................... #define NUM_TX_PREAMBLE 3 
....................  
....................  
.................... // Protocol defines 
.................... #define PKT_START 0x02 
....................  
.................... // States of packet decoder state machine 
.................... #define PKT_WAIT_START      0 
.................... #define PKT_WAIT_ADDR_HIGH  1 
.................... #define PKT_WAIT_ADDR_LOW   2 
.................... #define PKT_WAIT_LEN        3 
.................... #define PKT_CMD             4 
.................... #define PKT_WAIT_DATA       5 
.................... #define PKT_WAIT_CRC_HIGH   6 
.................... #define PKT_WAIT_CRC_LOW    7 
.................... #define PKT_COMPLETE        8 
.................... #define PKT_VALID           9 
.................... #define PKT_INVALID       255 
....................  
.................... // Error codes 
.................... #define BAD_LENGTH 1 
.................... #define BAD_CRC    2 
....................  
....................  
.................... // Packet types 
.................... #define SENSOR_POLL_CMD        0xA1 
.................... #define SENSOR_PING_SYNC       0xA2 
.................... #define SENSOR_CONFIG          0xA3 
.................... #define SENSOR_GET_DATA        0xA4 
....................  
....................  
....................  
.................... #define SENSOR_ACK             0xB0 
.................... #define SENSOR_NAK             0xC0 
....................  
....................  
....................  
....................  
....................  
....................  
....................  
.................... 
.................... #include <lcd2.h> 
....................  /***********************************************/ 
.................... /* LCD.H   Header for LCD routines             */ 
.................... /***********************************************/ 
....................  
....................    void WaitBusyFlag(void); 
....................    void LCDSetup(void); 
....................    void FunctionMode(void); 
....................    void DataMode(void); 
....................    void Write_8_Bit( char dh ); 
....................    void LcdWrite(char dl ); 
....................    void Delay(void); 
....................    void Clear(void); 
....................    void SetPos(char Pos); 
....................    void Line1(void); 
....................    void Line2(void); 
....................    void ClearLine1(void); 
....................    void ClearLine2(void); 
....................    void WriteString( char *lcdptr ); 
....................  
.................... 
.................... // #include <lcd873.h> 
....................  
....................  
....................  
.................... //struct 
.................... //{ 
.................... static char cOurAddrHigh; 
.................... static char cOurAddrLow; 
.................... static char cRs485RxChar; 
.................... static char cRS485State; 
....................  
.................... static char cStart; 
.................... static char cNetAddrHigh, cNetAddrLow; 
.................... static char cLenExpected; 
.................... static char cCommand; 
.................... static char c485Buf[64]; 
.................... static char cRxCrcHigh, cRxCrcLow; 
.................... static char cCalcCrcHigh, cCalcCrcLow; 
.................... static char cBufPtr; 
.................... static char cError; 
.................... static char cCrcTmp, cCrcTmp5, cCrcTmp4, cCrcTmp3, cCrcTmp2; 
.................... //} RS485_Protocol; 
....................  
....................  
.................... //**************************************************************************** 
.................... // void Rs485Initialise(void) 
.................... // 
.................... // Initialise RS485 network driver 
.................... //**************************************************************************** 
.................... void Rs485Initialise(char cAddrHigh, char cAddrLow) 
.................... { 
....................   cOurAddrHigh = cAddrHigh; 
*
025D:  BSF    03,5
025E:  MOVF   32,W
025F:  BCF    03,5
0260:  MOVWF  2E
....................   cOurAddrLow = cAddrLow; 
0261:  BSF    03,5
0262:  MOVF   33,W
0263:  BCF    03,5
0264:  MOVWF  2F
....................   cRS485State = PKT_WAIT_START; 
0265:  CLRF   31
....................   BIT_CLEAR( RS485_CONTROL, OUTPUTS_ON );           // Disable driver 
0266:  BCF    07,5
....................   BIT_SET( PIE1, RCIE );                            // Enable Receive Interrupt 
0267:  BSF    03,5
0268:  BSF    0C,5
0269:  BCF    03,5
026A:  RETLW  00
.................... } 
....................  
....................  
.................... //**************************************************************************** 
.................... // 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; 
*
0392:  BSF    03,5
0393:  CLRF   32
....................     cPktReady = FALSE; 
0394:  CLRF   33
....................  
....................     disable_interrupts(GLOBAL); 
0395:  BCF    03,5
0396:  BCF    0B,6
0397:  BCF    0B,7
0398:  BTFSC  0B,7
0399:  GOTO   397
....................  
....................     if ( cRS485State == PKT_COMPLETE ) 
039A:  MOVF   31,W
039B:  SUBLW  08
039C:  BTFSS  03,2
039D:  GOTO   485
....................     { 
....................        	if ( ( cNetAddrHigh == cOurAddrHigh )&&   // Invalid and destined for this node 
.................... 					( cNetAddrLow == cOurAddrLow ) ) 
039E:  MOVF   2E,W
039F:  SUBWF  33,W
03A0:  BTFSS  03,2
03A1:  GOTO   3AC
03A2:  GOTO   3A3
03A3:  MOVF   2F,W
03A4:  SUBWF  34,W
03A5:  BTFSS  03,2
03A6:  GOTO   3AC
.................... 			{ 
.................... 			   cOurPkt = TRUE; 
03A7:  MOVLW  01
03A8:  BSF    03,5
03A9:  MOVWF  32
.................... 			} 
.................... 			else 
03AA:  BCF    03,5
03AB:  GOTO   3C2
.................... 			{ 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -