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.

serial port transmission pic18f4620

Status
Not open for further replies.

Vandal S

Member level 3
Joined
May 2, 2009
Messages
54
Helped
3
Reputation
6
Reaction score
1
Trophy points
1,288
Activity points
1,749
serial port transmission

can i have direct acces on the bits trasmitted to serial port? if i wanted to transfer 1011 on the serial port, can i make a program in c# which only transmits those bits (those are bits, not characters)? or the serial port just transmits ASCII characters?
 

proteus compim port for testing at commands

If by "serial port" you are referring to a UART, then it depends on the UART. Some will allow you to set the number of data bits, although I'm not aware of any that will let you set fewer than 5 data-bits. Most likely you would have to write code to 'bit-bang' the data - that is toggle a line that will emulate a UART.

Perhaps you could use the DTR or RTS line and have your code drive that to output data. Of course, you would have to rewire your connector to use that line as the TX line.
 

    Vandal S

    Points: 2
    Helpful Answer Positive Rating
+pic16f +uart +terminal

it's the serial port from the pc and i want to interface it with the UART from a PIC (with the help of rf modules between). there are 8 bits of data. but i heard(that's why i'm asking) that there is a problem with the rs-232, you can only send ASCII codes, or something like that, anyway, not 10101010 for example. i was hoping to write a program in c# which would do exactly that, send what bits i want (in groups of 8 if that's the only way for example 10101011 <-sync and start bit 11001100 <-the data bits)
 

hex transmission thru serial port +c#

Vandal S said:
but i heard(that's why i'm asking) that there is a problem with the rs-232, you can only send ASCII codes, or something like that, anyway, not 10101010 for example.
Not true for either the PC or PIC side - you can send any value from 0 (0000000) to 255 (11111111) inclusive. I'm not sure what the 'problem' you heard was referring to.
 

8 bytes serial transmission

thank you very much. probably we were both right, but i haven't made my self clear so that's why that person told me so. the last is FF which is 11111111;

later edit: which is 255 in decimal, just as you said. anyway, i am only interested in binary
 

using mscomm for com port pc transmission coding

i tested with an osciloscope with hyperterminal and terminal1.9b.
when i press 1, there are indeed 8 bits, 4 of them (the first from left) i recognized; they respond the numbers, 8 is 1000 in inverse order and negated. some bits from right responded to some letters.
but is there a program in which i enter 10011011 for example and i see on the osciloscope that thing? if i do that, there would be 8 bytes; i want to see them in a single byte.
i think this was what that person tried to explain to me.
 

manchester coding for serial transmission pic

Vandal S said:
i tested with an osciloscope with hyperterminal and terminal1.9b.
when i press 1, there are indeed 8 bits, 4 of them (the first from left) i recognized; they respond the numbers, 8 is 1000 in inverse order and negated. some bits from right responded to some letters.

When you press the '1' key on the PC it generate an ASCII character whose value is 31 hex or '00110001' binary. When this is sent over the serial port it is sent least-significant-bit first, so it appears on the TX line as '10001100' binary.

Vandal S said:
but is there a program in which i enter 10011011 for example and i see on the osciloscope that thing? if i do that, there would be 8 bytes; i want to see them in a single byte.
i think this was what that person tried to explain to me.
If you enter '10011011' as eight individual characters on hyperterminal then of course you will get eight bytes. If you want it as a single byte then you have to enter that value as a single character. This is problematic for non-displayable characters. However, Windows provides a way for you to do this:
- Make sure NumLock is enabled on your keyboard.
- Press and hold the ALT key.
- While holding down the ALT key, enter the 3-digit decimal code for the extended ASCII character you want to generate using the numeric keypad.
- Release the ALT key.
NOTE that you need to enter the DECIMAL value of the charater
For example to enter '10011011' as a single character:
- Convert '10011011' binary to decimal - it equal 155 decimal
- Press and hold the ALT key
- On the numeric keypad press 1, then 5, then 5
- Release the alt key
If you need to send lots of non-displayable characters you may want to create a file containing the data you want to send, and then use the Send File feature to send the data. I don't use Hyperterminal so I don't if you can send a file using it, but there are other serial communiations programs that allow you to do that.
 

    Vandal S

    Points: 2
    Helpful Answer Positive Rating
hex data transmission using c# serial port

all i have to do is implement that in c# (which probably won't be so easy), as hyperterminal was just a way to test things.
thank you very much for the information and the willing to teach others.

later edit: and another question: i observed that if i send UU, there is a variable time between transmissions, like 10...10 10..10, other times 10...10 <-more time elapsed-> 10..10 (i represented 10..10 the hole transmission of U, whici is 10101010). why is that and is there a way to make things stable?
i'm thinking that if i send to the rf a sync byte and then the data, it will mess up the data a little if there is a big time window
 

c# serial port 8-bit ascii

I think you have some confusion what exactly the serial port is doing. All this talk about characters etc is meaningless, it only matters if you are sending data to a PC or Terminal. In that case numbers are interpreted as either characters or control codes. The serial port does not care what format your data is in, it will just send in binary whatever you put into its buffer. 0A, 10, or 00001010 is just the same. We will just use decimal for reasons of clarity, but any number base will do. Say if you send the number 32 to a PC it will be interpreted as a space. If you were to send the same number to another controller, it would just be received as the number 32, and could be acted on accordingly. I am really saying that it is really a matter of interpretation at the receiving end, the data is no different. Hope this helps a little.
 

replace max232 with diod

maybe there are packets lost from your explanations to me:D
the first idea [when i didn't know that there are only ASCI codes sent on serial] to send just a byte and could control 4 motors in every way (which ever variable in the array was 1, that motor was active, a[1]=1 turned direction motor, for example. i still can do this if i use some numbers (1-1000 forward, 2-0100 left, so 1100<-3 was left and forward. but in the light of the new knoledge, i can make at the transmitter some sort of code: if left and back pressed, send p (for example; and i don't care for the code) and at the receiver, if p was received, do that thing.

but what about the different timing between 2 bytes (it's the same question as in the last post)? will it affect things? and most of all, i am a beginner at uC programming; do you have some easy codes for pic progrmming? i saw some codes, but i need some elementary things, setting the uart, checking for received (about that, how do i know that there is something received? is there an interupt or something?),cheking that there is a sync byte (AA), cheking the data (if(data=x) led on)
i'm sorry if i ask so much, but my project time is running out.
 

c# send ascii character to serial port

I think you're still misunderstanding asynchronous serial communications and ASCII encoding.

ASCII is just a coding scheme that relates 7-bit codes to a set of control codes (0 to 31), and displayable characters (32 to 127). You can see a mapping of these codes at: www.ASCIITable.com. You can send any 8-bit data via the UART - assuming the device is set for 8-bit mode. This data does not have to be an ASCII character. The limitaion is if you are using a terminal program (like hyperterminal) you can only send what you can input via the keyboard.

When a byte is sent by the UART, it is sent independent of other bytes. The time between two bytes is dependent on the sending program. If you are using hyperterminal it will send the characters as fast as you type them. You may want to look at the Wikipedia entry for Asyanchronous Serial Communications for a primer: https://en.wikipedia.org/wiki/Asynchronous_serial_communication

It may be helpful if you described your application so we could have a better understanding of what you are trying to accomplish, and therefore suggest the best approach to your problem.
 

Re: serial port transmission

again, thank you for trying :D
i entered in a program 2 characters and pressing enter and there was different distance, that's way i'm asking.
the application, thich would be easy for someone who's not a total beginner si this:
i want to make a c# program at the computer side that sends at least data as it can through rs232 - max232 - rf modules - pic16f and control 2 dc motors on and off.

so i have to make the sending program in c# (multiple keypress and stuff) and the receiving program for the pic(recognising the initialisation sequence 1010.. which is for stabilise the rf receiver and the data, a single byte that can tell the status of those 2 motors.) and there are multiple problems that i haven't taken into consideration, are there buffers at tx and rx that could get full if i press too much, etc, that's way i was asking for some examples.
 

serial port transmission

Most micros with hardware serial ports can buffer 1 extra character, other than the one they are processing. Do you really need to send a sync byte before any data, or just at power up? If you are both reading and writing to the serial port, you will need to use some software handshaking. Could be as simple as sending a byte to request motor status, and waiting for the reply. You may need software buffers, depends on how you do it. Programming at first seems almost impossible, it does get easier. always a challenge though. Sorry no specific example of code for this application, maybe others can help.
 

    Vandal S

    Points: 2
    Helpful Answer Positive Rating
Re: serial port transmission

sorry for forgetting the last time to post a diagram. i think i'm better at drawing then explaining in words :)
-i don't think sending the sync byte every time is necessary *1 , but since i control a toy car, there is 4600baud rate, i think i can send an extra byte without timing problems.
-the pic is 16f877
-there sould be no handshaking i think, the data flow is pc->pic, no data going back*2
i have seen some uart examples in mikroc folder; i most probably am wrong, but i think that they made things easy with their functions and everything

another questions:
-if i use max232, would the UART unit in the pic be able to understand the character sent? or it would have to be inverted?
-in the diagram, there is the notion of "go front", which is just a port is 1 (5V), but for how long? i am not clear yet about this thing, there is 4800 baud rate, every second that the keyboard button is pressed, there are 2 bytes transmitted, so for how long i should enable the engine?


more explanation - probably boring
*1 if i send 2 commands quickly, it wouldn't, but in rest it would, as it is RF transmission, no Manchester encoding, no cheksum, no nothing. maybe AGC (i read a little about it here) would mess up things, but to be sure i'm sending everytime
*2 just like in the army. whenever it's a command, there should be immediat execution of orders, without questions asked, time for being prepared, discussions :)
*3 i figured that the characters should be w-front, s-back, a-left d-right, q-front and left, e-front and right,etc
 

serial port transmission

Drivers do invert the signal, you need to make sure that the receiver does the same. No sure about MicroC, but in CCS you can invert the data stream in software. I assume MicroC could do the same if required.

Normally ports would latch on until turned off with another signal. I think you will have difficulty controlling motors with this method, this is due among other factors, the amount of electrical noise generated from the motors. Any reason you do not want to use PWM and model servos. Sorry It is many years since I played about with models. I am sure the techniques of transmission have changed somewhat from my days. Don't really keep up to date in this area.
 

Re: serial port transmission

as you can see in my replys, i don't have many knoledge, so i was trying to keep things as simple as i can, no pwm, no speed control, servo, etc.
powering dc motors is like lighting a led (5v on one port, 0 on the other, reverse for reverse spin), it just passes through a h bidge ic, for more power.
 

Re: serial port transmission

Your project is a very simple one and I think your lack of knowledge is making it appear much more difficult that it really is.

Below is a diagram of what I believe your hardware setup is. The MAX 232 is simply level-translating and inverting the RS232 output from the PC. Its output will be correct for the rest of the hardware - you do not need to invert the signal at the PIC!

As to the software. Each command you send is a packet consisting of the Sync Byte, and your command. Sending the data from the PC is easy using the MSCOMM control. I assume it is now clear to you how to do that.

On the PIC side the code is trivial. Below is some code to do what you want. Of course, you have to write the functions 'SioInit', 'MotorInit', 'SioCharRdy', 'SioGetChar', etc. This simple example doesn't include code to handle the case of losing commands, or it the vehicle goes out of range.

Code:
#define SYNC_BYTE	'U'	
#define CMD_FORWARD 'W'
#define CMD_REVERSE 'S'				

void main(void)
{
    char data_byte;	// To hold the byte we receive from the UART
    
    SioInit(4800);	// Initialize the UART hardware to 4800 baud
    MotorInit();	// Setup the Motor Driver Hardware
	
    while(1)		// Stay in this loop forever
    {
        while (!SioCharRdy());	       // Loop until we get a byte from UART
        data_byte = SioGetChar();	       // Get the character from the UART
        if (data_byte == SYNC_BYTE)    // If we received a SYNC character...
        {
            while (!SioCharRdy());	      // Loop until we get command from UART
            data_byte = SioGetChar();   // Get the command byte from the UART
			
            switch(data_byte)	      // Set motors based on reeived command
           {
                case CMD_FORWARD:     // If Forward command...
                    MotorForward();	     // make the motors go forward
                    break;

                case CMD_REVERSE:	    // If Reverse command...
                    MotorReverse();	    // make the motors go backward
                    break;

	case statements for other commands here
			
                default:		    // For invalid commands...
                    MotorStop();	   // Stop the car		
                    break;
            }
        }
    }
}
 

    Vandal S

    Points: 2
    Helpful Answer Positive Rating
Re: serial port transmission

Thank you very much!! That's the exact diagram i'm using.
I made a program based on this code (thank you again); i added some things, just for testing now; i have to press U then A, or S.

/*
*1 probably i shouldn't be so communicative and more work oriented, but maybe i could help someone sometime; for example i (don't know if that is the general case) had problems using the serial port with 2 programs at one time. so i installed virtual serial port driver and made a loopback connection between 2 ports. so terminal1.9b uses one and proteus the other, and i can make them communicate
*2 if you reffer to mscom.ocx or something like that, from .net 2 there was a library or something implemented that made things easier;
*3 the first way i tested, with 2 terminals, a max232 and an inverter: in one terminal i press a character and if it's not U, it's error, if it's U and other character, error, if it's U followed by W A S D, it's good and this appears in the other terminal; this works well if i press rarely, the faster i press, the more errors occur; maybe i should use a timer, delay or something ( the ideal thing was to transmit only when the key is pressed and only when it's released=>2 data bytes , not the full time it's pressed=>much more data.)
the secound way, i put a compim, virtual rs232, which is connected in some way to terminal. if a rarely send, it's error, if i keep the send button pressed, every time a succes, but even if it's typed UA in terminal1.9, it recoqnises only the U, so maybe a little delay at sending. there will be further testing
*/

TODAY edit: sorry for the long and not so useful message; i am sleep deprived for days and not focusing very well
the question is: is it not really a need for invertor?
in proteus i simulate using compim, max232 and without an inverter it doesn't work.
it there a code to invert ASCII code in the pic?
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top