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

Wed Nov 08 2017, 01:35 PM
Anirudh jain
I want to burn code in atmel 89s51 24pc 0329 but cannot get the connection with the burner. Can somebody provide me with the circuit diagram for connecting controller to burner. I'm using usb burner
Thu Nov 02 2017, 11:44 AM
karpal
write a program to exchange the content of b register with external RAM address 02CFH help me
Wed Nov 01 2017, 05:47 PM
sanae
i am using the DHT11 but i'm getting some errors in the simulate the programe in proteus 8 professional,: 1)cannot find model file 'DHTXX.MDF'.2)simulation failed due to netlist linker error(s).
Fri Sep 29 2017, 02:45 AM
Maaz
i am doing the same project on verilog please contact me [email protected]
Wed Sep 20 2017, 12:36 AM
Ted
In a missing pulse detector using a 555 timer connected as a monostable vibrator with a PNP transistor,how does the positve input pulse turn on the PNP transistor?
Sun Sep 03 2017, 02:29 PM
martymarty
How do I connect an <NOKIA 1100 LCD > to an atmega328P ?
poojab51296
Sun Aug 20 2017, 10:23 AM
i am intending to interface sd card with STM32F4 and want to write a file in sd card using SPI protocol but using HAL only. Can anyone help me PLEASE!!!!
poojab51296
Sun Aug 20 2017, 10:22 AM
hello
rawat742
Fri Aug 18 2017, 11:37 AM
hi @ angel patel i will attach a file i hope it will be helpfull for you
ExperimenterUK
Mon Aug 14 2017, 09:57 PM
@ angel patel... just log in and post your problem

Downloads

Comments

PANDIKOTESWARARAO
Mon Nov 20 2017, 07:40 PM
Hemantika
Sun Nov 19 2017, 07:34 PM
Wed Nov 15 2017, 10:46 AM
Ansh12
Sat Nov 11 2017, 03:43 AM
AngelGT93
Fri Nov 10 2017, 06:53 AM
JardCrocker
Wed Nov 08 2017, 05:27 PM
vivitern
Tue Nov 07 2017, 09:08 AM
catrons
Sat Nov 04 2017, 10:46 PM

Online

Guests: 41, Members: 0 ...

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

Members: 37918
Newest member: PANDIKOTESWARARAO
Forum Activity
ExperimenterUK Avatar
Posted By ExperimenterUK
Mon Nov 20 2017, 10:18 PM
Here is a version of the tutorial that reads a key...
Ansh12 Avatar
Posted By Ansh12
Mon Nov 20 2017, 02:15 AM
As a starting point, can you tell us how much of t...
ExperimenterUK Avatar
Posted By ExperimenterUK
Sun Nov 19 2017, 08:35 PM
As a starting point, can you tell us how much of t...
Ansh12 Avatar
Posted By Ansh12
Fri Nov 17 2017, 04:07 AM
I am looking keypad interfacing program. I was rea...
JardCrocker Avatar
Posted By JardCrocker
Wed Nov 08 2017, 05:29 PM
Hi...i am a new user here. I was also facing the s...
ExperimenterUK Avatar
Posted By ExperimenterUK
Tue Nov 07 2017, 07:13 PM
> It employs a fairly advanced technique to detect...
vivitern Avatar
Posted By vivitern
Tue Nov 07 2017, 09:33 AM
I own a used Clavia ddrum4 SE drum module. It empl...
bvdb Avatar
Posted By bvdb
Wed Oct 18 2017, 01:50 AM
I purchased the RKitL51 8051 C compiler from Raiso...
ExperimenterUK Avatar
Posted By ExperimenterUK
Mon Oct 16 2017, 11:44 PM
That looks complicated.Is 8051_BLK a standard 80...
ExperimenterUK Avatar
Posted By ExperimenterUK
Sun Oct 15 2017, 07:29 PM
According to this, gmail should support POP3.https...