Thread Subject: RS-232 and RTWT

Subject: RS-232 and RTWT

From: Alp

Date: 3 Nov, 2009 19:10:19

Message: 1 of 7

We have a project which contains PIC microcontroller, RS-232 interface and PC (with Matlab, Simulink).We have to connect PIC to PC via RS-232 and sent datas in real time.
I found this link when I search related topics includes RS-232 and PIC keywords.
http://hades.mech.northwestern.edu/index.php/Serial_communication_with_Matlab
However, this example does not working in Simulink's Embedded Matlab Block. PIC should get the 4digit number, we could not send the data from simulink to PIC. Is there anybody work on RTWT Simulink , RS-232 and PIC to help us?

Subject: RS-232 and RTWT

From: Jan Houska

Date: 4 Nov, 2009 08:42:34

Message: 2 of 7

Hi,

Alp wrote:
> We have a project which contains PIC microcontroller, RS-232 interface and PC (with Matlab, Simulink).We have to connect PIC to PC via RS-232 and sent datas in real time.
> I found this link when I search related topics includes RS-232 and PIC keywords.
> http://hades.mech.northwestern.edu/index.php/Serial_communication_with_Matlab
> However, this example does not working in Simulink's Embedded Matlab Block. PIC should get the 4digit number, we could not send the data from simulink to PIC. Is there anybody work on RTWT Simulink , RS-232 and PIC to help us?

You can try the Packet Output or Stream Output block, and choose
Standard Devices, Serial Port as the data acquisition driver. These
blocks are available since R2007b. Please see the "rtpacketio" and
"rtstreamio" examples - although they use UDP and not RS232, the
principle is the same and they can be changed to use RS232 just by
changing the data acquisition driver.

Good Luck, Jan


--
Jan Houska HUMUSOFT s.r.o.
houska@humusoft.com Pobrezni 20
http://www.humusoft.com 186 00 Praha 8
tel: ++ 420 284 011 730 Czech Republic
fax: ++ 420 284 011 740

Subject: RS-232 and RTWT

From: Alp

Date: 5 Nov, 2009 10:27:01

Message: 3 of 7

Thank you for your help. We tried your way ,but we cannot achived to get proper result. I am telling you our project with its details. I hope you can find our missings. We want to produce PWM signal (from B0 pin) from PIC according to coming signal from Simulink (0 to 1000). We used MAX232 IC for converting +12V,-12V to 5V,0V.
CCS C Program of PIC16F877:

#include <16f877.h>
#include <stdlib.h>
#fuses XT,NOWDT,NOPROTECT,NOBROWNOUT,NOLVP,NOPUT,NOWRT,NODEBUG,NOCPD
#use delay (clock=4000000)
#use rs232 (baud=9600, xmit=pin_C6, rcv=pin_C7, parity=N, stop=1)
#use fast_io(b)

long int A=63536;
char klavye[16];
int16 x=0;
#int_rda
void serihaberlesme_kesmesi ()
{
   disable_interrupts(int_rda);
   output_high(pin_c5);
   gets(klavye);
   output_low(pin_c5);
}
//****************** Timer1 Interrupt (1-2 ms) ****
#int_timer1
void timer1_kesme ()
{
    output_low(pin_b0);
}

//****************** Timer0 Interrupt (20 ms) *****
#int_timer0
void timer0_kesme ()
{
    output_high(pin_b0);
    set_timer0(177); // TMR0
    set_timer1(A); // TMR1
}

void main ( )
{
   setup_psp(PSP_DISABLED);
   setup_timer_2(T2_DISABLED,0,1);
   setup_CCP1(CCP_OFF);
   setup_CCP2(CCP_OFF);
   setup_adc_ports(NO_ANALOGS);
   setup_adc(ADC_OFF);
   
   set_tris_b(0x00);
   output_b(0x00);
    
   setup_timer_0(RTCC_INTERNAL | RTCC_DIV_256); // Timer0
   set_timer0(177); // TMR0
   setup_timer_1(T1_INTERNAL | T1_DIV_BY_1); // Timer1
   enable_interrupts(INT_timer1);
   enable_interrupts(INT_timer0);
   enable_interrupts(GLOBAL);


   while(1)
   {
   x=atol(klavye);
   A=64536-x;
   enable_interrupts(int_rda);
   }
}

Simulink File:

Constant-->Slider Gain-->Stream Output (or Packet Output)

Thank you again..

Subject: RS-232 and RTWT

From: Jan Houska

Date: 5 Nov, 2009 11:09:35

Message: 4 of 7

Hi Alp,

have you been able to control your PIC from another Windows application
- not MATLAB, not Real-Time Windows Target? What data format were you using?

 From the PIC code below it looks like the device expects the data in
ASCII, so you would need to use Stream Output. Further, it looks like
you are expecting an integer decimal number, so you should set the
format string to '%d'. Of course, you must ensure that baud rate, bit
length, parity, etc. settings of the PIC match those of Real-Time
Windows Target (settable in Board Setup).

Other than this, I can't think of anything else at the moment. But I'd
definitely start from a working setup (if you have any) and gradually
change thing to the desired setup with Real-Time Windows Target.

Good Luck, Jan


Alp wrote:
> Thank you for your help. We tried your way ,but we cannot achived to get proper result. I am telling you our project with its details. I hope you can find our missings. We want to produce PWM signal (from B0 pin) from PIC according to coming signal from Simulink (0 to 1000). We used MAX232 IC for converting +12V,-12V to 5V,0V.
> CCS C Program of PIC16F877:
>
> #include <16f877.h>
> #include <stdlib.h>
> #fuses XT,NOWDT,NOPROTECT,NOBROWNOUT,NOLVP,NOPUT,NOWRT,NODEBUG,NOCPD
> #use delay (clock=4000000)
> #use rs232 (baud=9600, xmit=pin_C6, rcv=pin_C7, parity=N, stop=1)
> #use fast_io(b)
>
> long int A=63536;
> char klavye[16];
> int16 x=0;
> #int_rda
> void serihaberlesme_kesmesi ()
> {
> disable_interrupts(int_rda);
> output_high(pin_c5);
> gets(klavye);
> output_low(pin_c5);
> }
> //****************** Timer1 Interrupt (1-2 ms) ****
> #int_timer1
> void timer1_kesme ()
> {
> output_low(pin_b0);
> }
>
> //****************** Timer0 Interrupt (20 ms) *****
> #int_timer0
> void timer0_kesme ()
> {
> output_high(pin_b0);
> set_timer0(177); // TMR0
> set_timer1(A); // TMR1
> }
>
> void main ( )
> {
> setup_psp(PSP_DISABLED);
> setup_timer_2(T2_DISABLED,0,1);
> setup_CCP1(CCP_OFF);
> setup_CCP2(CCP_OFF);
> setup_adc_ports(NO_ANALOGS);
> setup_adc(ADC_OFF);
>
> set_tris_b(0x00);
> output_b(0x00);
>
> setup_timer_0(RTCC_INTERNAL | RTCC_DIV_256); // Timer0
> set_timer0(177); // TMR0
> setup_timer_1(T1_INTERNAL | T1_DIV_BY_1); // Timer1
> enable_interrupts(INT_timer1);
> enable_interrupts(INT_timer0);
> enable_interrupts(GLOBAL);
>
>
> while(1)
> {
> x=atol(klavye);
> A=64536-x;
> enable_interrupts(int_rda);
> }
> }
>
> Simulink File:
>
> Constant-->Slider Gain-->Stream Output (or Packet Output)
>
> Thank you again..


--

--
Jan Houska HUMUSOFT s.r.o.
houska@humusoft.com Pobrezni 20
http://www.humusoft.com 186 00 Praha 8
tel: ++ 420 284 011 730 Czech Republic
fax: ++ 420 284 011 740

Subject: RS-232 and RTWT

From: Alp

Date: 5 Nov, 2009 21:48:02

Message: 5 of 7

Thank you Jan
We had connected PC to PIC via RS-232 using HyperTerminal Application. Also Matlab, but not real time. As you know HyperTerminal sends data as character string. When we write the 4-digit number and press Enter it sends to the PIC. According to signal our PWM value changes.

We worked procceses on things you said. However, it did not worked. When the PIC get signals from Stream Output Block resets to zero the initial PWM value of the PIC. I think it is not a understandable signal for PIC. I tried all character types for sending to PIC, but again it is not working. Also, I am pretty sure for the RS-232 spesifications like boud rate or stop bits.

Port:COM1
Boud Rate : 9600
Parity: N
Stop Bit: 1

Do you think there could be a problem about buffering? That is the last thing to coming my mind. Again Thank You


Jan Houska <houska@humusoft.com> wrote in message <hcubpg$bei$1@ns.felk.cvut.cz>...
> Hi Alp,
>
> have you been able to control your PIC from another Windows application
> - not MATLAB, not Real-Time Windows Target? What data format were you using?
>
> From the PIC code below it looks like the device expects the data in
> ASCII, so you would need to use Stream Output. Further, it looks like
> you are expecting an integer decimal number, so you should set the
> format string to '%d'. Of course, you must ensure that baud rate, bit
> length, parity, etc. settings of the PIC match those of Real-Time
> Windows Target (settable in Board Setup).
>
> Other than this, I can't think of anything else at the moment. But I'd
> definitely start from a working setup (if you have any) and gradually
> change thing to the desired setup with Real-Time Windows Target.
>
> Good Luck, Jan
>
>
>
> Jan Houska HUMUSOFT s.r.o.
> houska@humusoft.com Pobrezni 20
> http://www.humusoft.com 186 00 Praha 8
> tel: ++ 420 284 011 730 Czech Republic
> fax: ++ 420 284 011 740

Subject: RS-232 and RTWT

From: Jan Houska

Date: 7 Nov, 2009 07:59:46

Message: 6 of 7

Hi Alp,

if you need to press Enter in HyperTerminal for your device to accept
the data, there's chance that you need to terminate the data by either
CR or LF or both. Please try changing the format string to '%d\r' (for
CR) or '%d\n' (for LF) of to '%d\r\n' (for both).

If that still doesn't help, please try to connect another PC instead of
your device to the other end of the communication line. Then, try to
send data from both HyperTerminal and Real-Time Windows Target and
compare the differences. This way, you should be able to figure out what
you should do with the format string to exactly match the format you are
sending from HyperTerminal.

Good Luck, Jan


Alp wrote:
> Thank you Jan
> We had connected PC to PIC via RS-232 using HyperTerminal Application. Also Matlab, but not real time. As you know HyperTerminal sends data as character string. When we write the 4-digit number and press Enter it sends to the PIC. According to signal our PWM value changes.
>
> We worked procceses on things you said. However, it did not worked. When the PIC get signals from Stream Output Block resets to zero the initial PWM value of the PIC. I think it is not a understandable signal for PIC. I tried all character types for sending to PIC, but again it is not working. Also, I am pretty sure for the RS-232 spesifications like boud rate or stop bits.
>
> Port:COM1
> Boud Rate : 9600
> Parity: N
> Stop Bit: 1
>
> Do you think there could be a problem about buffering? That is the last thing to coming my mind. Again Thank You
>
>
> Jan Houska <houska@humusoft.com> wrote in message <hcubpg$bei$1@ns.felk.cvut.cz>...
>> Hi Alp,
>>
>> have you been able to control your PIC from another Windows application
>> - not MATLAB, not Real-Time Windows Target? What data format were you using?
>>
>> From the PIC code below it looks like the device expects the data in
>> ASCII, so you would need to use Stream Output. Further, it looks like
>> you are expecting an integer decimal number, so you should set the
>> format string to '%d'. Of course, you must ensure that baud rate, bit
>> length, parity, etc. settings of the PIC match those of Real-Time
>> Windows Target (settable in Board Setup).
>>
>> Other than this, I can't think of anything else at the moment. But I'd
>> definitely start from a working setup (if you have any) and gradually
>> change thing to the desired setup with Real-Time Windows Target.
>>
>> Good Luck, Jan
>>
>>
>>
>> Jan Houska HUMUSOFT s.r.o.
>> houska@humusoft.com Pobrezni 20
>> http://www.humusoft.com 186 00 Praha 8
>> tel: ++ 420 284 011 730 Czech Republic
>> fax: ++ 420 284 011 740


--

--
Jan Houska HUMUSOFT s.r.o.
houska@humusoft.com Pobrezni 20
http://www.humusoft.com 186 00 Praha 8
tel: ++ 420 284 011 730 Czech Republic
fax: ++ 420 284 011 740

Subject: RS-232 and RTWT

From: Alp

Date: 7 Nov, 2009 14:01:05

Message: 7 of 7

Jan Thank You so much
System is working :) we used %g\r\n

Jan Houska <houska@humusoft.com> wrote in message <hd39dj$2d9j$1@ns.felk.cvut.cz>...
> Hi Alp,
>
> if you need to press Enter in HyperTerminal for your device to accept
> the data, there's chance that you need to terminate the data by either
> CR or LF or both. Please try changing the format string to '%d\r' (for
> CR) or '%d\n' (for LF) of to '%d\r\n' (for both).
>
> If that still doesn't help, please try to connect another PC instead of
> your device to the other end of the communication line. Then, try to
> send data from both HyperTerminal and Real-Time Windows Target and
> compare the differences. This way, you should be able to figure out what
> you should do with the format string to exactly match the format you are
> sending from HyperTerminal.
>
> Good Luck, Jan
>
>
> Alp wrote:
> > Thank you Jan
> > We had connected PC to PIC via RS-232 using HyperTerminal Application. Also Matlab, but not real time. As you know HyperTerminal sends data as character string. When we write the 4-digit number and press Enter it sends to the PIC. According to signal our PWM value changes.
> >
> > We worked procceses on things you said. However, it did not worked. When the PIC get signals from Stream Output Block resets to zero the initial PWM value of the PIC. I think it is not a understandable signal for PIC. I tried all character types for sending to PIC, but again it is not working. Also, I am pretty sure for the RS-232 spesifications like boud rate or stop bits.
> >
> > Port:COM1
> > Boud Rate : 9600
> > Parity: N
> > Stop Bit: 1
> >
> > Do you think there could be a problem about buffering? That is the last thing to coming my mind. Again Thank You
> >
> >
> > Jan Houska <houska@humusoft.com> wrote in message <hcubpg$bei$1@ns.felk.cvut.cz>...
> >> Hi Alp,
> >>
> >> have you been able to control your PIC from another Windows application
> >> - not MATLAB, not Real-Time Windows Target? What data format were you using?
> >>
> >> From the PIC code below it looks like the device expects the data in
> >> ASCII, so you would need to use Stream Output. Further, it looks like
> >> you are expecting an integer decimal number, so you should set the
> >> format string to '%d'. Of course, you must ensure that baud rate, bit
> >> length, parity, etc. settings of the PIC match those of Real-Time
> >> Windows Target (settable in Board Setup).
> >>
> >> Other than this, I can't think of anything else at the moment. But I'd
> >> definitely start from a working setup (if you have any) and gradually
> >> change thing to the desired setup with Real-Time Windows Target.
> >>
> >> Good Luck, Jan
> >>
> >>
> >>
> >> Jan Houska HUMUSOFT s.r.o.
> >> houska@humusoft.com Pobrezni 20
> >> http://www.humusoft.com 186 00 Praha 8
> >> tel: ++ 420 284 011 730 Czech Republic
> >> fax: ++ 420 284 011 740
>
>
> --
>
> --
> Jan Houska HUMUSOFT s.r.o.
> houska@humusoft.com Pobrezni 20
> http://www.humusoft.com 186 00 Praha 8
> tel: ++ 420 284 011 730 Czech Republic
> fax: ++ 420 284 011 740

Tags for this Thread

Everyone's Tags:

Add a New Tag:

Separated by commas
Ex.: root locus, bode

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Tag Activity for This Thread
Tag Applied By Date/Time
rs232 Andreas Possl 9 Nov, 2011 08:17:02
pic Alp 5 Nov, 2009 05:29:06
rtwt Alp 5 Nov, 2009 05:29:06
rs232 Alp 5 Nov, 2009 05:29:05
rtwt rs232 pic Alp 3 Nov, 2009 14:14:08
rssFeed for this Thread

Contact us at files@mathworks.com