手頭上攢了一些手機電池不知道干嘛就尋思著做一些小玩意,于是就有了一個主意就是搖搖棒即簡單又好玩,本人喜歡小而巧的東西所以就把這個東西做的很小。
上圖是搖搖棒的原理利用人的視覺暫留結(jié)合單片機控制LED的亮滅就可以在空中顯示字符或者圖標(biāo)。
上圖是電路圖為了把東西做小我選擇了STC15F104單片機它有8個引腳去除VCC和GND還有6個I/O口可以顯示一些簡單的字母和數(shù)字單片機內(nèi)部自帶了EEPROM所以利用它可以存儲每一次顯示的狀態(tài)(每一次的開關(guān)電源就可以切換到下一個顯示的字符)
程序在最后面。
代碼在下面:
#include
#define uchar unsigned char
#define EN_EPROM 0x83; //使能EEPROM
#define S_MODE 0x00; //待機模式
#define R_MODE 0x01; //讀模式
#define W_MODE 0x02; //寫模式
#define F_MODE 0x03; //擦除模式
sfr IAP_DATA = 0xC2;
sfr IAP_ADDRH = 0xC3;
sfr IAP_ADDRL = 0xC4;
sfr IAP_CMD = 0xC5;
sfr IAP_TRIG = 0xC6;
sfr IAP_CONTR = 0xC7;
void init();
uchar num=0,m=0;
uchar read_dat(uchar addr_h,uchar addr_l);
void erase_dat(uchar addr_h,uchar addr_l);
void write_dat(uchar addr_h,uchar addr_l,uchar w_dat);
unsigned char code tab[10][32]={
{
0xe7,0xc3,0xc1,0xe0,0xc1,0xc3,0xe7,0xff,
0xff,0xff,0xff,0xe7,0xc3,0xc1,0xe0,0xc1,
0xc3,0xe7,0xff,0xff,0xff,0xff,0xe7,0xc3,
0xc1,0xe0,0xc1,0xc3,0xe7,0xff,0xff,0xff
},
{
0xff,0xff,0xde,0xc0,0xde,0xff,0xff,0xff,
0xff,0xff,0xff,0xe7,0xc3,0xc1,0xe0,0xc1,
0xc3,0xe7,0xff,0xff,0xff,0xff,0xff,0xc1,
0xfe,0xfe,0xfe,0xfe,0xc1,0xff,0xff,0xff
},
{
0xde,0xed,0xf3,0xf3,0xed,0xde,0xff,0xff,
0xff,0xff,0xff,0xde,0xed,0xf3,0xf3,0xed,
0xde,0xff,0xff,0xff,0xff,0xff,0xff,0xde,
0xed,0xf3,0xf3,0xed,0xde,0xff,0xff,0xff
},
{
0xfb,0xfd,0xd6,0xe8,0xd6,0xfd,0xfb,0xff,
0xff,0xff,0xff,0xfb,0xfd,0xd6,0xe8,0xd6,
0xfd,0xfb,0xff,0xff,0xff,0xff,0xfb,0xfd,
0xd6,0xe8,0xd6,0xfd,0xfb,0xff,0xff,0xff
},
{
0xea,0xd5,0xea,0xd5,0xea,0xd5,0xea,0xd5,
0xea,0xd5,0xea,0xd5,0xea,0xd5,0xea,0xd5,
0xea,0xd5,0xea,0xd5,0xea,0xd5,0xea,0xd5,
0xea,0xd5,0xea,0xd5,0xea,0xd5,0xea,0xd5
},
{
0xfa,0xfd,0xfa,0xff,0xef,0xd7,0xef,0xff,
0xfa,0xfd,0xfa,0xff,0xef,0xd7,0xef,0xff,
0xfa,0xfd,0xfa,0xff,0xef,0xd7,0xef,0xff,
0xfa,0xfd,0xfa,0xff,0xef,0xd7,0xef,0xff
},
{
0xf0,0xed,0xdd,0xed,0xf0,0xff,0xff,0xdf,
0xdf,0xc0,0xdf,0xdf,0xff,0xff,0xc1,0xfe,
0xfe,0xfe,0xc1,0xff,0xff,0xc1,0xfe,0xf1,
0xfe,0xc1,0xff,0xff,0xe1,0xde,0xde,0xe1
},
{
0xc0,0xfb,0xfb,0xfb,0xc0,0xff,0xff,0xff,
0xff,0xff,0xde,0xed,0xf3,0xf3,0xed,0xde,
0xff,0xff,0xff,0xff,0xff,0xff,0xcf,0xf7,
0xf8,0xf7,0xcf,0xff,0xff,0xff,0xff,0xff
},
{
0xc0,0xde,0xd2,0xda,0xda,0xc2,0xfe,0xc0,
0xdf,0xd0,0xd6,0xd6,0xd2,0xde,0xc0,0xff,
0xc0,0xde,0xd2,0xda,0xda,0xc2,0xfe,0xc0,
0xdf,0xd0,0xd6,0xd6,0xd2,0xde,0xc0,0xff
},
{
0xdf,0xef,0xf7,0xfb,0xfd,0xfe,0xfd,0xfb,
0xf7,0xef,0xdf,0xef,0xf7,0xfb,0xfd,0xfe,
0xfd,0xfb,0xf7,0xef,0xdf,0xef,0xf7,0xfb,
0xfd,0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xff
},
};
void delay(uchar xms)
{
char i,j;
for(i=0;i for(j=0;j《50;j++);
}
void main()
{
init();
num=read_dat(0x00,0x01);
++num;
if(num》=11)
{
num=1;
};
erase_dat(0x00,0x01);
write_dat(0x00,0x01,num);
while(1)
{
uchar i=0;
for(i=0;i《32;i++)
{
P3=tab[num-1][i];
delay(100);
}
}
}
void init()
{
IAP_CONTR = EN_EPROM; //把第7位置1允許EEPROM可以讀寫
IAP_CMD = S_MODE; //待機模式無ISP操作
IAP_ADDRH = 0x00;
IAP_ADDRL = 0x00;
}
uchar read_dat(uchar addr_h,uchar addr_l)
{
uchar E_dat = 0x00;
IAP_CMD = R_MODE; //模式選擇為讀EEPROM
IAP_ADDRH = addr_h;
IAP_ADDRL = addr_l;
IAP_TRIG = 0x5A; //使命令有效
IAP_TRIG = 0xA5; //使命令有效
E_dat = IAP_DATA;
return E_dat;
}
void write_dat(uchar addr_h,uchar addr_l,uchar w_dat)
{
IAP_CMD = W_MODE; //模式選擇為寫EEPROM
IAP_ADDRH = addr_h;
IAP_ADDRL = addr_l;
IAP_DATA = w_dat;
IAP_TRIG = 0x5A; //使命令有效
IAP_TRIG = 0xA5; //使命令有效
}
void erase_dat(uchar addr_h,uchar addr_l)
{
IAP_CMD = F_MODE; //模式選擇為擦除EEPROM
IAP_ADDRH = addr_h;
IAP_ADDRL = addr_l;
IAP_TRIG = 0x5A; //使命令有效
IAP_TRIG = 0xA5; //使命令有效
}
-
單片機
+關(guān)注
關(guān)注
6037文章
44558瀏覽量
635354 -
搖搖棒
+關(guān)注
關(guān)注
10文章
61瀏覽量
30895
發(fā)布評論請先 登錄
相關(guān)推薦
評論