RS485网络协议的测试程序

发布时间:2022-12-27  

//****************************************************************************

// TsRs485.c

// Test of RS485 network protocol

// Version 1.0 beta

// Processor: PIC16F87x

// Compiler: CCS PCM 3.02

//****************************************************************************

#DEVICE PIC16F873 *=16

#include <16f873.h>

#define MASTER

#use delay(clock=4000000)

#fuses HS,NOWDT,PUT,BROWNOUT,NOPROTECT,NOLVP,NOCPD,NOWRT

#include

#include

#include // RS485 packet protocol driver

//#include // LCD routine

#include // Keypad example LCD routine

#use fast_io(A)

#use fast_io(B)

#use fast_io(C)

#use fast_io(D)

#use fast_io(E)

char cTest[10];

//**************************************************

// Timer Interrupt service routine

//**************************************************

#INT_RTCC

clock_isr()

{

TMR0 = TMR0_RELOAD; // Reload PICs timer

BIT_CLEAR( INTCON, T0IF ); // Clear interrupt flag

}

//**************************************************

// USART interrupt service routine

//**************************************************

#INT_RDA

void UsartInterrupt(void)

{

while ( BIT_TEST( PIR1, RCIF ) ) // If USART RX Interrupt

{

TMR0 = TMR0_RELOAD;

cRs485RxChar = RCREG;

if ( !( RCSTA & 6 ) ) // Then if no errors

{ // Process received character

Rs485Decode(); // Decode RS485 network trafic

bit_set( RCSTA, CREN ); // Enable receiver.

}

else

{

bit_clear( RCSTA, CREN ); // Clear any errors

cRs485RxChar = RCREG;

cRs485RxChar = RCREG;

bit_set( RCSTA, CREN ); // Enable receiver.

}

bit_clear( PIR1, RCIF );

}

}


void main(void)

{

char cPacketReady;

char cLenOfData;

char cCmd, c;

Setup();

BIT_CLEAR( INTCON, T0IE ); // Timer interrupts off

#ifndef MASTER

LcdWrite("Waiting........");

#else

LcdWrite("Sending message");

#endif

// Just setup the network address

//write_eeprom( NET_ADDRESS_HIGH, 0x10 );

//write_eeprom( NET_ADDRESS_LOW, 0x10 );

//cOurAddrHigh = read_eeprom( NET_ADDRESS_HIGH );

//cOurAddressLow = read_eeprom( NET_ADDRESS_LOW );


while ( 1 )

{

cOurAddrHigh = 0x10;

cOurAddrLow = 0x10;

Rs485Initialise( cOurAddrHigh,cOurAddrLow );

#ifdef MASTER

cTest[0] = 'H';

cTest[1] = 'e';

cTest[2] = 'l';

cTest[3] = 'l';

cTest[4] = 'o';

cTest[5] = 0;

ClearLine2();

LcdWrite("GET DATA PKT");

Rs485SendPacket( SENSOR_GET_DATA, 5, &cTest[0] );

delay_ms(250);

delay_ms(250);

delay_ms(250);

delay_ms(250);

ClearLine2();

LcdWrite("POLL PKT");

Rs485SendPacket( SENSOR_POLL_CMD, 0, &cTest[0] );

delay_ms(250);

delay_ms(250);

delay_ms(250);

delay_ms(250);

cOurAddrHigh = 0x10;

cOurAddrLow = 0x18;

// Send a packet to another node

Rs485Initialise( cOurAddrHigh,cOurAddrLow );

Rs485SendPacket( SENSOR_POLL_CMD, 0, &cTest[0] );

delay_ms(250);

delay_ms(250);

delay_ms(250);

delay_ms(250);

#endif

cPacketReady = Rs485Process();

if ( cPacketReady )

{

Rs485GetPacket( &cCmd, &cLenOfData, &cTest[0] );

ClearLine2();

LcdWrite( "CMD: " );

BinToHexAscii( cCmd );

LcdWrite( ' ' );

if ( cLenOfData != 0 )

{

for ( c=0; c}

//Rs485SendPacket( SENSOR_ACK, 0, NULL );

delay_ms(255);

Rs485Initialise(cOurAddrHigh,cOurAddrLow);

ClearLine1();

LcdWrite("Waiting........");

}

}

}

//**************************

// Setup Hardware

//**************************

void Setup(void)

{

OPTIONREG = 0x84; // pullups,prescailer = TMR0 , rate = 1:32

INTCON = 0; // disable global interupts

ADCON1 = 0x07; // Disable ADC

TRISA = PortAConfig;

TRISB = PortBConfig;

TRISC = PortCConfig;

TRISD = PortDConfig;

TRISE = PortEConfig;

PORTC = 0xFF;

LCDSetup(); // Setup the LCD Display

ConfigureComms(); /* Configure USART for Asyncronous Comms */

PORTC = 0xF7;

}


//*******************************************************/

//* 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


// 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

}


文章来源于:电子工程世界    原文链接
本站所有转载文章系出于传递更多信息之目的,且明确注明来源,不希望被转载的媒体或个人可与我们联系,我们将立即进行删除处理。

我们与500+贴片厂合作,完美满足客户的定制需求。为品牌提供定制化的推广方案、专属产品特色页,多渠道推广,SEM/SEO精准营销以及与公众号的联合推广...详细>>

利用葫芦芯平台的卓越技术服务和新产品推广能力,原厂代理能轻松打入消费物联网(IOT)、信息与通信(ICT)、汽车及新能源汽车、工业自动化及工业物联网、装备及功率电子...详细>>

充分利用其强大的电子元器件采购流量,创新性地为这些物料提供了一个全新的窗口。我们的高效数字营销技术,不仅可以助你轻松识别与连接到需求方,更能够极大地提高“闲置物料”的处理能力,通过葫芦芯平台...详细>>

我们的目标很明确:构建一个全方位的半导体产业生态系统。成为一家全球领先的半导体互联网生态公司。目前,我们已成功打造了智能汽车、智能家居、大健康医疗、机器人和材料等五大生态领域。更为重要的是...详细>>

我们深知加工与定制类服务商的价值和重要性,因此,我们倾力为您提供最顶尖的营销资源。在我们的平台上,您可以直接接触到100万的研发工程师和采购工程师,以及10万的活跃客户群体...详细>>

凭借我们强大的专业流量和尖端的互联网数字营销技术,我们承诺为原厂提供免费的产品资料推广服务。无论是最新的资讯、技术动态还是创新产品,都可以通过我们的平台迅速传达给目标客户...详细>>

我们不止于将线索转化为潜在客户。葫芦芯平台致力于形成业务闭环,从引流、宣传到最终销售,全程跟进,确保每一个potential lead都得到妥善处理,从而大幅提高转化率。不仅如此...详细>>