Skip to content

Commit 0502c69

Browse files
committed
add i2c and ws2812
1 parent c77605a commit 0502c69

31 files changed

Lines changed: 1850 additions & 2 deletions
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
#include <SoftI2C.h>
2+
3+
void setup() {
4+
scl_pin = 30; //extern variable in SoftI2C.h
5+
sda_pin = 31;
6+
I2CInit();
7+
}
8+
9+
void loop() {
10+
11+
uint8_t ack_bit, readData1, readData2;
12+
13+
USBSerial_println_s("\nDump first 8 bytes of 24C32 on 0x50 addr:");
14+
USBSerial_flush();
15+
I2CStart();
16+
ack_bit = I2CSend(0x50 << 1 | 1); //last bit is r(1)/w(0).
17+
I2CStop();
18+
19+
if (ack_bit == 0) {
20+
for (uint8_t i = 0; i < 8; i += 2) {
21+
I2CStart();
22+
ack_bit = I2CSend(0x50 << 1 | 0); //last bit is r(1)/w(0).
23+
if (ack_bit != 0) break;
24+
ack_bit = I2CSend(0); //high EEPROM addr
25+
if (ack_bit != 0) break;
26+
ack_bit = I2CSend(i); //low EEPROM addr
27+
if (ack_bit != 0) break;
28+
I2CRestart();
29+
ack_bit = I2CSend(0x50 << 1 | 1); //last bit is r(1)/w(0).
30+
if (ack_bit != 0) break;
31+
readData1 = I2CRead();
32+
I2CAck();
33+
readData2 = I2CRead();
34+
I2CNak();
35+
I2CStop();
36+
USBSerial_print_s("Get 2 bytes start from addr ");
37+
USBSerial_flush();
38+
USBSerial_print_ub(i, HEX);
39+
USBSerial_print_s(": ");
40+
USBSerial_print_ub(readData1, HEX);
41+
USBSerial_print_s(", ");
42+
USBSerial_println_ub(readData2, HEX);
43+
USBSerial_flush();
44+
}
45+
46+
} else {
47+
USBSerial_println_s("No response from 0x50");
48+
USBSerial_flush();
49+
}
50+
51+
delay(1000);
52+
53+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#include <SoftI2C.h>
2+
3+
void setup() {
4+
scl_pin = 30; //extern variable in SoftI2C.h
5+
sda_pin = 31;
6+
I2CInit();
7+
}
8+
9+
void loop() {
10+
11+
uint8_t ack_bit;
12+
13+
USBSerial_println_s("\nScanning:");
14+
15+
for (uint8_t i = 0; i < 128; i++) {
16+
I2CStart();
17+
ack_bit = I2CSend(i << 1 | 1); //last bit is r(1)/w(0).
18+
I2CStop();
19+
delay(1);
20+
if (ack_bit == 0) {
21+
USBSerial_print_s("I2C got ACK from: 0x");
22+
USBSerial_println_ub(i, HEX);
23+
}
24+
}
25+
delay(1000);
26+
27+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#######################################
2+
# Syntax Coloring Map For SoftI2C
3+
#######################################
4+
5+
#######################################
6+
# Datatypes (KEYWORD1)
7+
#######################################
8+
9+
#######################################
10+
# Methods and Functions (KEYWORD2)
11+
#######################################
12+
13+
14+
#######################################
15+
# Instances (KEYWORD2)
16+
#######################################
17+
18+
19+
20+
#######################################
21+
# Constants (LITERAL1)
22+
#######################################
23+
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
name=SoftI2C
2+
version=1.0
3+
author=Deqing Sun
4+
maintainer=Deqing Sun <[email protected]>
5+
sentence=Enables the communication with devices that use the Inter-Integrated Circuit (I2C) Bus.
6+
paragraph=The Inter-Integrated Circuit (I2C) Protocol is a protocol intended to allow multiple "peripheral" digital integrated circuits ("chips") to communicate with one or more "controller" chips. It only requires two signal wires to exchange information.
7+
category=Communication
8+
url=https://github.com/DeqingSun/ch55xduino
9+
architectures=mcs51
10+
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
/*
2+
* Copyright (c) 2020 by Deqing Sun <[email protected]> (c version for CH552 port)
3+
* Soft I2C library for arduino.
4+
*
5+
* This file is free software; you can redistribute it and/or modify
6+
* it under the terms of either the GNU General Public License version 2
7+
* or the GNU Lesser General Public License version 2.1, both as
8+
* published by the Free Software Foundation.
9+
*/
10+
11+
12+
#include "SoftI2C.h"
13+
14+
__xdata uint8_t scl_pin = 30;
15+
__xdata uint8_t sda_pin = 31;
16+
17+
void I2CInit(){
18+
pinMode(scl_pin, OUTPUT_OD);
19+
pinMode(sda_pin, OUTPUT_OD);
20+
21+
digitalWrite(sda_pin, HIGH);
22+
digitalWrite(scl_pin, HIGH);
23+
}
24+
25+
void I2CStart(){
26+
digitalWrite(sda_pin, LOW);
27+
digitalWrite(scl_pin, LOW);
28+
}
29+
30+
void I2CRestart(){
31+
digitalWrite(sda_pin, HIGH);
32+
digitalWrite(scl_pin, HIGH);
33+
digitalWrite(sda_pin, LOW);
34+
digitalWrite(scl_pin, LOW);
35+
}
36+
37+
void I2CStop(){
38+
digitalWrite(scl_pin, LOW);
39+
digitalWrite(sda_pin, LOW);
40+
digitalWrite(scl_pin, HIGH);
41+
digitalWrite(sda_pin, HIGH);
42+
}
43+
44+
void I2CAck(){
45+
digitalWrite(sda_pin, LOW);
46+
digitalWrite(scl_pin, HIGH);
47+
digitalWrite(scl_pin, LOW);
48+
digitalWrite(sda_pin, HIGH);
49+
}
50+
51+
void I2CNak(){
52+
digitalWrite(sda_pin, HIGH);
53+
digitalWrite(scl_pin, HIGH);
54+
digitalWrite(scl_pin, LOW);
55+
digitalWrite(sda_pin, HIGH);
56+
}
57+
58+
uint8_t I2CSend(uint8_t i2cData){
59+
uint8_t i, ack_bit;
60+
for (i = 0; i < 8; i++) {
61+
if ((i2cData & 0x80) == 0)
62+
digitalWrite(sda_pin, LOW);
63+
else
64+
digitalWrite(sda_pin, HIGH);
65+
digitalWrite(scl_pin, HIGH);
66+
delayMicroseconds(3);
67+
digitalWrite(scl_pin, LOW);
68+
i2cData <<= 1;
69+
}
70+
digitalWrite(sda_pin, HIGH);
71+
digitalWrite(scl_pin, HIGH);
72+
ack_bit = digitalRead(sda_pin);
73+
digitalWrite(scl_pin, LOW);
74+
return ack_bit;
75+
}
76+
77+
uint8_t I2CRead(){
78+
uint8_t i, Data=0;
79+
for (i = 0; i < 8; i++) {
80+
digitalWrite(scl_pin, HIGH);
81+
if(digitalRead(sda_pin))
82+
Data |=1;
83+
if(i<7)
84+
Data<<=1;
85+
digitalWrite(scl_pin, LOW);
86+
}
87+
return Data;
88+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*
2+
* Copyright (c) 2020 by Deqing Sun <[email protected]> (c version for CH552 port)
3+
* Soft I2C library for arduino.
4+
*
5+
* This file is free software; you can redistribute it and/or modify
6+
* it under the terms of either the GNU General Public License version 2
7+
* or the GNU Lesser General Public License version 2.1, both as
8+
* published by the Free Software Foundation.
9+
*/
10+
11+
#ifndef _SOFTI2C_H_INCLUDED
12+
#define _SOFTI2C_H_INCLUDED
13+
14+
#include <Arduino.h>
15+
16+
extern __xdata uint8_t scl_pin;
17+
extern __xdata uint8_t sda_pin;
18+
19+
void I2CInit();
20+
void I2CStart();
21+
void I2CRestart();
22+
void I2CStop();
23+
void I2CAck();
24+
void I2CNak();
25+
uint8_t I2CSend(uint8_t i2cData);
26+
uint8_t I2CRead();
27+
28+
#endif
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#include <WS2812.h>
2+
3+
#define NUM_LEDS 8
4+
#define COLOR_PER_LEDS 3
5+
#define NUM_BYTES (NUM_LEDS*COLOR_PER_LEDS)
6+
7+
#if NUM_BYTES > 255
8+
#error "NUM_BYTES can not be larger than 255."
9+
#endif
10+
11+
__xdata uint8_t ledData[NUM_BYTES];
12+
13+
void setup() {
14+
pinMode(15, OUTPUT); //Possible to use other pins.
15+
}
16+
17+
void loop() {
18+
19+
for (uint8_t i = 0; i < NUM_LEDS; i++) {
20+
set_pixel_for_GRB_LED(ledData, i, 1, 0, 0); //Choose the color order depending on the LED you use.
21+
neopixel_show_P1_5(ledData, NUM_BYTES); //Possible to use other pins.
22+
delay(100);
23+
}
24+
for (uint8_t i = 0; i < NUM_LEDS; i++) {
25+
set_pixel_for_GRB_LED(ledData, i, 0, 1, 0);
26+
neopixel_show_P1_5(ledData, NUM_BYTES);
27+
delay(100);
28+
}
29+
for (uint8_t i = 0; i < NUM_LEDS; i++) {
30+
set_pixel_for_GRB_LED(ledData, i, 0, 0, 1);
31+
neopixel_show_P1_5(ledData, NUM_BYTES);
32+
delay(100);
33+
}
34+
35+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#######################################
2+
# Syntax Coloring Map For WS2812
3+
#######################################
4+
5+
#######################################
6+
# Datatypes (KEYWORD1)
7+
#######################################
8+
9+
#######################################
10+
# Methods and Functions (KEYWORD2)
11+
#######################################
12+
13+
14+
#######################################
15+
# Instances (KEYWORD2)
16+
#######################################
17+
18+
19+
20+
#######################################
21+
# Constants (LITERAL1)
22+
#######################################
23+
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
name=WS2812
2+
version=1.0
3+
author=Deqing Sun
4+
maintainer=Deqing Sun <[email protected]>
5+
sentence=Use WS2812 (NeoPixel or other compatibles) on CH552.
6+
paragraph=Use WS2812 (NeoPixel or other compatibles) on CH552. This version supports 24M clock for 800Khz signal.
7+
category=Display
8+
url=https://github.com/DeqingSun/ch55xduino
9+
architectures=mcs51
10+
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/*
2+
* Copyright (c) 2020 by Deqing Sun <[email protected]> (c version for CH552 port)
3+
* WS2812 library for arduino CH552.
4+
*
5+
* This file is free software; you can redistribute it and/or modify
6+
* it under the terms of either the GNU General Public License version 2
7+
* or the GNU Lesser General Public License version 2.1, both as
8+
* published by the Free Software Foundation.
9+
*/
10+
11+
12+
#include "WS2812.h"
13+

0 commit comments

Comments
 (0)