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

📄 text1.c

📁 C8051F120读写SPI接口的AT45DB081,输入和输出由UART完成.内含UART和SPI的读写驱动.载发环境为KEIL
💻 C
字号:
/////////////////////////////////////
//  Generated Initialization File  //
/////////////////////////////////////

#include "C8051F120.h"
#include<stdio.h>
#include<string.h>

#define UART_BUF_LEN 40    //数据缓冲长度
unsigned char UART_R_COUNT=0,UART_T_COUNT=0,R_OK=0,T_OK=0,U_STR_LEN=0;
unsigned char SBUF_temp=0;
unsigned char UART_BUF[UART_BUF_LEN];
unsigned char str1[]={" I love you ,China ! Come on ! Beijing ! "};

sbit CS1=P3^0;
sbit WP1=P3^1;
sbit RDY1=P3^2;
sbit RST1=P3^3;

sbit CS2=P3^4;

unsigned char spi_flag=1;

// Peripheral specific initialization functions,
// Called from the Init_Device() function

void Reset_Sources_Init() //复位源设置
{
    EA=0;				  //关总中断
    WDTCN     = 0xDE;	  //关看门狗
    WDTCN     = 0xAD;
}

void Timer_Init()		 //定时器设置
{
    SFRPAGE   = TIMER01_PAGE;
   
    TMOD      = 0x20;	//定时器1:8位自动重载
    CKCON     = 0x10;	//定时器1使用系统时钟
    TL1       = 0xF3;	//波特率设为115200
    TH1       = 0xF3;	
	TR1       = 1;	    //起动定时器1  TCON=0x40;
}

void UART_Init()		//串口设置
{
    SFRPAGE   = UART0_PAGE;	 
    SCON0     = 0x50;	 //UART0工作在方式1,8位可变波特率,允许接收中断	REN0=1;
    SSTA0     = 0x10;	 //禁止2分频,发送、接收时钟都由定时器1产生
}

void SPI_Init()
{
  
    SFRPAGE   = SPI0_PAGE;
    SPI0CFG   = 0x77;  //(CKPOL=1, CKPHA=1),SCK 在空闲状态时处于高电平,在SCK 周期的第一个边沿采样数据,
    SPI0CKR   = 0x01;  //时钟频率为5MHz			 
	SPI0CN    = 0x07;  //3 线单主方式,使能SPI0

}


void Port_IO_Init()		  //交叉开关设置
{
    SFRPAGE   = CONFIG_PAGE;
	P0MDOUT   = 0x14;
    P3MDOUT   = 0xFF;
    XBR0      = 0x06;	 //使能UART0和SPI0
    XBR2      = 0x40;	 //交叉开关总使能
}

void Oscillator_Init()	 //时钟设置
{
    int i = 0;
    SFRPAGE   = CONFIG_PAGE;
    OSCXCN    = 0x67;
    for (i = 0; i < 3000; i++);  // Wait 1ms for initialization
    while ((OSCXCN & 0x80) == 0);
    OSCICN    = 0x83;
    CLKSEL    = 0x01;

    //SFRPAGE   = CONFIG_PAGE;
    //OSCICN    = 0x83;	 //使用内部24。5M时钟,不分频
}

void Interrupts_Init()	 //中断使能设置
{
    ES0=1;				 //1:开放串口中断
	//EIE1      = 0x01;	 //开放SPI0中断
    EA=1;                //开放总中断
}

// Initialization function for device,
// Call Init_Device() from your main program
void Init_Device(void)
{
    Reset_Sources_Init();	   //复位源设置
	Port_IO_Init();			   //交叉开关设置
	Oscillator_Init();		    //时钟设置
    Timer_Init();			    //定时器设置
    UART_Init();			  	//串口设置
	SPI_Init();				   //SPIO设置
    Interrupts_Init();		  //中断使能设置
}


void delay1ms(int time)
{
    int i,j;
    for	(j = time;j > 0; j--)
    for (i = 0; i < 2999; i++);  // Wait 1ms for initialization
}

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

//向串口发送一个字符串,strlenth为该字符串长度,str_start为发送字符串的起始位置, 
void send_UART_string(unsigned char *str,unsigned int str_start)
{
    unsigned char k=0;
	unsigned int strlenth=0;
    ES0=0;	 //关串口中断
	strlenth=strlen(str);  //计算字符串长度
    k=str_start;
	do 
      {
         send_UART_char(*(str+k));
         k++;
      }while(k<strlenth);
	ES0=1;	 //关串口中断
}




void UART0_interrpt() interrupt 4 	using 2	   //c8051f12x数据手册第130页
{	
    if(RI0==1)
	   {
		  UART_BUF[UART_R_COUNT]=SBUF0;
		  RI0=0;
		  UART_R_COUNT++;
		  if(UART_R_COUNT==U_STR_LEN)
		    {
			   REN0=0; //UART 接收禁止
		       R_OK=1;//接收满
		       UART_R_COUNT=0;//接收计数复位
		    }
	   }
	if(TI0==1)
	   {
		  TI0=0;
		  UART_T_COUNT++; 
		  if(UART_T_COUNT==U_STR_LEN)
			{
			   REN0=1;	//UART 接收允许
			   T_OK=1;	//发送完成
	     	   UART_T_COUNT=0;//发送计数复位
			}
		
	   }
}


void SPI_interrpt() interrupt 6	//SPI中断服务程序
{
  if(SPIF==1)
	{SPIF=0;spi_flag=1;}
}
/*发送一个字节*/
void SendSPI(unsigned int spidat)	 
{
  SPIF=0;
  SPI0DAT=spidat;
  while(SPIF==0);
  SPIF=0;
}

unsigned char GetSPI(void)	 
{
  SPIF=0;
  SPI0DAT=0;     //产生接收脉冲
  while(SPIF==0);
  SPIF=0;
  return SPI0DAT;
}
void buffer_write(unsigned char cmd_code,unsigned int star_addr,unsigned int len)		 
{ 
  int dat=0,i=0;
  CS1=0;                 //芯片允许操作
  SendSPI(cmd_code);     //写buf1命令84H,,buf2:87H
  SendSPI(0x00);		 //写无效页地址位
  SendSPI(star_addr>>8); //写地址BA8位
  SendSPI(star_addr);	 //写地址BA7--BA0位
  for(i=0;i<len;i++)
     {
	    dat=i;	 //取UART0接收到的数据
	    SendSPI(dat);		 //写一个字节
	 }
  CS1=1;				 //禁止芯片操作
}

void buffer_read(unsigned char cmd_code,unsigned int star_addr,unsigned int len)		 
{ 
  int dat=0,i=0;
  CS1=0;                 //芯片允许操作
  SendSPI(cmd_code);     //读buf1命令54H,buf2:56H
  SendSPI(0x00);		 //写无效页地址位
  SendSPI(star_addr>>8); //写地址BA8位
  SendSPI(star_addr);	 //写地址BA7--BA0位
  SendSPI(0x00);			     //写无效位
  for(i=0;i<len;i++)
     {
	   UART_BUF[i]=GetSPI(); //暂存读取数据
	   SPIF=0;
	 }
  CS1=1;				 //禁止芯片操作
}

void buffer_mem(unsigned char cmd_code,unsigned int star_addr)		 
{ 
//  int dat=0,i=0;
  CS1=0;                 //芯片允许操作
  SendSPI(cmd_code);     //buf1命令83H,buf2:86H
  SendSPI(star_addr>>7); //写地址PA11-PA7
  SendSPI(star_addr<<1);	 //写地址PA6--PA0位
  SendSPI(0x00);			 //写无效位
  CS1=1;				 //禁止芯片操作
}

void mem_buffer(unsigned char cmd_code,unsigned int star_addr)		 
{ 
//  int dat=0,i=0;
  CS1=0;                 //芯片允许操作
  SendSPI(cmd_code);     //buf1命令53H,buf2:55H
  SendSPI(star_addr>>7); //写地址PA11-PA7
  SendSPI(star_addr<<1);	 //写地址PA6--PA0位
  SendSPI(0x00);			 //写无效位
  CS1=1;				 //禁止芯片操作
}


unsigned char status_read(unsigned char cmd_code)		 
{ 
  unsigned char dat=0;
  CS1=0;                 //芯片允许操作
  SendSPI(cmd_code);     //命令57H
  dat=GetSPI();
  CS1=1;				 //禁止芯片操作
  return dat;
}

void main()
{
    int i=0;
	unsigned int temp1=0;



	Init_Device();
	CS2=1;
	delay1ms(1);
	U_STR_LEN=8; //设置数组长度
	temp1=0;
	R_OK=0;//接收清0,等待接收
	T_OK=0;//发送清0
	while(1)
	{
	 
	 /*中断接收函数调用方法*/  //需预先设置数组长度	U_STR_LEN=8; //设置数组长度
	  while(R_OK==0); //接收
	  R_OK=0;		 //为下一次接收做准备
	 /**********************/

	 for(i=0;i<4096;i++)		  //全片读写
	 {
      buffer_write(0x84,0x0000,264);//写buf1命令84H,,buf2:87H	 
	  
	  buffer_mem(0x83,i);//buf1命令83H,buf2:86H
	  
	  while((status_read(0x57) & 0x80)==0);//查询忙标志位
	  
	  mem_buffer(0x55,i);//buf1命令53H,buf2:55H
	  
	  while((status_read(0x57) & 0x80)==0);	//查询忙标志位
	 
	  buffer_read(0x56,134,8); //读buf1命令54H,buf2:56H
	  }
	 /*中断发送函数调用方法*/  //需预先设置数组长度	U_STR_LEN=8; //设置数组长度
	//  REN0=0;
	//  while(T_OK==0){SBUF0=UART_BUF[UART_T_COUNT];delay1ms(1);}//发送; 
	//  T_OK=0; 		 //为下一 次发送做准备


	temp1=0;
	while(temp1<264)	  // 指定页据全发送
	{
	  mem_buffer(0x55,56);//buf1命令53H,buf2:55H
	  
	  while((status_read(0x57) & 0x80)==0);	//查询忙标志位
	 
	  buffer_read(0x56,temp1,8); //读buf1命令54H,buf2:56H

	 // send_UART_string(UART_BUF,0);
	  REN0=0;
	  while(T_OK==0){SBUF0=UART_BUF[UART_T_COUNT];delay1ms(1);}//发送; 
	  T_OK=0; 		 //为下一 次发送做准备

	  temp1+=8;
	}

      
	  

	  /*查询方式发送数组*/
   // send_UART_string(str1,0); 
	}
}

⌨️ 快捷键说明

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