|
|
View previous topic :: View next topic |
Author |
Message |
Labiran, Simeon Olusola Guest
|
RS232 and 18F452 communication with Matlab |
Posted: Thu Jul 21, 2005 11:38 am |
|
|
I have a project that involves the serial communication between RS232 on PC computer and PIC 18F452 by using Matlab Simulink and CCS Compiler.
I wrote a C Langauge to configure the baud rate at 9600 with 4MHz crystal as follows:
TXSTA1 =0x24; /* enable 8- bit transmission*/
SPBRG1 =25; /* set baud rate to 9600*/
TRISC |=0x80: /*configure RC7/RX1 pin for input*/
TRISC &=0xBF; /*Configure RC6/TX1 pin for output*/
RCSTAbits.SPEN=1; /*enableUSART1*/
I need your advice to write a Programme that will enable proper communication between RS232 on PC Computer and PIC 18F452.
I shall be very grateful if my request is considered.
Yours Sincerely,
Simeon Olusola Labiran |
|
|
rwyoung
Joined: 12 Nov 2003 Posts: 563 Location: Lawrence, KS USA
|
|
Posted: Thu Jul 21, 2005 11:58 am |
|
|
Read about the #use RS232 statement in the manual and look at the example code that comes with the compiler.
Also read up on putc, getc, printf. Finding one or two in the help file or manual will give you links to related commands. _________________ Rob Young
The Screw-Up Fairy may just visit you but he has crashed on my couch for the last month! |
|
|
Catilo Guest
|
Using Matlab with RS232 |
Posted: Sun Jul 24, 2005 10:31 am |
|
|
HI. You can test Data Communications ToolBox from Matlab documentation to find the way to link RS232 and Matlab. I've done some
projects with that and work fine. |
|
|
Simeon Labiran Guest
|
Request for advice |
Posted: Sun Jul 24, 2005 7:00 pm |
|
|
Dear Rwyoung & Catilo,
I programmed my PIC 18F452 and RS232 to transmitt and receive information from PC computer through DB9 port in C language as follow:
#include <18F452.h>
#include <Stdio.h>
#include <stdlib.h>
#Fuses.HS NOWDT NOPROTECT NOLVP
#Use fixed_io (C_outputs=PIN_C6) /* speeds up port use*/
#use delay (clock = 4000000)
#use rs232(baud= 9600,xmit=PIN_C6,rcv=PIN_C7)
Void init (void);
Void init (void){
// set up UART
setup_uart(9600);
setup_uart(9600,rsOut);
}
// set baud rate base on setting of PINS B0 and B1
setup_ccpl (CCP_PWM); /* configure UART to transmit and receive data in asynchronous, 8-bit data format */
/* The cycle time will be (1/baud * 64 * (SPBRG +1))
switch (input_b() & 3){
case 0: set_uart_speed(2400);
break;
Case1: set_uart_speed(4800);
break;
Case2: set_uart_speed(9600);
break;
Case3: set_uart_speed(19200);
break;
#define CR 0x0D
void main (void){
gets_uart(char*ptr)
char xx;
while(1)
{
xx=getc_uart(); /* read a character*/
if(xx==CR){ /* is it a cariage return?*/
*ptr='\0'; /* terminate the string with a NULL*/
return;
}
ptr++=xx; /* store the received character in the buffer */
}
}
After compile this programme with CCS C Compiler software,the build programme failed with the following error:
Line 14, undefined indetifier rsOut
Line 16,undefined identifier setup_ccpl
Line 29, A numerical expression must appear here
Line 34, Undefined identifier getc_uart
Line 36, undefined identifier ptr
Line 39, undefined identifier ptr++
Line 45, expect }
The Baud rate generate register (SPBRG) is calculated as follow:
Parameters used are;
Frequency of crystal oscillator = 4MHz
Baud = 9600
Baud rate = 4000000 /(64(SPBRG +1))
When BRGH = 1 (high speed), SPBRG = 4000000 /(16 *9600) - 1
= ~ 25
When BRGH =0 (low speed), SPBRG = 4000000 / (64* 9600) - 1
= ~ 5.5
The actual baud rate by low speed (BRGH = 0)
= 4000000 /(64*6.5)
= ~ 9615.384615
The actual baud rate by high speed (BRGH = 1)
= 4000000 / (16*26)
= 9615.384615
Therefore, the resulatant error rate = 0
SOURCES: PIC Microcontroller:[u]An Introduction to Software & Hardware
Interfacing, Han - Way Huang, page 421 - 426,2005.
PIC MCU ' C COMPILER, Reference Manual, CCS,
Page 160 and 180, May 2005.
QUESTION
* Is it this value i will put in line 29 ?
* How would i define these identifiers?
I need your advice on how to write a successful built programme to allow my PIC 18F452 communicate with PC through MAX232.
If i want to transmit at high speed is it advisable to use 40MHz crystal oscillator for a baud rate of 19200 ? |
|
|
Simeon Labiran Guest
|
Request for advice |
Posted: Sun Jul 24, 2005 7:01 pm |
|
|
Dear Rwyoung & Catilo,
I programmed my PIC 18F452 and RS232 to transmitt and receive information from PC computer through DB9 port in C language as follow:
#include <18F452.h>
#include <Stdio.h>
#include <stdlib.h>
#Fuses.HS NOWDT NOPROTECT NOLVP
#Use fixed_io (C_outputs=PIN_C6) /* speeds up port use*/
#use delay (clock = 4000000)
#use rs232(baud= 9600,xmit=PIN_C6,rcv=PIN_C7)
Void init (void);
Void init (void){
// set up UART
setup_uart(9600);
setup_uart(9600,rsOut);
}
// set baud rate base on setting of PINS B0 and B1
setup_ccpl (CCP_PWM); /* configure UART to transmit and receive data in asynchronous, 8-bit data format */
/* The cycle time will be (1/baud * 64 * (SPBRG +1))
switch (input_b() & 3){
case 0: set_uart_speed(2400);
break;
Case1: set_uart_speed(4800);
break;
Case2: set_uart_speed(9600);
break;
Case3: set_uart_speed(19200);
break;
#define CR 0x0D
void main (void){
gets_uart(char*ptr)
char xx;
while(1)
{
xx=getc_uart(); /* read a character*/
if(xx==CR){ /* is it a cariage return?*/
*ptr='\0'; /* terminate the string with a NULL*/
return;
}
ptr++=xx; /* store the received character in the buffer */
}
}
After compile this programme with CCS C Compiler software,the build programme failed with the following error:
Line 14, undefined indetifier rsOut
Line 16,undefined identifier setup_ccpl
Line 29, A numerical expression must appear here
Line 34, Undefined identifier getc_uart
Line 36, undefined identifier ptr
Line 39, undefined identifier ptr++
Line 45, expect }
The Baud rate generate register (SPBRG) is calculated as follow:
Parameters used are;
Frequency of crystal oscillator = 4MHz
Baud = 9600
Baud rate = 4000000 /(64(SPBRG +1))
When BRGH = 1 (high speed), SPBRG = 4000000 /(16 *9600) - 1
= ~ 25
When BRGH =0 (low speed), SPBRG = 4000000 / (64* 9600) - 1
= ~ 5.5
The actual baud rate by low speed (BRGH = 0)
= 4000000 /(64*6.5)
= ~ 9615.384615
The actual baud rate by high speed (BRGH = 1)
= 4000000 / (16*26)
= 9615.384615
Therefore, the resulatant error rate = 0
SOURCES: PIC Microcontroller:[u]An Introduction to Software & Hardware
Interfacing, Han - Way Huang, page 421 - 426,2005.
PIC MCU ' C COMPILER, Reference Manual, CCS,
Page 160 and 180, May 2005.
QUESTION
* Is it this value i will put in line 29 ?
* How would i define these identifiers?
I need your advice on how to write a successful built programme to allow my PIC 18F452 communicate with PC through MAX232.
If i want to transmit at high speed is it advisable to use 40MHz crystal oscillator for a baud rate of 19200 ? |
|
|
Guest
|
Re: RS232 and 18F452 communication with Matlab |
Posted: Mon Feb 06, 2006 5:04 am |
|
|
Labiran, Simeon Olusola wrote: | I have a project that involves the serial communication between RS232 on PC computer and PIC 18F452 by using Matlab Simulink and CCS Compiler.
I wrote a C Langauge to configure the baud rate at 9600 with 4MHz crystal as follows:
TXSTA1 =0x24; /* enable 8- bit transmission*/
SPBRG1 =25; /* set baud rate to 9600*/
TRISC |=0x80: /*configure RC7/RX1 pin for input*/
TRISC &=0xBF; /*Configure RC6/TX1 pin for output*/
RCSTAbits.SPEN=1; /*enableUSART1*/
I need your advice to write a Programme that will enable proper communication between RS232 on PC Computer and PIC 18F452.
I shall be very grateful if my request is considered.
Yours Sincerely,
Simeon Olusola Labiran |
|
|
|
rnielsen
Joined: 23 Sep 2003 Posts: 852 Location: Utah
|
|
Posted: Mon Feb 06, 2006 9:34 am |
|
|
I wonder how many other boards this guys makes phantom posts to. |
|
|
sonicdeejay
Joined: 20 Dec 2005 Posts: 112
|
|
|
|
|
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
|