?? t1pwm.txt
字號:
//***************FileName:PWM.C***************//
//***************ICCAVR V6.30編譯*****************//
#include <iom16v.h>
#define uchar unsigned char
#define uint unsigned int
//數(shù)碼管字型表,對應(yīng)0,1,2,3,4,5,6,7,8,9,E//
uchar Table[12]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x79};
uchar Data1[4]={10,0,5,0}; //定義初始方波占空比:50%
uchar Data2[4]={2,0,4,6}; //設(shè)定PWM頻率:8M/2046=3910Hz
uchar Key1_Flag,Key2_Flag;
void DelayMs(uint i ) //Ms級延時(shí),參數(shù)i為延時(shí)時(shí)間
{uint j;
for(;i!=0;i--)
{for(j=1142;j!=0;j--) {;}} //原來8000,改為1142//
}
void Display(uchar *p) //動態(tài)顯示函數(shù),參數(shù)p為待顯示的數(shù)組名
{uint i,sel=0xff7f;
for(i=0;i<4;i++)
{PORTC=sel; //選通最右邊的數(shù)碼管
PORTA=Table[p[i]]; //送字型碼
DelayMs(2); //顯示延時(shí)
sel=sel>>1; //移位以顯示前一位
}
}
void Add_Process(uchar *p)
{if(p[1]!=1)
{p[2]=p[2]+1;
if(p[2]==10) {p[2]=0;p[1]++;}
}
}
void Sub_Process(uchar *p)
{uchar temp;
temp=p[1]*10+p[2];
if(temp!=0)
{temp=temp-1;
p[1]=temp/10;
p[2]=temp-p[1]*10;
}
}
void Key_Process()
{while((PINB&0x01)==0) {Display(Data1);Key1_Flag=1;}
while((PINB&0x02)==0) {Display(Data1);Key2_Flag=1;}
if(Key1_Flag==1)
{Add_Process(Data1);
Key1_Flag=0;}
if(Key2_Flag==1)
{Sub_Process(Data1);
Key2_Flag=0;}
}
void Set_Process(uchar *p)
{uint i;
i=p[1]*10+p[2];
i=1023*i/10;
OCR1AH=i>>8;
OCR1AL=i&0x00ff;
}
void Init_IO(void) //初始化I/O口
{DDRA=0xff; //設(shè)置A口為推挽1輸出
PORTA=0xff;
DDRC=0xff; //設(shè)置C口為推挽1輸出
PORTC=0xff;
DDRB=0x00; //設(shè)置B口為三態(tài)輸入
PORTB=0x00;
DDRD=0xff; //設(shè)置D口為推挽1輸出
PORTD=0xff;
}
void main(void)
{uchar i;
Init_IO(); //初始化I/O口
PORTA=0xff; //點(diǎn)亮以測試所有的數(shù)碼管
PORTC=0x00;
DelayMs(300); //延時(shí)
PORTC=0xff; //熄滅所有的數(shù)碼管
TCCR1A=0xC3; //10位PWM,向上計(jì)數(shù)清除OC1A,向下計(jì)數(shù)置位OC1A
TCCR1B=0x02; //時(shí)鐘8分頻,最小頻率1M/2046=0.5KHz
while(1)
{Key_Process();
Set_Process(Data1); //設(shè)置PWM占空比
for(i=50;i!=0;i--)
Display(Data1);
for(i=50;i!=0;i--)
Display(Data2);
}
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -