PIOC-PIOC参考应用例程使用说明
CH32X035 芯片 PIOC 参考应用例程使用说明
引言:
CH32X035 芯片内,嵌入了一个可编程协议 I/O 微控制器 PIOC,即 eMCU,该 eMCU 基于单时钟周期精简指令集的 RISC8B 内核,运行于系统主频,具有 2K 指令的程序 ROM 和 49 个 SFR 寄存器及 PWM 定时/计数器,支持 2 个 I/O 引脚的协议控制。
在 EVT 中,我们提供了 PIOC 的参考工程,这里仅以 WS2812 为例,简单介绍代码结构及实际使用方法。
代码结构:
在参考工程中,我们需要关注 User 文件夹中 main.c 与 RGB1W.c 两个文件。
RGB1W.c 文件中,PIOC_1W_CODE 数组,为使用汇编语言实现的 PIOC 操作 DS1820 与 WS2812 两种器件编译后的二进制数据;RGB1W_Init 函数用于初始化 PIOC 功能,对于具有 PC7 引脚的 X035 芯片可以在此进行选择。
const unsigned char PIOC_1W_CODE[] ={ 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x16,0x60,0x00,0x00,0x00,0x00,0x00,0x00, /* .........`...... */ 0x00,0x00,0x0A,0x70,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* ...p............ */ 0x00,0x00,0x00,0x00,0x00,0x00,0x30,0x00,0x00,0x00,0x00,0x00,0x1C,0x5E,0x1C,0x47, /* ......0......^.G */ 0x1C,0x5D,0x16,0x60,0x1C,0x47,0x1E,0x02,0x09,0x10,0x31,0xC1,0x3E,0xC2,0x34,0xC3, /* .].`.G....1.>.4. */ 0x2F,0x80,0x20,0x2F,0x41,0x38,0x87,0x2F,0x2F,0x38,0xFC,0x2F,0x93,0x38,0x00,0x00, /* ....A8...8...8.. */ 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x28, /* ...............( */ 0x8E,0x60,0x00,0x00,0x2C,0x71,0x8E,0x60,0x00,0x00,0x2C,0x71,0x8E,0x30,0x4B,0x24, /* .`..,q.`..,q.0K$ */ 0x28,0x22,0xE9,0x70,0x04,0x15,0x39,0x30,0x09,0x15,0x38,0x30,0x00,0x00,0x38,0x71, /* (".p..90..80..8q */ 0x8E,0x60,0x00,0x00,0x08,0x01,0x0B,0x40,0x0A,0x48,0x0C,0x01,0x06,0x28,0x0B,0x54, /* .`.....@.H...(.T */ 0x8E,0x60,0x09,0x02,0x04,0x10,0x20,0x24,0x00,0x00,0x0B,0x48,0x0B,0x70,0x01,0x5F, /* .`.....$...H.p._ */ 0x0B,0x40,0x08,0x70,0x0B,0x40,0x0A,0x70,0x0B,0x48,0x0B,0x70,0x01,0x5E,0x0B,0x40, /* .@.p.@.p.H.p.^.@ */ 0x08,0x70,0x0B,0x40,0x0A,0x70,0x0B,0x48,0x0B,0x70,0x01,0x5D,0x0B,0x40,0x08,0x70, /* .p.@.p.H.p.].@.p */ 0x0B,0x40,0x0A,0x70,0x0B,0x48,0x0B,0x70,0x01,0x5C,0x0B,0x40,0x08,0x70,0x0B,0x40, /* .@.p.H.p.\.@.p.@ */ 0x0A,0x70,0x0B,0x48,0x0B,0x70,0x01,0x5B,0x0B,0x40,0x08,0x70,0x0B,0x40,0x0A,0x70, /* .p.H.p.[.@.p.@.p */ 0x0B,0x48,0x0B,0x70,0x01,0x5A,0x0B,0x40,0x08,0x70,0x0B,0x40,0x0A,0x70,0x0B,0x48, /* .H.p.Z.@.p.@.p.H */ 0x0B,0x70,0x01,0x59,0x0B,0x40,0x08,0x70,0x0B,0x40,0x0A,0x70,0x0B,0x48,0x0B,0x70, /* .p.Y.@.p.@.p.H.p */ 0x01,0x58,0x0B,0x40,0x09,0x70,0x01,0x02,0x0B,0x40,0x0D,0x70,0x04,0x15,0x4D,0x30, /* .X.@.p...@.p..M0 */ 0x1E,0x28,0xEE,0x70,0xEE,0x70,0x04,0x00,0x0B,0x54,0x04,0x28,0x1D,0x10,0x1C,0x4F, /* .(.p.p...T.(...O */ 0x16,0x60,0x02,0x28,0x8E,0x60,0x00,0x00,0x08,0x01,0x0B,0x40,0x80,0x28,0x0C,0x10, /* .`.(.`.....@.(.. */ 0x20,0x02,0x21,0x0A,0x91,0x34,0x0A,0x48,0x09,0x47,0x3E,0x01,0x04,0x01,0x06,0x28, /* ..!..4.H.G>....( */ 0x0B,0x54,0x8E,0x60,0x02,0x28,0x3F,0x10,0x18,0x00,0x1F,0x10,0x80,0x29,0x09,0x0A, /* .T.`.(?......).. */ 0x00,0x00,0x08,0x10,0x13,0x00,0xAE,0x00,0x13,0x00,0xAD,0x00,0x13,0x00,0xAC,0x00, /* ................ */ 0x13,0x00,0xAB,0x00,0x13,0x00,0xAA,0x00,0x13,0x00,0x20,0x15,0x20,0x04,0x03,0x52, /* ...............R */ 0x21,0x15,0xA9,0x00,0x13,0x00,0x20,0x02,0x21,0x0A,0xE3,0x34,0xA8,0x00,0x13,0x00, /* !.......!..4.... */ 0x04,0x02,0x1F,0x10,0xAF,0x00,0x13,0x00,0xAE,0x00,0x13,0x00,0xAD,0x00,0x13,0x00, /* ................ */ 0xAC,0x00,0x13,0x00,0x20,0x15,0x20,0x04,0x03,0x52,0x21,0x15,0xAB,0x00,0x13,0x00, /* .........R!..... */ 0x3E,0x14,0x03,0x52,0x3F,0x14,0xAA,0x00,0x13,0x00,0x3E,0x02,0x04,0x10,0xA9,0x00, /* >..R?.....>..... */ 0x13,0x00,0x20,0x02,0x21,0x0A,0xE3,0x34,0x3F,0x02,0xA8,0x00,0x13,0x00,0x18,0x00, /* ....!..4?....... */ 0x1F,0x10,0xAF,0x00,0xAA,0x60,0xA8,0x00,0x13,0x00,0x08,0x47,0x12,0x00,0x08,0x01, /* .....`.....G.... */ 0x88,0x60,0xFA,0x28,0xEE,0x60,0x41,0x28,0xEE,0x60,0x02,0x28,0x0D,0x70,0x00,0x00, /* .`.(.`A(.`.(.p.. */ 0x0D,0x70,0x00,0x00,0x0D,0x70,0x00,0x00,0x0D,0x70,0x00,0x00,0xFF,0x2C,0x00,0x00, /* .p...p...p...,.. */ 0xEE,0x30,0x30,0x00,0x0A,0x40,0x08,0x01,0x0C,0x01,0x05,0x28,0xEE,0x70,0x0B,0x40, /* .00..@.....(.p.@ */ 0x0A,0x48,0xE9,0x70,0xE9,0x70,0x0A,0x40,0xEB,0x70,0x09,0x01,0x0B,0x54,0x04,0x24, /* .H.p.p.@.p...T.$ */ 0xE9,0x70,0x09,0x02,0x30,0x00,0x09,0x10,0x08,0x22,0x0B,0x40,0x0A,0x48,0xED,0x70, /* .p..0....".@.H.p */ 0x09,0x50,0x0A,0x40,0xEB,0x70,0x0A,0x40,0x05,0x28,0x09,0x58,0xEE,0x70,0x09,0x1F, /* .P.@.p.@.(.X.p.. */ 0x04,0x15,0x0D,0x31,0x30,0x00,0x08,0x22,0x0B,0x40,0x0A,0x48,0xED,0x70,0x0A,0x40, /* ...10..".@.H.p.@ */ 0x0A,0x28,0xEE,0x70,0x09,0x1F,0x09,0x47,0x0B,0x54,0x09,0x4F,0x37,0x28,0xEE,0x70, /* .(.p...G.T.O7(.p */ 0x04,0x15,0x1C,0x31,0x09,0x02,0x30,0x00,0xFA,0x70,0x37,0x31,0xCC,0x28,0x0B,0x71, /* ...1..0..p71.(.q */ 0x44,0x28,0x0B,0x71,0x1C,0x5C,0x36,0x61,0x0B,0x48,0x0A,0x48,0x04,0x00,0x30,0x00, /* D(.q.\6a.H.H..0. */ 0xFA,0x70,0x43,0x31,0xCC,0x28,0x0B,0x71,0xBE,0x28,0x0B,0x71,0x1B,0x71,0x20,0x10, /* .pC1.(.q.(.q.q.. */ 0x1B,0x71,0x21,0x10,0xFA,0x70,0x30,0x00,0x00,0x00}; /* .q!..p0... */
main.c 文件中,RGBpbuf 数组,按 GRB 的顺序,保存了每颗灯珠的每种颜色所需显示的亮度信息;宏定义 rgb_data_bytes 使用 sizeof 函数,根据 RGBpbuf 数组的大小,自动获取所需驱动的灯珠个数。在 while 循环中,根据标志位的转换,使用 RGB1W_SendRAM 函数,通过 PIOC 引脚,将三个 RGBpbuf 数组循环发送,以此实现灯带的动态效果。
1. u8 RGBpbuf[] = {
2. 0x00, 0xFF, 0x00,
3. 0x00, 0x00, 0xFF,
4. 0xFF, 0x00, 0x00,
5. 0x00, 0xFF, 0x00,
6. 0x00, 0x00, 0xFF,
7. 0xFF, 0x00, 0x00,
8. 0x00, 0xFF, 0x00,
9. 0x00, 0x00, 0xFF,
10. 0xFF, 0x00, 0x00,
11. 0x00, 0xFF, 0x00,
12. };
13.
14. #define rgb_data_bytes sizeof(RGBpbuf)
使用方法:
以 X035F8U6 芯片为例,PIOC 占用 PC18 引脚,因此在初始化的过程中,关闭了芯片的 SWD 功能。这一操作将导致再次使用 SWD 接口下载时出现失败的问题,可以在下载前,使用 MRS 或 Utility 中的代码擦除功能,抹除片内 Flash 的全部数据,或使用 PC17 引脚,在复位后触发单片机跳转至 BOOT 区域,在其超时退出前(即未执行用户区代码前)使用 SWD 接口完成固件的更新。
RGBpbuf 数组,保存了 WS2812 灯珠的亮度信息,以蓝色为例,如果需要调整灯珠的发光强度,应在对应灯珠蓝色数据所在的位置,填入 0x00~0xFF 范围内的数值(十进制0~255);如果需要混光,可以首先确定某一颜色对应的 RGB 亮度值,再将数值转换为十六进制写入数组。在参考应用例程的基础上,可以根据实际的需求,灵活运用 RGB1W_SendRAM 函数,实现最大1024颗 WS2812 的动态刷新。