本文介绍arduino如何连接多个74hc165
1 .知识预览介绍程序中的BitSet (关于函数,在C语言中0UL或1UL在什么意义上是使用多个扩展寄存器的移位算法2 .模拟和预览是74hc 165 shift
3.74hc165芯片简介
点击零成本学arduino教程——74hc165扩展寄存器教程了解更多信息
4 .代码介绍开始设定
控制端子
其中uint32_t oldOptionSwitch=0; //previous state of all the inputs保存最后的击键值
时钟宽度
设置波特率和读状态的主函数
数据锁存
延迟微代码(pulse width ); 所有延迟保证数据均锁存
读取按下键值
显示按键值的按下
下次保存当前键值
位读(option switch,I )!=bitread(oldoptionswitch,I ); ReadOne156 ) )函数准备
bitset (函数
bitSet函数的实现只是一个宏定义,是内存消耗比digitalWrite函数少的bitSet函数宏定义
#defineBitset(value,bit ) ) (value ) 1ul ) bit ) )
0UL表示无符号长整数0
1UL表示无符号长整数1
使用bitSet改进代码质量审查
5 .主函数//file : cascade 165.ino//version 1,5 August 2021,bykoepel//initial version.//version 2, 5 August 2021 bykoepel//layoutofthewiringmadebetter.//version 3,13 August 2021, bykoepel//changed ' sck ' to ' clock pin './//cascadeoffour 74hc 165 shift-in registers.//onlythreepinsareusedonthe becauseapulsetothelatchpin//(pl ' on the 74hc 165 ) willmakeanewstarteverytime.//incaseofanerrorawrongclockpulsebynoime itsynchronizesthedatawheninputsarereadthenexttime.//based on ://(1)/demosketchtoreadfroma 744 HC 165 inputshom id=74hc 165 shiftregisterinputexample//byurishaked,https://wok wi.com/arduino/projects/30603138088//tolatchtheinputsintsinted //ichoosethesckpinconstbytedatapin=12; //ichoosethemisopinuint 32 _ toldoptionswitch=0; //previousstateofalltheinputsconstintpulsewidth=10; //pulsewidthinmicrosecondsvoidsetup ((serial.begin ) 115200; serial.println (turnonandofftheswitches ); serial.println (toprowisswitch0(right ) toswitch7) left ); serial.println (secondrowis 8to 15,and so on ); pinmode(clockpin,OUTPUT ); //clock signal,idlelowpinmode(latchpin,OUTPUT ); //latch(copyinputintoRegisters ),idlehighdigitalwrite (latch pin,high ); } void loop ((/giveapulsetotheparallelloadlatchofall 74hc 165 digital write ) latchpin,LOW ); 延迟微代码(pulse width ); 数字写入(latch pin,HIGH ); //reading one 74hc 165 atatimeandcombiningthemintoa 32 bit variable//the last 74hc 165 isatthebottom, buttheswitchesstartnumbering//at the top.sothefirstbytehastobeshiftedintothehighestplace.uint 32 _ toptionswitch=0; for(intI=24; i=0; i-=8)//24/8=4片(Optionswitch|=((uint32_t ) ReadOne165 ) ) ) I; }for(intI=0; i32; I ) if(bitread(Optionswitch,I )!=bitread(oldoptionswitch,I ) (serial.print ) ) switch ); if(I10 ) Serial.print (' ); serial.print(I; serial.print(isnow ); serial.println (bit read (option switch,I )==0? ' down ' : 'up '; } } oldOptionSwitch=optionSwitch; 延迟(25; //slowdownthesketchtoavoidswitchbounce }//thereadone 165 (functionreadsonly 8bit s、 //becauseofthesimilarfunctionsshiftin (and SPI.transfer ) )/whichbothuse 8bit s.//theshiftin ) cannotbeusedhat bechat andtheshiftin (makestheclockhightoreadabit.//the 74hc 165 requiretoreadthengen//thefirstonethatisreadisthehed i=0; I--}{if(digitalread(datapin )==HIGH ) bitset ) ret,I ); 数字写入(clock pin,HIGH ); 延迟微代码(pulse width ); 数字写入(clock pin,LOW ); }return(ret ); (6.结束
不要谈论天分和运气。 需要截稿日和不交稿就打烂狗头的人。 然后,我会对自己的才能感到惊讶。 查理布洛克