📄 iar9-4.c
字号:
#include <iom16.h>
#include<intrinsics.h>
#define uchar unsigned char
#define uint unsigned int
#define CPL_BIT(x,y) (x^=(1<<y))
#define CLR_BIT(x,y) (x&=~(1<<y))
#define SET_BIT(x,y) (x|=(1<<y))
#define GET_BIT(x,y) (x&(1<<y))
#define xtal 8
uchar flagA=1,flagB=0;
//****************************************
void Delay_1ms(void)
{ uint i;
for(i=1;i<(uint)(xtal*143-2);i++)
;
}
//=============================================
void Delay_nms(uint n)
{
uint i=0;
while(i<n)
{Delay_1ms();
i++;
}
}
void port_init(void)
{
PORTB = 0xFF;
DDRB = 0xFF;
}
//TIMER1 initialize - prescale:256
// WGM: 7) PWM 10bit fast, TOP=0x03FF
// desired value: 32.768mSec
// actual value: 32.768mSec (0.0%)
void timer1_init(void)
{
TCCR1B = 0x00; //stop
TCNT1H = 0x00; //setup
TCNT1L = 0x00;
OCR1AH = 0x03;
OCR1AL = 0xFF;
OCR1BH = 0x03;
OCR1BL = 0xFF;
ICR1H = 0x03;
ICR1L = 0xFF;
TCCR1A = 0x03;
TCCR1B = 0x8C; //start Timer
}
//call this routine to initialize all peripherals
void init_devices(void)
{
__disable_interrupt(); //disable all interrupts
port_init();
timer1_init();
MCUCR = 0x00;
GICR = 0x00;
TIMSK = 0x1C; //timer interrupt sources
__enable_interrupt();//re-enable interrupts
}
void main(void)
{
init_devices();
while(1)
{
Delay_nms(100);
if(GET_BIT(flagA,0)==1)OCR1A+=20;
if(OCR1A>1000)CLR_BIT(flagA,0);
if(GET_BIT(flagA,0)==0)OCR1A-=20;
if(OCR1A<20)SET_BIT(flagA,0);
//---------------------------
if(GET_BIT(flagB,0)==1)OCR1B+=10;
if(OCR1B>1010)CLR_BIT(flagB,0);
if(GET_BIT(flagB,0)==0)OCR1B-=10;
if(OCR1B<10)SET_BIT(flagB,0);
}
}
#pragma vector=TIMER1_COMPA_vect
__interrupt void timer1_compa_isr(void)
{
//compare occured TCNT1=OCR1A
CLR_BIT(PORTB,0);
}
#pragma vector=TIMER1_COMPB_vect
__interrupt void timer1_compb_isr(void)
{
//compare occured TCNT1=OCR1B
CLR_BIT(PORTB,7);
}
#pragma vector=TIMER1_OVF_vect
__interrupt void timer1_ovf_isr(void)
{
//TIMER1 has overflowed
SET_BIT(PORTB,0);
SET_BIT(PORTB,7);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -