这个例程是控制伺服电动机角度控制的。通过外部中断0和外部中断1来加减一个变量来实现。
以下是程序
- /*
- * GccApplication3.c
- *
- * Created: 2014-9-25 19:12:03
- * Author: Administrator
- */
- #include <avr/io.h>
- #include <avr/interrupt.h>
- #define uchar unsigned char
- #define uint unsigned int
- #define c1 0//PORTB.0
- #define c2 1//PORTB.1
- #define c3 2//PORTB.2
- #define c4 3//PORTB.3
- uchar tab[]={0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8, //共阳极LED 0~F的段码
- 0x80,0x90,0x88,0x83,0xC6,0xA1,0x86,0x8E,0xBF,};
- uchar a=62;
- uchar i,i_temp1,i_temp2,i_temp3;
- void delayms(void) //LED数码管切换时间函数
- {
- uint j;
- for(j=0;j<100;j++);
- }
- //interrupt[EXT_INT0] void int0_isr(void)
- ISR(INT0_vect)
- {
- a=a+1;
- i=i+3;
- if(a>=160)
- {
- a=160;
- i=180;
- }
- }
- //interrupt[EXT_INT1] void int1_isr(void)
- ISR(INT1_vect)
- {
- if(a<=32)
- {
- a=32;
- i=0;
- }
- else
- {
- a=a-1;
- i=i-3;
- }
- }
- void display(void)
- {
- i_temp1=i/100;
- i_temp2=(i-i_temp1*100)/10;
- i_temp3=(i-i_temp1*100)%10;
- //c2=1;
- PORTB |= c2;
- PORTC=tab[i_temp1];
- delayms();
- //c2=0;
- PORTB &= ~c2;
- //c3=1;
- PORTB |= c3;
- PORTC=tab[i_temp2];
- delayms();
- //c3=0;
- PORTB &= ~c3;
- //c4=1;
- PORTB |=c4;
- PORTC=tab[i_temp3];
- delayms();
- //c4=0;
- PORTB &= ~c4;
- //c1=0;
- PORTB &= ~c1;
- }
- int main(void)
- {
- DDRB=0xFF;
- PORTB=0xFF;
- DDRC=0xFF;
- PORTC=0xFF;
- DDRD=0xF3;
- PORTD=0xFF;
- TCCR1A=0x83; //10位快速PWM,比较匹配时清零OC1A(输出低电平),64分频
- TCCR1B=0x03;
- MCUCR=0x0A; //定义INT0和INT1为下降沿时产生中断
- GICR=0xC0; //允许INT0和INT1产生中断
- //#asm("sei") //开启总中断
- sei();
- while(1)
- {
- OCR1AH=0;
- OCR1AL=a;
- display();
- }
- }
|