|
发表于 2006-10-13 21:24:56
|
显示全部楼层
#include <iom8v.h>
#include <macros.h>
#include <math.h>
#define Uchar unsigned char
#define Uint unsigned int
#define Ulong unsigned long
Uchar time_init;
Uchar CNT1;
Uchar CNT2;
Uchar CNT3;
Uchar LMT1;
Uchar LMT2;
Uchar LMT3;
//TIMER1 initialize - prescale:1024
// WGM: 0) Normal, TOP=0xFFFF
// desired value: 1Hz
// actual value: 1.000Hz (0.0%)
void timer1_init(void)
{
TCCR1B = 0x00; //stop
TCNT1H = 0xfe; //setup
TCNT1L = 0x00;
OCR1AH = 0x0F;
OCR1AL = 0x42;
OCR1BH = 0x0F;
OCR1BL = 0x42;
ICR1H = 0x0F;
ICR1L = 0x42;
TCCR1A = 0x00;
TCCR1B = 0x01; //start Timer
}
#pragma interrupt_handler timer1_ovf_isr:9
void timer1_ovf_isr(void)
{
TCNT1H = 0xfe; //reload counter high value
TCNT1L = 0x00; //reload counter low value
++CNT1;
if(CNT1>=LMT1)
{
CNT1=0;
if(++CNT2>=LMT2)
{
CNT2=0;
CNT3=~CNT3;
}
}
time_init=1;
}
/*void flush(UCHAR ios,UCHAR ioc)
{
UCHAR a;
if(time_init)
{
time_init=0;
if(CNT1<=CNT2)
{
PORTB=ios;
}
else
PORTB=ioc;
}
}
*/
void main(void)
{
OSCCAL=0X9d;
CLI(); //disable all interrupts
timer1_init();
PORTB = 0x00;
DDRB = 0xff;
MCUCR = 0x00;
GICR = 0x00;
TIMSK = 0x04; //timer interrupt sources
time_init=0;
LMT1=250;
LMT2=250;
LMT3=1;
CNT1=CNT2=CNT3=0;
SEI(); //re-enable interrupts
while(1)
{
if (!CNT3)
{
if(time_init)
{
time_init=0;
if(CNT1<=CNT2)
{
PORTB=1;
}
else
PORTB=0;
}
}
else
if(time_init)
{
time_init=0;
if(CNT1<=CNT2)
{
PORTB=0;
}
else
PORTB=1;
}
}
}
文中的Uchar CNT1;CNT2;CNT3;LMT1;LMT2;LMT3;分别是什么用途?
这是怎么实现LED渐变的?
望高手指点一下。
有谁有PWM方法实现的可以参考下吗?或者传到邮箱:qzrrrr@163.com
谢谢 |
|