MC9S08AC16串口程序
使用MC9S08AC16单片机的串口接收数据,然后将接收到的数据回送至上位机。单片机总线频率20Mhz,串口通信波特率9600,下面是程序代码。
#include /* for EnableInterrupts macro */
#include "derivative.h" /* include peripheral declarations */
void clock_init(void){
/* Common initialization of the write once registers */
/* SOPT: COPE=0,COPT=1,STOPE=0,??=1,??=0,??=0,??=1,??=1 */
SOPT=0x53;
/* System clock initialization */
/* ICGC1:
HGO=0,RANGE=1,REFS=1,CLKS1=1,CLKS0=1,OSCSTEN=1,LOCD=0,??=0 */
ICGC1=0x7C;
/* ICGC2:
LOLRE=0,MFD2=0,MFD1=1,MFD0=1,LOCRE=0,RFD2=0,RFD1=0,RFD0=0 */
ICGC2=0x30;
if (*(unsigned char*)0xFFBE != 0xFF) { /* Test if the device
trim value is stored on the specified address */
ICGTRM = *(unsigned char*)0xFFBE; /*
Initialize ICGTRM register from a non volatile memory */
}
while(!ICGS1_LOCK)
{;
/* Wait */
}
}
/*
**
===================================================================
** Method
: uart1_init (component AsynchroSerial)
**
** Description :
** Initializes the
associated peripheral(s) and the bean internal
** variables. The
method is called automatically as a part of the
** application
initialization code.
** This method is
internal. It is used by Processor Expert only.
**
===================================================================
*/
void uart1_init(void)
{
/* SCI1C1:
LOOPS=0,SCISWAI=0,RSRC=0,M=0,WAKE=0,ILT=0,PE=0,PT=0 */
SCI1C1=0x00;
/* Configure the SCI */
/* SCI1C3:
R8=0,T8=0,TXDIR=0,TXINV=0,ORIE=0,NEIE=0,FEIE=0,PEIE=0 */
SCI1C3=0x00;
/* Disable error interrupts */
/* SCI1S2:
LBKDIF=0,RXEDGIF=0,??=0,RXINV=0,RWUID=0,BRK13=0,LBKDE=0,RAF=0 */
SCI1S2=0x00;
/* SCI1C2: TIE=0,TCIE=0,RIE=0,ILIE=0,TE=0,RE=0,RWU=0,SBK=0
*/
SCI1C2=0x00;
/* Disable all interrupts */
SCI1BDH =
0x00;
/* Set high divisor register (enable device) */
SCI1BDL =
0x82;
/* Set low divisor register (enable device) */
SCI1C2_TE =
0x01;
/* Enable transmitter */
SCI1C2_RE =
0x01;
/* Enable receiver */
}
/*
**
===================================================================
** Method
: uart1_recvchar (component AsynchroSerial)
**
** Description :
** If any data is
received, this method returns one
** character,
otherwise it returns an error code (it does
** not wait for
data). This method is enabled only if the
** receiver
property is enabled.
** [Note:] Because
the preferred method to handle error and
** break exception
in the interrupt mode is to use events
**
<&111nError> and the return value ERR_RXEMPTY has
** higher priority
than other error codes. As a consequence
** the information
about an exception in interrupt mode is
** returned only if
there is a valid character ready to be
** read.
** Parameters :
**
NAME
- DESCRIPTION
** *
Chr
- Pointer to a received character
** Returns :
**
---
- Error code, possible codes:
**
ERR_OK - OK
**
ERR_SPEED - This device does not work in
**
the active speed mode
**
ERR_RXEMPTY - No data in receiver
**
ERR_BREAK - Break character is detected
**
(only when the
**
property is disabled and the
**
signal> property is enabled)
**
ERR_COMMON - common error occurred (the
**
method can be used for error
**
specification)
**
===================================================================
*/
unsigned char uart1_recvchar(unsigned char *ch)
{
unsigned char Result =
0;
/* Return error code */
unsigned char StatReg =
SCI1S1;
/* Read status register */
if (StatReg &
(SCI1S1_OR_MASK|SCI1S1_NF_MASK|SCI1S1_FE_MASK|SCI1S1_PF_MASK)) { /*
Is any error set? */
Result =
0;
/* If yes then set common error value */
}
else if (!(StatReg & SCI1S1_RDRF_MASK)) { /*
Is the reciver empty and no error is set? */
return
0;
/* If yes then error */
}
else {
/* Intentionally left empty due to compatibility with MISRA rule
60*/
*ch =
SCI1D;
/* Read data from the receiver */
Result=1;
}
return
Result;
/* Return error code */
}
/*
**
===================================================================
** Method
: uart1_sendchar (component AsynchroSerial)
**
** Description :
** Sends one
character to the channel. If the bean is
** temporarily
disabled (Disable method) SendChar method
** only stores data
into an output buffer. In case of a zero
** output buffer
size, only one character can be stored.
** Enabling the
bean (Enable method) starts the transmission
** of the stored
data. This method is available only if the
** transmitter
property is enabled.
** Parameters :
**
NAME
- DESCRIPTION
**
Chr
- Character to send
** Returns :
**
---
- Error code, possible codes:
**
ERR_OK - OK
**
ERR_SPEED - This device does not work in
**
the active speed mode
**
ERR_TXFULL - Transmitter is full
**
===================================================================
*/
unsigned char uart1_sendchar(unsigned char ch)
{
if (!SCI1S1_TDRE)
{
/* Is the transmitter empty? */
return
0;
/* If no then error */
}
SCI1D = (unsigned
char)ch;
/* Store char to the transmitter register */
return
0;
/* OK */
}
void main(void) {
unsigned char ch;
EnableInterrupts; /* enable interrupts */
/* include your code here */
clock_init();
uart1_init();
for(;;) {
if(uart1_recvchar(&ch)){
uart1_sendchar(ch);
}
// __RESET_WATCHDOG(); /* feeds the dog */
} /* loop forever */
/* please make sure that you never leave main */
}