软件产生PWM控制直流电机C程序
扫描二维码
随时随地手机看文章
//软件产生PWM控制直流电机C程序
//软件版本: ICCAVR ver 7.05
// Target : M48
// Crystal: 8.0000MHz
#include
#include
unsigned int Pwmw;
unsigned int adc_v;
//ADC 转换函数
unsigned int adc_get(void)
{
ADCSRA |= (1
//电机驱动、调速子程序
void PWMsub(unsigned int Pwmh)
{
unsigned int i;
unsigned int Pwml;
Pwml = Pwmw - Pwmh;
PORTD |= 0x10; //高电平
for (i = 0;i < Pwmh;i++)
;
PORTD = 0x00; //低电平
for (i = 0;i < Pwml;i++)
;
}
//主函数
void main()
{
PORTD = 0x00; //PD端口置0
DDRD = 0xF0; //PD7,、PD6、PD5、PD4输出
ADCSRA = 0x00; //设置前,先关闭ADC
ADMUX = (1<
ADCSRA = (1
{
adc_v = adc_get(); //ADC转换
PWMsub(adc_v);
}
}