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

rs232 hardware question
Goto page Previous  1, 2
 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
gjs_rsdi



Joined: 06 Feb 2006
Posts: 468
Location: Bali

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

PostPosted: Fri Jun 16, 2017 1:15 am     Reply with quote

Thanks Jay Smile

More I am reading about the optocouplers, more I am convinced they don't fit my project. They have delays in us an the rising and falling edge is problematic also.
Digital isolators are more expensive but they re working like logic gates.

Best wishes
Joe
Ttelmah



Joined: 11 Mar 2010
Posts: 19195

View user's profile Send private message

PostPosted: Fri Jun 16, 2017 2:24 am     Reply with quote

Unless you are trying for silly speeds, the delays for most optocouplers will be minor.
Remember digital isolators have delays as well, and faster ones are expensive.
Look for 'logic level optocouplers'. These have logic gate outputs, instead of a direct transistor connection. H11L1 is a 'classic' single version. Requires a pull up resistor on the output, but makes sending logic signals easy. Smile
gjs_rsdi



Joined: 06 Feb 2006
Posts: 468
Location: Bali

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

PostPosted: Fri Jun 16, 2017 3:43 pm     Reply with quote

Thanks again Ttelmah for the help in the opto subject, especially that is a CCS forum and not general electronics Smile

Downloaded a few data sheets, learning the subject, first impression very good, especially that have through hole components that are good for my prototyping.

Best wishes
Joe
gjs_rsdi



Joined: 06 Feb 2006
Posts: 468
Location: Bali

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

PostPosted: Thu Jul 20, 2017 12:05 am     Reply with quote

Hi
I am back with this topic
Started to implement the serial communication, have a problem with FLOAT_HIGH
Code:
#use rs232(FLOAT_HIGH, baud=9600, UART1, stream=PORT1, errors)

I am getting:
Quote:
*** Error 100...USE parameter value is out of range H/W USART can not float

Have some solution?
What about a use declaration:
Code:
#use rs232(baud=9600, UART1, stream=PORT1, errors)

and then a use declaration that have just RX and the TX pin as input?
How to make a declaration without TX if possible?

Best wishes
Joe
Ttelmah



Joined: 11 Mar 2010
Posts: 19195

View user's profile Send private message

PostPosted: Thu Jul 20, 2017 12:31 am     Reply with quote

No, and yes....

Float high on a hardware UART, does work on some of the more modern PIC24's etc., that support this. Unfortunately most chips don't.

On just using the RX, declare as you currently do, and then turn off the TX hardware. This is the TXEN bit. so:

Code:

#use rs232(baud=9600, UART1, stream=PORT1, errors)
//at this point the compiler now 'knows' about the hardware UART
#bit TXEN=GETENV("BIT:TXEN")

void main(void)
{
    TXEN=FALSE; //the UART transmit is now off.
//etc...


The compiler will not actually generate the transmit code for the UART, unless you ever do a putc to it, so this costs just one machine instruction, and gives you a hardware 'receive only' UART. Smile
gjs_rsdi



Joined: 06 Feb 2006
Posts: 468
Location: Bali

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

PostPosted: Thu Jul 20, 2017 2:08 am     Reply with quote

Thank you for the answer Ttelmah Smile
I would like to ask a few more questions:

* I understand correct the settings at the beginning should be:
Code:
#use rs232(baud=9600, UART1, stream=PORT1, errors)
#bit TXEN=GETENV("BIT:TXEN")

And in the main:
Code:
TXEN=FALSE;


* I am enabling TX interrupt in my programs after getting a message in (RX) so now I should do:
Code:
TXEN=TRUE;
enable_interrupts(INT_TBE);

It is correct?

* At the end of the TX I am disabling TX, so I should do:
Code:
TXEN=FALSE;
disable_interrupts(INT_TBE);

It is correct? Or I should make it in the next "case"?

* The port become input if "TXEN=FALSE"?. I have a pull-up resistor on the line.

Sorry if my English somehow heavy Smile

Best wishes
Joe
Ttelmah



Joined: 11 Mar 2010
Posts: 19195

View user's profile Send private message

PostPosted: Thu Jul 20, 2017 7:05 am     Reply with quote

You normally would not enable the transmitter interrupt until you are actually sending data, and disable it when the data is finished. Remember the transmitter interrupt is true whenever the transmitter buffer is empty.

So if you look at the example ex_STISR.c, you will see that the interrupt is only enabled when the bputc is actually called with data, and the interrupt is disabled inside the interrupt handler when there is no more data to send.
You would then need to pause for one character time, before turning off the TXEN bit (or test the TRMT bit and wait for this to go high).
gjs_rsdi



Joined: 06 Feb 2006
Posts: 468
Location: Bali

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

PostPosted: Thu Jul 20, 2017 7:06 pm     Reply with quote

Hi Ttelmah
I am enabling TX interrupt just when I need to send a message, disable interrupt in the last case of the switch.
My last question was:
If I make TXEN=FALSE; and then disable_interrupts(INT_TBE); the last byte will be sent?
Without the TXEN=FALSE; the last byte is sent after disable_interrupts(INT_TBE);

Best wishes
Joe
Ttelmah



Joined: 11 Mar 2010
Posts: 19195

View user's profile Send private message

PostPosted: Thu Jul 20, 2017 11:17 pm     Reply with quote

No.
That is the point.

You need to wait one character time after the last byte is loaded, or till the TRMT bit goes high after disabling the interrupt, before you turn off TXEN.

You can disable the interrupt once the last byte is loaded into the output shift register, but the actual TXEN, needs to remain on, till this byte has sent.
gjs_rsdi



Joined: 06 Feb 2006
Posts: 468
Location: Bali

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

PostPosted: Fri Jul 21, 2017 4:18 pm     Reply with quote

Thanks Ttelmah

Everything is clear now in the subject Smile

Best wishes
Joe
gjs_rsdi



Joined: 06 Feb 2006
Posts: 468
Location: Bali

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

PostPosted: Fri Jul 21, 2017 6:42 pm     Reply with quote

Sorry I wrote "Everything is clear now in the subject" as I have an additional
question:
In the data sheet (16F1847 page 291) is written:
Quote:
Setting the TXEN bit of the TXSTA register enables the
transmitter circuitry of the EUSART
Setting the SPEN bit of the
RCSTA register enables the EUSART and automatically
configures the TX/CK I/O pin as an output.

So I added a new line on Ttelmah proposal:
Code:
#bit TXEN=GETENV("BIT:TXEN")
#bit SPEN=GETENV("BIT:SPEN")

This will make the tx pin an input, or I need to write something like:
Code:
txpinstatus=input(PIN_B2);

Reading again the data sheet, I understand that if I clear SPEN bit it will disable the EUSART. It is correct?

Best wishes
Joe
Ttelmah



Joined: 11 Mar 2010
Posts: 19195

View user's profile Send private message

PostPosted: Fri Jul 21, 2017 11:43 pm     Reply with quote

The compiler has code to do this.

Do your #use rs232 with 'NOINIT' added.

Then you can use:

setup_uart(FALSE);

To disable the UART, and

setup_uart(TRUE);

to enable it.

SPEN, enables/disables the whole port. The point about TXEN, is it disables/enables just the transmit side, leaving the RX still enabled.
gjs_rsdi



Joined: 06 Feb 2006
Posts: 468
Location: Bali

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

PostPosted: Sat Jul 22, 2017 1:42 am     Reply with quote

Thanks Ttelmah

I implemented your proposals and the program works.
I would like to see in the LST file if PIN_B2 become input after
transmitting, but my knowledge not enough.
Code:
/////////////////////////////////////////////////////////////////////                     
//serial communication test program
//CCS PCM C Compiler, Version 5.062, 31220
/////////////////////////////////////////////////////////////////////
#include <16F1847.h>
#device ADC=10
#FUSES NOWDT,PUT,MCLR                 
#FUSES NOPROTECT,NOCPD       
#FUSES BROWNOUT,BORV25
#FUSES INTRC_IO,PLL,NOCLKOUT,NOIESO,NOFCMEN
#FUSES NOWRT,STVREN,NOLVP
#use delay(internal=32M)
#use rs232(baud=9600,parity=N,xmit=PIN_B2,rcv=PIN_B1,bits=8,stream=PORT1,errors)
/////////////////////////////////////////////////////////////////////
#bit TXEN=GETENV("BIT:TXEN")//new************************************
#bit TRMT=GETENV("BIT:TRMT")//new************************************
/////////////////////////////////////////////////////////////////////
#define flashled PIN_A3//for AirPV2 board
short txpinstatusF=0;//new*******************************************
short txendF=0;//new*************************************************
short timerssincF=0;
int scomtxstartcnt=0;
int flashcnt=0;
short txupdateF=0;
int scomtxwords=0;
int scomtxw2=0;
int scomtxw3=0;
int scomtxw4=0;
int scomtxwchs=0;
short rxnewmsgF=0;
short rxfail=0;
int rxwords=0;
int rxdata0=0;
int rxdata1=0;
int rxdata2=0;
int rxdata3=0;
int rxdata4=0;
int rxdata5=0;
int rxwchs=0;
/////////////////////////////////////////////////////////////////////
#INT_TIMER1
void  TIMER1_isr(void)
{
      set_timer1(45536);
      timerssincF=1;
}
/////////////////////////////////////////////////////////////////////
void FUNCTIONS(void)
{
   if(timerssincF==1)
   {
      flashcnt++;   
      scomtxstartcnt++;
      timerssincF=0;
   }
//-------------------------------------------------------------------
   if(flashcnt>=25)
   {
      flashcnt=0;
      output_toggle(flashled);
   }   
//-------------------------------------------------------------------
   if((rxnewmsgF==1)||(scomtxstartcnt==100))
   {
      rxnewmsgF=0;
      scomtxw2=rxdata2;
      scomtxw3=rxdata3;
      scomtxw4=rxdata4;
      txupdateF=0;
      scomtxstartcnt=0;
      TXEN=TRUE;//new**********************************************
      enable_interrupts(INT_TBE);   
   }
//-------------------------------------------------------------------
//new****************************************************************
//PIC16F1847 data sheet page 292:
//TRMT bit remains clear until all bits have been shifted out of the TSR register
//txendF=0; to desable entance when TXEN=TRUE; before TXREG & TSR loaded
   if((txendF==1)&&(TRMT==1))
   {
      txendF=0;
      TXEN=FALSE;   
      txpinstatusF=input(PIN_B2);      
   }
}
/////////////////////////////////////////////////////////////////////
#int_TBE
void TBE_isr(void)
{
   switch(scomtxwords)
   {
      case 0:
      {
         putc(85);
         scomtxwchs=85;   
         scomtxwords++;      
      }
      break;
      case 1:
      {
         putc(170);
         scomtxwchs=255;
         scomtxwords++;         
      }
      break;
      case 2:
      {
         putc(scomtxw2);
         scomtxwchs=scomtxwchs+scomtxw2;
         scomtxwords++;         
      }
      break;
      case 3:
      {
         putc(scomtxw3);
         scomtxwchs=scomtxwchs+scomtxw3;
         scomtxwords++;         
      }
      break;
      case 4:
      {
         putc(scomtxw4);
         scomtxwchs=scomtxwchs+scomtxw4;
         scomtxwords++;         
      }
      break;
      case 5:
      {
         putc(scomtxwchs);
         scomtxwords=0;
         txendF=1;//new*******************************************
         disable_interrupts(INT_TBE);            
      }
      break;
   }   
}
//-------------------------------------------------------------------
#INT_RDA
void  RDA_isr(void)
{
   switch(rxwords)
   {
      case 0:
      {
         rxdata0=getc();
         if (rxdata0==85)
         {
            rxwchs=85;
            rxwords=1;
         }
         else
         {
            rxwords=0;
         }   
      }
      break;
      case 1:
      {
         rxdata1=getc();
         if (rxdata1==170)
         {
            rxwchs=255;
            rxwords++;
         }
         else
         {
            rxwords=0;
         }         
      }
      break;
      case 2:
      {
         rxdata2=getc();
         rxwchs=rxwchs+rxdata2;
         rxwords++;         
      }
      break;
      case 3:
      {
         rxdata3=getc();
         rxwchs=rxwchs+rxdata3;
         rxwords++;                  
      }
      break;
      case 4:
      {
         rxdata4=getc();
         rxwchs=rxwchs+rxdata4;
         rxwords++;            
      }
      break;
      case 5:
      {
         rxdata5=getc();
         if (rxwchs==rxdata5)
         {
            rxnewmsgF=1;
            rxfail=0;
         }
         else
         {
            rxfail=1;
         }
         rxwords=0;   
      }
      break;
   }
}
/////////////////////////////////////////////////////////////////////

#ZERO_RAM

void main()
{
   TXEN=FALSE;//new*************************************************
   setup_adc_ports(NO_ANALOGS);
   setup_timer_1(T1_INTERNAL|T1_DIV_BY_8);//65.5 ms overflow
   enable_interrupts(INT_TIMER1);
   disable_interrupts(INT_TBE);
   enable_interrupts(INT_RDA);
   enable_interrupts(GLOBAL);
   while(TRUE)
   {
      delay_cycles(2);
      FUNCTIONS();
   }
}

Marked the additions to the original program by:
//new***********************************

Best wishes and thank you again Smile
Joe
gjs_rsdi



Joined: 06 Feb 2006
Posts: 468
Location: Bali

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

PostPosted: Sat Jul 22, 2017 2:30 am     Reply with quote

Hi

Finally found in the LST, PIN_B2 made input Smile
Quote:
.................... txpinstatusF=input(PIN_B2);
0109: MOVLB 01
010A: BSF TRISB.TRISB2


Best wishes
Joe
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Goto page Previous  1, 2
Page 2 of 2

 
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