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 CCS Technical Support

PIC18F452 RS232 Receive Problem

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



Joined: 13 Jul 2023
Posts: 37

View user's profile Send private message

PIC18F452 RS232 Receive Problem
PostPosted: Sat Aug 12, 2023 5:16 am     Reply with quote

Hello everyone.

I am trying to provide RS232 communication with Pic18f452 chip. With the code I shared below, I can send data to the serial port via Putty, but unfortunately, when I try to receive data from the putty terminal to the PIC, the code stops for some reason.
Code:


#use delay(clock=20000000)
#fuses HS, NOWDT, PUT, BROWNOUT, NOLVP
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, parity=N, stop=1, bits=8, RECEIVE_BUFFER=128, ERRORS/*, TXISR*/)

void main()
{
while (1) {
            printf("Enter:    \n");
            putc(54);
            output_low(PIN_D4);
            delay_ms(5);
           
            c = getc();
           
            if (c == 'a')
            {
            output_high(PIN_C2);
            delay_ms(5);
            printf(" yes ");
            }
            else
            printf(" no ");
            delay_ms(1000);

}
}



In the code above, no matter what value I send from the terminal, the code stops, so it's like it can't call the getc function.

I used to communicate with the PIC18f2520 with a very similar configuration and without any problems, but for some reason I cannot receive data with this chip.

I would be very grateful if you could help me with this.
Ttelmah



Joined: 11 Mar 2010
Posts: 19540

View user's profile Send private message

PostPosted: Sat Aug 12, 2023 5:35 am     Reply with quote

Code:

void main()
{
enable_interruptsGLOBAL);
while (1) {


Note the single extra line.
You are not enabling the global interrupt, so with the receive buffer, the
receive cannot work.


Last edited by Ttelmah on Sat Aug 12, 2023 5:35 am; edited 1 time in total
temtronic



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

View user's profile Send private message

PostPosted: Sat Aug 12, 2023 5:35 am     Reply with quote

I don't like the ...ERRORS/*...... )
Maybe the compiler can properly decode and not complain BUT just delete instead,compile and test run. see if that helps ???
Ttelmah



Joined: 11 Mar 2010
Posts: 19540

View user's profile Send private message

PostPosted: Sat Aug 12, 2023 5:38 am     Reply with quote

He isn't enabling the TX buffer, so TXISR doesn't do anything.
He is though enabling the receive interrupt, but not enabling the global
interrupt, so it won't work....
bmete



Joined: 13 Jul 2023
Posts: 37

View user's profile Send private message

PostPosted: Sat Aug 12, 2023 6:12 am     Reply with quote

Hello Ttelmah, temtronic. Thanks for quick replies.

Code:


#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, parity=N, stop=1, bits=8, RECEIVE_BUFFER=128, ERRORS)


enable_interrupts(GLOBAL);



I enabled global interrupts and edited fuses as you said. However, I still cannot use the getc function in the same way.
Previously, when working with the 18F2520, I had to switch the enable pin before each transmit or receive operation. So I did the same, thinking that this will be the case with this chip as well. Could there be a problem with it?
Ttelmah



Joined: 11 Mar 2010
Posts: 19540

View user's profile Send private message

PostPosted: Sat Aug 12, 2023 11:12 am     Reply with quote

Enable pin????.
What enable pin?.
Is this RS485, not RS232.
How is it wired?.
bmete



Joined: 13 Jul 2023
Posts: 37

View user's profile Send private message

PostPosted: Sat Aug 12, 2023 5:59 pm     Reply with quote

Yes this is RS485 I am using SN75176.

As far as I know, when I configure RS232 to the PIC and use it with an IC like SN75176, I create a proper RS485 communication. Please correct me if I'm wrong.

It looks like I'm currently communicating properly with the following settings.

Code:


#use delay(crystal=20MHz)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, parity=N, stop=1, bits=8, RECEIVE_BUFFER=128, ERRORS, TXISR)
#define USE_TX_ISR
#define RS232ENABLE PIN_D4

void main()
{

enable_interrupts(GLOBAL);

while(TRUE)
   {

   output_low(RS232ENABLE);
   delay_ms(5);
   if (kbhit()) {     
   c = getc();
   if (c == '1')
   {
    output_high(OUT1);
    printf("\r\n#OUT1 ");
    delay_ms(5);
    }
   }
}
}

Ttelmah



Joined: 11 Mar 2010
Posts: 19540

View user's profile Send private message

PostPosted: Sun Aug 13, 2023 1:27 am     Reply with quote

No, you don't......

You have to tell the #USE RS232 to operate the direction control pin.

#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, parity=N, stop=1, bits=8, RECEIVE_BUFFER=32, ERRORS, ENABLE=PIN_D4)

I'm assuming what you are calling 'enable' is actually the direction
control line.
You only need to enable TXISR, if you enable a transmit buffer. Seriously,
128, is a huge receive buffer. I have turned this down. If you want to
use interrupt driven transmit, then setup a transmit buffer, and enable
this.
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