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

Tx\Rx of String Problems

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



Joined: 06 Jan 2014
Posts: 2

View user's profile Send private message

Tx\Rx of String Problems
PostPosted: Mon Jan 06, 2014 8:03 am     Reply with quote

Hi All

I'm currently working on a project that requires string transmission and reception using interrupts. To start myself off, I decided to try out the example code EX_SISR.c. However the example code does not seem to work as expected.

Here is all the relevant information -
CCS Compiler: v5.015
Target Device: PIC16F1509
Target Voltage: 12V

Code:

#include <16F1509.h>
#fuses HS,NOWDT,PROTECT,BROWNOUT
#use delay(Clock=16000000)
#use rs232(baud=9600, xmit=PIN_B7, rcv=PIN_B5, parity=N, bits=8, ENABLE=PIN_A1, ERRORS)


My Tx\Rx goes through a ADM1485 and then to my PC. As far as I understand my "#use rs232(...)" is correct.

Here is an example of the output that I receive on the CCS Serial Monitor running the EX_SISR.c code on my PIC:

Running...

Buffered data =>
Running...
hello
Buffered
Running...
hello
Buffered
Running...
hello
Buffered
Running...
hello
Buffered
Running...
hello
Buffered
Running...
hello
Buffered
Running...

The strange thing is that the initial delay takes place as expected, however after I send some data to the PIC it just outputs the above text in a loop without delay. I tried used Hypertermial but the result was the same. I've done a fair amount of reading on other CCS forum threads and I think the problem may have something to do with the receive buffer, or perhaps my ADM1485 hardware configuration.

I hope I have included enough information. Any help is greatly appreciated.

Regards
temtronic



Joined: 01 Jul 2010
Posts: 9120
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Mon Jan 06, 2014 8:30 am     Reply with quote

comment: Welcome to the forum! First off, I know the ex_sisr.c works fine so we need more information. Please post your code(using code button) so we can see what you're doing.You should also post a link to the adm1485 as there might be a hardware config issue.
A simple test of removing PIC and linking tx to rx of the adm1485 should make a 'loopback' connection, so that anything typed in the 'terminal program' will be displayed on the PC screen. If this works, then you've eliminated the PC, terminal program,wiring and the adm1485 so prolem will be with the PIC and or program.
Another test is try is the '1Hz flashing LED' program.Simple use the 'toggle' function to flash an LED at 1Hz.This will confirm the PIC, fuses,power supply, etc. are A-OK.

If both these work, then it must be your 'sisr' program.
That's why we need to see it. Could be a simple typo!

hth
jay
asmboy



Joined: 20 Nov 2007
Posts: 2128
Location: albany ny

View user's profile Send private message AIM Address

PostPosted: Mon Jan 06, 2014 2:07 pm     Reply with quote

what are you toggling with the ENABLE pin declaration ???
i sure hope its not the level translator chip
Very Happy Very Happy Very Happy Very Happy
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Mon Jan 06, 2014 5:45 pm     Reply with quote

ADM1485 is an RS485 transceiver.
http://www.analog.com/static/imported-files/data_sheets/ADM1485.pdf
A PC normally has only an RS-232 com-port. To connect a PIC to a PC,
you normally use a Max232A or similar RS-232 level converter chip.
DanH



Joined: 06 Jan 2014
Posts: 2

View user's profile Send private message

PostPosted: Tue Jan 07, 2014 8:04 am     Reply with quote

Hi All

Thanks for all your responses thus far; temtronic's comments were especially helpful. I figured out what the the main issue is and it has to do with hardware. The Receiver Output Enable pin on the ADM1485 has been wired to ground, which means that my PIC always receives data, even when transmitting (I enable transmit via PIN_A1). Unfortunately, all the PCB's for the current project have been printed so I will need to work around the issue. For example, disabling INT_RDA before transmission and clearing the buffer after transmission so that I don't get the transmitted data echoed back.

However, I have another issue. My latest code:

Code:
#include <16F1509.h>

#fuses HS,NOWDT,PROTECT,BROWNOUT
#device ADC=10    // Use ADC with 10 bits
#use delay(Clock=16000000)
#use rs232(baud=9600, xmit=PIN_B7, rcv=PIN_B5, parity=N, bits=8, ENABLE=PIN_A1, ERRORS)

#define BUFFER_SIZE 32
BYTE buffer[BUFFER_SIZE];
BYTE next_in = 0;
BYTE next_out = 0;

#int_rda

void serial_isr() {
    int t;

    buffer[next_in] = getc();
    t = next_in;
    next_in = (next_in + 1) % BUFFER_SIZE;
    if (next_in == next_out)
        next_in = t; // Buffer full !!
}

#define bkbhit (next_in!=next_out)

BYTE bgetc() {
    BYTE c;

    while (!bkbhit);
    c = buffer[next_out];
    next_out = (next_out + 1) % BUFFER_SIZE;
    return (c);
}

void clear_usart(void) {
    char c;
    while (kbhit()) c = getc();
}

void main() {
    setup_oscillator(OSC_INTRC | OSC_16MHZ);
    enable_interrupts(GLOBAL);

    disable_interrupts(INT_RDA);
    printf("\r\nRunning...\r\n");
    clear_usart();
    enable_interrupts(INT_RDA);

//    The program will delay for 10 seconds and then display
//    any data that came in during the 10 second delay

        do {
            enable_interrupts(INT_RDA);
            delay_ms(10000);
            disable_interrupts(INT_RDA);
            printf("\r\nBuffered data => ");
            clear_usart();
            while (bkbhit)
                putc(bgetc());
        } while (TRUE);
}


produces the following:

Running...

Buffered data => hello
Buffered data => heworld
Buffered data => he
Buffered data => he

I'm one step closer, but I don't understand why those first two characters are stuck there, even after I enter a new string.

One more thing,

Quote:
ADM1485 is an RS485 transceiver.
http://www.analog.com/static/imported-files/data_sheets/ADM1485.pdf
A PC normally has only an RS-232 com-port. To connect a PIC to a PC,
you normally use a Max232A or similar RS-232 level converter chip


My ADM1485 connects to my PC via a USB to RS485 converter, my apologies for forgetting to mention that.

Regards
asmboy



Joined: 20 Nov 2007
Posts: 2128
Location: albany ny

View user's profile Send private message AIM Address

PostPosted: Tue Jan 07, 2014 8:42 am     Reply with quote

1- stop disabling the RDA -

2- get rid of the long delay.

3-stop clearing the usart ( think about it: you can collapse the buffer simply by manipulating its byte removal pointers !!! set nextin=next out.....)

4-let the RDA buffering operate continuously and make clever use of what you find in the buffer......

5- be smart and get rid of the modulus operator , as has been suggested.
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