Advertisement
microrobotics

CJMCU-752 SC16IS752

Apr 4th, 2023
1,506
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2. Here is an example code to use the CJMCU-752 SC16IS752 I2C to dual channel UART conversion module with an Arduino using the I2C interface. In this example, we will use the first UART channel to communicate with a serial device.
  3.  
  4. First, download and install the SC16IS752 Arduino library from the following GitHub repository: https://github.com/ernestk-git/SC16IS752-Arduino-Library.
  5.  
  6. This code creates a simple "echo" between the Serial Monitor and a serial device connected to the first UART channel of the SC16IS752 module. It reads data from the Serial Monitor and sends it to the UART channel, and vice versa.
  7.  
  8. Connect the CJMCU-752 SC16IS752 module to the Arduino as follows:
  9.  
  10. VCC pin to 5V (or 3.3V, depending on your module's voltage level)
  11. GND pin to GND
  12. SDA pin to the SDA pin on your Arduino (A4 on the Uno, Leonardo, or Nano; pin 20 on the Mega)
  13. SCL pin to the SCL pin on your Arduino (A5 on the Uno, Leonardo, or Nano; pin 21 on the Mega)
  14. If your SC16IS752 module has a different I2C address, you can change the sc16is752Address constant accordingly.
  15.  
  16. Please note that you may need to use pull-up resistors (typically 4.7kΩ) for the SDA and SCL lines. Some SC16IS752 modules might already have pull-up resistors on board; in that case, you don't need to add external ones.
  17. */
  18.  
  19. #include <Arduino.h>
  20. #include <Wire.h>
  21. #include <SC16IS752.h>
  22.  
  23. const byte sc16is752Address = 0x4D; // I2C address of the SC16IS752 module (depends on A0, A1 pin configuration)
  24. SC16IS752 sc16is752 = SC16IS752(sc16is752Address, &Wire);
  25.  
  26. void setup() {
  27.   Serial.begin(9600);
  28.   Wire.begin();
  29.   sc16is752.begin(SC16IS752_PROTOCOL_I2C);
  30.  
  31.   // Configure UART channel 1
  32.   sc16is752.beginUART(9600, SC16IS752::CONFIG_8N1, SC16IS752::CHANNEL_1);
  33.   sc16is752.enableFIFO(SC16IS752::CHANNEL_1, true);
  34.   sc16is752.setRxTriggerLevel(SC16IS752::CHANNEL_1, 1);
  35.  
  36.   Serial.println("CJMCU-752 SC16IS752 I2C Dual Channel UART Module Example");
  37. }
  38.  
  39. void loop() {
  40.   // Read data from Serial Monitor and send it to UART channel 1
  41.   if (Serial.available()) {
  42.     char data = Serial.read();
  43.     sc16is752.write(SC16IS752::CHANNEL_1, data);
  44.   }
  45.  
  46.   // Read data from UART channel 1 and send it to Serial Monitor
  47.   if (sc16is752.available(SC16IS752::CHANNEL_1)) {
  48.     char data = sc16is752.read(SC16IS752::CHANNEL_1);
  49.     Serial.print(data);
  50.   }
  51. }
  52.  
  53.  
  54.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement