步进电机数码管显示转动代码
时间: 2023-12-20 17:30:10 浏览: 78
以下是步进电机数码管显示转动的代码示例:
```c
#include <reg52.h>
#include <intrins.h>
#define uint unsigned int
#define uchar unsigned char
sbit LSA=P2^2;
sbit LSB=P2^3;
sbit LSC=P2^4;
sbit EN=P2^5;
sbit RW=P2^6;
sbit RS=P2^7;
sbit key=P3^4;
uchar code table[]={
0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71
};
void delay(uint x)
{
uint i,j;
for(i=x;i>0;i--)
for(j=110;j>0;j--);
}
void write_com(uchar com)
{
RW=0;
RS=0;
P0=com;
delay(5);
EN=1;
_nop_(); _nop_();
EN=0;
}
void write_data(uchar dat)
{
RW=0;
RS=1;
P0=dat;
delay(5);
EN=1;
_nop_();
_nop_();
EN=0;
}
void init()
{
write_com(0x38);
write_com(0x0c);
write_com(0x06);
write_com(0x01);
}
void display(uchar num,uchar speed)
{
uchar i;
for(i=0;i<speed;i++)
{
LSA=table[num/1000];
LSB=table[num%1000/100];
LSC=table[num%100/10];
write_data(table[num%10]);
delay(1000);
write_com(0x01);
}
}
void main()
{
uchar speed=10;
uint num=0;
init();
while(1)
{
display(num,speed);
num++;
if(num==10000)
num=0;
}
}
```
阅读全文