在线时间1258 小时
UID3174487
注册时间2015-10-31
NXP金币2015
TA的每日心情 | 开心 2025-5-29 08:54 |
---|
签到天数: 2992 天 连续签到: 8 天 [LV.Master]伴坛终老
金牌会员
 
- 积分
- 12035
- 最后登录
- 2025-5-29
|
在mbed平台下对GPIO的操作还是相对简单的,如果你要将一个GPIO口作为输出只需要像下边实例化一个对象,然后赋值0或者1就可以了。
- DigitalOut led(LED1);
- led = 0;
- led = 1;
复制代码 或者要将一组GPIO口作为输出还可以使用BusOut类,最多支持操作16个GPIO口。
- BusOut(PinName p0, PinName p1 = NC, PinName p2 = NC, PinName p3 = NC,
- PinName p4 = NC, PinName p5 = NC, PinName p6 = NC, PinName p7 = NC,
- PinName p8 = NC, PinName p9 = NC, PinName p10 = NC, PinName p11 = NC,
- PinName p12 = NC, PinName p13 = NC, PinName p14 = NC, PinName p15 = NC);
复制代码 这里的演示我用4片74HC595拓展出来32个IO口,这样只需要3个IO口就可以操作4个TIL311了,使用起来非常方便。
操作非常简单,直接上代码。
- #include "mbed.h"
- /*-------------------------data format-------------------------------
- TIL311 3 2 1 0
- Left Decimal LP OFF == HIGH ON == LOW
- Right Decimal RP OFF == HIGH ON == LOW
- Blanking input BI BI == HIGH the display is blank BI == LOW display as determined by the data in the latches.
- data0 == D1 C1 B1 A1 D0 C0 B0 A0
- data1 == D3 C3 B3 A3 D2 C2 B2 A2
- data2 == RP3 LP3 RP2 LP2 RP1 LP1 RP0 LP0
- data3 == BI3 BI2 BI1 BI0 X X X X
- -------------------------------------------------------------------*/
- DigitalOut HC595_DS(P0_11);
- DigitalOut HC595_ST_CP(P0_23);
- DigitalOut HC595_SH_CP(P0_12);
- void HC595SendData_four(char data0, char data1, char data2, char data3);
- int main(){
- HC595_DS = 0;
- HC595_SH_CP = 0;
- HC595_ST_CP = 0;
- HC595SendData_four(0xff, 0xff, 0x00,0x00);
- wait_ms(2000);
-
- while(1){
- for(int i = 0; i < 16; i++){
- HC595SendData_four(i+(i<<4), i+(i<<4), 0xff, 0x00);
- wait_ms(500);
- }
- }
- }
- void HC595SendData_four(char data0, char data1, char data2, char data3){
- int i = 0;
- for (i = 0; i < 8; i++){
- if (data0 & 0x80) HC595_DS=1;
- else HC595_DS=0;
- data0 <<= 1;
- HC595_SH_CP=1; HC595_SH_CP=0;
- }
- for (i = 0; i < 8; i++){
- if (data1 & 0x80) HC595_DS=1;
- else HC595_DS=0;
- data1 <<= 1;
- HC595_SH_CP=1; HC595_SH_CP=0;
- }
- for (i = 0; i < 8; i++){
- if (data2 & 0x80) HC595_DS=1;
- else HC595_DS=0;
- data2 <<= 1;
- HC595_SH_CP=1; HC595_SH_CP=0;
- }
- for (i = 0; i < 8; i++){
- if (data3 & 0x80) HC595_DS=1;
- else HC595_DS=0;
- data3 <<= 1;
- HC595_SH_CP=1; HC595_SH_CP=0;
- }
- HC595_ST_CP=1;
- HC595_ST_CP=0;
- }
复制代码 上两张图片看看效果。
|
|