求助 使用的examplesperipheralsi2c目录下的i2c 从接收程序,不触发接收中断程序,下面是代码
//*****************************************************************************
//
// hello.c - Simple hello world example.
//
// Copyright (c) 2013-2014 Texas Instruments Incorporated. All rights reserved.
// Software License Agreement
//
// Texas Instruments (TI) is supplying this software for use solely and
// exclusively on TI's microcontroller products. The software is owned by
// TI and/or its suppliers, and is protected under applicable copyright
// laws. You may not combine this software with "viral" open-source
// software in order to form a larger program.
//
// THIS SOFTWARE IS PROVIDED "AS IS" AND WITH ALL FAULTS.
// NO WARRANTIES, WHETHER EXPRESS, IMPLIED OR STATUTORY, INCLUDING, BUT
// NOT LIMITED TO, IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE APPLY TO THIS SOFTWARE. TI SHALL NOT, UNDER ANY
// CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL, OR CONSEQUENTIAL
// DAMAGES, FOR ANY REASON WHATSOEVER.
//
// This is part of revision 2.1.0.12573 of the EK-TM4C1294XL Firmware Package.
//
//*****************************************************************************
#include <stdint.h>
#include <stdbool.h>
#include "inc/hw_memmap.h"
#include "inc/hw_types.h"
#include "inc/hw_i2c.h"
#include "inc/hw_ints.h"
#include "driverlib/gpio.h"
#include "drivers/pinout.h"
#include "driverlib/pin_map.h"
#include "driverlib/rom.h"
#include "driverlib/rom_map.h"
#include "driverlib/sysctl.h"
#include "driverlib/uart.h"
#include "driverlib/i2c.h"
#include "utils/uartstdio.h"
//*****************************************************************************
//
//! addtogroup example_list
//! <h1>Hello World (hello)</h1>
//!
//! A very simple ``hello world'' example. It simply displays ``Hello World!''
//! on the UART and is a starting point for more complicated applications.
//!
//! Open a terminal with 115,200 8-N-1 to see the output for this demo.
//
//*****************************************************************************
//*****************************************************************************
//
// System clock rate in Hz.
//
//*****************************************************************************
uint32_t g_ui32SysClock;
#define SLAVE_ADDRESS 0x3C
static uint32_t g_ui32DataRx;
static bool g_bIntFlag = false;
//*****************************************************************************
//
// The error routine that is called if the driver library encounters an error.
//
//*****************************************************************************
#ifdef DEBUG
void
__error__(char *pcFilename, uint32_t ui32Line)
{
}
#endif
//*****************************************************************************
//
// Configure the UART and its pins. This must be called before UARTprintf().
//
//*****************************************************************************
void
ConfigureUART(void)
{
//
// Enable the GPIO Peripheral used by the UART.
//
ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOA);
//
// Enable UART0
//
ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_UART2);
//
// Configure GPIO Pins for UART mode.
//
ROM_GPIOPinConfigure(GPIO_PA6_U2RX);
ROM_GPIOPinConfigure(GPIO_PA7_U2TX);
ROM_GPIOPinTypeUART(GPIO_PORTA_BASE, GPIO_PIN_6 | GPIO_PIN_7);
//
// Initialize the UART for console I/O.
//
UARTStdioConfig(2, 115200, g_ui32SysClock);
}
void
I2C0SlaveIntHandler(void)
{
//
// Clear the I2C0 interrupt flag.
//
I2CSlaveIntClear(I2C0_BASE);
//
// Read the data from the slave.
//
g_ui32DataRx = I2CSlaveDataGet(I2C0_BASE);
//
// Set a flag to indicate that the interrupt occurred.
//
g_bIntFlag = true;
}
//*****************************************************************************
//
// Print "Hello World!" to the UART on the Intelligent UART Module.
//
//*****************************************************************************
int
main(void)
{
uint32_t ui32DataTx;
//
// Run from the PLL at 120 MHz.
//
g_ui32SysClock = MAP_SysCtlClockFreqSet((SYSCTL_XTAL_25MHZ |
SYSCTL_OSC_MAIN | SYSCTL_USE_PLL |
SYSCTL_CFG_VCO_480), 120000000);
//
// The I2C0 peripheral must be enabled before use.
//
SysCtlPeripheralEnable(SYSCTL_PERIPH_I2C0);
//
// For this example I2C0 is used with PortB[3:2]. The actual port and
// pins used may be different on your part, consult the data sheet for
// more information. GPIO port B needs to be enabled so these pins can
// be used.
// TODO: change this to whichever GPIO port you are using.
//
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOB);
//
// Configure the pin muxing for I2C0 functions on port B2 and B3.
// This step is not necessary if your part does not support pin muxing.
// TODO: change this to select the port/pin you are using.
//
GPIOPinConfigure(GPIO_PB2_I2C0SCL);
GPIOPinConfigure(GPIO_PB3_I2C0SDA);
//
// Select the I2C function for these pins. This function will also
// configure the GPIO pins pins for I2C operation, setting them to
// open-drain operation with weak pull-ups. Consult the data sheet
// to see which functions are allocated per pin.
// TODO: change this to select the port/pin you are using.
//
GPIOPinTypeI2CSCL(GPIO_PORTB_BASE, GPIO_PIN_2);
GPIOPinTypeI2C(GPIO_PORTB_BASE, GPIO_PIN_3);
//
// Enable loopback mode. Loopback mode is a built in feature that helps
// for debug the I2Cx module. It internally connects the I2C master and
// slave terminals, which effectively lets you send data as a master and
// receive data as a slave. NOTE: For external I2C operation you will need
// to use external pull-ups that are faster than the internal pull-ups.
// Refer to the datasheet for more information.
//
HWREG(I2C0_BASE + I2C_O_MCR) |= 0x01;
//
// Enable the I2C0 interrupt on the processor (NVIC).
//
ROM_IntEnable(INT_I2C0);
//
// Configure and turn on the I2C0 slave interrupt. The I2CSlaveIntEnableEx()
// gives you the ability to only enable specific interrupts. For this case
// we are only interrupting when the slave device receives data.
//
I2CSlaveIntEnableEx(I2C0_BASE, I2C_SLAVE_INT_DATA);
//
// Enable and initialize the I2C0 master module. Use the system clock for
// the I2C0 module. The last parameter sets the I2C data transfer rate.
// If false the data rate is set to 100kbps and if true the data rate will
// be set to 400kbps. For this example we will use a data rate of 100kbps.
//
I2CMasterInitExpClk(I2C0_BASE, g_ui32SysClock, false);
//
// Enable the I2C0 slave module.
//
I2CSlaveEnable(I2C0_BASE);
//
// Set the slave address to SLAVE_ADDRESS. In loopback mode, it's an
// arbitrary 7-bit number (set in a macro above) that is sent to the
// I2CMasterSlaveAddrSet function.
//
I2CSlaveInit(I2C0_BASE, SLAVE_ADDRESS);
//
// Tell the master module what address it will place on the bus when
// communicating with the slave. Set the address to SLAVE_ADDRESS
// (as set in the slave module). The receive parameter is set to false
// which indicates the I2C Master is initiating a writes to the slave. If
// true, that would indicate that the I2C Master is initiating reads from
// the slave.
//
I2CMasterSlaveAddrSet(I2C0_BASE, SLAVE_ADDRESS, false);
//
// Set up the serial console to use for displaying messages. This is just
// for this example program and is not needed for proper I2C operation.
//
ConfigureUART();
//
// Enable interrupts to the processor.
//
ROM_IntMasterEnable();
//
// Display the example setup on the console.
//
UARTprintf("I2C Slave Interrupt Example ->");
UARTprintf("
Module = I2C0");
UARTprintf("
Mode = Receive interrupt on the Slave module");
UARTprintf("
Rate = 100kbps
");
//
// Initialize the data to send.
//
ui32DataTx = 'I';
//
// Indicate the direction of the data.
//
UARTprintf("Transferring from: Master -> Slave
");
//
// Display the data that I2C0 is transferring.
//
UARTprintf(" Sending: '%c'", ui32DataTx);
//
// Place the data to be sent in the data register.
//
I2CMasterDataPut(I2C0_BASE, ui32DataTx);
//
// Initiate send of single piece of data from the master. Since the
// loopback mode is enabled, the Master and Slave units are connected
// allowing us to receive the same data that we sent out.
//
I2CMasterControl(I2C0_BASE, I2C_MASTER_CMD_SINGLE_SEND);
//
// Wait for interrupt to occur.
//
while(!g_bIntFlag)
{
}
//
// Display that interrupt was received.
//
UARTprintf("
Slave Interrupt Received!
");
//
// Display the data that the slave has received.
//
UARTprintf(" Received: '%c'
", g_ui32DataRx);
//
// Loop forever.
//
while(1)
{
}
}
此帖出自
小平头技术问答
一周热门 更多>