上回發(fā)帖(http://m.findthetime.net/bbs/View.asp?S=101&I=38287) 在大俠指點下?lián)Q了單片機晶振為11.059M,調(diào)試初始化仍然不通,請教。
目標(biāo):U盤讀寫
環(huán)境參數(shù): 51單片機 STC11F32XE:32K flash,1280 on-chip RAM,接11.0592M晶振 CH375B芯片:接12M晶振,交叉串口方式相連,INT#接P3.2; 采用文件級子程序CH375HF5.LIB,F(xiàn)ILELIB5/EXAM7示例程序為原型。
現(xiàn)狀描述: 波特率9600下向CH375發(fā)出更改波特率為57600的命令,MCU端波特率同步修改,下面在等待CH375對波特率修改后的確認(rèn)時卡住。也就執(zhí)行不到后面的CH375LibInit()函數(shù)了。
求指正,求解決方法!
關(guān)鍵程序如下:(省略部分皆依EXAM7示例程序中默認(rèn)設(shè)置)
#include "CommonHead.h" //引腳地址定義,包含了REG51.H, intrins.h 等 #include "ST7920.h" //LCD
#define MAX_BYTE_IO 16
/* 以下定義的詳細(xì)說明請看CH375HF5.H文件 */ #define LIB_CFG_FILE_IO 1 #define LIB_CFG_INT_EN 0 //#define LIB_CFG_UPD_SIZE 1
#define DISK_BASE_BUF_ADDR 0x0000 #define DISK_BASE_BUF_LEN 512
#define CH375_INT_WIRE INT0 // P3.2, INT0
#define NO_DEFAULT_CH375_F_ENUM 1 #define NO_DEFAULT_CH375_F_QUERY 1
#include "CH375HF5.H"
#define UINT8 unsigned char
void mDelay1_2uS( ) /* 至少延時1.2uS,根據(jù)單片機主頻調(diào)整 */ { return; }
// 如果單片機與CH375是串口連接,那么參考下面的子程序 void CH375_PORT_INIT( ) { SCON = 0xD0; //設(shè)置串口為9位數(shù)據(jù) //設(shè)置波特率和其它串口參數(shù) TMOD = 0x20; //timer 1, mode 2, 8-bits reload TH1 = 0xFD; TL1 = 0xFD; PCON = 0x00;
ES = 1; //enable serial interrupt TR1 = 1; //timer1 run,即TCON=TCON|0x40
}
void xWriteCH375Cmd( UINT8 mCmd ) { TB8 = 1; SBUF = mCmd; while ( TI == 0 ); TI = 0;
}
void xWriteCH375Data( UINT8 mData ) {
TB8 = 0; SBUF = mData; while ( TI == 0 ); TI = 0;
}
UINT8 xReadCH375Data( void ) { while ( RI == 0 ); RI = 0; return( SBUF ); } //
// 延時100毫秒,不精確 // void mDelay100mS( ) { uchar i, j, c; for ( i = 200; i != 0; i -- ) for ( j = 200; j != 0; j -- ) c+=3; }
// 將程序空間的字符串復(fù)制到內(nèi)部RAM中,返回字符串長度 UINT8 mCopyCodeStringToIRAM( UINT8 idata *iDestination, UINT8 code *iSource ) { UINT8 i = 0; while ( *iDestination = *iSource ) { iDestination ++; iSource ++; i ++; } return( i ); }
// 檢查操作狀態(tài),如果錯誤則顯示錯誤代碼并停機 // void mStopIfError( UINT8 iError ) { if ( iError == ERR_SUCCESS ) return; // 操作成功 //printf( "Error: %02X\n", (UINT16)iError ); // 顯示錯誤 while ( 1 ) { RefreshCurChDisp(3,7,99); mDelay100mS( ); } }
//更改波特率 void CH375SetBaudrate( UINT16 baud ) { UINT8 mCoef, mDivConst; UINT8 temp;
switch( baud ) { case 9600: { mCoef = 0x02; mDivConst = 0xB2; temp = 0xF4; }break; case 19200: { mCoef = 0x02; mDivConst = 0xD9; temp = 0xFA; }break; case 57600: { mCoef = 0x03; mDivConst = 0x98; temp = 0xFE; }break; case 115200: { mCoef = 0x03; mDivConst = 0xCC; temp = 0xFF; }break; }
xWriteCH375Cmd( 0x02 ); xWriteCH375Data( mCoef ); xWriteCH375Data( mDivConst );
TR1 = 0; TH1 = temp; //單片機波特率同步更改 TR1 = 1;
RefreshCurChDisp(1,0,1); //LCD顯示調(diào)試信息
mCoef = 5; while( mCoef -- ) if( xReadCH375Data( ) == 0x51 ) break; //等待CH375確認(rèn),卡住!
mDelay100mS( );
}
main( ) {
uchar tmp,i;
P3_2 = 1; //外部中斷口電平初始化 AUXR1 = 0x00; //p30 p31 正常串口
IT0 = 0; //外部中斷低電平有效 EX0 = 0; CH375_PORT_INIT( ); EA = 1;
SP = 0x5f; //LCD初始化 P4SW = 0x70; //LCD初始化 init_lcd(); //LCD初始化 RefreshCurChDisp(1,2,9); //LCD顯示調(diào)試信息
EX0 = 1; //預(yù)先開一次中斷,以清除歷史殘余 for(tmp = 0;tmp < 20;tmp++) ; EX0 = 0; for(tmp = 0;tmp < 20;tmp++) ; EX0 = 1;
RefreshCurChDisp(0,0,1); //LCD顯示調(diào)試信息 AUXR1 = 0x80; //p16 p17 STC11Fxx的串口復(fù)用,測試通過 mDelay100mS( );
CH375SetBaudrate( 57600 ); //更改波特率,以便CH375在12M晶振下避開9600波特率下的高串口誤差
RefreshCurChDisp(0,1,2); //LCD顯示調(diào)試信息
i = CH375LibInit( ); //初始化CH375程序庫和CH375芯片,操作成功返回0 RefreshCurChDisp(0,2,3); mStopIfError( i ); RefreshCurChDisp(0,3,4); while(1) { mDelay100mS( ); } } [Emot]20[/Emot]