#include "ioCC2530.h" #include "../lib/delay.h" unsigned char seg7table[16] = { /* 0 1 2 3 4 5 6 7*/ 0xc0, 0xf9, 0xa4, 0xb0, 0x99, 0x92, 0x82, 0xf8, /* 8 9 A B C D E F*/ 0x80, 0x90, 0x88, 0x83, 0xc6, 0xa1, 0x86, 0x8e }; unsigned char i; void SegInit() { P1INP &= ~( 0x1 << 2 ); //P1.2上拉 P1SEL &= ~( ( 1 << 0 ) | ( 1 << 1 ) | ( 1 << 2 ) ); //P1.0,P1.1,P1.2为GPIO P1DIR |= ( 1 << 0 ) | ( 1 << 1 ) | ( 1 << 2 ); //P1.0,P1.1,P1.2为OUTPUT P0SEL &= ~( 1 << 4 ); //P0.4为GPIO P0DIR |= ( 1 << 4 ); //P0.4为OUTPUT //74HC595初始化 P1 |= ( 0x1 << 0 ); //P1.0 CLR, 复位 P1 |= ( 0x1 << 1 ); //P1.1 DATA,数据寄存器 P1 |= ( 0x1 << 2 ); //P1.2 CLK,位移寄存器时钟输入 P0 |= ( 0x1 << 4 ); //P0.4 LOCK,数据寄存器开始时钟 } void SendSeg7( unsigned char value ) { unsigned char i; P0 &= ~( 0x1 << 4 ); //P0.4 LOCK P1 &= ~( 0x1 << 2 ); //P1.2 CLK P1 &= ~( 0x1 << 1 ); //P1.1 DATA //将数据value的8个bit按串行方式分时赋值P1.1,P1.2控制时钟值高时读取P1.1 for( i = 0; i < 8; i++ ) { if( value & 0x80 ) { P1 |= ( 0x1 << 1 ); //P1.1值1,DATA } else { P1 &= ~( 0x1 << 1 ); //P1.1值0,DATA } value <<= 1; P1 &= ~( 0x1 << 2 ); //P1.2 CLK P1 |= ( 0x1 << 2 ); //P1.2 CLK } P0 &= ~( 0x1 << 4 ); //P0.4 LOCK P0 |= ( 0x1 << 4 ); //P0.4 LOCK } void main( void ) { SegInit(); while( 1 ) { for( int i = 0; i < 0x10; ++i ) { SendSeg7( seg7table[i] ); //循环显示0-F delay( 2000 ); } SendSeg7( 0xff ); delay( 200 ); } }