use ieee.std_logic_1164.all
use ieee.std_logic_arith.all
use ieee.std_logic_unsigned.allentity yan is
port(clk:in std_logic
po:out std_logic)
end yanarchitecture zheng of yan is
signal co:integer range 0 to 1000000
begin
process(clk)
if clk'event and clk='1' then
co<=co+1
if co<75000 then
po<='1'
else
po<='0'
end if
end if
end process
end zheng
//12MHz#include <reg51.h>
void InitTimer0(void)
{
TMOD = 0x01
TH0 = 0x0B1
TL0 = 0x0E0
EA = 1
ET0 = 1
TR0 = 1
}
void delay(void) //误差 0us 延时1ms 此处可以修改高电平周期
//修改此处的延时可以更改舵机转的角度 ,45度具体是多少 你可以试试
{
unsigned char a,b,c
for(c=1c>0c--)
for(b=142b>0b--)
for(a=2a>0a--)
}
void main(void)
{
InitTimer0()
P1_2=0
while(1)
}
void Timer0Interrupt(void) interrupt 1
{
//20ms中断
TH0 = 0x0B1
TL0 = 0x0E0
P1_2=1
delay()
P1_2=0
}
#include<reg52.h>#define uint unsigned int
#define uchar unsigned char
uint count
uchar jd
sbit le1=P1^0//*光电传感器*//
sbit PWM=P3^5//舵机pwm//
init()
{
TMOD=0x01//设定定时器
TH0 = 0xFE
TL0 = 0x0c//设定定时初始值
EA=1
ET0=1
TR0=1
}
void delay(uint z) //延时
{
uint x,y
for(x=zx>0x--)
for(y=125y>0y--)
}
void timer0() interrupt 1 //产生pwm信号控制舵机,周期20ms
{
TH0 = 0xFE
TL0 = 0x0c //设定定时初始值
if(count<jd) //判断0.5ms次数是否小于角度标识
{
PWM=1 //确定小于,pwm输出高电平
// delay(180) //延时一会,可以减慢舵机转速
}
else
PWM=0 //大于则输出低电平
count=count+1 //0.5ms次数加1
count=count%40 //次数始终保持为40即保持周期为20ms
}
void xunji()//循迹函数,读取光电管状态
{
if(le1!=0)
{
delay(10)
if(le1==0)
{
jd++
count=0
while(le1==0)
}
} //分析光电管状态,看你的电路检测到黑线输出1或者是0
else
{
count=0
jd=3 //舵机归中
}
}
void main()
{
jd=3 //角度初始化90°: 1=0.5ms 舵机为0° 2=1ms 舵机为45°3、4、5、同上
count=0 //初始化赋值零
init() //定时器初始化
while(1)
{
xunji() //舵机检测
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)