CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

Need help w/ pic code to change RS232 Rates

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
iso9001



Joined: 02 Dec 2003
Posts: 262

View user's profile Send private message

Need help w/ pic code to change RS232 Rates
PostPosted: Wed Mar 16, 2005 1:05 pm     Reply with quote

Hi, using 16F876

I have a device that is setup to use rs232 on a non standard baud rate (7812.5) and need to convert that into something the PC can see.

I have no idea if someone makes a terminal that uses software "listening" to so that it can opertate at funny baud rates like this. That ofcourse would be the most practical, but I cant find anything like that.

My ideas are: Create a simple timer/delay/check system to monitor the lines, and then get the data in that way, then spit it out to the computer via hardware rs232.

Or if that gets two complex, split the load over two pics. Mabe use 1 for getting the non standard baud in and i2c it over to the other pic waiting to take data in and send it to the pc ?


I've not really used a ton of rs232 before so go easy on me Smile

TIA
SherpaDoug



Joined: 07 Sep 2003
Posts: 1640
Location: Cape Cod Mass USA

View user's profile Send private message

PostPosted: Wed Mar 16, 2005 1:34 pm     Reply with quote

The CCS software UART can do any baud rate if the Xtal is fast enough. I would look at a chip with a hardware UART for the standard speed side, and a fast clock so you can do the odd speed in a software UART.

Does the odd speed change? Or can you measure it and write it into the code? Twenty years ago I worked on a product that had to deal with doppler shifted data sent acoustically. The baud rate could change noticably during a single message. That was a really complex software UART!
_________________
The search for better is endless. Instead simply find very good and get the job done.
iso9001



Joined: 02 Dec 2003
Posts: 262

View user's profile Send private message

PostPosted: Wed Mar 16, 2005 1:40 pm     Reply with quote

No its steady 7812.5 baud. Its a stupid motorolla chip inside this thing. They like to use rates like that apparently.

I have some 20Mhz xtals here. You're saything this should be pretty easy using a single pic then ?

I'm in a time crush and having never coded rs232 and I only have 2 days or so to do this is going to suck. Havn't even seen how I pull a byte from the buffer yet.

I really wish someone just made terminal software to read in oddball rates.
newguy



Joined: 24 Jun 2004
Posts: 1902

View user's profile Send private message

PostPosted: Wed Mar 16, 2005 1:49 pm     Reply with quote

iso9001,

Here is an example that I gave to one of my classes recently. It's pretty self-explanatory. It will help you get started.

Code:
#include <18F452.h>
#device adc=8
#use delay(clock=4000000,RESTART_WDT)
#fuses XT, BROWNOUT, BORV20, PUT, STVREN, NOLVP
#use rs232(baud=9600,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8)

#include "i:\ELEN\LCD Task\lcd_driver.c"

// this program is based on one found in the text,
// "Embedded C Programming and the Microchip PIC",
// by Barnett, Cox, and O'Cull

#define RX_BUFFER_SIZE 80
#define TX_BUFFER_SIZE 80

int8 rx_wr_index = 0, tx_rd_index = 0, tx_wr_index = 0, tx_counter = 0, received = 0;
unsigned int8 rx_buffer[RX_BUFFER_SIZE + 1], tx_buffer[TX_BUFFER_SIZE + 1];
int1 data_avail = FALSE;

#int_RDA
void RDA_isr(void) {
   rx_buffer[rx_wr_index] = getc();
   rx_wr_index++;
   if (rx_wr_index > RX_BUFFER_SIZE) {
      rx_wr_index = 0;
   }
   data_avail = TRUE;
}

#int_TBE
void TBE_isr(void) {
   if (tx_counter != 0) {
      putc(tx_buffer[tx_rd_index]);
      if (++tx_rd_index > TX_BUFFER_SIZE) {
         tx_rd_index = 0;
      }
      tx_counter--;
      if (tx_counter == 0) {
         disable_interrupts(INT_TBE);
      }
   }
}

void bputc(int c) {
   int restart = 0;

   while (tx_counter > (TX_BUFFER_SIZE - 1));

   if (tx_counter == 0) {
      restart = 1;
   }
   tx_buffer[tx_wr_index++] = c;
   if (tx_wr_index > TX_BUFFER_SIZE) {
      tx_wr_index = 0;
   }
   tx_counter++;
   if (restart == 1) {
      enable_interrupts(INT_TBE);
   }
}

void main() {

   setup_adc_ports(NO_ANALOGS);
   setup_adc(ADC_OFF);
   setup_psp(PSP_DISABLED);
   setup_spi(FALSE);
   setup_wdt(WDT_ON);
   setup_timer_0(RTCC_INTERNAL|RTCC_OFF|RTCC_8_bit);
   setup_timer_1(T1_DISABLED);
   setup_timer_2(T2_DISABLED,0,1);
   setup_timer_3(T3_DISABLED|T3_DIV_BY_1);
   enable_interrupts(INT_RDA);
   //enable_interrupts(INT_TBE);
   enable_interrupts(global);

   set_tris_e(0);
   lcd_init();
   lcd_putc("\f");

   while (TRUE) {
      restart_wdt();
      if (data_avail) {
         data_avail = FALSE;
         received++;
         if (received == 21) {
            lcd_gotoxy(1,2);
         }
         else if (received == 41) {
            received = 0;
            lcd_putc("\f");
         }
         if (rx_wr_index == 0) {
            lcd_putc(rx_buffer[RX_BUFFER_SIZE]);
            bputc(rx_buffer[RX_BUFFER_SIZE]);
         }
         else {
            lcd_putc(rx_buffer[rx_wr_index - 1]);
            // echo is below
            bputc(rx_buffer[rx_wr_index - 1]);
         }
      }
   }

}
bluetooth



Joined: 08 Jan 2005
Posts: 74

View user's profile Send private message

PostPosted: Wed Mar 16, 2005 1:52 pm     Reply with quote

iso:

I haven't tried it, but I notice that CCS's Port Monitor lets you set the baud rate to "other" - I entered 7812 in mine and it at least took it. Don't know if it works (might be PC dependent based on UART), but it might be worth a try....

Let us know if it works!
iso9001



Joined: 02 Dec 2003
Posts: 262

View user's profile Send private message

PostPosted: Wed Mar 16, 2005 2:17 pm     Reply with quote

Newguy: Thank Very Much! I looked through it quick and looks very easy to understand. I didn't look at the TX part too close so I'm not 100% on that but I just need to read instead of skim.

bluetooth: AH! You are the man! I'll try this out, its a good thing I got some max232s in as smaples awhile back Smile Now why don't any other terminal software program do this ?

I'll try the software first as all I have to do there is set up a max232 and some wire. But If that doesnt work I'll have to build a pic setup as I just don't have the time for any crazy terminal software seraching
SherpaDoug



Joined: 07 Sep 2003
Posts: 1640
Location: Cape Cod Mass USA

View user's profile Send private message

PostPosted: Wed Mar 16, 2005 2:54 pm     Reply with quote

iso9001 wrote:

I really wish someone just made terminal software to read in oddball rates.


Terminal.exe available shareware at http://bray.velenje.cx/avr/terminal/ takes custom baudrates. It is my favorite terminal program. For the price (free) it is worth a try.
_________________
The search for better is endless. Instead simply find very good and get the job done.
Humberto



Joined: 08 Sep 2003
Posts: 1215
Location: Buenos Aires, La Reina del Plata

View user's profile Send private message

PostPosted: Wed Mar 16, 2005 5:06 pm     Reply with quote

The original request:

Quote:

Hi, using 16F876

I have a device that is setup to use rs232 on a non standard baud rate
(7812.5) and need to convert that into something the PC can see.



The following code is to start testing, it receives a non standard baud rate
and echoes using another port to the PC at 9600 Bauds . This code is
short and self-explanatory for starting purposes. Of course the deal is
using a ring buffer, but to implement that we need to know the
header/trailer character and/or how long the data stream zise is.
Code:

#include <16F876.h>
#device *=16
#fuses NOWDT,HS, PUT, NOPROTECT, BROWNOUT, NOLVP, NOCPD, NOWRT, NODEBUG
#use delay(clock=20000000)
#use rs232(baud=9600,parity=N,xmit=PIN_C6,bits=8,STREAM=PC)
#use rs232(baud=7912,parity=N, rcv=PIN_B0,bits=8,STREAM=MOTOROLA)

#zero_ram

#define YES 1
#define NO  0

static int8 data, char_rcved;

#int_EXT
void EXT_isr()
{
   if(kbhit(MOTOROLA))
     {
      data = fgetc(MOTOROLA);
      char_rcved = YES;
     }
}


void main()
{
   ext_int_edge( H_TO_L );   
   enable_interrupts(INT_EXT);
   enable_interrupts(GLOBAL);

   while(1)
        {
         if(char_rcved)
           {
            fputc(data, PC);
            char_rcved = NO;
           }
        }
}


hope this help,

Humberto
Mark



Joined: 07 Sep 2003
Posts: 2838
Location: Atlanta, GA

View user's profile Send private message Send e-mail

PostPosted: Wed Mar 16, 2005 9:21 pm     Reply with quote

I'd just get a PIC with 2 UARTs on it. Very Happy Or if it is okay, USB to the PC.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group