jssd 发表于 2012-5-8 16:47:20

用C8051F320怎么样初始化才能使串口波特率为9600?

用C8051F320怎么样初始化才能使串口波特率为9600?
下面代码,串口输出的波特率貌似不对。错在哪?(感觉不对是因为用同样的设置,F340串口输出的波形宽度大概是F320的1/4)#include"c8051F320.h"
#include "RS485.h"

sbit RELAY0 = P0^2;
sbit RELAY1 = P0^3;
sbit RELAY2 = P0^7;

/*
================================================================================
Function name : Clock_Init( )
Description   : Initialize the system and USB clock
Input         : None
Output      : None
================================================================================
*/
void Clock_Init( void )
{
        unsigned int i;
        if( ( CLKMUL & ( 1<<5 ) ) ) return;
        CLKMUL = 0;
        CLKMUL |= ( 1<<7 );
        for( i = 0; i < 250; i ++ );
        CLKMUL |= 0xC0;
        for( i = 0; i < 250; i ++ );
        while( !( CLKMUL & ( 1<<5 ) ) );
        CLKSEL = ( 2<<0 );
}

/*
================================================================================
Function name : GPIO_Init( )
Description   : Initial the GPIOs and the crossbar
Input         : None
Output      : None
================================================================================
*/
void GPIO_Init( void )
{
        P0MDOUT |= 0xdc;
        P1MDOUT |= 0x00;
        P2MDOUT |= 0xff;
        XBR1 |= ( 1<<6 );
        XBR0 |= ( 1<<0 );
}

/*
================================================================================
* Name      : SystermInit( )
* Description : Initialize the Systerm
* Input       : None
* Output      : None
* Note      : None
================================================================================
*/
void SystermInit(void)
{
        RELAY0 = 0;
        RELAY1 = 0;
        RELAY2 = 0;

        UART0_Init();

        UART_SendStatue(0xa0,0x55);
}

/*
================================================================================
=================================The main entry=================================
================================================================================
*/

int main( void )
{
    GPIO_Init();
        Clock_Init();                   //Config the clock
        SystermInit();

        while( 1 )
        {
                P2 = ~P2;
        }
        return 0;
}
/*
================================================================================
====================================End of file=================================
================================================================================
*/

#include "C8051F320.h"
#include "RS485.h"


/*
================================================================================
* Name      : Timer1_Init( )
* Description : Initialize the timer1 for baudrate
* Input       : None
* Output      : None
* Note      : None
================================================================================
*///9600
void Timer1_Init( void )
{
    TH1 = 0x30;
    TL1 = 0x30;
    TCON &= 0x3f;
    TMOD &= 0x0F;
    TMOD |= ( 2<<4 );
    CKCON |= 0x00 | ( 0<<3 );
        TCON |= ( 1<<6 );
}
/*
================================================================================
* Name      : UART0_Init( )
* Description : Initialize the UART0, baudrate
* Input       : None
* Output      : None
* Note      : None
================================================================================
*/
void UART0_Init( void )
{
        RD485 = 1;
    SCON0 = (1<<4)|(1<<5)|(1<<7); //(SCON0):S0MODE - MCE0 REN0 TB80 RB80 TI0 RI0
        IE |= ( 1<<4 );
        P0MDOUT |= ( 1<<4 );
    Timer1_Init( );
}

void UART_SendChar(unsigned char ch)
{
        SBUF0 = ch;
        while(!TI0);
        TI0 = 0;
}

void UART_SendStatue(unsigned char addr,unsigned char dat)
{
        //Send Address
        RD485 = 0;
        TB80 = 1;
        UART_SendChar(addr);
        //Send Data
        TB80 = 0;
        UART_SendChar(dat);
        RD485 = 1;
}
/*
void UART0_ISR( void ) interrupt 4
{
    unsigned char i;
    if( SCON0 & ( 1<<1 ) )
    {
      //TX ok
      SCON0 &= ~( 1<<1 );

    }
    else if( SCON0 & ( 1<<0 ) )
    {
      //RX ok
      SCON0 &= ~( 1<<0 );
      i = SBUF0;
                SBUF0 = i;
    }
}
*/

DOER 发表于 2012-5-8 16:55:01

你不会用它的配置向导吗?

jssd 发表于 2012-5-8 17:15:53

DOER 发表于 2012-5-8 16:55 static/image/common/back.gif
你不会用它的配置向导吗?

{:sweat:} 还真不会用。
看datasheet,320和340其他的都差不多。可是就是波特率那里不一样。
页: [1]
查看完整版本: 用C8051F320怎么样初始化才能使串口波特率为9600?