求一个红外对管测速的STC89C52单片机的程序~~~~急用~~~小弟跪谢了! 15
红外对管的作用是测量转盘的速度并通过四位八段数码管显示出来。转盘是透明的,其上贴有挡光条,当转盘转动一圈后,挡光条通过红外对管,然后红外对管的信号传送到计数器。数码管每5...
红外对管的作用是测量转盘的速度并通过四位八段数码管显示出来。转盘是透明的,其上贴有挡光条,当转盘转动一圈后,挡光条通过红外对管,然后红外对管的信号传送到计数器。数码管每5秒钟显示一次该5秒内圆盘的平均转速,单位是转/分钟(rpm)。其程序编写使数码管显示5秒内转数×12得到1min的转数,所以其转速总是12的倍数。(和四位八段数码管的显示程序)
展开
展开全部
#include <mega16.h>
#include <delay.h>
#define clr_595_1 PORTB|= (1<<6)
#define clr_595_0 PORTB&=~(1<<6)
#define clk_595_1 PORTB|= (1<<7)
#define clk_595_0 PORTB&=~(1<<7)
#define cp_595_1 PORTB|= (1<<4)
#define cp_595_0 PORTB&=~(1<<4)
#define di_595_1 PORTB|= (1<<5)
#define di_595_0 PORTB&=~(1<<5)
flash unsigned char disp[]={0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F,0x77,0x7C,0x39,0x5E,0x79,0x71,0x00};
unsigned char position[]={0,0,0,0};
unsigned char posit=0;
unsigned int counter=0;
unsigned char state=0;
int n=0;
void show_595(unsigned char c)/////////595显示
{
int w,x;
unsigned char num;
clr_595_1;
num=disp[c]; //num表示c对应的段码
for(w=0;w<8;w++)
{
clk_595_0;
x=num&0x80; //取段码最高位
if(x==0)
{
di_595_0; //打入0
}
else
{
di_595_1;
}
delay_us(2);
num=num<<1; //段码左移一位,取相邻较小位
clk_595_1; //clk上升沿,进行移位
}
cp_595_0;
delay_us(2);
cp_595_1; //cp上升沿,将移位寄存器中数据移入数据寄存器
}
// Timer 0 output compare interrupt service routine
interrupt [TIM0_COMP] void timer0_comp_isr(void)
{
show_595(position[posit]);
PORTB&=0xFC;
PORTB|=posit; //取后三位控制74LS138
if(++posit>=4)
posit=0;
if(PINA.0==1)
{
state=0;
}
if(PINA.0==0&&state<=1)
{
if(n<8)
{
state=1;
n++;
}
if(n>=8)
counter++;
}
}
// Declare your global variables here
void main(void)
{
DDRA=0x00;
PORTA.0=1;
PORTB=0x00;
DDRB=0xFF;
TCCR0=0x0B;
TCNT0=0x00;
OCR0=0x7C;
TIMSK=0x02;
#asm("sei")
while (1)
{
// Place your code here
};
}
程序在这,自己研究吧。
#include <delay.h>
#define clr_595_1 PORTB|= (1<<6)
#define clr_595_0 PORTB&=~(1<<6)
#define clk_595_1 PORTB|= (1<<7)
#define clk_595_0 PORTB&=~(1<<7)
#define cp_595_1 PORTB|= (1<<4)
#define cp_595_0 PORTB&=~(1<<4)
#define di_595_1 PORTB|= (1<<5)
#define di_595_0 PORTB&=~(1<<5)
flash unsigned char disp[]={0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F,0x77,0x7C,0x39,0x5E,0x79,0x71,0x00};
unsigned char position[]={0,0,0,0};
unsigned char posit=0;
unsigned int counter=0;
unsigned char state=0;
int n=0;
void show_595(unsigned char c)/////////595显示
{
int w,x;
unsigned char num;
clr_595_1;
num=disp[c]; //num表示c对应的段码
for(w=0;w<8;w++)
{
clk_595_0;
x=num&0x80; //取段码最高位
if(x==0)
{
di_595_0; //打入0
}
else
{
di_595_1;
}
delay_us(2);
num=num<<1; //段码左移一位,取相邻较小位
clk_595_1; //clk上升沿,进行移位
}
cp_595_0;
delay_us(2);
cp_595_1; //cp上升沿,将移位寄存器中数据移入数据寄存器
}
// Timer 0 output compare interrupt service routine
interrupt [TIM0_COMP] void timer0_comp_isr(void)
{
show_595(position[posit]);
PORTB&=0xFC;
PORTB|=posit; //取后三位控制74LS138
if(++posit>=4)
posit=0;
if(PINA.0==1)
{
state=0;
}
if(PINA.0==0&&state<=1)
{
if(n<8)
{
state=1;
n++;
}
if(n>=8)
counter++;
}
}
// Declare your global variables here
void main(void)
{
DDRA=0x00;
PORTA.0=1;
PORTB=0x00;
DDRB=0xFF;
TCCR0=0x0B;
TCNT0=0x00;
OCR0=0x7C;
TIMSK=0x02;
#asm("sei")
while (1)
{
// Place your code here
};
}
程序在这,自己研究吧。
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询