Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

pic16f877 + rs232 + matlab

Status
Not open for further replies.

a_sniper

Newbie level 3
Newbie level 3
Joined
Dec 30, 2011
Messages
3
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Visit site
Activity points
1,312
hi every one
i am trying to write program to make communication between matlab and pic16f877

but when i tried to send string value from matlab to pic using "fprintf" in matlab and "gets" in ccs compiler it didn't work
although when i tried to send character from matlab to pic using "fprintf" in matlab and "getc" in ccs compiler it was ok
so can any one help me

and when i used virtual terminal for rs232 in Proteus instead of matlab it was ok

notice
i used in simulation Proteus 7 Professional

HTML:
#include <16f877.h>
#device adc=10
#use delay(clock=20M)
#byte port_a=5               // port_a address 5
#byte port_b=6               // port_b address 6
#byte port_c=7               // port_c address 7
#byte port_d=8               // port_d address 8
#byte port_e=9               // port_e address 9
#fuses HS
#use rs232(baud=9600,xmit=pin_c6,rcv=pin_c7,BITS =8,PARITY=n,STOP=1,ERRORS)

#include <stdlib.h>
#include <string.h> 
char c;
float data;
float  volt;

int pwm1=0;
char vstr[10]; 

void io_set()
{
   set_tris_a(0xff);
   set_tris_b(0x00);
   set_tris_e(0xff);
   output_b(0x00);
}


////////////////////////////////////////////////////////////////////////////////
////////////////////////////// control /////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
void read_analog()
{
   while(true)
      {
         if(kbhit()) 
         {
            gets(vstr);
            pwm1=atoi(vstr);
            output_b(pwm1);
            break; 
         }
      }
}

void main()
{
   setup_adc_ports(A_ANALOG);      // PA all a/d ports and Vdd ref+  Vss ref-
   setup_adc(ADC_CLOCK_DIV_32); 
   
   io_set();
   while(true)
   {
      read_analog();
   }
}

and matlab code
HTML:
s=serial('COM1');
set(s,'BaudRate',9600);
set(s,'DataBits',8);
set(s,'Parity','none');
set(s,'StopBits',1);
set(s,'FlowControl','none');
fopen(s);

x=input('enter number \n');
ch=num2str(x);
fprintf(s,'%s',ch);

fclose(s)
delete(s)
clear s
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top