中级
30 分钟

使用SC18IS602B和PIC18F57Q43在接口之间建立安全连接

I2C遇见SPI:革命性的接口桥接解决方案!

I2C to SPI Click with Curiosity Nano with PIC18F57Q43

已发布 6月 24, 2024

点击板

I2C to SPI Click

开发板

Curiosity Nano with PIC18F57Q43

编译器

NECTO Studio

微控制器单元

PIC18F57Q43

通过我们的桥接技术,将您的项目提升到接口精度的新高度。它能够弥合I2C和SPI之间的差距,优化数据交换,减少复杂性,并增强您电子应用中的兼容性。

A

A

硬件概览

它是如何工作的?

I2C to SPI Click基于两个NXP Semiconductor的SC18IS602B,这是一个I2C总线到SPI桥接器。该IC桥接了两种接口之间的数据通信,提供了许多附加功能,如可编程I/O、内部振荡器选项、低电平有效中断输出、低功耗模式等。SC18IS602B作为I2C总线的从机发送器或从机接收器以及SPI主机操作。SC18IS602B控制所有SPI总线特定的序列、协议和定时。它还具有自己的内部振荡器,并支持SPI芯片选择输出,在未使用时可配置为GPIO。这使得软件易于编写或从其他平台移植。I2C to SPI Click提供面向字节的I2C总线接口,支持高达400 kHz的数据传输。当I2C总线主机从Click板™读取数据时,该设备将是从机发送器。当I2C总线主机发送数据时,它也可以是从机接收器。SC18IS602B在任何时候都不会作

为I2C总线主机操作。然而,它确实具有在字节之间保持SCL线为低电平的能力,以完成其内部过程。SC18IS602B的从机地址由固定部分和可编程部分组成。从机地址的可编程部分使得尽可能多的此类设备能够连接到I2C总线上。由于SC18IS602B具有三个可编程地址位(由A2、A1和A0引脚定义),因此在同一总线上最多可以有八个此类设备。因此,该Click板™配备了三个SMD跳线,归类在ADDR SEL标签下,用于选择I2C从机地址。通过将跳线移动到所需位置,用户可以选择用于与主机MCU通信的地址。#RESET引脚执行SC18IS602B IC的硬件复位。#RESET引脚连接到mikroBUS™的RST引脚,并且为低电平有效。#INT允许主机MCU从SC18IS602B IC接收中断。在完成任何SPI传输后,

SC18IS602B生成中断。因此,SC18IS602B的#INT连接到mikroBUS™插座的INT引脚。可以通过发送“清除中断”命令来清除中断(INT引脚为高电平),尽管这不是必需的。这允许编写更优化的软件(固件),因为主机MCU不必连续轮询LSR寄存器以查看是否需要服务任何中断。SC18IS602B的数据手册提供了有关使用和配置SC18IS602B IC的更多信息。然而,Click板™由mikroSDK库支持,提供简化原型设计和固件开发的功能。该Click板™只能在3.3V逻辑电压水平下运行。在使用具有不同逻辑电平的MCU之前,必须进行适当的逻辑电压电平转换。此外,它配备了一个库,包含函数和示例代码,可用作进一步开发的参考。

I2C to SPI Click top side image
I2C to SPI Click bottom side image

功能概述

开发板

PIC18F57Q43 Curiosity Nano 评估套件是一款尖端的硬件平台,旨在评估 PIC18-Q43 系列内的微控制器。其设计的核心是包含了功能强大的 PIC18F57Q43 微控制器(MCU),提供先进的功能和稳健的性能。这个评估套件的关键特点包括一个黄 色用户 LED 和一个响应灵敏的机械用户开关,提供无

缝的交互和测试。为一个 32.768kHz 水晶振荡器足迹提供支持,确保精准的定时能力。套件内置的调试器拥有一个绿色电源和状态 LED,使编程和调试变得直观高效。此外,增强其实用性的还有虚拟串行端口 (CDC)和一个调试 GPIO 通道(DGI GPIO),提供广泛的连接选项。该套件通过 USB 供电,拥有由

 MIC5353 LDO 调节器提供支持的可调目标电压功能,确保在 1.8V 至 5.1V 的输出电压范围内稳定运行,最大输出电流为 500mA,受环境温度和电压限制。

PIC18F57Q43 Curiosity Nano double side image

微控制器概述 

MCU卡片 / MCU

default

建筑

PIC

MCU 内存 (KB)

128

硅供应商

Microchip

引脚数

48

RAM (字节)

8196

你完善了我!

配件

Curiosity Nano Base for Click boards 是一款多功能硬件扩展平台,专为简化 Curiosity Nano 套件与扩展板之间的集成而设计,特别针对符合 mikroBUS™ 标准的 Click 板和 Xplained Pro 扩展板。这款创新的基板(屏蔽板)提供了无缝的连接和扩展可能性,简化了实验和开发过程。主要特点包括从 Curiosity Nano 套件提供 USB 电源兼容性,以及为增强灵活性而提供的另一种外部电源输入选项。板载锂离子/锂聚合物充电器和管理电路确保电池供电应用的平稳运行,简化了使用和管理。此外,基板内置了一个固定的 3.3V 电源供应单元,专用于目标和 mikroBUS™ 电源轨,以及一个固定的 5.0V 升压转换器,专供 mikroBUS™ 插座的 5V 电源轨,为各种连接设备提供稳定的电力供应。

Curiosity Nano Base for Click boards accessories 1 image

使用的MCU引脚

mikroBUS™映射器

NC
NC
AN
Reset
PA7
RST
SPI Chip Select
PD4
CS
SPI Clock
PC6
SCK
SPI Data OUT
PC5
MISO
SPI Data IN
PC4
MOSI
Power Supply
3.3V
3.3V
Ground
GND
GND
NC
NC
PWM
Interrupt
PA6
INT
NC
NC
TX
NC
NC
RX
I2C Clock
PB2
SCL
I2C Data
PB1
SDA
NC
NC
5V
Ground
GND
GND
1

“仔细看看!”

原理图

I2C to SPI Click Schematic schematic

一步一步来

项目组装

Curiosity Nano Base for Click boards front image hardware assembly

从选择您的开发板和Click板™开始。以Curiosity Nano with PIC18F57Q43作为您的开发板开始。

Curiosity Nano Base for Click boards front image hardware assembly
Barometer 13 Click front image hardware assembly
PIC18F57Q43 Curiosity Nano front image hardware assembly
Prog-cut hardware assembly
Curiosity Nano with PICXXX MB 1 - upright/background hardware assembly
Necto image step 2 hardware assembly
Necto image step 3 hardware assembly
Necto image step 4 hardware assembly
Necto image step 5 hardware assembly
Necto image step 6 hardware assembly
PIC18F57Q43 Curiosity MCU Step hardware assembly
Necto No Display image step 8 hardware assembly
Necto image step 9 hardware assembly
Necto image step 10 hardware assembly
Debug Image Necto Step hardware assembly

实时跟踪您的结果

通过调试模式的应用程序输出

1. 一旦代码示例加载完成,按下 "DEBUG" 按钮将启动构建过程,并将其编程到创建的设置上,然后进入调试模式。

2. 编程完成后,IDE 中将出现一个带有各种操作按钮的标题。点击绿色的 "PLAY" 按钮开始读取通过 Click board™ 获得的结果。获得的结果将在 "Application Output" 标签中显示。

DEBUG_Application_Output

软件支持

库描述

此库包含I2C to SPI Click驱动程序的API。

关键功能:

  • i2ctospi_spi_write_byte - 此功能通过SPI将数据字节写入I2C to SPI Click上的SC18IS602B I2C总线到SPI桥接器的目标8位寄存器地址。

  • i2ctospi_spi_read_byte - 此功能通过SPI从I2C to SPI Click上的SC18IS602B I2C总线到SPI桥接器的目标8位寄存器地址读取数据字节。

  • i2ctospi_clear_interrupt - 此功能在完成任何SPI传输后清除由SC18IS602B生成的中断。

开源

代码示例

这个示例可以在 NECTO Studio 中找到。欢迎下载代码,或者您也可以复制下面的代码。

/*!
 * \file 
 * \brief I2cToSpi Click example
 * 
 * # Description
 * I2C to SPi Click allows serving as an interface between a standard I2C-bus of a microcontroller 
 * and an SPi bus, which allows the microcontroller to communicate directly with SPi devices 
 * through its I2C-bus. By offering an I2C-bus slave-transmitter or slave-receiver and SPI master, 
 * this Click controls all the SPi bus-specific sequences, protocol, and timing. It also has its own 
 * internal oscillator, and it supports the SPi chip select output that may be configured as GPIO when not used.
 *
 * The demo application is composed of two sections :
 * 
 * ## Application Init 
 * Initialization driver enable's - I2C,
 * hardware reset, SS0 ( CS ) configured to be used as slave select outputs, set the configuration of SPI:
 * order MSB first, clock Idle low, leading-edge transition, SPI clock rate to 115kHz,
 * set SPI EEPROM write enable SS0, clear  interrupt,
 * clear RT5 register, sets starting time: hours, minutes and seconds ( enable counting ), also write log.
 * 
 * ## Application Task  
 * This is an example which demonstrates the use of RTC 5 click is wired to I2C to SPI click board.
 * I2C to SPI click communicates with register via the I2C interface,
 * serve as an interface between a standard I2C-bus of a microcontroller and an SPI bus.
 * RTC 5 click communicates with register via SPI interface.
 * In this examples, we display RTC time which we received reading from the target register 
 * address of MCP79510 chip on RTC 5 click board via I2C interface of I2C to SPI click board.
 * Results are being sent to the Usart Terminal where you can track their changes.
 * All data logs write on usb uart changes for every 1 sec.
 * 
 * *note:* 
 * <pre>
 * Additional Functions :
 *  - void display_log_uart( uint8_t value ) - Write the value of time or date as a two-digit number.
 *  - void rtc5_clear( i2ctospi_t *ctx, i2ctospi_spi_t *spi ) - Clear RTCC and SRAM memory of RTC 5 click.
 *  - void rtc5_set_time_seconds( i2ctospi_t *ctx, i2ctospi_spi_t *spi, uint8_t seconds ) - Set the seconds and enable counting.
 *  - uint8_t rtc5_get_time_seconds( i2ctospi_t *ctx, i2ctospi_spi_t *spi ) - Get the seconds.
 *  - void rtc5_set_time_minutes( uint8_t minutes ) - Set the minutes.
 *  - uint8_t rtc5_get_time_minutes( i2ctospi_t *ctx, i2ctospi_spi_t *spi ) - Get the minutes.
 *  - void rtc5_set_time_hours( i2ctospi_t *ctx, i2ctospi_spi_t *spi, uint8_t hours ) - Set the hours.
 *  - uint8_t rtc5_get_time_hours( i2ctospi_t *ctx, i2ctospi_spi_t *spi ) - Get the hours.
 * </pre>
 * 
 * \author MikroE Team
 *
 */
// ------------------------------------------------------------------- INCLUDES

#include "board.h"
#include "log.h"
#include "i2ctospi.h"

// ------------------------------------------------------------------ VARIABLES

static i2ctospi_t i2ctospi;
static i2ctospi_spi_t i2ctospi_spi;
static i2ctospi_gpio_t i2ctospi_gpio;
static log_t logger;

static uint8_t time_hours;
static uint8_t time_minutes;
static uint8_t time_seconds;
static uint8_t time_seconds_new = 0xFF;

// ------------------------------------------------------- ADDITIONAL FUNCTIONS

void display_log_uart ( uint8_t value )
{
    log_printf( &logger, " %d%d ", ( uint16_t )( value / 10 ), ( uint16_t )( value % 10 ) );
}

void rtc5_clear ( i2ctospi_t *ctx, i2ctospi_spi_t *spi )
{
    uint8_t reg_add;
    
    spi->slave_device = I2CTOSPI_SLAVEDEVICE_SS0;
    spi->function_id = I2CTOSPI_RTC5_COMMAND_WRITE;
    spi->reg_addr = reg_add;

    for ( reg_add = 0; reg_add < 0x20; reg_add++ )
    {
        i2ctospi_spi_write_byte( ctx, spi, 0x00 );
        Delay_1us( );
    }

    spi->reg_addr = I2CTOSPI_RTC5_COMMAND_CLEAR;
    i2ctospi_spi_write_byte( ctx, spi, 0x00 );

    i2ctospi_clear_interrupt( ctx );
}

void rtc5_set_time_seconds ( i2ctospi_t *ctx, i2ctospi_spi_t *spi, uint8_t seconds )
{
    uint8_t ones;
    uint8_t tens;
    uint8_t temp;

    ones = 0x00;
    tens = 0x00;

    seconds %= 60;

    ones = seconds % 10;

    tens = ( seconds / 10 ) << 4;

    temp = tens | ones;
    temp |= I2CTOSPI_RTC5_COMMAND_ENABLE_COUNTING;

    spi->slave_device = I2CTOSPI_SLAVEDEVICE_SS0;
    spi->function_id = I2CTOSPI_RTC5_COMMAND_WRITE;
    spi->reg_addr = I2CTOSPI_RTC5_REG_TIME_SEC;

    i2ctospi_spi_write_byte( ctx, spi, temp );
}

uint8_t rtc5_get_time_seconds ( i2ctospi_t *ctx, i2ctospi_spi_t *spi )
{
    uint8_t ones;
    uint8_t tens;
    uint8_t result_sec;
    uint8_t temp;

    spi->slave_device = I2CTOSPI_SLAVEDEVICE_SS0;
    spi->function_id = I2CTOSPI_RTC5_COMMAND_READ;
    spi->reg_addr = I2CTOSPI_RTC5_REG_TIME_SEC;

    temp = i2ctospi_spi_read_byte( ctx, spi );

    ones = temp & 0x0F;

    tens = ( temp & 0x70 ) >> 4;

    result_sec = ( 10 * tens ) + ones;

    return result_sec;
}

void rtc5_set_time_minutes ( i2ctospi_t *ctx, i2ctospi_spi_t *spi, uint8_t minutes )
{
    uint8_t ones;
    uint8_t tens;
    uint8_t temp;

    ones = 0x00;
    tens = 0x00;

    minutes %= 60;

    ones = minutes % 10;

    tens = ( minutes / 10 ) << 4;

    temp = tens | ones;

    spi->slave_device = I2CTOSPI_SLAVEDEVICE_SS0;
    spi->function_id = I2CTOSPI_RTC5_COMMAND_WRITE;
    spi->reg_addr = I2CTOSPI_RTC5_REG_TIME_MIN;

    i2ctospi_spi_write_byte( ctx, spi, temp );
}

uint8_t rtc5_get_time_minutes ( i2ctospi_t *ctx, i2ctospi_spi_t *spi )
{
    uint8_t ones;
    uint8_t tens;
    uint8_t result_min;
    uint8_t temp;

    spi->slave_device = I2CTOSPI_SLAVEDEVICE_SS0;
    spi->function_id = I2CTOSPI_RTC5_COMMAND_READ;
    spi->reg_addr = I2CTOSPI_RTC5_REG_TIME_MIN;

    temp = i2ctospi_spi_read_byte( ctx, spi );

    ones = temp & 0x0F;

    tens = ( temp & 0x70 ) >> 4;

    result_min = ( 10 * tens ) + ones;

    return result_min;
}

void rtc5_set_time_hours ( i2ctospi_t *ctx, i2ctospi_spi_t *spi, uint8_t hours )
{
    uint8_t ones;
    uint8_t tens;
    uint8_t temp;

    ones = 0x00;
    tens = 0x00;

    hours %= 24;

    ones = hours % 10;

    tens = ( hours / 10 ) << 4;

    temp = tens | ones;

    spi->slave_device = I2CTOSPI_SLAVEDEVICE_SS0;
    spi->function_id = I2CTOSPI_RTC5_COMMAND_WRITE;
    spi->reg_addr = I2CTOSPI_RTC5_REG_TIME_HOUR,

    i2ctospi_spi_write_byte( ctx, spi, temp );
}

uint8_t rtc5_get_time_hours ( i2ctospi_t *ctx, i2ctospi_spi_t *spi )
{
    uint8_t ones;
    uint8_t tens;
    uint8_t result_hours;
    uint8_t temp;

    spi->slave_device = I2CTOSPI_SLAVEDEVICE_SS0;
    spi->function_id = I2CTOSPI_RTC5_COMMAND_READ;
    spi->reg_addr = I2CTOSPI_RTC5_REG_TIME_HOUR;

    temp = i2ctospi_spi_read_byte( ctx, spi );

    ones = temp & 0x0F;

    tens = ( temp & 0x30 ) >> 4;

    result_hours = ( 10 * tens ) + ones;

    return result_hours;
}

// ------------------------------------------------------ APPLICATION FUNCTIONS

void application_init ( void )
{
    log_cfg_t log_cfg;
    i2ctospi_cfg_t cfg;

    /** 
     * Logger initialization.
     * Default baud rate: 115200
     * Default log level: LOG_LEVEL_DEBUG
     * @note If USB_UART_RX and USB_UART_TX 
     * are defined as HAL_PIN_NC, you will 
     * need to define them manually for log to work. 
     * See @b LOG_MAP_USB_UART macro definition for detailed explanation.
     */
    LOG_MAP_USB_UART( log_cfg );
    log_init( &logger, &log_cfg );
    log_info( &logger, "---- Application Init ----" );

    //  Click initialization.

    i2ctospi_cfg_setup( &cfg );
    I2CTOSPI_MAP_MIKROBUS( cfg, MIKROBUS_1 );
    i2ctospi_init( &i2ctospi, &cfg );

    i2ctospi_default_cfg( &i2ctospi );
    
    //Set Time :  23h 59m 48s
    rtc5_clear( &i2ctospi, &i2ctospi_spi );  
    rtc5_set_time_hours( &i2ctospi, &i2ctospi_spi, 23 );
    Delay_1ms( );
    rtc5_set_time_minutes( &i2ctospi, &i2ctospi_spi, 59 );
    Delay_1ms( );
    rtc5_set_time_seconds( &i2ctospi, &i2ctospi_spi, 48 );
    Delay_1ms( );
}

void application_task ( void )
{
    time_seconds = rtc5_get_time_seconds( &i2ctospi, &i2ctospi_spi );
    Delay_1ms( );
    time_minutes = rtc5_get_time_minutes( &i2ctospi, &i2ctospi_spi );
    Delay_1ms( );
    time_hours = rtc5_get_time_hours( &i2ctospi, &i2ctospi_spi );
    Delay_1ms( );

    if ( time_seconds_new != time_seconds )
    {
        log_printf( &logger, " Time :  " );
    
        display_log_uart( time_hours );
        log_printf( &logger, ":" );
    
        display_log_uart( time_minutes );
        log_printf( &logger, ":" );
    
        display_log_uart( time_seconds );
        log_printf( &logger, "\r\n" );
        
        log_printf( &logger, "------------------\r\n" );

        time_seconds_new = time_seconds;
    }

    Delay_1ms( );
}

void main ( void )
{
    application_init( );

    for ( ; ; )
    {
        application_task( );
    }
}


// ------------------------------------------------------------------------ END

额外支持

资源

喜欢这个项目吗?

'购买此套件' 按钮会直接带您进入购物车,您可以在购物车中轻松添加或移除产品。