We are running a survey to help us improve the experience for all of our members. If you see the survey appear, please take the time to tell us about your experience if you can.
Hai...i tried to send the letter 'L','S','R' from matlab just opening in command line and typing...
se=serial('COM7'); set(se,'BaudRate',9600); fopen(se); fprintf(se,'%c','L','async'); fclose(se)
in matlab uc(at89c51)...and in response it should change its port settings accordingly... Here is my code...
#include<reg51.h> sbit mlp=P1^0; sbit mln=P1^1; sbit mrp=P1^2; sbit mrn=P1^3; void main(void) { char i; SCON=0x50; // to receive and transmitt serially TMOD=0x20; // timer in mode 2 8 bit auto reload mode TH1=-3; // baud rate 9600 TR1 = 1; while(1) { if(TI==1) TI=0; else if(RI ==1) { i=SBUF; RI=0; } if(i=='S') { mlp=mrp=1; mrn=mln=0; } else if(i=='L') { mlp=mrn=1; mrp=mln=0; } else if(i=='R') { mlp=mrn=0; mrp=mln=1; } else { mlp=mrn=0; mrp=mln=0; } } }
I have my uc connections on bread board...and i dnt knw wat is the Tx,Rx pin in my connector... So what i did was put a 2k Resistor in between so that not to fry the ckt accidentatally in case i changed the pins...the character is not at all being sent...the four pins are in 0 condition...see the ELSE part...in abv prgm... Please help... re soon...please....
So - have you verified that Matlab is sending anything?
Have you made sure that you have a level converter chip, like a MAX232 that converts from the RS-232 signal levels to the logic levels your processor uses?
Next thing - your code makes use of the content of the variable "i" without caring if you really did receive a character or not. You either need to give "i" a blank value at the start of each loop iteration, or move the use of "i" inside the part of the if statement that gets trigged if you have incomming data. Never use uninitialized variables!
Thank you very much for replying... I am using a usb to serial converter from my laptop....so i think the MAX chip is inbuilt...and
secondly fprintf executed in matlab command line flawlessly so i assumed there is no problem in sending...
and since my project involves only reception at the uc end can i just use rx pin alone???....to avoid confusion??.... and is there any seperate ckt for sync and async method of transmission...which is better???
"your code makes use of the content of the variable "i" without caring if you really did receive a character or not. You either need to give "i" a blank value at the start of each loop iteration, or move the use of "i" inside the part of the if statement that gets trigged if you have incoming data. Never use uninitialized variables!"
But what i want with 'i' is i dont care whatever the value of 'i' is..all i want to do is to hold the previously received value of 'i'...and keep the ports in that corresponding value... For example..if in an instant if i receive...'S' i have to keep the port in that corresponding value...till it receives next char... if i initialise i in the next iteration then the value of i will be overwritten and the control goes to the ELSE block...and the port all becomes zero... I am completely new to serial so i may be wrong...
If you have a USB-to-serial cable with any internal MAX-chip, then that chip is there to create RS-232 signals, i.e. to create a standard RS-232 port. Such a cable requires that there are a second MAX-232 chip to convert back from RS-232 voltage levels to the logic levels of your microprocessor.
There also exists USB-to-serial cables that delivers logic-level signals. This type can be found with 3.3V or 5V logic levels and can then be connected directly to the processor pins.
But a standard USB-to-serial dongle gives RS-232 levels and requires that you have a transceiver chip between the dongle and your microcontroller.
It doesn't matter if fprintf executed "flawlessly". Have you verified it? Have you connected the serial signal to another computer or a different serial port and verified? Or used an oscilloscope to check? Not getting a runtime error doesn't mean "flawlessly". How have you verified that the data was sent out correctly?'
Yes, you can use just ground + tx on the cable, connected to ground + rx on your circuit. But you either have to configure Matlab to not use hardware handshake, or you need to loopback the hardware handshake signals. I don't know if Matlab defaults to using hardware handshake or not - something for you to figure out.
RS-232 is always asynchronous.
Synchronous communication can run at higher speeds, but either requires separate clocking signals, or special communication patterns so the receiver can recreate the clock from the bit pattern.
But what will your program do until you receive the first character? What value will i contain then? Never, ever, write code that makes use of an uninitialized variable like that. It just gives a bloody nose. Or maybe many bloody noses.
If you initialize i to an "no value" value, then you can have an "else" that doesn't do anything. Same if you move the "do" code inside the block that performs the receive - then you will only look for the contents of i after you have received a character. And when you do not receive any value, you can't have any change of i so no need to perform any action.
There also exists USB-to-serial cables that delivers logic-level signals. This type can be found with 3.3V or 5V logic levels and can then be connected directly to the processor pins. these are so rare that I doubt you have one such (if you bought it in a computer store it is not), so the rest of this post is based on you do not have a such.
If you have a USB-to-serial cable with any internal MAX-chip, then that chip is there to create RS-232 signals, i.e. to create a standard RS-232 port. so with these signals connected directly to your uC it went pop, blop fizzz ad burned one or both of the Rx and Tx pins.
It doesn't matter if fprintf executed "flawlessly". this seems to indicate that the Tx pin survived. RS232 levels are such that SOME inputs will correctly catch TTL levels.
Yes, you can use just ground + tx on the cable, if you did it went pop, blop fizzz ad burned one or both of the Rx and Tx pins.
Put A MAX232 (equivalent) on your board, replace the uC and buy a new cable.
In the unlikely event it 'works' after just adding the MAX232(equivalent) STILL replace the uC and buy a new cable. The previous persom that I know of that 'forgot' the MAX232 did not buy new parts because "evrything worked" later he spent a very long time debugging because the weakened parts finally broke (went intermittent)
Erik
The USB-to-serial with logic level output are most definitely not sold in normal computer shops.
It's something like www.ftdichip.com/.../USBTTLSerial.htm
If there is a standard 9-pin RS-232 connector on the cable, then it is not supplying logic levels, and a MAX232, MAX3232 or similar is needed.
"these are so rare"
Nonsense!
They are widely available from both "mainstream" and "hobby" electronics/robotics distributors.
"Break-out" boards & modules are also widely available to give the same function; eg,
www.sparkfun.com/.../718
thank u for replying...now is this program okay???
#include<reg51.h> sbit mlp=P1^0; sbit mln=P1^1; sbit mrp=P1^2; sbit mrn=P1^3; void main(void) { char i='\0'; SCON=0x50; // to receive and transmitt serially TMOD=0x20; // timer in mode 2 8 bit auto reload mode TH1=-3; // baud rate 9600 TR1 = 1; while(1) { if(TI==1) { TI=0; } else if(RI ==1) { i=SBUF; if(i=='S') { mlp=mrp=1; mrn=mln=0; } else if(i=='L') { mlp=mrn=1; mrp=mln=0; } else if(i=='R') { mlp=mrn=0; mrp=mln=1; } else { mlp=mrn=0; mrp=mln=0; } RI=0; } } }
i still dint get the o/p...i have a cheap usb to serial converter along with transreceiver and my friend said he even did interrupts with this cable....so i dnt tink that wd be a problem.... and i didnt chk o/p of serial com still to do....tel me f dis prog is okay??? also for serial communication do we have to mess with dat ale and psen pins...or anything out of ordinary u wd do for normal line follower???
also please note that all the above program...simulated prefectly in keil....
dnt tink wd chk tel f dis prog dat u wd
I will copy your idea saving on typing and type 'no' instead of 'yes'