📄 oc1ab输出pwm._c
字号:
//ICC-AVR application builder : 2009-3-11 13:52:41
// Target : M8
// Crystal: 8.0000Mhz
#include <iom8v.h>
#include <macros.h>
void port_init(void)
{
PORTB = 0x00;
DDRB = 0x00;
PORTC = 0x00; //m103 output only
DDRC = 0x00;
PORTD = 0x00;
DDRD = 0x00;
}
//call this routine to initialize all peripherals
void init_devices(void)
{
//stop errant interrupts until set up
CLI(); //disable all interrupts
port_init();
MCUCR = 0x00;
GICR = 0x00;
TIMSK = 0x00; //timer interrupt sources
SEI(); //re-enable interrupts
//all peripherals are now initialized
}
/************************************
用 途:用TC1输出PWM
Taget :mega8
crystal :8M
介 绍:PB1(OC1A),PB2(OC1B)
入口参数:A,B的占空比
出口参数:
*************************************/
void init_timer1_PWM(unsigned int x,unsigned int y)
{
DDRB|=(1<<PB1)|(1<<PB2);
TCCR1B=0x0;//先停止TC1
TCNT1=0;//设置TCNT1的初值
OCR1A=x;//设置OC1A的初值
OCR1B=y;//设置OC1B的初值
ICR1=0xff;//设置ICR的初值
//系统时钟64分频CS1[2、0]=0x03
//COM1A[1-0]=0x00,COM1B[1-0]=0x02
TCCR1A|=(1<<COM1A1)|(1<<COM1B1)|(1<<WGM11);
TCCR1B|=(1<<WGM13)|(1<<CS11)|(1<<CS10);//Starttimer
}
void timer1_PWM(unsigned int x,unsigned int y)
{
OCR1A=x;
OCR1B=y;
}
void main()
{
port_init();
init_devices();
init_timer1_PWM(100,200);
while(1)
{
PORTB=0xff;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -