Discussion in "Project Help" started by    mem    Sep 25, 2008.
Sat Sep 27 2008, 06:26 pm
#11
hi mem(ory),

first things first, your serial port init is wrong, you are using 9600 baud values for 11.0592 xtal and the TH1 and TL1 values are swapped.




pl.change them to 4800 baud for 24 Mhz xtal ( as given in my last post)


in comm program you have to select flow control as : None






try these changes and get back


Arun
 memnischay kumar like this.
Tue Sep 30 2008, 06:21 pm
#12
I double and triple checked my circuit (diagram and actual) but everything matches up to the tutorial.

I have simplified the code further, looking at the serial_read I see it will wait until it receives a value before returning (correct ?) so the should be no need to check for null characters.

#include <REGX51.H>

void serial_init()
{
        TMOD = 0x20;
        SCON = 0x50;
        TH1  = 0x00;
        TL1  = 0xF3;
        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 )
{
	signed char c = 0x00;
	serial_init();
	while(1)
	{
		serial_send(0x41);// send A

//		c = serial_read();
//		P0 = c;		
	}
}


Unfortunately neither serial_read or serial_send seem to work (comment out one to test the other).

Putty Settings:

Flow control disabled as recommended.

If I turn the project off and on it will sometimes send rubbish back to putty.

I am currently opening putty then turning the project on, though I do try the reverse when trying all new suggestions, should this matter ?.

So I am stuck again, looking forward to any insight people can offer.


[ Edited Tue Sep 30 2008, 06:23 pm ]
Tue Sep 30 2008, 11:50 pm
#13

Hello mem,

i think you are repeating the same mistake again, please refer to my last post about swapped values of TH1 and TL1. this time you have corrected the timer values for 4800 baud using 24 Mhz xtal but look where are you loading the values ?

since the timer 1 is in 8 bit mode 2 (auto reload) the TH1 needs to be loaded with 0F3h and TL1 with 00h, but check what you are doing !



Arun
 mem like this.
Wed Oct 01 2008, 04:58 am
#14
Arun, sorry I missed that last part about having my values swapped, I missed that section when rereading this thread last night. Thank you for your patience. I shall try again tonight after I have corrected my mistakes.
Wed Oct 01 2008, 06:13 pm
#15
Thanks Arun, indeed it was as simple as setting the correct values for the registers like you said.

The completed code:
#include <REGX51.H>

void serial_init()
{
        TMOD = 0x20;
        SCON = 0x50;
        TL1  = 0x00;
        TH1  = 0xF3;
        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 )
{
	signed char c = 0x00;
	signed char c_old = 0xFF;

	serial_init();

	while(1)
	{
		c = serial_read();
		P0 = c;
		serial_send(c);
	}
}


For sending data, flow control can be either XON/XOFF or none (rest untested, but it does not seem to matter).
For receiving however flow control NONE does not work, it returns a short bit of random characters then the chip locks up (as far as I can tell). Using XON/XOFF flow control does work perfectly though, all characters pressed are echoed back to the putty terminal. A guy I work with (who used todo alot of high level electronics) said the way the diagram is wired in mine and the tutorial it is ment for XON/XOFF, I have forgotten his explanation but I will post tomorrow when I see him.

Correct putty settings:


Now a few quick follow up questions
1) Only indirectly releated to electronics, but can anyone suggest a plugin for winamp/xmms/amarok that will allow me to send data to the comport (I dont mind having to adjust code).
2) Does someone have a chart or a formula for calculating correct crystal values for a given baud rate (will be really handy for the howto I will write up).

Thanks again everyone
Wed Oct 01 2008, 09:50 pm
#16

Hi mem,

so finally your project worked as expected, congrats ! !dance



Arun
 mem like this.
Thu Oct 02 2008, 02:06 am
#17
mem you could face problem, just thinking about default values.
i mean when u power up your controller, TI is 0 by default. so when sending data on UART, you will get stuck in while loop of transmit function.
here i mean
while(!TI);


so best thing is, set this bit to 1 during initializing uart.
and great job keep practicing..
 mem like this.
Thu Oct 02 2008, 05:17 am
#18
Ajay,
I believe thats the intended behavior of those sub routines (from the tutorial), ie HALT until data is received (RI) and wait till port is ready for a transmission (TI) and then send.
So (and please correct me if Im wrong) but wouldnt setting TI to 1 have no effect as the chip will reset it ?
Fri Oct 03 2008, 02:57 am
#19
no there is no effect on setting TI bit as you are doing it at the starting of your program, when everything is getting initialized. and its just for safety. The TX part of 8051 uart never starts working unless you move data to SBUF register or say TX works only after this instruction
mov SBUF, A ;or any register

or in C
SBUF = data_to_send;


so by setting TI bit you mean assuming that TX part is ready to send next data.
This practice is used in many other microcontrollers too. so its good to add that TI = 1; instruction in your code.
 mem like this.
Fri Oct 03 2008, 08:36 am
#20
Cool mate, thanks for explaining that to me

I have been working on the PC side of my project atm, I have gutted an xmms (linux clone of winamp) plugin and got it to output the data in text I want in sync with the music, today Im going to try out an example serial port c program I found here: com.c

Once I have modified that program todo as I want I will join it to my xmms plugin and tada, a very basic VU meter .

I also found some good information on calculating the timings in a 8052.com tutorial

Looks fairly detailed, so when I have a chance (been very busy of late) I will read through that and include the relevant info in the howto.

Get Social

Information

Powered by e107 Forum System

Downloads

Comments

Bobbyerilar
Thu Mar 28 2024, 08:08 am
pb58
Thu Mar 28 2024, 05:54 am
Clarazkafup
Thu Mar 28 2024, 02:24 am
Walterkic
Thu Mar 28 2024, 01:19 am
Davidusawn
Wed Mar 27 2024, 08:30 pm
Richardsop
Tue Mar 26 2024, 10:33 pm
Stevencog
Tue Mar 26 2024, 04:26 pm
Bernardwarge
Tue Mar 26 2024, 11:15 am