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

📄 pwm2.c

📁 用T0比较匹配模式输出周期为25MS;占空比为1:1
💻 C
字号:
//用T0比较匹配模式输出周期初始值为25MS(可调);占空比为50%
#include <iom16v.h>
#include <macros.h>
#include <math.h>
/**********************************************/
#define pc0_high()  asm("sbi 0x15,0")
#define pc0_low()   asm("cbi 0x15,0")
#define pc1_high()  asm("sbi 0x15,1")
#define pc1_low()   asm("cbi 0x15,1")
unsigned char i;
unsigned char temp=0x00;

/*端口初始化
PA口为输入口,内部上拉电阻有效,PB3作为输出口,并输出高电平,
PC0和PC1为输出口,PC口的其它为输入口并且上拉有效*/

//微秒延时程序
 void delay_us(int time)
      {
	   do   
	       {
		     time--;
		    }	
	   while(time>1);
	   }
	   
//毫秒延时程序
void delay_ms(unsigned int time)
     {
	  while(time!=0)
	       {
		    delay_us(1000);
			time--;
			}
	  }
	  
void port_init(void)
{
DDRA=0x00;
PORTA=0xff;
DDRC=0x03;
PORTC=0xff;
DDRB=0xff;
PORTB=0x08;
}

//定时器T0初始化
void timer0_init(void)
{
 TCCR0  = 0x00;//停止定时器
 TCNT0  = 0x00;//初始值
 OCR0   = 0xc3;//匹配值
 TCCR0  = 0x1b;//启动定时器T0工作在CTC,OC0取反,CLK/64分频
}

void init_devices(void)
{
 CLI(); //禁止所有中断
 MCUCR  = 0x00;
 MCUCSR = 0x80;//禁止JTAG
 GICR   = 0x00;
 port_init();
 timer0_init();
 SEI();//开全局中断
}
//主函数********************************************
void main(void)
{
unsigned char j=0xc3;
unsigned char k=0xc3;
init_devices();
while(1)
 {
for(i=0;i<2;i++)
   {
unsigned char temp1;
PORTC=~(1<<i);                       //PORTC的i位为零,使行线为低电平
delay_ms(10);                      
temp=PINA;                           //读PINA的值                    
if(temp!=0xff)                       //有键按下吗
    {
delay_ms(20);                       //延时,去抖
temp1=PINA;                          //再次读PINA的值
if(temp==temp1)                      //有键按下吗
  {
switch(temp)                      //  计算键值
    {
	 case 0x7f:
	 TCCR0  = 0x00;//停止定时器
     TCNT0  = 0x00;//初始值
	 j=(j-0x0a);
     OCR0   = j;//匹配值
     TCCR0  = 0x1b;//启动定时器T0工作在CTC,OC0取反,CLK/64分频;
	 break;     
	 
	 case 0xbf:
	 TCCR0  = 0x00;//停止定时器
     TCNT0  = 0x00;//初始值
	 k=(k+0x0a);
     OCR0   = k;//匹配值
     TCCR0  = 0x1b;//启动定时器T0工作在CTC,OC0取反,CLK/64分频;
     break;   
	  
	 default:;
    }
  }
}
}
}
}

⌨️ 快捷键说明

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