⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 main.c

📁 atmega32对ch375的读写程序
💻 C
📖 第 1 页 / 共 2 页
字号:
#include <string.h>
#include <stdio.h>
#include <avr/delay.h>
#include <avr/io.h>
//#include  <ch375.c>
//#include   <key.c>
#include<usart.c>
//#include    <lcd.c>
#include <i2c.c>


#define EN_H PORTC |= 0x40
#define EN_L PORTC &= 0xBF

#define RS_H (PORTC|= 0X80)  
#define RS_L  (PORTC&= 0X7F) 

//#define RW_H PORTC |= 0x40
#define RW_L PORTC |= 0x40
#define zlg7290 0x70 
#define uchar unsigned char
#define uint unsigned int
#define ulonglong unsigned long long

#define has_volatile 1
#if has_volatile 
volatile unsigned int h=0;//data[512]; //全局变量,会在中断服务程序中被修改,须加volatile限定
volatile unsigned char j=0, data[512];
volatile unsigned int x=0;
volatile unsigned int c=0;
volatile unsigned int write_times=0;
volatile unsigned int t;
volatile unsigned char flag;
volatile uchar do_flag=0;
volatile uchar key=0,set_flag=0;
volatile uchar s1,s2,s3,s4,s5,s6,s7,s8;

#else
uchar key=0;
uchar do_flag=0;
uchar s1,s2,s3,s4,s5,s6,s7,s8;

unsigned int x=0;
unsigned int c=0;
unsigned char flag,set_flag=0;
unsigned int t;
unsigned int write_times=0;
unsigned int h=0;//data[512]; //全局变量.
unsigned char j=0,data[512];



	
#endif
char *pstr;

#define LIB_CFG_FILE_IO			1		/* 文件读写的数据的复制方式,0为"外部子程序",1为"内部复制" */
#define LIB_CFG_INT_EN			0		/* CH375的INT#引脚连接方式,0为"查询方式",1为"中断方式" */
//#define FILE_DATA_BUF_ADDR		0x0200	/* 外部RAM的文件数据缓冲区的起始地址,缓冲区长度不小于一次读写的数据长度 */

/* 单片机的RAM有限,其中CH375子程序用512字节,剩余RAM部分可以用于文件读写缓冲 */
#define FILE_DATA_BUF_LEN		0x0200	/* 外部RAM的文件数据缓冲区,缓冲区长度不小于一次读写的数据长度 */
/* 如果准备使用双缓冲区交替读写,那么不要定义FILE_DATA_BUF_LEN,而是在参数中指定缓冲区起址,用CH375FileReadX代替CH375FileRead,用CH375FileWriteX代替CH375FileWrite */

#define CH375_INT_WIRE			( PIND & 0x04 )	/* PINB.4, CH375的中断线INT#引脚,连接CH375的INT#引脚,用于查询中断状态 */

#define NO_DEFAULT_CH375_F_ENUM		1		/* 未调用CH375FileEnumer程序故禁止以节约代码 */
#define NO_DEFAULT_CH375_F_QUERY	1		/* 未调用CH375FileQuery程序故禁止以节约代码 */

#include "CH375HFB.H"
#define CS_1        (PORTB|= 0X08)  
#define CS_0        (PORTB&= 0XF7) 
#define CS_A        (PORTB&= 0XEF) 
#define CS_B        (PORTB|= 0X10)
#define LED_1        (PORTC|= 0X80)  
#define LED_0        (PORTC&= 0X7F) 
void mDelay1uS1(void )  /* 至少延时1uS,根据单片机主频调整 */
{
	UINT8	i;
	for ( i = 5; i != 0; i -- );
}

void CH375_PORT_INIT(void )  /* 由于使用通用I/O模块并口读写时序,所以进行初始化 */
{
	DDRA = 0x00;  /* 设置8位并口为输入 */
	PORTD = 0x70;  /* 设置CS,WR,RD默认为高电平 */
	DDRD = 0xF0;  /* 设置CS,WR,RD,A0为输出,设置INT#为输入 */
}

void xWriteCH375Cmd( UINT8 mCmd )		/* 外部定义的被CH375程序库调用的子程序,向CH375写命令 */
{
	mDelay1uS1( ); mDelay1uS1( );  /* 至少延时1uS */
/*	*(volatile unsigned char *)CH375_CMD_PORT_ADDR = mCmd;  通过并口直接读写CH375而非普通I/O模拟 */
	PORTD |= 0x80;  /* 输出A0=1 */
	PORTA = mCmd;  /* 向CH375的并口输出数据 */
	DDRA = 0xFF;  /* 并口D0-D7输出 */
	PORTD &= 0x9F;  /* 输出有效写控制信号, 写CH375芯片的命令端口, A0=1; CS=0; WR=0; RD=1; */
	DDRA = 0xFF;  /* 该操作无意义,仅作延时,CH375要求读写脉冲宽度大于100nS */
	PORTD |= 0xF0;  /* 输出无效的控制信号, 完成操作CH375芯片, A0=1; CS=1; WR=1; RD=1; */
	DDRA = 0x00;  /* 禁止数据输出 */
	PORTD &= 0x7F;  /* 输出A0=0; 可选操作 */
	mDelay1uS1( ); mDelay1uS1( );  /* 至少延时2uS */
}

void xWriteCH375Data( UINT8 mData )		/* 外部定义的被CH375程序库调用的子程序,向CH375写数据 */
{
/*	*(volatile unsigned char *)CH375_DAT_PORT_ADDR = mData;  通过并口直接读写CH375而非普通I/O模拟 */
	PORTA = mData;  /* 向CH375的并口输出数据 */
	DDRA = 0xFF;  /* 并口D0-D7输出 */
	PORTD &= 0x1F;  /* 输出有效写控制信号, 写CH375芯片的数据端口, A0=0; CS=0; WR=0; RD=1; */
	DDRA = 0xFF;  /* 该操作无意义,仅作延时,CH375要求读写脉冲宽度大于100nS */
	PORTD |= 0x70;  /* 输出无效的控制信号, 完成操作CH375芯片, A0=0; CS=1; WR=1; RD=1; */
	DDRA = 0x00;  /* 禁止数据输出 */
	mDelay1uS1( );  /* 至少延时1.2uS */
}

UINT8 xReadCH375Data( void )			/* 外部定义的被CH375程序库调用的子程序,从CH375读数据 */
{
	UINT8	mData;
/*	mData = *(volatile unsigned char *)CH375_DAT_PORT_ADDR;  通过并口直接读写CH375而非普通I/O模拟 */
	mDelay1uS1( );  /* 至少延时1.2uS */
	DDRA = 0x00;  /* 数据输入 */
	PORTD &= 0x2F;  /* 输出有效读控制信号, 读CH375芯片的数据端口, A0=0; CS=0; WR=1; RD=0; */
	DDRA = 0x00;  /* 该操作无意义,仅作延时,CH375要求读写脉冲宽度大于100nS */
	mData = PINA;  /* 从CH375的并口PA输入数据 */
	PORTD |= 0x70;  /* 输出无效的控制信号, 完成操作CH375芯片, A0=0; CS=1; WR=1; RD=1; */
	return( mData );
}


void	mDelaymS1( UINT8 ms )
{
	UINT16	i;
	while ( ms -- ) for ( i = 2600; i != 0; i -- );
}

/* 检查操作状态,如果错误则显示错误代码并停机 */
void	mStopIfError( UINT8 iError )
{
	if ( iError == ERR_SUCCESS ) return;  
	
	while ( 1 ) {

	}
}


//**********************短延时程序50us**************************// 
void delay50us(uint t) 
{ 
    uint j;       
    for(;t>0;t--)          
        for(j=0;j<70;j++) ;   
             
} 
//**********************短延时程序5us**************************// 
void delay5us(uint t) 
{ 
    uint j;       
    for(;t>0;t--)          
        for(j=0;j<7;j++)    
            ; 
} 

void LCD_portinit(void);
void initial_GLCD(void);
void command_GLCD(uchar ord);
void chkbusy_GLCD(void);
void write_GLCD(uchar dat);
void delay12(uint);





void	delay_1ms12( uint ms )
{
	uint	i;
	while ( ms -- ) for ( i = 10; i != 0; i -- );
//	while ( ms -- ); 
//	 mDelay1uS( );
}

void	delay12( uint ms )
{
//	uint	i;
//	while ( ms -- ) for ( i = 100; i != 0; i -- );
		while ( ms -- ); 
}















void lcd(uchar tempa1[],uchar tempa2[],uchar tempa3[],uchar  tempa4[])
{
uchar i=0;
  	uchar j;
	
    initial_GLCD();        //初始化LCD
	
   //---------------------------------------------------------

  //temp=0x00;                      //line 1
  // command_GLCD(0x80|temp);
command_GLCD(0x80);

		   for(j=0;j<8;j++)
		   { 
		   write_GLCD(tempa1[i]);
		   i++;
		   write_GLCD(tempa1[i]);
		  i++;
		 //  
		
		  // write_GLCD(0x30+j);
		  // i++;
		   }
	  i=0;

   //---------------------------------------------------------
 //   temp=0x10;                      //line2
 //  command_GLCD(temp|0x80);
//command_GLCD(0x88);
		   for(j=0;j<8;j++)
		   {
		   write_GLCD(tempa3[i]);
		   
		   i++;
		
		   write_GLCD(tempa3[i]);
		   i++;
		   }
		   i=0;
   //---------------------------------------------------------
 // temp=0x08;                       //line3
 //  command_GLCD(temp|0x80);
//command_GLCD(0x90);
		   for(j=0;j<8;j++)
		   {
		   write_GLCD(tempa2[i]);
		  
		  i++;
		
		   write_GLCD(tempa2[i]);
		   i++;
		   }
		 i=0;
   //---------------------------------------------------------
//   temp=0x18;                      //line4
 //  command_GLCD(temp|0x80);
//command_GLCD(0x98);
		   for(j=0;j<8;j++)
		   {
		   write_GLCD(tempa4[i]);
		   
		   i++;
		
		   write_GLCD(tempa4[i]);
		   i++;
		   }
		   i=0;
		   
  }
















void dis_str(char *pstr1,char *pstr2,char *pstr3,char *pstr4)
{
	uchar j;
    initial_GLCD();        //初始化LCD	
	
	command_GLCD(0x80);
	for(j=0;j<12;j++)
	{ 
		   write_GLCD(*(pstr1+j));
  	  
	}
	for(j=0;j<4;j++)
	{ 
		   write_GLCD(0x20);
  	  
	}
	
	
	
	//command_GLCD(0x90);
	for(j=0;j<12;j++)
	{ 
		   write_GLCD(*(pstr2+j));	  
	}
		for(j=0;j<4;j++)
	{ 
		   write_GLCD(0x20);
  	  
	}
	
	//command_GLCD(0x88);
	for(j=0;j<12;j++)
	{ 
		   write_GLCD(*(pstr3+j));	  
	}
		for(j=0;j<4;j++)
	{ 
		   write_GLCD(0x20);
  	  
	}
	
	//command_GLCD(0x98);
	for(j=0;j<12;j++)
	{ 
		   write_GLCD(*(pstr4+j));	  
	}
		for(j=0;j<4;j++)
	{ 
		   write_GLCD(0x20);
  	  
	}
	
	
}

void initial_GLCD()
{ 
command_GLCD(0x30);
delay_1ms12(2000);
command_GLCD(0x30);
delay_1ms12(2000);
command_GLCD(0x30);
delay_1ms12(2000);
//command_GLCD(0x06);    //显示器控制:游标不显示;
delay_1ms12(2000);
command_GLCD(0x01);    //清屏幕
delay_1ms12(3000);
delay_1ms12(3000);
delay_1ms12(3000);
delay_1ms12(3000);
command_GLCD(0x0c);    
delay_1ms12(2000);//进入模式设定
command_GLCD(0x02);    //清屏幕
delay_1ms12(3000);
delay_1ms12(3000);
delay_1ms12(3000);
delay_1ms12(3000);
delay_1ms12(3000);	
}

//--------------------------------------
//写命令到GLCD
//--------------------------------------
void command_GLCD(uchar ord)
{
 	 delay12(2000);
 	 EN_H; 
	 EN_L;
	 RS_L;//RS_L;
	 RW_L;
	 EN_H; 
	 PORTB = ord;
	 EN_L;	
}

//----------------------------------
//写数据到GLCD
//----------------------------------
void write_GLCD(uchar dat)
{
	 delay12(2000);	
	 EN_L;
	 RS_H;
	 RW_L;
	 EN_H; 
	 PORTB = dat;
	 EN_L;
}


     
void tishi(void)
{
	uchar   DHZTAB1[]="中国地质大学北京";
	uchar   DHZTAB2[]="  USB 存储系统  ";
	uchar   DHZTAB3[]="  请插入U 盘    ";
	uchar   DHZTAB4[]="................";
	lcd(DHZTAB1,DHZTAB2,DHZTAB3,DHZTAB4);
}



void start_set(void)
{


uchar   HZTAB1[]="设置存储日期....";
uchar   HZTAB2[]="        月......";
uchar   HZTAB3[]="        日......";
uchar   HZTAB4[]="  确定#   取消* ";
*(HZTAB2+6)=s1+0x30;*(HZTAB2+5)=s2+0x30;
*(HZTAB3+6)=s3+0x30;*(HZTAB3+5)=s4+0x30;
lcd(HZTAB1, HZTAB2,HZTAB3, HZTAB4);


}




void Start3(void)
{
uchar   HZTAB1[]="存储已设定      ";
uchar   HZTAB2[]="存储日期  月  日";
uchar   HZTAB3[]="存储文件名为....";
uchar   HZTAB4[]="................";


*(HZTAB2+9)=s1+0x30;*(HZTAB2+8)=s2+0x30;
*(HZTAB2+13)=s3+0x30;*(HZTAB2+12)=s4+0x30;


*(HZTAB4+1)=s1+0x30;*HZTAB4=s2+0x30;
*(HZTAB4+3)=s3+0x30;*(HZTAB4+2)=s4+0x30;
*(HZTAB4+5)=0x30;*(HZTAB4+4)=0x30;

*(HZTAB4+11)=s1+0x30;*(HZTAB4+10)=s2+0x30;
*(HZTAB4+13)=s3+0x30;*(HZTAB4+12)=s4+0x30;
*(HZTAB4+15)=0x39;*(HZTAB4+14)=0x39;



lcd(HZTAB1, HZTAB2,HZTAB3, HZTAB4);

}

/*
void lcd_system(void)
{
uchar   DHZTAB1[]="中国地质大学北京";
uchar   DHZTAB2[]="................";
uchar   DHZTAB3[]="U 盘总容量......";
uchar   DHZTAB4[]="          M     ";
*(DHZTAB4+5)=s1+0x30;*(DHZTAB4+6)=s2+0x30;*(DHZTAB4+7)=s3+0x30;


lcd(DHZTAB1, DHZTAB2,DHZTAB3, DHZTAB4);


}
*/




void lcd_system1(void)
{
uchar   HZTAB1[]="中国地质大学北京";
uchar   HZTAB2[]="文件名..........";
uchar   HZTAB3[]="................";
uchar   HZTAB4[]="请按键 B输出波形";

//*(DHZTAB3+0)=s1+0x30;*(DHZTAB3+1)=s2+0x30;*(DHZTAB3+2)=s3+0x30;
for(char p=0;p<12;p++)
{
*(HZTAB3+p)=data[c+p];
}
lcd(HZTAB1, HZTAB2,HZTAB3, HZTAB4);
}

void dis_str1(uchar line , uchar *pstr)
{
	uchar j;
    initial_GLCD();        //初始化LCD	

	command_GLCD(0x80);
	for(j=0;j<16&&(*pstr!='\0');j++)
	{ 
		   write_GLCD(*(pstr+j));	  
	}
}
//lcd("中国地质大学北京","测试波形输出....","1 正弦波 2方波","3 三角波 4......");	

void set(void)
{if (set_flag==1)
{

switch(s5)
{
case 0:s2=key;break;
case 1:s1=key;break;
case 2:s4=key;break;
case 3:s3=key;break;
default:break;
}
}

}



int  main(void ) {
	UINT8 	i,f;
	UINT8	*pCodeStr;
	write_times=0;
//	UINT8	*p1,*p2,*p3,*p4;
	unsigned char hj;
	char  name_tem[12];
	UINT8 name[4]={0,0,0,0};

    PORTC|=0XFF;                    //断口初始化
    DDRB |= 0xFF;
	PORTB |= 0xFF;
	DDRC |= 0xFF;
    s1=0;s2=0;s3=0;s4=0;s5=0;s6=0;x=0;t=0;
    TWI_init();                     //i2c初始化波特率用于键盘LED
	MCUCR=MCUCR&0xFB;               //设置外部1中断为下降沿触发

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -