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

📄 main.c

📁 ix2-lib-spi-1_2_0.zip源码
💻 C
字号:
/*C**************************************************************************
* NAME:         main.c
*----------------------------------------------------------------------------
* Copyright (c) 2002 Atmel.
*----------------------------------------------------------------------------
* RELEASE:      ix2-lib-spi-1_2_0      
* REVISION:     1.2     
*----------------------------------------------------------------------------
* PURPOSE: 
* Main sample program for use with SPI library for T89C51RC/RB2 products
* This software application is for evaluation and example purpose.
*****************************************************************************/



/*_____ I N C L U D E S ____________________________________________________*/

#include "config.h"


/*_____ M A C R O S ________________________________________________________*/


/*_____ D E F I N I T I O N ________________________________________________*/

#define OWN_SS			P1_1
#define BUFFER_SIZE		100

/*_____ D E C L A R A T I O N ______________________________________________*/


/*F**************************************************************************
* NAME: uart_init_internal_bdrate
*----------------------------------------------------------------------------
*----------------------------------------------------------------------------
* PARAMS:  none
* return:  none
*----------------------------------------------------------------------------
* PURPOSE: 
* Initialize the UART with internal baud rate for  57600 baud @ 22MHz
*----------------------------------------------------------------------------
* NOTE: 
*****************************************************************************/
void uart_init_internal_bdrate (void)
{

  SCON = 0x50;			//UART mode 2
  BRL=	0xFA;			// 22Mhz X1; 57600
  BDRCON = 0x1E;		// Set SPD = 1, BRG run on uart
  PCON &= 0X80;			//Set SMOD1=1 : double baud rate
  TI=1;

}


/*F**************************************************************************
* NAME: main
*----------------------------------------------------------------------------
*----------------------------------------------------------------------------
* PARAMS:  none
* return:  none
*----------------------------------------------------------------------------
* PURPOSE: 
* Main entry point function for SPI library routines example
*----------------------------------------------------------------------------
* NOTE: 
* This example file ouput SPI EEPROM operations on UART interface 57600 baud @ 22MHz
*****************************************************************************/
void main(void)
{

	unsigned char adr;
	unsigned char xdata buf[BUFFER_SIZE+1];

	SET_XRAM_1024;	// Enable all XRAM (1024) for T89C51RC/RB2 product
	ENABLE_IT;		// Enable Interrupts

	uart_init_internal_bdrate();	// Initialize UART 57600 bauds @ 22.1 MHz X1


	printf("\n\nSPI LIBRARY example\n");
	OWN_SS=1; 						// Set SS line to 1 when in master mode
	SPI_set_speed(32);				// Init SPI baud Rate
	SPI_MODE_MASTER;				// SPI in master mode


	//clear all flags
	b_SPI_busy=0;
	b_SPI_transmit_completed=1;
	b_SPI_error=0;
	b_SPI_overrun=0;

 	SPI_ENABLE;						// SPI enable
	
	/** WRITE AND READ STATTUS BYTE ********************************/
	at25010_clear_write_enable_latch();
	printf("\nE2Status: %x",(unsigned int)at25010_read_status());
	at25010_set_write_enable_latch();
	printf("\nE2Status: %x",(unsigned int)at25010_read_status());


	/** WRITE AND READ SINGLE BYTE *********************************/
	printf("\nRead at 0x55: %x", (unsigned int)at25010_read_byte(0x55));
	printf("\nWriting 0x55 at 0x55");
	while ( ~at25010_ready() );	//Wait
	at25010_write_byte(0x55,0x55);

	while ( ~at25010_ready() );	//Wait
	printf("\nRead at 0x55: %x", (unsigned int)at25010_read_byte(0x55));

	at25010_set_write_enable_latch();
	printf("\nWriting 0xAA at 0x55");
	at25010_write_byte(0x55,0xAA);

	while ( ~at25010_ready() );	//Wait
	printf("\nRead at 0x55: %x", (unsigned int)at25010_read_byte(0x55));


	printf("\n Writing 0..100");	
	for ( adr=0; adr < BUFFER_SIZE; adr++ )
	{
		while ( ~at25010_ready() );	//Wait
		at25010_set_write_enable_latch();
		at25010_write_byte(adr,adr);
	}
	printf("\n Reading 0..100\n");	
	for ( adr=0; adr < BUFFER_SIZE; adr++ )
	{
		while ( ~at25010_ready() );	//Wait
		printf("%x ",(unsigned int)at25010_read_byte(adr));
	}

	/** WRITE AND READ DATA STRING ON POLLING *************************/
	printf("\n Reading in burst 0..100 on polling\n");	
	at25010_read_burst_polling( 0, &buf, 100);
	for ( adr=0; adr < BUFFER_SIZE; adr++ )
	{
		printf("%x ",(unsigned int)buf[adr]);
	}

	// Prepare data
	for ( adr=0; adr < BUFFER_SIZE; adr++ )
	{
		buf[adr]=~adr;
	}

	printf("\n Writing in burst 0..100 on polling\n");	
	at25010_set_write_enable_latch();
	at25010_write_burst_polling( 0, &buf, BUFFER_SIZE);

	printf("\n Reading in burst 0..100 on polling \n");	
	at25010_read_burst_polling( 0, &buf, BUFFER_SIZE);
	for ( adr=0; adr < BUFFER_SIZE; adr++ )
	{
		printf("%x ",(unsigned int)buf[adr]);
	}
	/** WRITE AND READ DATA STRING ON INTERRUPT ************************/

	//clear data buffer
	for ( adr=0; adr < BUFFER_SIZE; adr++ )
	{
		buf[adr]=0xff;
	}
	printf("\n Reading in burst 0..100 on interrupt\n");	
	at25010_read_burst_interrupt( 0, &buf, BUFFER_SIZE);
	for ( adr=0; adr < BUFFER_SIZE; adr++ )
	{
		printf("%x ",(unsigned int)buf[adr]);
	}

	// Prepare data
	for ( adr=0; adr < BUFFER_SIZE; adr++ )
	{
		buf[adr]=adr;
	}
	printf("\n Writing in burst 0..5 on interrupt\n");	
	at25010_write_burst_interrupt( 0, &buf, 5);


	// Clear Buffer
	for ( adr=0; adr < BUFFER_SIZE; adr++ )
	{
		buf[adr]=0xff;
	}
	printf("\n Reading in burst 0..5 on interrupt\n");	
	at25010_read_burst_interrupt( 0, &buf, 5);
	for ( adr=0; adr < 5; adr++ )
	{
			printf("%x ",(unsigned int)buf[adr]);
	}

    while (1);
}

⌨️ 快捷键说明

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