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

📄 main.c

📁 Demo for I2C Master and Slave
💻 C
字号:
/*------------------------------------------------------------------------------
main.c

Version:
June 2004 Version 1.0 - Initial Version

Dependencies:
upsd_lcd.c   - LCD driver.
upsd_i2c.c   - I2C driver.
upsd_timer.c - Timer driver.


Description:  Demo for I2C Master
The main function of this code is to demonstrate the use of the I2C driver in
Master mode.  As a master, the uPSD device writes data to a slave device over
the I2C bus and reads it back.  It compares the received data with the sent data
and confirms with a message on the LCD if there is a match.  

It is intended that the I2C slave device this master communicates with is a uPSD
device programmed with the I2C Slave demo code.  The SCL and SDA lines of the
master and slave devices must be connected together and a pull-up is on each 
signal.  The two devices must also have their GNDs tied together.  Using two 
DK3200 boards, no pull-ups are required (they are included on the boards) but 
the two signals (SCL, SDA) plus GND must be connected.


Copyright (c) 2004 STMicroelectrons Inc.

This example demo code is provided as is and has no warranty,
implied or otherwise.  You are free to use/modify any of the provided
code at your own risk in your applications with the expressed limitation
of liability (see below) so long as your product using the code contains
at least one uPSD product (device).

LIMITATION OF LIABILITY:   NEITHER STMicroelectronics NOR ITS VENDORS OR 
AGENTS SHALL BE LIABLE FOR ANY LOSS OF PROFITS, LOSS OF USE, LOSS OF DATA,
INTERRUPTION OF BUSINESS, NOR FOR INDIRECT, SPECIAL, INCIDENTAL OR
CONSEQUENTIAL DAMAGES OF ANY KIND WHETHER UNDER THIS AGREEMENT OR
OTHERWISE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
------------------------------------------------------------------------------*/
#include "upsd3200.h"
#include "upsd_hardware.h"
#include "upsd_i2c.h"
#include "upsd_timer.h"
#include "upsd_LCD.h"

#define I2C_ADDR 0x88
#define buf_len 0x04

xdata unsigned char temp_xmit_buf[20];				// message transmit buffer
xdata unsigned char temp_rcv_buf[20];				// message receive buffer
xdata PSD_REGS PSD8xx_reg _at_ PSD_REG_ADDR; 


bit match_buf(unsigned char* a, unsigned char* b, unsigned char length)
{
	unsigned char i;
	for(i=1;i<=length;i++)
	{
		if(a[i-1]!=b[i-1])
			return 0;
	}
	return 1;
}

main()
{
	static unsigned char i,temp;
	static unsigned char databuf[4]={0x55, 0xAA, 0x5A, 0xA5};

	temp = 0x00;
   	WDKEY=0x55;											// disable watchdog timer
	PSD8xx_reg.VM |= 0x80;	
	timer0_init();   		 							// initialize timer0 interrupt 
	lcd_init();	  										// initialize LCD. 8 bits, 2 lines, 5x7 font,
	printfLCD("I2C Master");							// display on LCD
	printfLCD("\nWaiting on Slave\n");					// display on LCD
	delay_1sec();

	for(i=0;i<20;i++)
		temp_xmit_buf[i]=databuf[i];
	
	upsd_i2c_init (833,I2C_ADDR);						// I2C initialization

	while(1)
	{
		EA=0;
		while(temp!=I2C_MX_END)
			temp=upsd_i2c_Master_Xmit(I2C_ADDR,temp_xmit_buf,buf_len);

		printfLCD("\nTransmit OK     \n");
		delay_1sec();
		
		for(i=0;i<20;i++)
			temp_rcv_buf[i]=0;
		
		while(temp!=I2C_MR_END)
			temp=upsd_i2c_Master_Recv (I2C_ADDR,temp_rcv_buf,buf_len);

		printfLCD("\nReceive OK      \n");
		delay_1sec();
				
		if(match_buf(temp_xmit_buf,temp_rcv_buf,buf_len)==1)
		{
			printfLCD("\nMatch    OK     \n");
			delay_1sec();
		}
		else 
		{
			printfLCD("\nNo Match        \n");
			delay_1sec();
		}	
	}
}


⌨️ 快捷键说明

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