⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 qz__.c

📁 用M16定时器控制舵机,实现两个角度准确停止(可调)
💻 C
字号:
#include <mega16.h>
unsigned int i=0; 
char startposition;
// Declare your global variables here
void delay(int ms)                //延时
{
        int i,j;
	for(i=0;i<ms;i++)
	   {
	   for(j=0;j<1141;j++);
       }
}

void main(void)
{
DDRA.0=0;
PORTD=0x00;
DDRD=0x30; 
PORTD=0x00;
PORTB=0x00;
DDRB.3=1; 
PORTB.3=0;
// Timer/Counter 0 initialization
// Clock source: System Clock
// Clock value: 7.813 kHz
// Mode: Normal top=FFh
// OC0 output: Disconnected
TCCR0=0x05;
TCNT0=startposition;
OCR0=0x00;  

TCCR1A=0x00;
TCCR1B=0x00;
TCNT1H=0x00;
TCNT1L=0x00;
ICR1H=0x00;
ICR1L=0x00;
OCR1AH=0x00;
OCR1AL=0x00;
OCR1BH=0x00;
OCR1BL=0x00;
ASSR=0x00;
TCCR2=0x00;
TCNT2=0x00;
OCR2=0x00;

TIMSK=0x01;

ACSR=0x80;
SFIOR=0x00;

   
// Global enable interrupts
#asm("sei")

while (1)
      {
     

      };
}

// Timer 0 overflow interrupt service routine
interrupt [TIM0_OVF] void timer0_ovf_isr(void)
{
 i++;
   if (i==2)
     {i=0;}
 if (i==0)    
   {TCNT0=startposition;
   PORTB.3=1; }
 else
    {TCNT0=0x73;
   PORTB.3=0; } 
    if   (PINA.0==1)
   {startposition=0xf0;}
  else
   {startposition=0xf8;}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -