RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO| RCC_APB2Periph_GPIOA
| RCC_APB2Periph_TIM1, ENABLE)
void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure
/* TIM pin configuration */
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_9
GPIO_Init(GPIOA, &GPIO_InitStructure)
}
void TIM_Configuration(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure
TIM_OCInitTypeDef TIM_OCInitStructure
/* Time base configuration */
TIM_TimeBaseStructure.TIM_Period =19999
TIM_TimeBaseStructure.TIM_Prescaler = 71
TIM_TimeBaseStructure.TIM_ClockDivision = 0
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up
TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure)
/* TIM1 PWM1 Mode configuration: Channel 1,2*/
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable
TIM_OCInitStructure.TIM_Pulse = 1499
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High
TIM_OC1Init(TIM1, &TIM_OCInitStructure)
TIM_OC2Init(TIM1, &TIM_OCInitStructure)
}
//改变角度500——-90,2500——90
TIM_SetCompare1(TIM1, Compare1);
TIM_SetCompare2(TIM1, Compare1);
//双定时器产生PWM波,控制舵机//定时器0负责置高电平和控制定时器1的开停,定时器1负责置低电平
#include<STC12C5A.H>
#define uchar unsigned char
#define uint unsigned int
sbit PWM=P3^7
uint b=1500//1.5ms 舵机0位
void Delay1ms(uint i) //1ms延时程序
{
uint j
for(i>0i--)
{
for(j=0j<125j++)
{}
}
}
void main()
{
b=1500
TMOD=0X11
TH0=(65536-20000)/256
TL0=(65536-20000)%256
TH1=(65536-b)/256
TL1=(65536-b)%256
ET0=1
EA=1
TR0=1
PWM=1
P1M0=0X0F//P1低4位推挽输出 接L298N直流电机驱动模块 带动后轮两个电机
P1M1=0X00
while(1)
{
P1=0x0a//低4位 1010 正转
b=1500Delay1ms(2000)//直行
b=1000Delay1ms(1000)//左转
b=1500Delay1ms(2000)//直行
b=2000Delay1ms(1000)//右转
b=1500Delay1ms(2000)//直行
P1=0x05 //低4位 0101 反转
b=1500Delay1ms(2000)//后退
b=1000Delay1ms(1000)//后左转
b=1500Delay1ms(2000)//后退
b=2000Delay1ms(1000)//后右转
b=1500Delay1ms(2000)//后退
}
}
void timer0()interrupt 1//定时器0定时20ms
{
TH0=(65536-20000)/256
TL0=(65536-20000)%256
PWM=1
TR1=1
ET1=1
}
void timer1()interrupt 3//定时器1定时1到2ms
{
TH1=(65536-b)/256
TL1=(65536-b)%256
PWM=0
TR1=0
ET1=0
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)