Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Serial i/o programming using keil c

Status
Not open for further replies.

willysum

New Member
Hi. I'm working on a project involving a java program and the micro controller NXP P89v664.

The java program which utilises the RXTX package, outputs to the serial port a string of characters to be deciphered by the micro controller.

My java program is completed and outputs correctly as checked by virtual com ports and a c program.

I have written a keil c program to receive the input but I somehow receive random inputs instead.

I used the _getkey() fuction to receive the input and putchar() function to check the output.

I'm new to keil c can anyone shed some light on this pls?

Thanks.
 
Check the baud rate of yours. Since you said that you receive random input, the first thing of the baud rate strike.
 
The baudrate is set to 9600, no parity bit and 8 data bits.

I'm using timer 1 mode 2 to generate the baudrate. But what i'm not sure is, if the baud rate mismatches am i able to receive input at all? I tried to change my baudrate to something else and i could not receive any input from the serial.

I lifted this code for initializing the serial port and setting baudrate to 9600

void com_initialize (void)
{ /*------------------------------------------------
Setup TIMER1 to generate the proper baud rate.
------------------------------------------------*/
com_baudrate (9600);

/*------------------------------------------------
Clear com buffer indexes.
------------------------------------------------*/
EA = 0; /* Disable Interrupts */

t_in = 0;
t_out = 0;
t_disabled = 1;

r_in = 0;
r_out = 0;

/*------------------------------------------------
Setup serial port registers.
------------------------------------------------*/
SM0 = 0; SM1 = 1; /* serial port MODE 1 */
SM2 = 0;
REN = 1; /* enable serial receiver */

TI = 0; /* clear transmit interrupt */
RI = 0; /* clear receiver interrupt */

ES0 = 1; /* enable serial interrupts */
PS0 = 0; /* set serial interrupts to low priority */

EA = 1; /* Enable Interrupts */
}

void com_baudrate (unsigned baudrate)
{ EA = 0; /* Disable Interrupts */

/*------------------------------------------------
Clear transmit interrupt and buffer.
------------------------------------------------*/ TI = 0; /* clear transmit interrupt */ t_in = 0; /* empty transmit buffer */ t_out = 0; t_disabled = 1; /* disable transmitter */

/*------------------------------------------------
Set timer 1 up as a baud rate generator.
------------------------------------------------*/ TR1 = 0; /* stop timer 1 */ ET1 = 0; /* disable timer 1 interrupt */

PCON |= 0x80; /* 0x80=SMOD: set serial baudrate doubler */

TMOD &= ~0xF0; /* clear timer 1 mode bits */ TMOD |= 0x20; /* put timer 1 into MODE 2 */

TH1 = (unsigned char) (256 - (18432000 / (16L * 12L * baudrate))); //18432000=Crystal freq set in the project options

TR1 = 1; /* start timer 1 */

EA = 1; /* Enable Interrupts */
}

Thanks for the help.
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top