MC9s12dt256 serial communication newbie

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

MC9s12dt256 serial communication newbie

4,266 Views
Franklin
Contributor I
Hi,
 
I am trying to use the hcs12 microcontroller to read data from another device, do some calculations based on the data, and send a reply to the device. The data stream is from a DB9 serial connector, 115200 bits/second, 8 bit data width, no parity, no flow control.
 
I began two weeks ago with absolutely no background knowledge on serial communication, or the microcontroller. So far I've made little progress, but here is what i've tried.
 
I've tried to use the SCI 0 port (connecting through the pins on the 60 pin J1 connector). I connected leads from the other device's transmit and ground pins to the appropriate pins on the microcontroller. I added a processor expert bean AsynchroSerial, and picked a baud rate of 125000 (P.E. rounds up?) Input and output buffer size 128. There are other options, but as I don't really understand what's going on so I think it doesn't warrant going through everything.
 
I copied an example I found online which stores received characters in a variable "temp", using the function AS1_RecvChar(&temp). I connected the other device while it was sending data, and watched the variaable "temp" in the debugger. No signs of life. The variable stays at "0" no matter what I do.
 
If I could have some help troubleshooting, it would be greatly appreciated. I have checked all the relevant jumpers (such as Com_Enable) and they appear to be set properly.
Labels (1)
0 Kudos
19 Replies

1,072 Views
JimDon
Senior Contributor III
The baud rate mismatch is not good.
Are you using the PLL to jack up the clock?

PE should let you set the clock.

Also, are you sure that you are "cross connectiing" the TX and RX. RX should contect to TX.
0 Kudos

1,072 Views
Franklin
Contributor I
I don't know what PLL is, however the data stream coming in is at 115200 bits per second, and processor expert can be set to either baud (not sure if that is the same as bps) or ticks... which I believe has something to do with the clock.
 
There is an option in the same dialog box (bean settings) for overclocked. I left it alone...
 
I've tried cross connecting, to no avail. It could easily be that I was doing something else wrong at the same time though... suppose i'll try again. :smileysad: 
0 Kudos

1,072 Views
allawtterb
Contributor IV


Franklin wrote:
I don't know what PLL is, however the data stream coming in is at 115200 bits per second, and processor expert can be set to either baud (not sure if that is the same as bps) or ticks... which I believe has something to do with the clock.
 
There is an option in the same dialog box (bean settings) for overclocked. I left it alone...
 
I've tried cross connecting, to no avail. It could easily be that I was doing something else wrong at the same time though... suppose i'll try again. :smileysad: 


Part of the problem is that the baud rate being set by processor expert is not close enough to the target baud rate of 115200.  When you enter a baud rate PE will attempt to adjust a divisor to get the baud rate you want, the closest it can get for you is 125000 which gives an error of ~8.5% which is too high. 
Jim mentioned the PLL(Phase Locked Loop), it gives you the ability to change the bus clock speed.  The bus clock is used to generate the timing for your serial port, if you can change the bus clock to an appropriate value you should be able to get this error lower.  I would suggest trying to get it to within +/-3%. 
 
Under the CPU bean you have to Enable the PLL clock which will let you change the Internal bus clock.  A reasonable bus clock speed that I would try first is 14.74MHz, it will set it to the closest possible bus clock speed.  
0 Kudos

1,072 Views
JimDon
Senior Contributor III
Thats good advise, but you may not be able to get that exact clock rate.

I would try setting as high as you can, as the higher the clock rate the more resolution you will get.

After you adjust the CPU bean for a new clock, you can go back to the SCI bean and see how close it is as PE will automagically recalculate based on the new clock setting.

0 Kudos

1,072 Views
kef
Specialist I
Higher clock and resolution helps, but the best for 115200bps is not the max for S12DT 25MHz busclock (>3% baudrate error), but 24MHz - just 0.16% error.
 
0 Kudos

1,072 Views
Franklin
Contributor I
I set it to 24 MHz, 0.16% error, but still no luck.
 
When I run the program, some other unused variables are randomly changing, but the one I am trying to use to hold the received characters does not change.  Here is my code...
 
    AS1_Enable();
       for(;:smileywink:
       {          
           r = AS1_GetCharsInRxBuf();
          
          
          if(r>0)
          {
              AS1_OnRxCharExt(temp);
             //AS1_RecvBlock(&temp, r, &reall);
              AS1_RecvChar(&temp2);
             //AS1_SendChar(temp);
         
           
          }
       }
0 Kudos

1,072 Views
allawtterb
Contributor IV


Franklin wrote:
I set it to 24 MHz, 0.16% error, but still no luck.
 
When I run the program, some other unused variables are randomly changing, but the one I am trying to use to hold the received characters does not change.  Here is my code...
 
    AS1_Enable();
       for(;:smileywink:
       {          
           r = AS1_GetCharsInRxBuf();
          
          
          if(r>0)
          {
              AS1_OnRxCharExt(temp);
             //AS1_RecvBlock(&temp, r, &reall);
              AS1_RecvChar(&temp2);
             //AS1_SendChar(temp);
         
           
          }
       }


Does the code inside the the if statement ever run?  The call to AS1_OnRxCharExt won't do anything, unless you put some code in that function.  That function is called in the ISR when a character is received and is empty until you change it, so temp would not be changing.  temp2 should be changing, where have you declared these variables and how have you seen that they are not changing?
0 Kudos

1,072 Views
Franklin
Contributor I
the stuff inside the if statement never ran. I checked using the debugger to step through.
 
However, since then i've switched prototyping boards, and now the "temp2" variable changes. However, I believe it's not properly interpretting the data coming in.
 
The input data (i believe) is in the form of a character stream. I can read it on hyper terminal, and the output is stuff like ' DB = 442, Beacon ID: 12341234'
 
Now, the variable temp does not change as rapidly as it should be (I think). I have the debugger sampling as fast as it will go... and temp only changes to a new value every second or so.
 
Also of interest -- I have tried sending characters out through the serial port to the pc, to hyperterminal. I'm sure the baud rates are matched but only junk is coming out. Stuff like, "xxxxxxxxxxxx" or some funky epsilon. So, my theory is that the values the "temp2" variable is holding, is similarly being misinterpreted. (temp will vary, for example, as follows:  2, 3, 6, 214, 240, 6)
 
I think my problem now, has something to do with having the microcontroller interpretting the data properly-- and putting it in the proper form if sending.
 
Also, something I don't understand-- if I used the function AS1_RecvBlock(TComData *block, word size, word *num)    How can it store a whole block of data in what is essentially a character variable? (TComData is of type byte, i think? which is of type unsigned char? )  I am confused :smileysad:  Any help greatly appreciated!
0 Kudos

1,072 Views
nikosxan
Contributor I
Hello. I have also some problems with serial comm. Still i have some thinks to propose :
 
   a) Try first to correctly communicate with the PC (Hyper Term for example) using LOW speed baud rate, say 4800 and same setting (8 bits, no parity). Test both ways, S12 to PC, PC to S12. In this way you are sure H/W setup is OK.. Then go for higher speed.
 
   b) I'm not aware of the board you use, but i also propose use some H/W means to see if specific code runs .. For eg. put a command to flick an output bit (connected to a LED or a speaker for higher speed) when code execution is at a specific point of interest...When you see that it actually run up to that point move this cmd forward on....It helped in my case.......... LED is good for low speed code....speaker is better for higher speeds....
 
   c) Try to move from a code that works to the application you need (sipler to more dense code) when you have somthing that doesn;'t work........
 
Good luck
 
Nick
0 Kudos

1,072 Views
allawtterb
Contributor IV


Franklin wrote:
Also, something I don't understand-- if I used the function AS1_RecvBlock(TComData *block, word size, word *num)    How can it store a whole block of data in what is essentially a character variable? (TComData is of type byte, i think? which is of type unsigned char? )  I am confused :smileysad:  Any help greatly appreciated!


It a unsigned character that you must pass, you pass a pointer to an unsigned character.  In this case you would want to pass an array of unsigned characters that is large enough to hold the amount of data you are attempting to extract from the receive buffer.  Like:
 
Code:
word RxBytes;     // Number of bytes actually loaded into RxDatabyte RxData[10];  // Array to hold extracted databyte Status;      // Store any errors returned ...Status = AS1_RecvBlock(RxData,10,&RxBytes);  // Attempt to get 10 bytes from AS1

 
 
0 Kudos

1,072 Views
nikosxan
Contributor I
See link below (one of many) for Max232
 
 
 
0 Kudos

1,072 Views
ProcessorExpert
Senior Contributor III
Please make sure that the correct value of the crystal used on your development bouard is correctly filled in the "Clock frequency [MHz]" property in the CPU bean.

Could you please specify what development board are you using?

Could you pleas provide your project for us team?

best regards
Vojtech Filip
Processor Expert Support Team
UNIS

www.processorexpert.com


0 Kudos

1,072 Views
Franklin
Contributor I
I am using the prototyping board PBMCUSLK.
 
The data I am trying to receive looks like this on hyperterminal (at 115200 bps, 8-N-1)
 

VR=2.0,ID=01:8b:a2:a2:06:00:00:95,SP=BEACON-1,TS=56608
VR=2.0,ID=01:8b:a2:a2:06:00:00:95,SP=BEACON-1,TS=57536
VR=2.0,ID=01:8b:a2:a2:06:00:00:95,SP=BEACON-1,TS=58752
VR=2.0,ID=01:8b:a2:a2:06:00:00:95,SP=BEACON-1,TS=59520
VR=2.0,ID=01:8b:a2:a2:06:00:00:95,SP=BEACON-1,TS=60416
VR=2.0,ID=01:8b:a2:a2:06:00:00:95,SP=BEACON-1,DB=47,DR=1421,TM=1971,TS=61408
VR=2.0,ID=01:8b:a2:a2:06:00:00:95,SP=BEACON-1,DB=41,DR=1248,TM=1606,TS=62208
VR=2.0,ID=01:8b:a2:a2:06:00:00:95,SP=BEACON-1,DB=42,DR=1289,TM=1695,TS=63392
VR=2.0,ID=01:8b:a2:a2:06:00:00:95,SP=BEACON-1,DB=51,DR=1552,TM=1958,TS=64480
VR=2.0,ID=01:8b:a2:a2:06:00:00:95,SP=BEACON-1,DB=51,DR=1555,TM=1769,TS=65536
VR=2.0,ID=01:8b:a2:a2:06:00:00:95,SP=BEACON-1,DB=77,DR=2327,TM=2541,TS=66400
VR=2.0,ID=01:8b:a2:a2:06:00:00:95,SP=BEACON-1,TS=67776
VR=2.0,ID=01:8b:a2:a2:06:00:00:95,SP=BEACON-1,DB=374,DR=11244,TM=11650,TS=68928
 
Every line has a section, DB = xxx.  This is a distance measurement from the "listener" device to a beacon, which sends out an ultrasound and RF signal (the Cricket 2.0 system from MIT/crossbow). I need to get three distance readings (from 3 different beacons) and do trilateration to find the co-ordinates of the listener. This is what the hcs12 is intended for. The only problem is that I can't properly interpret the data coming in. I would know what to do if the "temp2" variable held the characters like 'V' and 'I' and 'D' ... clearly following what the input data is supposed to look like. However, I don't think it is being interpreted/translated properly for some reason.
 
I've attached a zip file of the project.
 
Message Edited by t.dowe on 2009-10-21 12:15 AM
0 Kudos

1,072 Views
allawtterb
Contributor IV


Franklin wrote:
I am using the prototyping board PBMCUSLK.
 
The data I am trying to receive looks like this on hyperterminal (at 115200 bps, 8-N-1)
 

VR=2.0,ID=01:8b:a2:a2:06:00:00:95,SP=BEACON-1,TS=56608
VR=2.0,ID=01:8b:a2:a2:06:00:00:95,SP=BEACON-1,TS=57536
VR=2.0,ID=01:8b:a2:a2:06:00:00:95,SP=BEACON-1,TS=58752
VR=2.0,ID=01:8b:a2:a2:06:00:00:95,SP=BEACON-1,TS=59520
VR=2.0,ID=01:8b:a2:a2:06:00:00:95,SP=BEACON-1,TS=60416
VR=2.0,ID=01:8b:a2:a2:06:00:00:95,SP=BEACON-1,DB=47,DR=1421,TM=1971,TS=61408
VR=2.0,ID=01:8b:a2:a2:06:00:00:95,SP=BEACON-1,DB=41,DR=1248,TM=1606,TS=62208
VR=2.0,ID=01:8b:a2:a2:06:00:00:95,SP=BEACON-1,DB=42,DR=1289,TM=1695,TS=63392
VR=2.0,ID=01:8b:a2:a2:06:00:00:95,SP=BEACON-1,DB=51,DR=1552,TM=1958,TS=64480
VR=2.0,ID=01:8b:a2:a2:06:00:00:95,SP=BEACON-1,DB=51,DR=1555,TM=1769,TS=65536
VR=2.0,ID=01:8b:a2:a2:06:00:00:95,SP=BEACON-1,DB=77,DR=2327,TM=2541,TS=66400
VR=2.0,ID=01:8b:a2:a2:06:00:00:95,SP=BEACON-1,TS=67776
VR=2.0,ID=01:8b:a2:a2:06:00:00:95,SP=BEACON-1,DB=374,DR=11244,TM=11650,TS=68928
 
Every line has a section, DB = xxx.  This is a distance measurement from the "listener" device to a beacon, which sends out an ultrasound and RF signal (the Cricket 2.0 system from MIT/crossbow). I need to get three distance readings (from 3 different beacons) and do trilateration to find the co-ordinates of the listener. This is what the hcs12 is intended for. The only problem is that I can't properly interpret the data coming in. I would know what to do if the "temp2" variable held the characters like 'V' and 'I' and 'D' ... clearly following what the input data is supposed to look like. However, I don't think it is being interpreted/translated properly for some reason.
 
I've attached a zip file of the project.


It seems that you are  setting the speed of your external clock to 16MHz when it should be 4MHz (from what I could read about your board).  Try setting this to the correct value.  I am not sure when you mean about "interpreted/translated" for the incoming data, there is no translation on the incoming data.  Your Cricket device is sending ASCII data and what you should receive on your board is the value of the ASCII characters that are being transmitted.  To get the numbers from these ASCII values will have to do the conversion in your software.
- Brett 
0 Kudos

1,072 Views
Franklin
Contributor I
Thank you Brett, I am glad to hear that it IS sending in ASCII and there is no strange sub-protocol or anything causing my problem.
 
I tried setting the Oscillator frequency [MHz] to 4.0, but I am still not getting any coherent data. The Internal bus clock is set to 24.0 mHz...  I am not sure what all of these settings do. It is likely something very simple, that I have just overlooked because I am so inexperienced. Your help is very appreciated! :smileyhappy:
0 Kudos

1,072 Views
JimDon
Senior Contributor III
Well, your board has an oscillator on it, that has a certain frequency. In this case 4 Mhz. There is a circuit in the mcu called a PLL ( Phase Locked Loop) that mulitples this frequency so that the mcu can run at a faster speed (in this case 24 Mhz). The muliplied frequency is what goes to the baud reate generator.

The values loaded into the baud rate generator to achieve a certain baud rate depend on the value of this clock. If you have set 4 Mhz in the cpu bean setting, then PE should correctly calculate the values for the baud rated generator.

Are you sure you have not cross conected the Rx and Tx wires?
There is a good chance you should be using a null moden cable, as both the device and the mcu board are DTEs so if you were to connect them together, you would need a different cable.



0 Kudos

1,072 Views
Franklin
Contributor I
Setting the external clock to 4 Mhz seems to have worked. I am getting coherent data through from my Crickets using the DB9 serial connector on the board. Great!
 
However, when I try to use the J1 connector pins, it no longer works. The AsynchroSerial bean says I should be using pins 5 and 7 for Tx and Rx respectively. However, when I connect the Cricket to the appropriate pins (and pin 3 for the ground) the data is no longer readable. Only the DB9 connector works.
 
This wouldn't be a problem except that I will eventually have to use both serial channels for my project, which means I will have to use the J1 connector pins.
 
Does anyone have any ideas as to why the J1 pins don't work? I was under the impression that serial channel 1 was hard-wired to the DB9 channel -and- pins 5 and 7, so that either way it should work the same.
0 Kudos

1,072 Views
JimDon
Senior Contributor III
Because the Cricket is outputting RS232 and the pins are TTL.
The levels are -/+ 3-5V on RS232 and are inverted.
See if you can find TTL level signals on the Cricket.

0 Kudos

1,072 Views
nikosxan
Contributor I
>Because the Cricket is outputting RS232 and the pins are TTL.
>The levels are -/+ 3-5V on RS232 and are inverted.
>See if you can find TTL level signals on the Cricket.
 
for the RS-232 to TTL level conversion you can build a circuit with MAX 232 (or similar)... In this way any TTL Rx, Tx pair could be connected to PC-232..... It justs needs 5Volts to work and 45- capacitors.......Circuit and connections are also in the MAX232 pdf........
0 Kudos