注:频率为488HZ 既 64分频 8us计数一次 256次即2048ns ==2.048ms 所以频率488HZ
CTC形式 比较匹配我设置为置位 当定时器值==OCR0 输出口为高电平,然后定时器主动清零从头计数开端如此重复。
输出电压为占空比值。一下是源码:
#include
#include
#define uchar unsigned char
#define uint unsigned int
__flash uchar seg[10]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07, //段码
0x7f,0x6f};
__flashuchar tab[8]={0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f}; //位选码
int wide=0; //比较匹配设置变量
void delay(uint k) //延时k ms子程序
{
uint i,j;
for(i=0;i
}
void show_dat(uint dat)
{
uchar i=0;
do
{
PORTA=seg[dat%10];
PORTC=tab[i];
delay(1);
PORTA=0xff;
PORTC=0xff;
dat=dat/10;
i++;
}
while(dat);
}
void port_init()
{
PORTA=0xff;
DDRA=0xff;
PORTC=0xff;
DDRC=0xff;
PORTD=0xff;
DDRD=0x00;
PORTB=0xff;
DDRB=0xff;
}
void time_init()
{
TCCR0=0x7b; //CTC形式设置 比较匹装备位
TCNT0=0x00; //定时器初值
OCR0=0x00; //比较匹配初值
}
void init()
{
MCUCR=0x0a;
GICR=0xc0;
SREG=0x80;
}
void main()
{
port_init();
init();
time_init();
while(1)
{
OCR0=wide;
show_dat(wide);
}
}
#pragma vector = 0x04
__interrupt void int0()
{
delay(100);
wide=wide+10;
if(wide>255)wide=0;
}
#pragmaadclass=0&app_id=0&c=news&cf=1001&ch=0&di=128&fv=17&is_app=0&jk=1b45ef2776d0b384&k=vector&k0=vector&kdi0=0&luki=4&n=10&p=baidu&q=98059059_cpr&rb=0&rs=1&seller_id=1&sid=84b3d07627ef451b&ssp2=1&stid=0&t=tpclicked3_hc&tu=u1831118&u=http%3A%2F%2Fwww%2E51hei%2Ecom%2Fmcu%2F2146%2Ehtml&urlid=0″ id=”0_nwl” mpid=”0″ target=”_blank”>vector= 0x08
__interrupt void int1()
{
delay(100);
wide=wide-10;
if(wide<0) wide=0;
}