sivo.c

来自「avr常用功能程序中包含了pwm、adc、中断、定时、i2c等各程序可供开发者直」· C语言 代码 · 共 62 行

C
62
字号
#include <avr/io.h>
#include <avr/signal.h>
#define uchar unsigned char
#define uint unsigned int
//the number of the "~" ;how_long<=1023

uint a,b;
unsigned int howlong=0;
void delay(unsigned int t)
{
    for(;t>0;t--);
}
void init()
{
    DDRB=0XFF;                //PORTB is setting on out
	DDRD=0X20;         		 //设置OC1A为输出
	PORTB=0X7F;           	 //PORTB is "1"
	TCCR1A=0x83;       		 //OC1A is "0",10 BITS PWM   C3,1;;;83,0
	TCCR1B=0X42;       		 //噪声消除,上升捕获   bu neng yong ping bi
	TCNT1=0;               	 //initial volue
    SREG=0X80;              //总中断开
	TIMSK=0X20;          	 //open the capture interrupt
	OCR1A=500;		 //zhan kong bi=x/1023
    howlong=2000;
}
SIGNAL(SIG_INPUT_CAPTURE1)
	{    
		TIMSK=00;                 //close capture interrupt
 	   	 PORTB=0X00;               //低4位亮
         delay(1);
         PORTB=0X7F;           //低4位灭
     	TIMSK=0X20;          //open capture interrupt
    	
	}
//================================================

//==============================================	
int main (void)
{
	
	init();
	b=100;
//    while(1);
	while(1)
	{ 	b+=5; 
	   	OCR1A=b;  		//定义占空比  
		if(b==1020)         //a 10 bit PMW
	   		{  
		   	 		while(b>=100)
		   				{
		         		b-=5;
		         		delay(5000);
			    		OCR1A=b;  		//定义占空比
		    			}         
   		  	}
		delay(5000);
   	
	}   
	
}
     

⌨️ 快捷键说明

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