Discussion in "Project Help" started by    mem    Sep 25, 2008.
Thu Sep 25 2008, 09:25 am
#1
Hello all,
I have moved on from my LED Chaser project

I am now attempting to control the LEDs via a serial / comport interface.

First Questions: How do I calculate a 9600 baud when using a 10Mhz crystal (or any speed crystal).

When I run the code atm, LED0 = OFF, LED1-7 = ON which is the pattern I am setting the leds to. Then I open up putty on the pc and open a serial connection to comport 1 at 9600.

What I expect to happen when I typed into putty is the leds to change with each key stroke to match the 8bit value the comport is sent.
What Actually happens is, the 1st key pressed sets all lights to on (ie 0x00) and then it does not respond to any more keystrokes.

I think its mainly an issue with the baud value currently being used (from the serial interface tutorial is for an 8Mhz crystal and I am using a 10Mhz.

Looking forward to replies,
mem.



#include <REGX51.H>


void serial_init(){
        TMOD = 0x20;
        SCON = 0x50;
        TH1  = 0xFD;
        TL1  = 0xFD;
        TR1  = 1;
}

void serial_send(unsigned char dat){
        while(!TI);
        TI = 0;
        SBUF = dat;
}
unsigned char serial_read(){
        while(!RI);
        RI = 0;
        return SBUF;
}

// =================================================================================
// Main
// =================================================================================

void main( void )
{
	char c;
	char c_old;
	serial_init();
	P0=0x01;

	while(1)
	{
		c_old = 0x00;
		c = serial_read();
		if(c != c_old && c != 0x00)
		{
			c = c_old;
			P0 = c;		
		}
	}
}
Thu Sep 25 2008, 10:15 am
#2
hello mem,

congrats, on completing the led chaser project !

first things first- don't use xtals like 10Mhz,8mhz,4mhz,12mhz etc
because the the margin of error is very high, for eg : if you try generating 9600 baud with 10meg xtal
it will give you 35 % error. which is very high and not acceptable. even if you try a 1200 or 2400 baud with this xtal it'll give you 2% error.

so for all you serial comport projects you have to use 11.0592 Mhz xtal which can generate different bauds without error.

with the new xtal you have to change only the delay/s in your code (led chaser)

here's bit of code for serial port init for 11.0592 meg at 9600 baud using timer 1

mov Tmod,#20h
mov scon,#50h
mov TL1,#00h
mov TH1,#FDh
setb TR1



Arun






[ Edited Thu Sep 25 2008, 10:17 am ]
 mem like this.
Thu Sep 25 2008, 11:23 am
#3
Thanks very much Arun,
I did think it might be the crystal, I shall buy a 11.0592 Mhz crystal tonight and have a play.
Thu Sep 25 2008, 11:47 pm
#4
If you want to post complete project on this website after finishing it, them do mail me after everything is done will be good to see it here..
Fri Sep 26 2008, 08:42 am
#5
hi mem,
just found a small logical error in ur code. the last conditional expression:
if(c != c_old && c != 0x00) would always translate to:
if(c != 0x00 && c != 0x00) which may not be the required logic. similarly, the expression:
c = c_old would always put 0x00 in c and consequently in port P0.

here is the corrected main function:


void main( void )
{
        char c;
        char c_old;
        serial_init();
        P0=0x01;
        c_old = 0x00;
        while(1)
        {
//                c_old = 0x00 ;                           //removed
                c = serial_read();
                if(c != c_old  &&  c != 0x00)
                {
                        c_old  =  c;                 //modified
                        P0 = c;        
                }
        }
}



[ Edited Fri Sep 26 2008, 08:51 am ]
 mem like this.
Sat Sep 27 2008, 10:07 am
#6
Ajay, I shall definitely do a howto when I get some spare time .

pdl33, thanks for catching my stupid error :blush , that is what happens when you code real tired .

Unfortunately my local electronics store didnt have the the 11.0952Mhz crystals, so I have put some on order from futurlec. In the meantime, here are the crystals I do have:
24Mhz
20Mhz
10Mhz
6Mhz (stolen from a broken xbox controller)
4Mhz
3.579545Mhz

If any of those would work (even with large amounts of errors) for the time being let me know please. Im also looking through this very handy calculator that keil provides:
http://www.keil.com/products/c51/baudrate.asp

Im assuming I set my Serial Clock Divisor to 1 ?

Sat Sep 27 2008, 12:24 pm
#7

Hello mem,

you have all the xtals except the one actually needed, i have checked all the values for suitable baudrates and they all give large errors ( from 35 % to 62%) only 24Mhz xtal is the one ideal chap which gave error of less than 1 % for 4800 Baud, so you may use 24 Meg to generate 4800 baud (for 9600 baud it gives 8.5 % error)

here's serial init for 4800 baud :

mov Tmod,#20h
mov scon,#50h
mov TL1,#00h
mov TH1,#0F3h
setb TR1



Arun
 mem like this.
Sat Sep 27 2008, 12:52 pm
#8

Hello mem,

you have all the xtals except the one actually needed

Arun Kumar V


Haha, I find that amusing , just my luck.
Im going to swap over to the 24Mhz and start testing.

Thanks heaps
Sat Sep 27 2008, 01:27 pm
#9
#include <REGX51.H>


void serial_init()
{
        TMOD = 0x20;
        SCON = 0x50;
        TH1  = 0x00;
        TL1  = 0xFD;
        TR1  = 1;
}

void serial_send(unsigned char dat)
{
        while(!TI);
        TI = 0;
        SBUF = dat;
}
unsigned char serial_read()
{
        while(!RI);
        RI = 0;
        return SBUF;
}

void main( void )
{
	unsigned char c;
	signed int rst=0;
	signed char c_old = 0x00;
	P0=0xFE;

	serial_init();

	while(1)
	{
		c = serial_read();
		if(c != c_old && c != 0x00)
		{
			c_old = c;
			P0 = c;		
		}
	}
}


When I open putty (connecting @ 4800 baud & default settings) and press any key all leds go off like it has received 0xFF. I does not matter what key I press after that no leds are displayed.

Is that link for the keil baud calculator I posted relevant to what Im trying to achieve. If it is do I use smod=0 or smod=0 ?.

my putty settings:

Am I using the correct comport settings ?

Thanks for the help.

*** EDIT ***

Holding down the spacebar for a long time will cause a single LED to blink off an on, not many other keys do anything.


[ Edited Sat Sep 27 2008, 01:32 pm ]
Sat Sep 27 2008, 06:14 pm
#10
1.another possible error usually made when using the serial port for the first time is:
Tx of com port ------ Tx of uC
Rx of com port ----- Rx of uC
which is wrong. u have to connect
Tx of port ----- Rx of uC
Rx of port ---- Tx of uC
hope u have connected it right.
2.Set the flow control to 'none' instead of Xon/Xoff
3.in ur above modified code u have not changed the initialisation of serial port as suggested by arun for 24Mhz crystal.
 mem like this.

Get Social

Information

Powered by e107 Forum System

Downloads

Comments

AntoniaRoons
Fri Apr 19 2024, 09:59 pm
carpinteyrowrl
Fri Apr 19 2024, 02:51 pm
DonaldJAX
Fri Apr 19 2024, 01:08 pm
Lewisuhakeply
Thu Apr 18 2024, 06:00 pm
Darrellciz
Thu Apr 18 2024, 11:07 am
Charlessber
Thu Apr 18 2024, 09:29 am
BartonSem
Thu Apr 18 2024, 04:56 am
DonaldKnown
Thu Apr 18 2024, 12:24 am