AVR CTC模式波形輸出實驗
CTC模式 比較匹配我設(shè)置為置位 當定時器值==OCR0 輸出口為高電平,然后定時器自動清零重新計數(shù)開始如此反復(fù)。
本文引用地址:http://www.ex-cimer.com/article/201611/323882.htm輸出電壓為占空比值。一下是源碼:
#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; //比較匹配設(shè)置變量
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模式設(shè)置 比較匹配置位
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;
}
評論