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

📄 sivo2.c

📁 avr常用功能程序中包含了pwm、adc、中断、定时、i2c等各程序可供开发者直接使用
💻 C
字号:
#include <avr/io.h>
#include <avr/signal.h>
#define uchar unsigned char
#define uint unsigned int
//how much moto running
#define stop 5   
uint a,b;
unsigned int howlong=0;       //run how much
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=0x82;       		 //OC1A is "0",9 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=90;		         //zhan kong bi=x/1023
}
void uart_init(void)
{
    UCSRB|=(1<<RXEN)|(1<<TXEN)|(1<<RXCIE);      //允许发送和接收
    //UBRRL=0X19;                                   //[fosc/16(BAUD+1)]%256
    UBRRH=0X00;                                   //[fosc/16(BAUD+1)]/256
    UCSRC|=(1<<URSEL)|(1<<UCSZ1)|(1<<UCSZ0);    //8位数据+1位停止位
   // UCR=6<<TXCIE;           //开发送中断
   UBRRL=0X38;
}
int uart_putchar(char c)
{
    //if(c=='\n') uart_putchar('\end');
    UDR=c;
    loop_until_bit_is_set(UCSRA,UDRE);
    sbi(UCSRA,UDRE);
    return 0;
}
SIGNAL(SIG_INPUT_CAPTURE1)
	{    
		TIMSK=00;                 //close capture interrupt
        howlong++;
        if(howlong==stop){howlong=0;PORTB=0XFF;delay(65000);delay(65000);PORTB=0X7F;}
        PORTB=0X00;
        delay(5);
        PORTB=0X7f;
     	TIMSK=0X20;          //open capture interrupt
    	
	}
//================================================

//==============================================	
int main (void)
{
	init();
    uart_init();
	b=80;
    while(1);
	while(1)
	{ 	b+=5; 
	   	OCR1A=b;  		//定义占空比
		if(b==510)         //a 10 bit PMW
	   		{  
		   	 		while(b>=100)
		   				{
		         		b-=5;
		         		delay(5000);
			    		OCR1A=b;  		//定义占空比
		    			}
   		  	}
		delay(5000);
        if((b%200)==0)uart_putchar(0xff);
   	
	}   
}
     

⌨️ 快捷键说明

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