这个程序是控制PWM的占空比来达到控制直流电机的目地。
以下是Proteus仿真结果
以下是程序
- /*
- * GccApplication4.c
- *
- * Created: 2014-9-26 21:59:42
- * Author: Administrator
- */
- #include <avr/io.h>
- #include <avr/interrupt.h>
- #define uchar unsigned char
- #define uint unsigned int
- #define k0 //PORTD.2 //与按钮连接
- #define k1 //PORTD.3
- uchar a;
- //interrupt [EXT_INT0] void int0_isr(void) //INT0中断函数
- ISR(INT0_vect)
- {
- a=a+20;
- if(a>=255)
- {a=254;}
- }
- //interrupt[EXT_INT1] void int1_isr(void) //INT1减速控制
- ISR(INT1_vect)
- {
- a=a-20;
- if(a<=0)
- {
- a=0;
- }
- }
- int main(void)
- {
- DDRC=0xFF;
- PORTC=0xFF;
- DDRD=0xF3;
- PORTD=0xFF;
- TCCR1A=0x81;
- TCCR1B=0x0A;
- MCUCR=0x0A; //定义INT0和INT1为下降沿时产生中断
- GICR=0xC0; //允许INT0和INT1产生中断
- //#asm("sei")
- sei();
- while(1)
- {
- OCR1A=a;
- }
- }
|