lguancho
Joined: 08 Sep 2003 Posts: 5
|
Winpenny RS485 Source Code Question |
Posted: Fri Feb 18, 2005 2:59 am |
|
|
Hi, all. I am trying to port and test Winpenny's RS485 code from Pic16F to Pic18F452. I redefine the register addresses (PORTX, TRISX etc) in the header file. The code could be compiled, and I am trying to test it in with a chip, sending some command string to my PC (turn on the serial port monitor in CCS IDE). I keep receiving a series of '0' , instead of some "Hello" string.
I find that the problem lies with the setup() and configurecom() function. When I disables these functions, and use CCS #use Rs232() fuction, I can receive without any problem.
Could anyone help to see what I have done wrong on the setup() and configurecom()
[code]
#define MASTER
#include <18F452.h>
#use delay(clock=20000000)
#fuses HS,NOLVP,NOWDT,PUT
#use rs232(baud=9600,parity=N,xmit=PIN_C6,rcv=PIN_C7,errors, enable=PIN_C5)
#include "D:\rs485net\Include\reg18F452.h"
#include "D:\rs485net\Include\rs485.h"
#include <stdlib.h>
#include "D:\rs485net\Include\lcd873b.h"
#include "D:\rs485net\lcd873b.c" // Keypad LCD routine
#include "D:\rs485net\Rs485.c" // RS485 protocol driver
#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()
{
TMR0L = TMR0_RELOAD; // Reload PICs timer
BIT_CLEAR( INTCON, TMR0IF ); // Clear interrupt flag
}
//**************************************************
// USART interrupt service routine
//**************************************************
#INT_RDA
void UsartInterrupt(void)
{
while ( BIT_TEST( PIR1, RCIF ) ) // If USART RX Interrupt
{
TMR0L = TMR0_RELOAD; //TMR0_RELOAD
cRs485RxChar = getc(); //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 = getc(); // =RCREG
cRs485RxChar = getc(); // =RCREG
bit_set( RCSTA, CREN ); // Enable receiver.
}
bit_clear( PIR1, RCIF );
}
}
void main(void)
{
char cPacketReady;
char cLenOfData;
char cCmd, c;
//Setup(); // Enable interrupts
//BIT_CLEAR( INTCON, TMR0IE ); // 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 = 'A'; //0x10;
cOurAddrLow = 'A'; //0x10;
Rs485Initialise( cOurAddrHigh,cOurAddrLow );
#ifdef MASTER
cTest[0] = 'H';
cTest[1] = 'e';
cTest[2] = 'l';
cTest[3] = 'l';
cTest[4] = 'o';
cTest[5] = 13;
ClearLine2();
LcdWrite("GET DATA PKT");
Rs485SendPacket( SENSOR_GET_DATA, 5, &cTest[0] );
delay_ms(2500);
delay_ms(2500);
delay_ms(2500);
delay_ms(2500);
/*
ClearLine2();
LcdWrite("POLL PKT");
Rs485SendPacket( SENSOR_POLL_CMD, 0, &cTest[0] );
delay_ms(2500);
delay_ms(2500);
delay_ms(2500);
delay_ms(2500);
*/
/*
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] );
delay_ms(10000);
Rs485SendPacket(cCmd, cLenOfData, &cTest[0] );
/*
ClearLine2();
LcdWrite( "CMD: " );
BinToHexAscii( cCmd );
LcdWrite( ' ' );
if ( cLenOfData != 0 )
{
for ( c=0; c<cLenOfData; c++ ) LcdWrite( cTest[c] );
}
//Rs485SendPacket( SENSOR_ACK, 0, NULL );
delay_ms(255);
Rs485Initialise(cOurAddrHigh,cOurAddrLow);
ClearLine1();
LcdWrite("Waiting........");
}
*/
}
}
//**************************
// Setup Hardware
//**************************
void Setup(void)
{
T0CON = 0xC4; // 8Bit TMR0 prescal = enable, rate = 1:32
INTCON = 0; // disable global interupts
// ADCON1 = 0x07; // Disable ADC
TRISA = PortAConfig; // 0b00111111 for slave
TRISB = PortBConfig; // 0b00000000 for slave
TRISC = PortCConfig; // 0b00000000
TRISD = PortDConfig;
TRISE = PortEConfig;
//PORTC = 0xFF;
LCDSetup(); // Setup the LCD Display
//ConfigureComms(); /* Configure USART for Asyncronous Comms */
//BIT_SET(INTCON,GIE);
}
//*******************************************************/
//* Configure PIC18F452 USART for communications */
//* */
//* Asynchronous mode */
//* 8 data bits ( For other rates see PIC16F8XX Data ) */
//* 1 stop bits */
//* No Parity */
//* */
//*******************************************************/
void ConfigureComms(void)
{
// SPBRG = 64; // Set Baud rate 19,200 20MHz clock
SPBRG = 129; // 9600 Baud at 20MHz
BIT_CLEAR( TXSTA, TX9 ); // 8 bit transmit mode
BIT_SET( TXSTA, BRGH ); // BRGH = 1 High speed mode- Note smaller baud rate error%
BIT_CLEAR( TXSTA, SYNC ); // Asyncronous mode;
BIT_SET( TXSTA, TXEN ); // Enable Transmitter
PIR1 = 0x00; // Clear all interrupt flags including AUSART TX & RX
BIT_SET( PIE1,TXIE ); // Enable Transmit Interrupts
BIT_SET( PIE1,RCIE ); // Enable Receive Interrupts
BIT_SET( INTCON, PEIE ); // Enable all Peripheral Interrupts
BIT_SET( RCSTA, CREN ); // Enable continuous receive
BIT_CLEAR( RCSTA, RX9 ); // 8 bit receive mode
BIT_SET( RCSTA, SPEN ); // Enable Serial port
}
[/code]
Question 2: Inside the serial rx ISR, the TMR0 is loaded with a value of 255 (defined by TMR0_Reload). I don't understand this? Could someone help to explain the purposes of this timer function within the rx ISR?
Warmest Regards
lguancho |
|