新聞中心

EEPW首頁 > 嵌入式系統(tǒng) > 設(shè)計應(yīng)用 > ATmage128 操作16位AD芯片MAX1168的完整程序

ATmage128 操作16位AD芯片MAX1168的完整程序

作者: 時間:2016-11-11 來源:網(wǎng)絡(luò) 收藏

/****************引腳定義*********************************
接口:硬件SPI
PB.1(SCK) SCLK 方向:1
PB.2(MOSI) DIN 方向:1
PB.3(MISO) DOUT 方向:0
PB.4 CS 方向:1
PB.5 EOC 方向:0 DRRB="0x06"

本文引用地址:http://www.butianyuan.cn/article/201611/317058.htm

MCU:ATmage128
AD:MAX1168(16bit)
晶振:11.0592M
編寫:Jesse Rei
**********************************************************/
#include
#include
#include"lcd.c"

#define uchar unsigned char
#define uint unsigned int

#define Command_H 0x01 //000 00 00 1 通道0,單通道轉(zhuǎn)換,無節(jié)電模式,內(nèi)部時鐘
#define Command_L 0x00 //命令低八位為任意數(shù)

#define CS_L PORTB&=~(1<<4) //PB.4
#define CS_H PORTB|=(1<<4)
#define READY PINB&0x20 //PB.5

uchar tab[10]={0,1,2,3,4,5,6,7,8,9};

/**************全局變量*****************************/
uchar buffer[5]={1,1,1,1,1};
uchar count="0";
uint sum="0";
/***************************************************/
/******************端口初始化***********************/
void port_init(void)
{
PORTA = 0xff;
DDRA = 0xff;

PORTB = 0x06;
DDRB = 0xff;

PORTC = 0x00; //m103 output only
DDRC = 0x00;

PORTD = 0xff;
DDRD = 0xff;

PORTE = 0x00;
DDRE = 0x00;

PORTF = 0x00;
DDRF = 0x00;

PORTG = 0x00;
DDRG = 0x00;
}
/*********************************************************/
/*********************SPI初始化***************************/
void spi_init(void)
{
SPCR="0x5d"; //SPI允許,主機(jī)模式,高字節(jié)在前,工作模式3,Fck/128
//SPIE SPE DORD MSTR CPOL CPHA SPR1 SPR0
// 0 1 0 1 1 0 1 1
SPSR = 0x00; //setup SPI,主機(jī)倍頻
}
/*********************************************************/
/*********************啟動AD轉(zhuǎn)換***************************/
void start(uchar Com_H,uchar Com_L)
{
CS_L;
SPDR="Com"_H;
while(!(SPSR&(1< SPDR="Com"_L;
while(!(SPSR&(1< }
/*********************************************************/
/******************拆數(shù),用以顯示*************************/
void data_to_buffer(uint a)
{
uchar i;
for(i=0;i<=4;i++)
{
buffer[i]=a%10;
a=a/10;
}
}
/**********************************************************/
/******************拆數(shù),用以顯示*************************/
void getdata(void)
{
uchar data_l=0,data_h=0;
uint ad_data=0,ad_data1=0,ad_data2;
uint ad_dat=0;

SPDR = 0xff; //發(fā)送數(shù)據(jù),給max1168提供脈沖
while (!(SPSR &(1< data_h=SPDR; //得到高八位

SPDR = 0xff; //發(fā)送數(shù)據(jù),給max1168提供脈沖
while (!(SPSR &(1< data_l=SPDR; //得到低八位

ad_data=(unsigned int)data_h*256+data_l;//高低八位合并
ad_dat=ad_data/16; //轉(zhuǎn)換成電壓值

sum+=ad_dat; //十六次采樣結(jié)果累加和
count++;
if(count>15)
{
count="0";
ad_data1=sum/16; //sum除以16得平均值
ad_data2=ad_data1*3;
data_to_buffer(ad_data2);
sum=0;
}

CS_H;
}
/**********************************************************/
/*************************延時1ms**************************
void delay1ms(uint t)
{
uint i;
for(;t>0;t--)
for(i=0;i<1106;i++)
;
}
**********************************************************/
/*************************主程序***************************/
void main()
{
port_init();
init_lcd();
lat_disp (0x00,0x00);
chn_disp (tab1);
delay1ms(5500);
chn_disp (tab2);

spi_init();

start(Command_H,Command_L);
while(1)
{
while(READY); //等待轉(zhuǎn)換結(jié)束
getdata();
disp_two_char(0x8a,tab[buffer[4]],tab[buffer[3]]);
disp_two_char(0x8b,tab[buffer[2]],tab[buffer[1]]);
disp_two_char(0x8c,tab[buffer[0]],V);

start(Command_H,Command_L);
}
}



關(guān)鍵詞: ATmage12816位AD芯片MAX116

評論


技術(shù)專區(qū)

關(guān)閉