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

Software RS232 in 16f873

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



Joined: 03 Jan 2006
Posts: 5

View user's profile Send private message

Software RS232 in 16f873
PostPosted: Tue Jan 03, 2006 5:21 am     Reply with quote

Hello.

I wan to connect two devices to my PIC but i have only one UART port.
Someone told me that it's good solution to implement software UART.
Problem is that i completaly don't know how to write this in CCS(neither with Assembler ).

Could someone support me with examples? Thank You very much.
Humberto



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

View user's profile Send private message

PostPosted: Tue Jan 03, 2006 6:39 am     Reply with quote

You will start here:

http://www.ccsinfo.com/faq/?5

and then use the search feature in this forum to read how had been done by other people.

Surelly you will get 90% of your problem solved !
At this point post your trial code and we will help you.


Humberto
McGregor80



Joined: 03 Jan 2006
Posts: 5

View user's profile Send private message

PostPosted: Tue Jan 03, 2006 7:02 am     Reply with quote

ThankYou. I've just read that. I understand that everything (printf,putc, getc etc.) what happend after #RS_232(......) depends on those settings(........)?!

Does it meen that if there is interrupt RDA in my software it will also react on the last settings and recieve from it?

If I understand it's not so difficult??

But can it be done that PIC will react on both "com" interrupt simultaneusly?

Once more. Thank You
Humberto



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

View user's profile Send private message

PostPosted: Tue Jan 03, 2006 9:36 am     Reply with quote

Quote:

Does it meen that if there is interrupt RDA in my software it will also react on the last settings and recieve from it?


Don't know what do you mean with 'last settings'.

Read this linked thread where I post an example to start with.
http://www.ccsinfo.com/forum/viewtopic.php?t=18044&highlight=stream+rs232

Quote:

But can it be done that PIC will react on both "com" interrupt simultaneusly?


You must know that the word 'simultaneously' should be readed as 'almost simultaneously'. If you want to know if it is possible to 'catch' chars arriving 'simultaneously' at different ports, the answer is yes.

The chars arriving at the UART Rx pin should be treated in the #INT_RDA interrupt that is triggered if enabled. It is the programmer responsability to code the handler.
To 'catch' the chars arriving at the software Rx pin, it is necesary to poll that pin looking for the falling edge of START bit. ie: if(kbhit()){....}

Polling is not the best way to use the microcontroller's resources, because at 9600 bauds, the START bit will long 104usec and the polling should arrive at least once to sense the level change, otherwise the 'char' is lost. This require a tight loop doing almost nothing but wasting time expecting a level change in the Rx pin (polling).

A trick used to ovoid polling is assigning the Rx pin (software UART) at the dedicated pin used to detect an external interrupt (mostly RB0 in middle-range uC). Used in this way, the microcontroller generate an EXTernal INTerrupt with the START bit of the incoming characters. It is not necesary to use if(kbhit()) inside INT_EXT interrumpt routine because the START bit already had been detected.

The INT_EXT handler should 'capture' the char using the built in function fgetc(stream) where the parameter stream is the same identifier used in your previous #use RS232(rcv=PIN_n, ...,STREAM=COMx)

That way fgetc(COM1) redirect the getc() to the hardware dedicated -factory defined- Rx input PIN.
And fgetc(COM2) redirect the getc() to the another -user defined- Rx I/O PIN.

#use RS232(........ stream=COM1) Hardware UART preprocessor directive.

#use RS232(........ stream=COM2) Software UART preprocessor directive.

That way the microcontroller 'knows' from where the chars come from.

Humberto
McGregor80



Joined: 03 Jan 2006
Posts: 5

View user's profile Send private message

PostPosted: Wed Jan 04, 2006 3:04 pm     Reply with quote

Ok. I wrote simple program to check if my software UART recieves anything.
Unfortunately it doesn't work like i would like to.
Maybe You'll find any errors and reason why it doesn't work.

Program increments on 8 LEDs when recieves something.
This is "H" file
Code:
#include <16F873A.h>
#use delay(clock=20000000)
#fuses HS,NOWDT,NOPROTECT,NOBROWNOUT,NOWRT,NOLVP
#use rs232(baud=9600,parity=N,xmit=PIN_C6,rcv=PIN_C7,stream=COM1,errors)
#use rs232(baud=4800,parity=N,xmit=PIN_B1,rcv=PIN_B0,stream=COM2)



and this is source file:

Code:

#include "F:\projecty\C_Lang\proj_test\Software_UART.h"
char in1;
char in2;
int licznik1=0;
int licznik2=0;
int licznik3=0;

#int_EXT
EXT_isr() {
in1=fgetc(COM1);
licznik1++;
if(licznik1==255) licznik1=0;
output_a(licznik1);
}

#int_RDA
RDA_isr() {
in2=fgetc(COM2);
licznik2++;
if(licznik2==255) licznik2=0;

}



void main() {
   port_b_pullups(true);
   setup_adc_ports(NO_ANALOGS);
   setup_adc(ADC_CLOCK_DIV_2);
   setup_spi(FALSE);
   setup_counters(RTCC_INTERNAL,RTCC_DIV_2);
   setup_timer_1(T1_DISABLED);
   setup_timer_2(T2_DISABLED,0,1);
   setup_ccp1(CCP_OFF);
   setup_ccp2(CCP_OFF);
   enable_interrupts(INT_EXT);
   ext_int_edge(H_TO_L);
   enable_interrupts(INT_RDA);
   enable_interrupts(global);
   output_a(0);
   
}


ThankYou fo any help
rwyoung



Joined: 12 Nov 2003
Posts: 563
Location: Lawrence, KS USA

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

PostPosted: Wed Jan 04, 2006 3:18 pm     Reply with quote

Look at the LST file generated by the compiler. At the end of your main() you will see that the compiler has inserted a SLEEP command. This means that right after your output_a(0) statement, the PIC goes to sleep.

Add an enless loop at the end of your main to prevent the PIC from hitting the SLEEP instruction.

Code:
while(1){}


for example.
_________________
Rob Young
The Screw-Up Fairy may just visit you but he has crashed on my couch for the last month!
McGregor80



Joined: 03 Jan 2006
Posts: 5

View user's profile Send private message

PostPosted: Wed Jan 04, 2006 3:33 pm     Reply with quote

Thanks for sugestion but i don't think its the reason. Earlier i made test also wit loop wich was incrementing port_a in main and showing it on LEDs(with 500ms delay) and i noticed no effect from Virtual com.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Wed Jan 04, 2006 3:47 pm     Reply with quote

In addition to the missing while(1); statement at the end of main()
you also have your streams reversed in your interrupt routines.
That's why it's better to give more descriptive names than "COM1"
and "COM2". Use names that tell you that the port is Hardware
or Software, such as "SOFT_UART" and "HW_UART" or something
like that.

Note that COM1 is hardware, but you have it in the INT_EXT routine
for the soft uart, and vice versa:
Quote:

baud=9600,parity=N,xmit=PIN_C6,rcv=PIN_C7,stream=COM1,errors)
#use rs232(baud=4800,parity=N,xmit=PIN_B1,rcv=PIN_B0,stream=COM2)

#int_EXT
EXT_isr() {
in1=fgetc(COM1);
licznik1++;
if(licznik1==255) licznik1=0;
output_a(licznik1);
}

#int_RDA
RDA_isr() {
in2=fgetc(COM2);
licznik2++;
if(licznik2==255) licznik2=0;

}
McGregor80



Joined: 03 Jan 2006
Posts: 5

View user's profile Send private message

PostPosted: Fri Jan 06, 2006 1:22 am     Reply with quote

ThankYou PCM Programer.
You Are right. When i changed COM1 with COM2 it works fine.

ThankYou all
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