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

ExperimenterUK
Sat Mar 25 2017, 09:56 PM
@Noreen I suggest you log in and post a full description of what you need in our PIC forum
Sat Mar 25 2017, 03:10 PM
Noreen
Hello I worked on a project related to Wireless Electricity theft detection using GSM during my bachelors. I used Nokia 6230 for sending sms using AT commands. I chose to work with SMS text mode. The microcontroller I used was PIC16F877A and its software coding done through PIC Basic Pro. Unfortunately, I lost my account password and do not have coding for generation sms using AT commands. I need that coding. Can some one help me to provide complete Pic Basic Pro code. In which step by step guidance is given so that I can understand easily. Regards,
Wed Mar 22 2017, 09:13 PM
ajay kumar
help me nokia lcd light
Wed Feb 22 2017, 06:14 AM
ChollyMo
Indeed, on the LCD tutorial, many missing Tables and figures !!
Wed Feb 22 2017, 06:06 AM
ChollyMo
I don't see any Table 3. I'm using Chrome.
Fri Feb 03 2017, 05:56 AM
irfan shaikh
hi, i want interface WS2811 pixel led using NUVOTON N79E352, but i dont know how to code it. can anyone help me or provide me sample coding for interfacing WS2811 PIXEL LED. THNX
Thu Jan 19 2017, 08:42 PM
abhays
Hi...i have made a circuit for interfacing PT100 with LM358 whose temperature is shown on the LCD...But the temperature is not getting incresed slowly ..a small change directly increses the temp...can any one help me
Thu Jan 19 2017, 04:27 PM
roba
send me alarm clock simulation using proteus software
Mon Jan 16 2017, 03:15 PM
Rutuja
How can we read notepad file using random acess file
Fri Jan 06 2017, 06:18 AM
muhammad Umar
i need cd4047 library file for protious please help me i s

Downloads

Comments

SAUnwin
Tue Mar 21 2017, 02:29 PM
letonarolikah
Mon Mar 20 2017, 11:21 AM
nerodear
Tue Mar 14 2017, 07:08 AM
XuZiling
Wed Feb 22 2017, 02:40 AM
diwakar_96
Thu Feb 16 2017, 02:40 PM
Helia
Thu Feb 09 2017, 09:59 AM
fasihaftab
Sat Jan 28 2017, 08:58 AM
jmunjr
Tue Jan 17 2017, 06:25 PM

Online

Guests: 60, Members: 0 ...

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

Members: 37804
Newest member: SAUnwin
Forum Activity
ExperimenterUK Avatar
Posted By ExperimenterUK
Sat Mar 25 2017, 09:49 PM
I have attached code showing one method you could ...
Deepakvaishu Avatar
Posted By Deepakvaishu
Sat Mar 25 2017, 07:59 AM
I am using Keil and 8051 microcontroller
ExperimenterUK Avatar
Posted By ExperimenterUK
Fri Mar 24 2017, 06:05 PM
The best way is to use an interrupt to run the cou...
Deepakvaishu Avatar
Posted By Deepakvaishu
Fri Mar 24 2017, 05:22 AM
Hi,I got that we should use timer......But I am a ...
ExperimenterUK Avatar
Posted By ExperimenterUK
Thu Mar 23 2017, 09:14 PM
You have largely answered your own question.Use on...
ExperimenterUK Avatar
Posted By ExperimenterUK
Thu Mar 23 2017, 07:03 AM
I have looked at your code.I think you are confuse...
Deepakvaishu Avatar
Posted By Deepakvaishu
Thu Mar 23 2017, 05:34 AM
hello,I can type the letters now when the particul...
SAUnwin Avatar
Posted By SAUnwin
Tue Mar 21 2017, 02:55 PM
HI,There are two ways I'd tackle thiss.Key down on...
Deepakvaishu Avatar
Posted By Deepakvaishu
Mon Mar 20 2017, 08:24 AM
hello,I am using a keypad to type the text message...
Helia Avatar
Posted By Helia
Thu Mar 16 2017, 09:15 AM
hi.. the following code is not working on hardware...