Discussion in "Project Help" started by    mem    Sep 25, 2008.
Thu Sep 25 2008, 03:55 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, 04:45 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, 04:47 AM ]
 mem like this.
Thu Sep 25 2008, 05:53 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, 06:17 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, 03:12 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, 03:21 AM ]
 mem like this.
Sat Sep 27 2008, 04:37 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, 06:54 AM
#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, 07:22 AM
#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, 07:57 AM
#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, 08:02 AM ]
Sat Sep 27 2008, 12:44 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

Conversation

Mon Jun 12 2017, 07:36 PM
madhusoodanan
how to receive string using 8051
ajay_bhargav
Thu Jun 08 2017, 05:48 PM
@Vinoth post in forum.
Wed Jun 07 2017, 06:40 AM
vinothkannan
how interface eprom in this code
Thu May 25 2017, 05:25 AM
Monir Hossain
Can i change the 7-Segment are common pin ? And how to?
Mon May 08 2017, 12:45 PM
ivan muvunyi
please can you send me wiring diagram for automatic control of 3 phase induction motor for production of maize flour
Wed May 03 2017, 08:39 PM
imran qureshi
hello friend
Sun Apr 30 2017, 05:49 AM
Lijin G Varghese
Can u send me the circuit diagram for Program to interface GSM Module with 8051 microcontroller (AT89C51) using PC and LCD
Fri Apr 28 2017, 03:47 PM
SANDEEP T S
I need pcb wizard 3.5 version kindly send valid link of yours
ExperimenterUK
Mon Apr 17 2017, 11:56 PM
Sorry..We had a problem with registration.Confirmation emails were not sent.Please re-register
Thu Apr 13 2017, 07:29 PM
Piyush Aughad
I want circuit & program for radar detection using Microcontroller 8051

Downloads

Comments

richard1976
Sat Jun 24 2017, 05:48 PM
DruvaN
Wed Jun 21 2017, 11:44 AM
lalala
Tue Jun 20 2017, 11:05 PM
Siddh@
Tue Jun 20 2017, 07:47 AM
firdaus0hsem
Sat Jun 17 2017, 04:53 PM
amir9297
Fri Jun 16 2017, 05:44 AM
madhusoodhanan
Mon Jun 12 2017, 07:31 PM
Aminookano
Sun Jun 11 2017, 05:50 AM

Online

Guests: 57, Members: 0 ...

most ever online: 182184
(Members: , Guests: 182184) on 06 Aug 2010: 05:37 AM

Members: 37847
Newest member: richard1976
Forum Activity
ExperimenterUK Avatar
Posted By ExperimenterUK
Mon Jun 26 2017, 08:42 PM
I have attached a demonstration file.It will echo ...
richard1976 Avatar
Posted By richard1976
Sat Jun 24 2017, 08:47 PM
Hi can you tell me how to send and recieve usart d...
ExperimenterUK Avatar
Posted By ExperimenterUK
Sat Jun 24 2017, 08:24 PM
HiUnfortunately there is too much code here to wor...
richard1976 Avatar
Posted By richard1976
Sat Jun 24 2017, 06:01 PM
hi i have written this programme and can send data...
ExperimenterUK Avatar
Posted By ExperimenterUK
Fri Jun 23 2017, 08:19 PM
>Should I need to check PWR_VLD bits every time on...
ExperimenterUK Avatar
Posted By ExperimenterUK
Fri Jun 23 2017, 07:42 PM
I would say yes, but data transfer will be slower ...
Utsavi Avatar
Posted By Utsavi
Thu Jun 22 2017, 12:14 PM
Hello ExperimenterUK, Thanks for your support. Fro...
DruvaN Avatar
Posted By DruvaN
Wed Jun 21 2017, 11:51 AM
Is it possible to Use 8051 bit-banging method to p...
ExperimenterUK Avatar
Posted By ExperimenterUK
Tue Jun 20 2017, 06:27 PM
You could start by looking at this tutorial on ke...
firdaus0hsem Avatar
Posted By firdaus0hsem
Sat Jun 17 2017, 04:58 PM
I have an issue regarding how to use 4x3 keypad to...