Hello,
I have AT89S52 controller and i m trying to establish communication between controller and my PC on serial line using MAX232N IC in between (see the picture attached for configuration). I have crystal of 12Mhz and trying to use baudrate 4800. in PC i have tried two tools one is hyper terminal and other is the tool i have written by myself (see the code attached) in hope that i can configure accurate baudrate (which is according to the calculation is 4464.28kbps) that is not supported by hyperterminal. and i am even not sure that 4464kbps which i have used in my tool is supported by windows API or not.
what i have observed is, i get some junk characters on hyperterminal only when i connect the cable (its only once, but my controller is continuously sending the character to the PC). that is strange. why?
another question is, as my caculated baudrate is 4464.28 (according the timer value i have loaded). is that okay to configure 4800 on hyperterminal and expect the correct charater on screen? or its too much diff to end up in junk character? and i have to change my crystal to some 11.059 or something else that can give me the accurate baudrate of 4800 or some other standard value?
or any other solution?
what should be the value of flow control in hyperterminal settings? hardware, None, or Xon/Off?
my code running in microcontroller (written & compiled with keil)
The Code i m using in my PC tool,
any help would be greatly appreciated
I have AT89S52 controller and i m trying to establish communication between controller and my PC on serial line using MAX232N IC in between (see the picture attached for configuration). I have crystal of 12Mhz and trying to use baudrate 4800. in PC i have tried two tools one is hyper terminal and other is the tool i have written by myself (see the code attached) in hope that i can configure accurate baudrate (which is according to the calculation is 4464.28kbps) that is not supported by hyperterminal. and i am even not sure that 4464kbps which i have used in my tool is supported by windows API or not.
what i have observed is, i get some junk characters on hyperterminal only when i connect the cable (its only once, but my controller is continuously sending the character to the PC). that is strange. why?
another question is, as my caculated baudrate is 4464.28 (according the timer value i have loaded). is that okay to configure 4800 on hyperterminal and expect the correct charater on screen? or its too much diff to end up in junk character? and i have to change my crystal to some 11.059 or something else that can give me the accurate baudrate of 4800 or some other standard value?
or any other solution?
what should be the value of flow control in hyperterminal settings? hardware, None, or Xon/Off?
my code running in microcontroller (written & compiled with keil)
Code:
#include <stdio.h>
#include <REGX51.H>
#define MICRO_FREQUENCY 12000000.0 // frequency of micro...
typedef unsigned char BYTE;
typedef unsigned long ULONG;
typedef unsigned int WORD;
int waiting_for_ti = 1;
#define UART0_VECTOR 4 //0x23 Serial Port 0
volatile BYTE TxBuffer[3];
volatile BYTE TxInChar;
volatile BYTE TxEnabled;
//=====================================================================
// init routine for ISR handler...
//=====================================================================
void SER_init()
{
/* disable interrupts... */
EA = 0;
/* clear com buffer indexes... */
TxEnabled = 0;
/* Setup serial port registers */
SM0 = 0;
SM1 = 1; // serial port MODE 1
SM2 = 0;
REN = 0; // enable serial receiver
TI = 0; // clear transmit interrupt
RI = 0; // clear receiver interrupt
ES = 1; // enable serial interrupts
PS = 0; // set to low priority
EA = 1; // Enable Interrupts
}
//=====================================================================
//=====================================================================
void SER_setbaud( ULONG BaudRate )
{
/* disable interrupts */
ES = 0;
/* Clear interrupt flag and buffer */
TI = 0;
/* transmitter is disabled */
TxEnabled = 0;
/* Set timer 1 up as a baud rate generator. */
TR1 = 0; // stop timer 1
ET1 = 0; // disable timer 1 interrupt
PCON |= 0x80; // 0x40=SMOD1: set serial baudrate doubler
TMOD &= 0x0F; // clear timer 1 mode bits
TMOD |= 0x20; // put timer 1 into MODE 2 : 8-bit, auto-reload
TH1 = (BYTE) (256.0 - (MICRO_FREQUENCY / (192.0 * (float)(BaudRate))));
TR1 = 1; // start timer 1
/* enable interrupts */
ES = 1;
}
//=====================================================================
//=====================================================================
void SER_ISR() interrupt UART0_VECTOR using 2
{
if( TI )
{
waiting_for_ti = 0;
}
}
//=====================================================================
//=====================================================================
void putmychar( char ThisChar )
{
/* add this character to the transmit buffer */
ES = 0;
waiting_for_ti = 1;
SBUF = ThisChar;
ES = 1;
while(waiting_for_ti != 0)
{
}
/* return character as default value */
}
void main()
{
// initialize serial interface...
SER_init();
SER_setbaud( 4800 );
// loop forever...
while(1)
{
//wait willbe handle in Putmychar function
putmychar( 'C' );
}
}
The Code i m using in my PC tool,
Code:
// Hypertrm.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#define TIMEOUT_CONSTANT 50
#define ESC 27
int main(int argc, char* argv[])
{
int key_pressed = 0;
char outchar = 0;
char inchar = 0;
DWORD bytes_written = 0; // Number of bytes written to port
DWORD bytes_read = 0; // Number of bytes read from port
COMMTIMEOUTS tempComTimeouts; // Our temporary time-outs for COM1
COMMTIMEOUTS savedComTimeouts; // Stores the original time-outs
HANDLE comport = NULL; // Handle for COM port
DCB comSettings; // Contains various port settings
char port[10];
long baudrate;
int databits;
char parity;
double stopbits;
char flowcontrol;
//printf("Sample UART Loopback Terminal Program v1.00.\n");
//printf("(c)Copyright Sealevel Systems, Inc., 1999.\n\n");
if(argc < 7)
{
printf("\nInvalid number of argument\n");
return 1;
}
strncpy(port, argv[1], 10);
baudrate = atol(argv[2]);
databits = atoi(argv[3]);
parity = *argv[4];
stopbits = atof(argv[5]);
flowcontrol = *argv[6];
// Open COM port
if ((comport =
CreateFile(port,
GENERIC_READ | GENERIC_WRITE, // for reading and writing
0, // exclusive access
NULL, // no security attributes
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
NULL)) == INVALID_HANDLE_VALUE)
{
printf("Unable to open COM1.\n\n");
printf("Press any key to exit.");
getch();
return(1);
}
printf("%s opened.\n\n", port);
// Save time-out parameters for COM1
GetCommTimeouts(comport,&savedComTimeouts);
// Set our time-outs
tempComTimeouts.ReadIntervalTimeout = TIMEOUT_CONSTANT;
tempComTimeouts.ReadTotalTimeoutMultiplier = TIMEOUT_CONSTANT;
tempComTimeouts.ReadTotalTimeoutConstant = TIMEOUT_CONSTANT;
tempComTimeouts.WriteTotalTimeoutMultiplier = TIMEOUT_CONSTANT;
tempComTimeouts.WriteTotalTimeoutConstant = TIMEOUT_CONSTANT;
SetCommTimeouts(comport,&tempComTimeouts);
// Set Port parameters.
// We make a call to GetCommState() first in order to fill
// the comSettings structure with all the necessary values.
// Then we change the ones we want and call SetCommState().
GetCommState(comport, &comSettings);
comSettings.BaudRate = baudrate;
switch(parity) {
case 'N':
comSettings.Parity = NOPARITY;
comSettings.fParity = FALSE;
break;
case 'E':
comSettings.Parity = EVENPARITY;
comSettings.fParity = FALSE;
break;
case 'O':
comSettings.Parity = ODDPARITY;
comSettings.fParity = FALSE;
break;
}
if(stopbits == 1)
comSettings.StopBits = ONESTOPBIT;
else if(stopbits == 1.5)
comSettings.StopBits = ONE5STOPBITS;
else if(stopbits == 2)
comSettings.StopBits = TWOSTOPBITS;
comSettings.ByteSize = databits;
SetCommState(comport, &comSettings);
printf("Ready to send/receive data. Hit ESC to exit.\n\n");
while(key_pressed != ESC)
{
if (kbhit())
{
key_pressed = getch();
outchar = (char)key_pressed;
if (key_pressed != ESC)
{
// Send data. if succesful, WriteFile() returns non-zero
WriteFile(comport, // Handle
&outchar, // Outgoing data
1, // Number of bytes to write
&bytes_written, // Number of bytes written
NULL);
}
}
// Read data. if succesful, ReadFile() returns non-zero
ReadFile(comport, // Handle
&inchar, // Incoming data
1, // Number of bytes to read
&bytes_read, // Number of bytes read
NULL);
if (bytes_read == 1)
if (inchar == 13);
//printf("\n");
else
printf("%c", inchar);
}
// Restore time-out parameters
SetCommTimeouts(comport,&savedComTimeouts);
CloseHandle(comport);
printf("\n");
return(0);
}
any help would be greatly appreciated