Truncated match.
PICList
Thread
'sending data via serial port to 16C84'
1998\07\24@001932
by
c1573
Hi,
I'm wondering if you have any code to send data via serial port to PIC
16C84
Thank you very much
Gus
1998\07\24@030348
by
Dr. Imre Bartfai
Do you need the PC or the PIC side?
Imre
On Fri, 24 Jul 1998, C1573 wrote:
{Quote hidden}> Hi,
>
> I'm wondering if you have any code to send data via serial port to PIC
> 16C84
>
> Thank you very much
>
> Gus
>
>
1998\07\24@093405
by
Andy Kunz
|
>> I'm wondering if you have any code to send data via serial port to PIC
>> 16C84
>>
>> Thank you very much
Gus,
Try this. It isn't fully tested, but should work.
Andy
#ifndef CLRWDT
#include "pic.h"
#endif
#include "defs.h"
#include "prog.h"
#include "progpins.h"
#include "rs485.h"
#if defined(_16C558)
byte bank1
txbuffer[MAX_RS485_TX_BUFFER]; // Transmit character buffer
byte rxbuffer[MAX_RS485_RX_BUFFER]; // Received character buffer
#endif
byte bank1
txcount,
txhead,
txtail,
rxcount,
rxhead,
rxtail;
byte TXREG; // Character bei
ng transmitted (shift register)
enum TXSTATE
{
TX_IDLE,
TX_START,
TX_DATA,
TX_STOP = TX_DATA+8 // Stays in here
for a while...
};
byte RCREG; // Character bei
ng received (shift register)
enum RXSTATE
{
RX_WAIT_FOR_START,
RX_IN_START,
RX_IN_DATA,
RX_IN_STOP
};
byte TXState, // Transmitter S
tate Machine
RXState; // Receiver Stat
e Machine
#define UART_TIMER0_FULLBIT ((byte)(254-104)) // 104 u
S delay
#define UART_TIMER0_HALFBIT ((byte)(254-52)) // 52 u
S delay
//
//////////////////////////////////////////////////////////////////////////////
//
// Interrupt Handler - UART
//
//////////////////////////////////////////////////////////////////////////////
//
// Author: Andy Kunz
// Date: n/a
// Modifications: none
//
static void interrupt UART (void)
{
/*
The ISR is called regularly once per bit at 9600 bps = every 104uS when
transmitting. Receiving is handled
in true bit-bop fashion, since it can't do much until the data is receiv
ed
and analyzed anyway.
The receiver hardware is configured so as to permit INT interrupts to ma
rk
the start of a bit.
This code does not permit simultaneous transmit and receive. That's fin
e,
because the hardware won't permit it
either.
*/
if (INTF) // Receiver gene
rated an interrupt?
{
TMR0 = UART_TIMER0_HALFBIT; // Set up a 1/2-
bit time delay
RXState = RX_IN_START; // Flag that we'
re checking the start bit
INTE = 0; // Disable furth
er edge interrupts until byte is complete
INTF = 0;
T0IF = 0; // Clear possibl
e TMR0 overflow false
}
if (T0IF) // 8-bit TMR0 ov
erflowed
{
T0IF = 0; // Clear interru
pt
TMR0 = UART_TIMER0_FULLBIT; // Reload the bi
t-timer
// RECEIVE
if (RXState) // Is the r
eceiver active?
{
if (RXState == RX_IN_START) // Yes, is
it the start bit (must still be
logic 0 here)
{
if (SER_RX)
{
RXState = RX_WAIT_FOR_START;
INTF = 0; //
It was a "1", so we must re-enable the INT peripheral
INTE = 1;
}
}
else if (RXState == RX_IN_STOP) // Are we g
etting the stop bit?
{
RXState = RX_WAIT_FOR_START; // Yes, mar
k end of receipt
INTF = 0; // Re-enabl
e start bit detection hardware
INTE = 1;
if (rxcount < MAX_RS485_RX_BUFFER)// Stick re
ceived character in
circular buffer
{
rxbuffer[rxtail] = RCREG;
rxtail++;
rxcount++;
if (rxtail >= MAX_RS485_RX_BUFFER)
rxtail = 0;
}
}
else
{
RCREG = RCREG >> 1; // Shift th
e received byte down 1 bit
if (!(SER_RX)) // Insert i
nverted data bit
RCREG |= 0x80;
}
}
// TRANSMIT
else // Must be trans
mitter-side (or nothing)
{
if (TXState == TX_IDLE) // transmit
ter is idle?
{
if (txcount != 0) // if char
acters to send
{
SER_DIR = SER_TRANSMIT; //
Set direction properly
SER_TX = 0; //
Start with a start bit
TXREG = txbuffer[txhead];//
Prepare character to send
txhead++; //
Bump pointer & counter
if (txhead >= MAX_RS485_TX_BUFFER)
txhead = 0;
txcount--;
TXState++; //
Record new state
}
else
SER_DIR = SER_RECEIVE; // Allow da
ta to flow back into here
}
else // transmitter i
s pumping out data
{
SER_TX = ! (TXREG & 0x01); // Output i
nverted data bit
TXREG = (TXREG >> 1) | 0x80; // Shift do
wn for next bit & insert a stop
bit
TXState++; // Bump sta
te counter
if (TXState == TX_STOP+1) // End of t
he byte?
TXState = TX_IDLE; //
Reset to 0
}
}
}
}
//***********************************************************************
//
// RS485_PUTC - Writes char to RS-485 buffer
//
//***********************************************************************
void rs485_putc (char c) // Write single character into t
ransmit buffer
{
do
CLRWDT();
while (txcount == MAX_RS485_TX_BUFFER); // Wait until there's room in th
e
buffer
if (txtail >= MAX_RS485_TX_BUFFER) // Limit txtail
txtail = 0;
txbuffer[txtail] = c; // Grab incoming character
txtail++; // Bump tail
txcount++; // and counter
}
//***********************************************************************
//
// RS485_GETC - Reads char from RS-485 buffer
//
//***********************************************************************
char rs485_getc (void) // Get next character from recei
ve buffer
{
char c;
while (rxcount == 0) // Wait for something to come in
CLRWDT();
rxcount--; // Decrement counter
c = rxbuffer[rxhead]; // fetch character
rxhead++; // Bump pointer
if (rxhead >= MAX_RS485_RX_BUFFER) // and wrap it around
rxhead = 0;
return (c);
}
//***********************************************************************
//
// RS485_KBHIT - Return number of chars in buffer
//
//***********************************************************************
byte rs485_kbhit (void) // Get next character from recei
ve buffer
{
return (rxcount);
}
//***********************************************************************
//
// RS485_INIT - sets up everything, starts it going
//
//***********************************************************************
void rs485_init (void)
{
di (); // Disable interrupts before mon
keying around here
txcount = 0; // First initialize variables
txhead = 0;
txtail = 0;
rxcount = 0;
rxhead = 0;
rxtail = 0;
TRIS_SER_RX = 1; // Always the correct direction
here
TRIS_SER_TX = 1; // Set up chip so that initially
it is a receive-only
device
TRIS_SER_DIR = 1; // This gets changed around once
we seize control of
the bus
SER_DIR = SER_RECEIVE; // Allow data to flow into this
PIC
RXState = RX_WAIT_FOR_START; // Just looking
TXState = TX_IDLE; // Nothing happening
TMR0 = UART_TIMER0_FULLBIT; // Set up for start of edge
T0IE = 1; // Enable timer0 interrupt
}
//***********************************************************************
//
// RS485_GRAB - grabs control of serial bus
//
//***********************************************************************
void rs485_grab (void)
{
di ();
SER_DIR = SER_RECEIVE; // Allow data to flow into this
PIC
TRIS_SER_DIR = 0; // Seize control of direction li
ne, force to receive mode
TRIS_SER_TX = 0; // Seize control of transmit dat
a line, force to idle
state (stop bit)
TRIS_SER_RX = 1; // Always the correct direction
here
ei ();
}
The important pins in PROGPINS.H are:
#define PIN(n,x,y) static volatile bit n @ (unsigned)&x*8+y
PIN (SER_RX, PORTB,0); // IN 6 7 RS-485 RX pin
PIN (SER_TX, PORTB,1); // I/O 7 8 RS-485 TX pin (set to in
put when PIC17
is active)
PIN (SER_DIR, PORTB,2); // I/O 8 9 Serial Direction control
for RS-485
(set to input when PIC17 is active)
==================================================================
Andy Kunz - Statistical Research, Inc. - Westfield, New Jersey USA
==================================================================
'sending data via serial port to 16C84'
1998\08\04@024628
by
Dr. Imre Bartfai
Hi,
on the PC side it is the question whether you want to use a terminal
emulation program (i. e. you want to send the data manually), or a program
want to communicate. On the other hand it is the question whether you want
only to send data or also receive from the PIC. The next question is,
whether you use DOS, or Windows, or something other OS. (I know, Windows
is really not an OS <G>).
On the PIC side it is the question whether you can poll the line, or you
need receiving in asynchronous mode, thus interrupt-driven.
The correct answer can be given if all these information are collected.
Imre
On Fri, 24 Jul 1998, Dr. Imre Bartfai wrote:
{Quote hidden}> Do you need the PC or the PIC side?
> Imre
>
>
> On Fri, 24 Jul 1998, C1573 wrote:
>
> > Hi,
> >
> > I'm wondering if you have any code to send data via serial port to PIC
> > 16C84
> >
> > Thank you very much
> >
> > Gus
> >
> >
>
>
More... (looser matching)
- Last day of these posts
- In 1998
, 1999 only
- Today
- New search...