Friday, 6 July 2007

Micro-p and RS232

If you want to control RS232 port from Microcontroller, its little different with control from Application. Because not all of Micro-p are support RS232. Eg: some of the chips form "Silicon Lab" not support RS232 such as C8051F120, some support RS232 such as "Hitachi" H8 series. Hitachi H8 series also support the operations of MMC (Multi-media Memory Card). The operations are Read from and Write to that sources. The following C program shows the basic operation of micro-p communicating RS232. Its used Hitachi H8/3052F chip.

#include <3052f.h>

void main(void)

{

int i;

SCI1.SCR.BYTE=0; /* stop SCI1, internal clock use */

SCI1.SMR.BYTE=0; /* async. mode, 8data, 1stop, no-parity */

SCI1.BRR=40; /* 19200bps */

for( i = 220 ; i > 0 ; i--); /* wait 1bit transfer time */

SCI1.SCR.BYTE = 0x30; /* enable Tx & Rx, disable interrupt */

SCI1.SSR.BYTE &amp;amp;amp;= 0x80; /* clear receive error flag all */

for(i=0;i<26;i++){ /* send 'ABC--XYZ' */

while(!SCI1.SSR.BIT.TDRE); /* send enable? */

SCI1.TDR='A'+i;

SCI1.SSR.BIT.TDRE = 0; /* now data sending */

}

while(1){

while(!SCI1.SSR.BIT.RDRF); /* data recieve? */

SCI1.SSR.BIT.RDRF = 0; /* clear receive data flag */

while(!SCI1.SSR.BIT.TDRE); /* send enable? */

SCI1.TDR=SCI1.RDR; /* echo back */

SCI1.SSR.BIT.TDRE = 0; /* now data sending */

if( SCI1.TDR == 0x0d ){ /* ? */

while(!SCI1.SSR.BIT.TDRE);

SCI1.TDR = 0x0a; /* yes then send */

SCI1.SSR.BIT.TDRE = 0;

} }

}

No comments:

Post a Comment