標(biāo)題:
atmega128 AD 轉(zhuǎn)換
[打印本頁(yè)]
作者:
liuqq
時(shí)間:
2015-5-21 23:50
標(biāo)題:
atmega128 AD 轉(zhuǎn)換
/*********************************包含頭文件********************************/
#include <iom128v.h>
#include <macros.h>
/********************************數(shù)碼管段碼表*******************************/
extern const unsigned char tab[]={0x3f,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,
0x7F,0x6F};
/*********************************端口定義**********************************/
#define sclon PORTG |=BIT(0); //時(shí)鐘高
#define scloff PORTG &=~BIT(0);
#define dion PORTG |=BIT(1); //數(shù)據(jù)高
#define dioff PORTG &=~BIT(1);
/*********************************全局變量**********************************/
unsigned int adc_rel;
int dat;
/****************************************************************************
函數(shù)功能:ADC初始化函數(shù)
入口參數(shù):
出口參數(shù):
****************************************************************************/
void adc_init(void)
{
ADCSRA=0x00; //關(guān)ADC
ADMUX = (1<<REFS1)|(1<<REFS0); // 2.56V
ADCSRA = (1<<ADEN)|(1<<ADSC)|(1<<ADATE)|(1<<ADIE)|(1<<ADPS2)|(1<<ADPS1);
}
/****************************************************************************
函數(shù)功能:ADC中斷函數(shù)
入口參數(shù):
出口參數(shù):
****************************************************************************/
#pragma interrupt_handler adc_isr:iv_ADC
void adc_isr(void)
{
int data_H,data_L;
ADCSRA = 0x00;
adc_rel=ADC;
ADCSRA = (1<<ADEN)|(1<<ADSC)|(1<<ADIE);
}
/****************************************************************************
函數(shù)功能:數(shù)據(jù)輸出程序
入口參數(shù):temp
出口參數(shù):
****************************************************************************/
void dataOUT(unsigned char temp)
{
unsigned char i,temp1;
temp1 = tab[temp];
for(i = 0;i < 8;i++ )
{
scloff;
if((temp1&0x80)!= 0x80)
{ dioff;}
else
{ dion;}
sclon;
temp1 <<=1;
scloff;
}
}
/****************************************************************************
函數(shù)功能:延時(shí)子程序
入口參數(shù):
出口參數(shù):
****************************************************************************/
void delay(void)
{
int i;
for(i=0;i<200;i++);
}
/****************************************************************************
函數(shù)功能:顯示子程序
入口參數(shù):k
出口參數(shù):
****************************************************************************/
void display(unsigned int k)
{
dataOUT(k/1000);
PORTG &=0x03;
delay();
dataOUT(k/100%10);
PORTG |=0x04;
delay();
dataOUT(k/10%10);
PORTG &=0x0B;
PORTG |=0x08;
delay();
dataOUT(k%10);
PORTG |=0x0C;
delay();
}
/****************************************************************************
函數(shù)功能:主程序
入口參數(shù):
出口參數(shù):
****************************************************************************/
void main(void)
{
DDRE=0x00;
DDRG=0xff;
adc_init();
SEI();
display(0);
while(1)
{
delay();
display(adc_rel);
}
}
復(fù)制代碼
作者:
jsh163
時(shí)間:
2019-9-21 11:05
學(xué)習(xí)一下,謝謝!
歡迎光臨 (http://www.torrancerestoration.com/bbs/)
Powered by Discuz! X3.1