📄 main.c
字号:
/*------------------------------------------------------------------------------
main.c
Version:
July 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 Slave
The main function of this code is to demonstrate the use of the I2C driver in
Slave mode. As a slave, the uPSD device receives data written to it and sends
the data back when read. The number of bytes received and sent is displayed
on the LCD.
It is intended that the I2C master device this slave communicates with is a uPSD
device programmed with the I2C Master demo code. The SCL and SDA lines of the
master and slave devices must be connected together with a pull-up on each
signal. The two devices must also have their GNDs tied together. Using two
DK3200 boards, no pull-ups are required (as they are already on the board) but
the two signals, SCL and SDA, plus GND must be connected.
Note:
This demo includes an option to enable or disable compilation of the code that
places the uPSD32xx in idle mode between I2C transfers as well as turn on
LED ONE and LED TWO on the DK3200 while in idle mode. This file, main.c, only
conditionally compiles the code that intializes port B for driving LED ONE and
LED TWO. Also see upsd_i2c.c where the code that places the uPSD32xx in idle
mode and toggles the LEDs is located.
Copyright (c) 2004 STMicroelectronic 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 products (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 IDLE_MODE_DEMO 0 //0 = don't compile slave idle mode demo code
//1 = compile slave idle mode demo code
//Note: This #define is also in upsd_i2c.c file
unsigned char temp_xmit_buf[10]; // I2C transmit buffer
unsigned char temp_rcv_buf[10]; // I2C receive buffer
xdata PSD_REGS PSD8xx_reg _at_ PSD_REG_ADDR;
main()
{
unsigned char i,temp;
static unsigned char *temp_data_len,r;
WDKEY=0x55; // disable watchdog timer
PSD8xx_reg.VM |= 0x80;
timer0_init(); // initialize timer0 interrupt
lcd_init(); // initialize LCD. 8 bits, 2 lines, 5x7 font,
// no blink, cursor off, clear
printfLCD("I2C Slave"); // display on LCD
printfLCD("\nWait for master\n");
#if IDLE_MODE_DEMO
printfLCD("I2C Slave - Idle"); // display on LCD - indicate idle mode demo
//initialize port B for driving LED ONE and LED TWO on DK3200 board
PSD8xx_reg.DIRECTION_B|=0x03; // set PB0 & PB1 to output
PSD8xx_reg.DRIVE_B|=0x03; // set PB0 & PB1 to open Drain
PSD8xx_reg.DATAOUT_B|=0x03; // PB0 & PB1 - (LEDs off)
// Note: PB0 and PB1 jumpers on DK3200 must be installed for LEDs to turn on.
#endif
upsd_i2c_init (833,I2C_ADDR); // initialize I2C interface
while(1)
{
temp=upsd_i2c_Slave_Recv(temp_rcv_buf,temp_data_len);
if(temp==I2C_SR_END)
{
r=*temp_data_len;
printfLCD("\nRec. %x bytes\n",r);
}
else if(temp==I2C_SX_APP)
{
for(i=0;i<r;i++)
temp_xmit_buf[i]=temp_rcv_buf[i];
if(upsd_i2c_Slave_Xmit(temp_xmit_buf,temp_data_len)==I2C_SX_END)
{
r=*temp_data_len;
printfLCD("\nSent %x bytes\n",r);
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -