我是用的ATMEGA2561 串口方式连接的CH376,可以连上U盘,可以建立文件.但文件不能写入数据.中断发现返回值为0x14; 相关程序如下;
/**************************************************************************** 函数功能:uart0初始化 入口参数:c 出口参数: ****************************************************************************/
void uart0_init() { int baud; baud=9600; UCSR0B=0x00; UCSR0A=0x00; UCSR0C=0x06; //0000 0110,UCSZ01=1,UCSZ00=1;8位字符,1位停止位 UBRR0L=(fosc/16/baud-1)%256; UBRR0H=(fosc/16/baud-1)/256; //波特率设置 UCSR0B=(1<<3)|(1<<4); //接受,发送使能;接受中断使能 }
/**************************************************************************** 函数功能:向CH376 写数据 入口参数:mData 出口参数: **************************************************************************/
void xWriteCH376Data(uchar mData) { while(!(UCSR0A&(1<<5))); UDR0=mData; } /**************************************************************************** 函数功能:从CH376 读数据 入口参数: 出口参数: ****************************************************************************/
uchar xReadCH376Data(void) { uchar Rdata; uint i; delay_us(2); while(!(UCSR0A&(1<<7))); Rdata=UDR0; return Rdata; }
/**************************************************************************** 函数功能:向CH376 写命令 入口参数: 出口参数: ****************************************************************************/ void xWriteCH376Cmd(uchar mCmd) { xWriteCH376Data(0x57); xWriteCH376Data(0xAB); xWriteCH376Data(mCmd); }
/**************************************************************************** 函数功能:等待CH376中断(INT#低电平) 入口参数: 出口参数:中断状态码,超时则返回ERR_USB_UNKNOWN ****************************************************************************/
uchar Wait376Interrupt( void ) /* 等待CH376中断(INT#低电平),返回中断状态码, 超时则返回ERR_USB_UNKNOWN */ { uint32 i; for ( i = 0; i < 5000000; i ++ ) { /* 计数防止超时,默认的超时时间,与单片机主频有关 */ if ( Query376Interrupt( ) ) return( CH376GetIntStatus( ) ); /* 检测到中断 */ /* 在等待CH376中断的过程中,可以做些需要及时处理的其它事情 */ } return( ERR_USB_UNKNOWN ); /* 不应该发生的情况 */ }
uchar Query376Interrupt( void ) { delay_us(1); if(!(UCSR0A&(1<<7))) return(FALSE); else { return(TRUE); } }
/**************************************************************************** 函数功能:获取中断状态并取消中断请求 入口参数: 出口参数: ****************************************************************************/
uchar CH376GetIntStatus( void ) /* 获取中断状态并取消中断请求 */ { uchar s; xWriteCH376Cmd( CMD_GET_STATUS ); s = xReadCH376Data( ); //delay_us(10); return( s ); }
uchar CH376SendCmdWaitInt( uchar mCmd ) /* 发出命令码后,等待中断 */ { xWriteCH376Cmd( mCmd ); return( Wait376Interrupt( ) ); }
/**************************************************************************** 函数功能:以字节为单位向当前位置写入数据块 入口参数: 出口参数: ****************************************************************************/
UINT8 CH376ByteWrite( PUINT8 buf, UINT16 ReqCount, PUINT16 RealCount ) /* 以字节为单位向当前位置写入数据块 */ { uchar s,l; xWriteCH376Cmd( CMD2H_BYTE_WRITE ); xWriteCH376Data( (UINT8)ReqCount ); xWriteCH376Data( (UINT8)(ReqCount>>8) ); if ( RealCount ) *RealCount = 0; (//l = Wait376Interrupt( ); //l = Wait376Interrupt( );) 加入这两行,可以写数据,但只能写30个字节,超过30出现乱码;
while ( 1 ) { s = Wait376Interrupt( ); if ( s == USB_INT_DISK_WRITE ) { l = CH376WriteReqBlock( buf ); /* 向内部指定缓冲区写入请求的数据块,返回长度 */ xWriteCH376Cmd( CMD0H_BYTE_WR_GO ); buf += l; if ( RealCount ) *RealCount += l; } else return( s ); /* 错误 */ } }
/**************************************************************************** 函数功能:向内部指定缓冲区写入请求的数据块 入口参数: 出口参数:返回长度 ****************************************************************************/
UINT8 CH376WriteReqBlock( PUINT8 buf ) /* 向内部指定缓冲区写入请求的数据块,返回长度 */ { UINT8 s, l; xWriteCH376Cmd( CMD01_WR_REQ_DATA ); l=s=xReadCH376Data( ); /* 长度 */ if ( l ) { do { xWriteCH376Data( *buf ); buf ++; } while ( -- l ); } nop(); return( s ); }
/**************************************************************************** 函数功能:保存表格抬头显示 入口参数: 出口参数: ****************************************************************************/
uchar Excel_Display_CH376() { uchar Dbuf[45]; uchar iSet,i;
/*产品编号*/ Dbuf[0]=0xb2; Dbuf[1]=0xfa; Dbuf[2]=0xc6; Dbuf[3]=0xb7; Dbuf[4]=0xb1; Dbuf[5]=0xe0; Dbuf[6]=0xba; Dbuf[7]=0xc5; Dbuf[8]=0x09;
/*测试参数*/ Dbuf[9]=0xb2; Dbuf[10]=0xe2; Dbuf[11]=0xca; Dbuf[12]=0xd4; Dbuf[13]=0xb2; Dbuf[14]=0xce; Dbuf[15]=0xca; Dbuf[16]=0xfd; Dbuf[17]=0x09;
/*实测电压*/ Dbuf[18]=0xca; Dbuf[19]=0xb5; Dbuf[20]=0xb2; Dbuf[21]=0xe2; Dbuf[22]=0xb5; Dbuf[23]=0xe7; Dbuf[24]=0xd1; Dbuf[25]=0xb9; Dbuf[26]=0x09;
/*实测电流(阻)*/ Dbuf[27]=0xca; Dbuf[28]=0xb5; Dbuf[29]=0xb2; Dbuf[30]=0xe2; Dbuf[31]=0xb5; Dbuf[32]=0xe7; Dbuf[33]=0xc1; Dbuf[34]=0xf7; Dbuf[35]=0x28; Dbuf[36]=0xd7; Dbuf[37]=0xe8; Dbuf[38]=0x29; Dbuf[39]=0x09;
/*分选*/ Dbuf[40]=0xb7; Dbuf[41]=0xd6; Dbuf[42]=0xd1; Dbuf[43]=0xa1; Dbuf[44]=0x09;
iSet=CH376ByteWrite(Dbuf,45,NULL);
return( iSet ); }
void main() { uchar iSet; uart0_init(); iSet=CH376FileCreate("\\GAB.XLS"); iSet=SetFileCreateTime( "\\GAB.XLS", MAKE_FILE_DATE( 2012,12, 26 ), MAKE_FILE_TIME( 15, 22, 30 ) ); iSet=CH376FileOpen("\\GAB.XLS"); if(iSet==USB_INT_SUCCESS) { iSet=CH376ByteLocate(0xFFFFFFFF);
Excel_Display_CH376(); iSet=CH376FileClose(TRUE); } }