void clock_initiate( void )
{
/***************************************************************************\
* first shut up watchdog programme, because this will change WDT's clock source!
* watchdog's clock source is SMCLK, SMCLK's source may be DCO or XT2.
* current operation change SMCLK's source from DCO to XT2
\***************************************************************************/
WDTCTL = WDTPW + WDTHOLD; // close watchdog
BCSCTL1 &= 0x07F; //open xt2 clock.
BCSCTL1 &= 0x07F; //open xt2 clock.
BCSCTL1 &= 0x07F; //open xt2 clock.
BCSCTL2 = 0; // clear flag
// 4M ?§??4????
if ( Oscillistor_4M_Or_8M )
BCSCTL2 |= 0x00C; // set xt2 clock as the source of SMCLK ,4 divide xt2.
// 8M ?§??8????
else
BCSCTL2 |= 0x00E; // set xt2 clock as the source of SMCLK ,8 divide xt2.
//BCSCTL2 |=0x090; // select XT2 2 divided as MCLK source
××oid_mad_jump ( );
××oid_mad_jump ( );
delay ( 500 ); // wait this clock sta××e
refresh_watchdog ( ); // re open watchdog function
return;
}
、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、
void baudrate_set ( void )
{
refresh_watchdog ( );
U1BR1 = 0; // adjust register setup 1M
U1BR0 = 0x06D;
U1MCTL = 0x03;
//U1BR1 = 0; // adjust register setup 1M
//U1BR0 = 0x0DA;
//U1MCTL = 0x055;
UCTL1 |= SWRST; //reset flag
UCTL1 |= CHAR; // not h××e PEV, 1 bit stop, 8 bit char
U1TCTL|= ( BIT4 + BIT5 ); // choose SMCLK as the source of communication clock
ME2 |= ( UTXE1 + URXE1 ); //interrupt register setup
/////////////////////////////////////////
IE2 |= URXIE1; // Ena××e USART1 RX interrupt
//////////////////////////////////////////////
UCTL1 &= ~SWRST;//ena××e all setup
P3SEL |= 0x0C0; //TXD RXD setup
P3DIR |= 0x040; //TXD
P3DIR &= ~( 0x080 ); //RXD
delay ( 20 );
// SCDE at P6^7
P6DIR |= BIT7;
P6OUT &= ~BIT7; // default status is receive mode
delay ( 20 );
IFG2 &= ~( UTXIFG1 + URXIFG1 ); //clear the Interrupt Flag
_EINT ( ); // main interrupt ena××e
delay ( 20 );
return;
}
void serial_initiate ( void )
{
clock_initiate( );
baudrate_set ( );
delay ( 100 );
return;
}
/********************************************************\
void transmit_mode ( void )
{
refresh_watchdog ( );
// SCDE at P6^7
P6OUT |= BIT7;
P6OUT |= BIT7;
P6OUT |= BIT7;
P6OUT |= BIT7;
delay ( 100 );
return;
}
void receive_mode ( void )
{
refresh_watchdog ( );
// SCDE at P6^7
P6OUT &= ~BIT7;
P6OUT &= ~BIT7;
P6OUT &= ~BIT7;
P6OUT &= ~BIT7;
P6OUT &= ~BIT7;
P6OUT &= ~BIT7;
delay ( 50 );
return;
}
/************************************************\
void transmit_char ( unsigned char CharBeSended )
{
unsigned char i;
unsigned int TimeOutFlag;
refresh_watchdog ( ); // re_start watchdog
TXBUF1 = CharBeSended;
//for ( i = 0; i < 10; i++ ) {
for( TimeOutFlag = 0x0FFFF; TimeOutFlag > 1; TimeOutFlag -- ) {
refresh_watchdog ( );
if ( ( IFG2 & UTXIFG1 )) {
IFG2 &= ~ ( UTXIFG1 ); //clear the Interrupt Flag
return ;
}
}
return;
}
\************************/
void transmit_string ( unsigned char *String, unsigned char Length )
{
unsigned char i;
transmit_mode ( );
//delay ( 50 );
for ( i = 0; i < Length; i++ )
transmit_char ( String [i] );
delay ( 100 );
return;
}
/*************************\
* Function : receive char
\*************************/
// UART1 RX ISR
#pragma vector=UART1RX_VECTOR
__interrupt void usart1_rx (void)
{
IE2 &= ~URXIE1;
DispArray[ReceiveNumFlag] = RXBUF1;
ReceiveNumFlag++;
IFG2 &= ~ ( URXIFG1 ); //clear the Interrupt Flag
IE2 |= URXIE1;
CheckOrOper = 1; // multi use this variant
return ;
}
unsigned char receive_char ( void )
{
unsigned char DataReceived;
unsigned char i;
unsigned int TimeOutFlag;
for ( i = 0; i < 10; i++ ) {
for( TimeOutFlag = 0x0FFFF; TimeOutFlag > 1; TimeOutFlag -- ) {
refresh_watchdog ( );
if ( ( IFG2 & URXIFG1 )) {
DataReceived = RXBUF1;
IFG2 &= ~ ( URXIFG1 ); //clear the Interrupt Flag
return ( DataReceived );
}
}
}
return ( 0x0FF );
}
/************************\
void receive_string ( unsigned char *String, unsigned char Length )
{
unsigned char i;
unsigned int TimeOutFlag;
ReceiveNumFlag = 0;
IE2 |= URXIE1;
receive_mode ( );
// detect lost package
for ( ; ReceiveNumFlag < Length; ) {
CheckOrOper = 0;
for ( i = 0; ( i < 10 )&& (!CheckOrOper ); i++ ) {
for ( TimeOutFlag = 0x0FFFF; ( TimeOutFlag > 1 )&&( !CheckOrOper ); TimeOutFlag -- )
refresh_watchdog ( );
}
if ( i >= 10) {
IE2 &= ~URXIE1; // close interruption
DispArray[0] = 0; // error flag
DispArray[7] = 0; // error flag
delay ( 20000 ); // ××oid collide
ReceiveNumFlag = Length + 2; // force jump out
break; // lost package processing
}
}
refresh_watchdog ( );
CheckOrOper = 0;
IE2 &= ~URXIE1;
for ( i = 0; i < Length ; i ++ ){
String [ i ] = ( unsigned char ) DispArray[i];
}
//delay ( 100 );
return ;
}
、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、
主要问题就是成帧数据的接收问题!
这是我的程序,后来测试查寻接收,比中断接收效果要好一点,在5帧内大都可以调整过来!
中断接收设置了一些全局变量
查寻接收就不用了,下面的是查寻接收程序!
、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、
unsigned char receive_char ( void )
{
unsigned char DataReceived;
unsigned char i;
unsigned int TimeOutFlag;
refresh_watchdog ( ); // re_start watchdog
for ( i = 0; i < 10; i++ ) {
for( TimeOutFlag = 0x0FFFF; TimeOutFlag > 1; TimeOutFlag -- ) {
refresh_watchdog ( );
if ( ( IFG2 & URXIFG1 )) {
DataReceived = RXBUF1;
IFG2 &= ~ ( URXIFG1 ); //clear the Interrupt Flag
return ( DataReceived );
}
}
}
return ( 0x0FF );
}
/************************\
* Function : receive string
\************************/
void receive_string ( unsigned char *String, unsigned char Length )
{
unsigned char i;
unsigned char ReceivedData;
receive_mode ( );
for ( i = 0; i < Length; i++ ) {
ReceivedData = ( unsigned char ) receive_char ( );
if ( ReceivedData == 0x0FF ) {
String [ 0 ] = 0;
return;
}
String [ i ] = ReceivedData;
}
return ;
}