?? chengxu.c
字號:
/********************************************************************
匯誠科技
網(wǎng)址:http://www.ourhc.cn
產(chǎn)品有售淘寶店:http://shop36330473.taobao.com
*********************************************************************/
#include <iom16v.h>
#include <macros.h>
#define uchar unsigned char
#define uint unsigned int
/**********************************************************************
定義數(shù)組
**********************************************************************/
uchar Table[10]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
uchar Data[4]={0,0,0,0};
/**********************************************************************
延時函數(shù)
**********************************************************************/
void DelayMs(uint i) //0.25ms
{uchar j;
for(;i!=0;i--)
{for(j=250;j!=0;j--) {;}}
}
/**********************************************************************
顯示函數(shù)
注意:使用開發(fā)板時sel=0xef;
**********************************************************************/
void Display(uchar *p) //動態(tài)顯示函數(shù),參數(shù)p為待顯示的數(shù)組名
{uchar i,sel=0x01;
for(i=0;i<4;i++)
{PORTD=sel; //選通最右邊的數(shù)碼管
PORTB=~Table[p[i]]; //送字型碼
if(i==0){PORTB=PORTB&0X7f;} //增加小數(shù)點
DelayMs(1); //顯示延時
sel=sel<<1; //移位以顯示前一位
}
}
/**********************************************************************
電壓值處理函數(shù)
**********************************************************************/
uint ADC_Convert(void)
{uint temp1,temp2;
temp1=(uint)ADCL;//先讀ADCL
temp2=(uint)ADCH;//再讀ADCH
temp2=(temp2<<8)+temp1;//求值
temp2=temp2*49/10;//求顯示的電壓值(0~5.12)V
return(temp2);
}
/**********************************************************************
顯示處理函數(shù)
**********************************************************************/
void Process(uint i,uchar *p)
{p[0]=i/1000;
i=i%1000;
p[1]=i/100;
i=i%100;
p[2]=i/10;
i=i%10;
p[3]=i;
}
/**********************************************************************
初始化I/O口
**********************************************************************/
void Init_IO(void) //初始化I/O口
{
DDRA=0x00; //設(shè)置A口為不帶上拉輸入;
PORTA=0x00;
DDRB=0xff; //設(shè)置B口為推挽1輸出;
PORTB=0xff;
DDRD=0xff; //設(shè)置C口為推挽1輸出;
PORTD=0xff;
}
/**********************************************************************
主函數(shù)
**********************************************************************/
void main(void)
{uint i;
Init_IO();
ADMUX=0x00; //選擇第一通道;
/**********************轉(zhuǎn)換速率應在50~200K****************************/
ADCSR=0xe6; //125K轉(zhuǎn)換速率,自由轉(zhuǎn)換模式;啟動AD轉(zhuǎn)換器;
DelayMs(300); //延時待系統(tǒng)穩(wěn)定;
while(1)
{
i=ADC_Convert(); //顯示電壓值(0~1024)
Process(i,Data);
Display(Data);
}
}
/**********************************************************************
程序結(jié)束
**********************************************************************/
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -