安装了OPENWRT系统的AR9331模块,打开配置网页的时候,发现时钟走的很准,使用的是网络时间;于是有了这个想法,把TM1637数码管模块连接到AR9331模块的2个IO口,用模拟IIC的方法驱动TM1637来显示时间;上IO连接的电路图和脚本代码;
脚本
#9-1
#/root/TM1637/gpio_init.sh
#!/bin/sh
echo "22" > /sys/class/gpio/export
echo "23" > /sys/class/gpio/export
echo "out" > /sys/class/gpio/gpio22/direction
echo "out" > /sys/class/gpio/gpio23/direction
#9-2
#/root/TM1637/IIC_Start.sh
#!/bin/sh
echo "out" > /sys/class/gpio/gpio22/direction
echo "1" > /sys/class/gpio/gpio22/value #sda
echo "1" > /sys/class/gpio/gpio23/value #clk
echo "0" > /sys/class/gpio/gpio22/value #sda
echo "0" > /sys/class/gpio/gpio23/value #clk
#9-3
#/root/TM1637/IIC_Stop.sh
#!/bin/sh
echo "out" > /sys/class/gpio/gpio22/direction
echo "0" > /sys/class/gpio/gpio23/value #clk=1
echo "0" > /sys/class/gpio/gpio22/value #sda=0
echo "1" > /sys/class/gpio/gpio23/value #clk
echo "1" > /sys/class/gpio/gpio22/value #sda
#9-4
#/root/TM1637/IIC_Wait_Ack.sh
#!/bin/sh
timeout=1
echo in > /sys/class/gpio/gpio22/direction
echo "1" > /sys/class/gpio/gpio23/value #clk
while [ $(cat /sys/class/gpio/gpio22/value) -eq 1 ]
do
let timeout+=1
if [ $timeout -gt 100 ]
then
/root/TM1637/IIC_Stop.sh
exit 0
fi
done
echo "0" > /sys/class/gpio/gpio23/value #clk
#9-5
#/root/TM1637/IIC_Send_Byte_addr.sh
#!/bin/sh
echo "out" > /sys/class/gpio/gpio22/direction
echo "0" > /sys/class/gpio/gpio23/value #clk
addr=`cat /tmp/addr`
for i in $(seq 1 8)
do
out=$(($addr&0x01))
addr=$(($addr>>1))
echo $out > /sys/class/gpio/gpio22/value #sda
echo "1" > /sys/class/gpio/gpio23/value #clk
echo "0" > /sys/class/gpio/gpio23/value #clk
done
/root/TM1637/IIC_Wait_Ack.sh
#9-6
#/root/TM1637/IIC_Send_Byte_data.sh
#!/bin/sh
echo "out" > /sys/class/gpio/gpio22/direction
echo "0" > /sys/class/gpio/gpio23/value #clk
data=`cat /tmp/data`
for i in $(seq 1 8)
do
out=$(($data&0x01))
data=$(($data>>1))
echo $out > /sys/class/gpio/gpio22/value #sda
echo "1" > /sys/class/gpio/gpio23/value #clk
echo "0" > /sys/class/gpio/gpio23/value #clk
done
/root/TM1637/IIC_Wait_Ack.sh
#9-7
#/root/TM1637/TM1637_writeCMD.sh
#!/bin/sh
/root/TM1637/IIC_Start.sh
/root/TM1637/IIC_Send_Byte_data.sh
/root/TM1637/IIC_Stop.sh
#9-8
#/root/TM1637/TM1637_writeData.sh
#!/bin/sh
/root/TM1637/IIC_Start.sh
/root/TM1637/IIC_Send_Byte_addr.sh
/root/TM1637/IIC_Send_Byte_data.sh
/root/TM1637/IIC_Stop.sh
#9-9
#/root/TM1637/TM1637_display.sh
#!/bin/sh
eval s0=63
eval s1=6
eval s2=91
eval s3=79
eval s4=102
eval s5=109
eval s6=125
eval s7=7
eval s8=127
eval s9=111
eval s10=119
eval s11=124
eval s12=57
eval s13=94
eval s14=121
eval s15=113
time=`date +%H%M`
H10=`echo $time | cut -c 1`
H10=$(eval echo "$s$H10")
H01=`echo $time | cut -c 2`
H01=$(eval echo "$s$H01")
let H01+=128
M10=`echo $time | cut -c 3`
M10=$(eval echo "$s$M10")
M01=`echo $time | cut -c 4`
M01=$(eval echo "$s$M01")
echo "68" > /tmp/data
/root/TM1637/TM1637_writeCMD.sh
echo "192" > /tmp/addr
echo $H10 > /tmp/data
/root/TM1637/TM1637_writeData.sh
echo "193" > /tmp/addr
echo $H01 > /tmp/data
/root/TM1637/TM1637_writeData.sh
echo "194" > /tmp/addr
echo $M10 > /tmp/data
/root/TM1637/TM1637_writeData.sh
echo "195" > /tmp/addr
echo $M01 > /tmp/data
/root/TM1637/TM1637_writeData.sh
echo "139" > /tmp/data
/root/TM1637/TM1637_writeCMD.sh
############
使用
1、把9-1的脚本设置开机启动
2、开启一个定时任务,每一分钟运行一次 /root/TM1637/TM1637_display.sh
2-1 定时任务使用crontab ;在终端输入crontab -e 在vi编辑模式下输入 */1 * * * * /root/TM1637/TM1637_display.sh 保存退出
2-2 使能开机启动 /etc/init.d/cron enable
2-3 运行 /etc/init.d/cron start
到此TM1637模块的数码管每隔1分钟刷新1次显示;
QQ群