tstrs485.c
上传用户:sanfwan_06
上传日期:2007-12-18
资源大小:190k
文件大小:6k
源码类别:

串口编程

开发平台:

C/C++

  1. //*********************************************************************************
  2. // TsRs485.c
  3. //
  4. //                      Test of RS485 network protocol
  5. //
  6. //
  7. // Version 1.0 beta 
  8. //
  9. // Processor: PIC16F87x
  10. // Compiler: CCS PCM 3.023
  11. //
  12. //
  13. // By: J.Winpenny
  14. // j.winpenny@ukonline.co.uk
  15. //
  16. //
  17. //
  18. //*********************************************************************************
  19. #DEVICE PIC16F873 *=16
  20. #include <16f873.h>
  21. #define MASTER
  22. #use delay(clock=4000000)
  23. #fuses HS,NOWDT,PUT,BROWNOUT,NOPROTECT,NOLVP,NOCPD,NOWRT
  24. #include <Tst485.h>
  25. #include <stdlib.h>
  26. #include <Rs485.c>    // RS485 packet protocol driver
  27. //#include <lcd2.c>   // LCD routine
  28. #include <lcd873b.c>  // Keypad example LCD routine
  29. #use fast_io(A)
  30. #use fast_io(B)
  31. #use fast_io(C)
  32. #use fast_io(D)
  33. #use fast_io(E)
  34. char cTest[10];
  35. //**************************************************
  36. // Timer Interrupt service routine
  37. //**************************************************
  38. #INT_RTCC
  39. clock_isr()
  40. {
  41.      TMR0 = TMR0_RELOAD;        // Reload PICs timer
  42.      BIT_CLEAR( INTCON, T0IF ); // Clear interrupt flag
  43. }
  44. //**************************************************
  45. // USART interrupt service routine
  46. //**************************************************
  47. #INT_RDA
  48. void UsartInterrupt(void)
  49. {
  50.     while ( BIT_TEST( PIR1, RCIF )  )              // If USART RX Interrupt
  51.     {
  52.          TMR0 = TMR0_RELOAD;
  53.          cRs485RxChar = RCREG;
  54.          if ( !( RCSTA & 6 ) )                 // Then if no errors
  55.          {                                     // Process received character
  56.              Rs485Decode();                    // Decode RS485 network trafic
  57.              bit_set( RCSTA, CREN );           // Enable receiver.
  58.          }
  59.          else
  60.          {
  61.              bit_clear( RCSTA, CREN );         // Clear any errors
  62.              cRs485RxChar = RCREG;
  63.              cRs485RxChar = RCREG;
  64.              bit_set( RCSTA, CREN );           // Enable receiver.
  65.          }
  66.         bit_clear( PIR1, RCIF );
  67.     }
  68. }
  69. void main(void)
  70. {
  71. char cPacketReady;
  72. char cLenOfData;
  73. char cCmd, c;
  74.    Setup();
  75.    BIT_CLEAR( INTCON, T0IE ); // Timer interrupts off
  76. #ifndef MASTER
  77.    LcdWrite("Waiting........");
  78. #else
  79.    LcdWrite("Sending message");
  80. #endif
  81.      // Just setup the network address
  82.      //write_eeprom( NET_ADDRESS_HIGH, 0x10 );
  83.      //write_eeprom( NET_ADDRESS_LOW, 0x10 );
  84.      //cOurAddrHigh = read_eeprom( NET_ADDRESS_HIGH  );
  85.      //cOurAddressLow = read_eeprom( NET_ADDRESS_LOW );
  86.       while ( 1 )
  87.       {
  88.         cOurAddrHigh = 0x10;
  89.         cOurAddrLow = 0x10;
  90.         Rs485Initialise( cOurAddrHigh,cOurAddrLow );
  91.         #ifdef MASTER
  92.         cTest[0] = 'H';
  93.         cTest[1] = 'e';
  94.         cTest[2] = 'l';
  95.         cTest[3] = 'l';
  96.         cTest[4] = 'o';
  97.         cTest[5] = 0;
  98.         ClearLine2();
  99.         LcdWrite("GET DATA PKT");
  100.         Rs485SendPacket( SENSOR_GET_DATA, 5, &cTest[0] );
  101.         delay_ms(250);
  102.         delay_ms(250);
  103.         delay_ms(250);
  104.         delay_ms(250);
  105.         ClearLine2();
  106.         LcdWrite("POLL PKT");
  107.         Rs485SendPacket( SENSOR_POLL_CMD, 0, &cTest[0] );
  108.         delay_ms(250);
  109.         delay_ms(250);
  110.         delay_ms(250);
  111.         delay_ms(250);
  112.         cOurAddrHigh = 0x10;
  113.         cOurAddrLow = 0x18;
  114.         // Send a packet to another node
  115.         Rs485Initialise( cOurAddrHigh,cOurAddrLow );
  116.         Rs485SendPacket( SENSOR_POLL_CMD, 0, &cTest[0] );
  117.         delay_ms(250);
  118.         delay_ms(250);
  119.         delay_ms(250);
  120.         delay_ms(250);
  121.         #endif
  122.         cPacketReady = Rs485Process();
  123.         if ( cPacketReady )
  124.         {
  125.            Rs485GetPacket( &cCmd, &cLenOfData, &cTest[0] );
  126.            ClearLine2();
  127.            LcdWrite( "CMD: " );
  128.            BinToHexAscii( cCmd );
  129.            LcdWrite( ' ' );
  130.            if ( cLenOfData != 0 )
  131.            {
  132.              for ( c=0; c<cLenOfData; c++ ) LcdWrite( cTest[c] );
  133.            }
  134.            //Rs485SendPacket( SENSOR_ACK, 0, NULL );
  135.            delay_ms(255);
  136.            Rs485Initialise(cOurAddrHigh,cOurAddrLow);
  137.            ClearLine1();
  138.            LcdWrite("Waiting........");
  139.         }
  140.    }
  141. }
  142. //**************************
  143. // Setup  Hardware
  144. //**************************
  145. void Setup(void)
  146. {
  147.     OPTIONREG = 0x84;             //  pullups,prescailer = TMR0 , rate = 1:32
  148.     INTCON = 0;                   // disable global interupts
  149.     ADCON1 = 0x07;                // Disable ADC
  150.     TRISA = PortAConfig;
  151.     TRISB = PortBConfig;
  152.     TRISC = PortCConfig;
  153.     TRISD = PortDConfig;
  154.     TRISE = PortEConfig;
  155.     PORTC = 0xFF;
  156.     LCDSetup();                         // Setup the LCD Display
  157.     ConfigureComms();                  /* Configure USART for Asyncronous Comms */
  158.     PORTC = 0xF7;
  159. }
  160. //*******************************************************/
  161. //* Configure PIC16F87x USART for communications        */
  162. //*                                                     */
  163. //* Asynchronous mode                                   */
  164. //* 8 data bits  ( For other rates see PIC16F8XX Data ) */
  165. //* 1 stop bits                                         */
  166. //* No Parity                                           */
  167. //*                                                     */
  168. //*******************************************************/
  169. void ConfigureComms(void)
  170. {
  171.     BIT_SET( RCSTA, SPEN );    // Enable Serial port
  172.     BIT_CLEAR( RCSTA, RX9 );   // 8 bit receive mode
  173.     BIT_CLEAR( TXSTA, TX9 );   // 8 bit transmit mode
  174.     // SPBRG = 0;              // SPBRG = 1  ( Set Baud rate 115,200 )
  175.     // SPBRG = 5;              // SPBRG = 5  ( Set Baud rate 38,400 )
  176.     // SPBRG = 22;             // SPBRG = 22 ( Set Baud rate 9,600 )
  177.     // SPBRG = 11;             // SPBRG = 11 ( Set Baud rate 19,200 )
  178.     // SPBRG = 22;             // SPBRG = 22 ( Set Baud rate 9,600 )
  179.     // SPBRG = 47;             // SPBRG = 47 ( 9600 Baud at 7.3728 Mhx clock )
  180.     // SPBRG = 22;             // SPBRG = 22 ( 9600 Baud at 3.579545Mhz clock )
  181.     SPBRG = 25;                // SPBRG = 25 ( 9600 Baud at 4 Mhx clock )
  182.     BIT_SET( TXSTA, BRGH );    // BRGH = 1   ( High speed mode )
  183.     BIT_CLEAR( TXSTA, SYNC );  // Asyncronous mode;
  184.     BIT_SET( TXSTA, TXEN );    // Enable Transmitter
  185.     BIT_SET( RCSTA, CREN );    // Enable continuous receive
  186.     BIT_SET( INTCON, PEIE );   // Enable all Peripheral Interrupts
  187. }