52单片机前进后退计数加一程序
时间:10-02
整理:3721RD
点击:
程序在我的小车上已经实现了。亲们可以参考。
#include <reg51.h>
#include<stdlib.h>
unsigned char Tab[10]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};
#define uint unsigned int
unsigned char i=0 ;
unsigned int j=0;
sbit P1=P2^0;
sbit P2=P2^1;
sbit P3=P2^2;
sbit P4=P2^3 ;
sbit M0=P2^4;
sbit M1=P2^5;
sbit M2=P2^6 ;
sbit M3=P2^7;
void delay(void)
{
unsigned int y;
for(y=0;y<300;y++);
}
void xianshi(unsigned int k)
{
P1=1;
P2=1;
P3=1;
P4=0;
P0=Tab[k/1000];
delay();
P4=1;
P3=0;
P0=Tab[(k%1000)/100];
delay();
P3=1;
P2=0;
P0=Tab[(k%100)/10];
delay();
P2=1;
P1=0;
P0=Tab[k%10];
delay();
P1=1;
}
void main()
{
M0=1;
M1=0;
M2=1;
M3=0;
TMOD=0X01;
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
EA=1;
ET0=1;
TR0=1;
while(1)
{
xianshi(j/2);
}
}
void Time(void) interrupt 1
{
TR0=0;
i++;
if(i==160)
{
M0=~M0;
M1=~M1;
M2=~M2;
M3=~M3;
j++;
i=0;
}
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
TR0=1;
}
#include <reg51.h>
#include<stdlib.h>
unsigned char Tab[10]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};
#define uint unsigned int
unsigned char i=0 ;
unsigned int j=0;
sbit P1=P2^0;
sbit P2=P2^1;
sbit P3=P2^2;
sbit P4=P2^3 ;
sbit M0=P2^4;
sbit M1=P2^5;
sbit M2=P2^6 ;
sbit M3=P2^7;
void delay(void)
{
unsigned int y;
for(y=0;y<300;y++);
}
void xianshi(unsigned int k)
{
P1=1;
P2=1;
P3=1;
P4=0;
P0=Tab[k/1000];
delay();
P4=1;
P3=0;
P0=Tab[(k%1000)/100];
delay();
P3=1;
P2=0;
P0=Tab[(k%100)/10];
delay();
P2=1;
P1=0;
P0=Tab[k%10];
delay();
P1=1;
}
void main()
{
M0=1;
M1=0;
M2=1;
M3=0;
TMOD=0X01;
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
EA=1;
ET0=1;
TR0=1;
while(1)
{
xianshi(j/2);
}
}
void Time(void) interrupt 1
{
TR0=0;
i++;
if(i==160)
{
M0=~M0;
M1=~M1;
M2=~M2;
M3=~M3;
j++;
i=0;
}
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
TR0=1;
}
其中2.4引脚到2.7引脚是控制的电机。所用的是直流电机
2.0到2.3是单片机上控制数码管的、
求加好友,求教师兄QQ565661477
。这个是很简单的