设计与开发

单片机摇摇棒 51单片机:自制迷你摇摇棒(附代码)

小编 2024-11-24 设计与开发 23 0

51单片机:自制迷你摇摇棒(附代码)

手头上攒了一些手机电池不知道干嘛就寻思着做一些小玩意,于是就有了一个主意就是摇摇棒即简单又好玩,本人喜欢小而巧的东西所以就把这个东西做的很小。

上图是摇摇棒的原理利用人的视觉暂留结合单片机控制LED的亮灭就可以在空中显示字符或者图标。

上图是电路图为了把东西做小我选择了STC15F104单片机它有8个引脚去除VCC和GND还有6个I/O口可以显示一些简单的字母和数字单片机内部自带了EEPROM所以利用它可以存储每一次显示的状态(每一次的开关电源就可以切换到下一个显示的字符)程序在最后面。

代码在下面:#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;

//使命令有效

}

​​若代码出现错误可以通过https://www.xinmanduo.com/m/view.php?aid=139706访问复制

文章素材来自互联网​​​

LED摇摇棒原理讲解

视频加载中...

相关问答

单片机 技术是一门即将被淘汰的技术吗?

感谢邀请答题!目前来说,这十几年内是不可能淘汰的,即使面临着人工AI、大数据和云计算等技术的多方面竞争,单片机也可以说是立于不败之地!毕竟需求放在那里。...

如何解决锂电池、控制器和电机之间的匹配问题?

😚😏真巧,能具备锂电池,电机,控制器的产品的话,目前最火爆的几款小家电。果汁杯,摇摇杯。吸尘器,扫地机。还有一种电热水壶。以上几款产品都会用到电池,...

猜你喜欢