ATmage128 complete program to operate 16-bit AD chip MAX1168

Publisher:bonbonoLatest update time:2016-07-28 Source: eefocusKeywords:ATmage128  MAX1168 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

/****************Pin Definition********************************
Interface: Hardware SPI
PB.1(SCK) SCLK Direction: 1
PB.2(MOSI) DIN Direction: 1
PB.3(MISO) DOUT Direction: 0
PB.4 CS Direction: 1
PB.5 EOC Direction: 0 DRRB="0x06"

MCU:ATmage128
AD:MAX1168(16bit)
Crystal:11.0592MWritten
by:Jesse Rei
**************************************************************/
#include
#include
#include"lcd.c"

#define uchar unsigned char
#define uint  unsigned int

#define Command_H 0x01 //000 00 00 1 Channel 0, single channel conversion, no power saving mode, internal clock
#define Command_L 0x00 //The lower eight bits of the command are any number

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

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

/**************Global variables********************************/
uchar buffer[5]={1,1,1,1,1};
uchar count="0";
uint sum="0";
/***************************************************/
/******************Port initialization***********************/
void port_init(void)
{
 PORTA = 0xff;
 DDRA = 0xff;
 
 PORTB = 0x06;
 DDRB = 0xff;
 
 PORTC = 0x00; //m103 output on ly
 DDRC = 0x00;
 
 PORTD = 0xff;
 DDRD = 0xff;
 
 PORTE = 0x00;
 DDRE = 0x00;
 
 PORTF = 0x00;
 DDRF = 0x00;
 
 PORTG = 0x00;
 DDRG = 0x00;
}
/************************************************************/
/*************************SPI initialization*******************************/
void spi_init(void)

  SPCR="0x5d"; //SPI enabled, host mode, high byte first, working mode 3, Fck/128
               //SPIE SPE DORD MSTR CPOL CPHA SPR1 SPR0
      // 0 1 0 1 1 0 1 1
  SPSR = 0x00; //setup SPI, host frequency multiplication     
}
/*************************************************************/
/*************************Start AD conversion***************************/
void start(uchar Com_H,uchar Com_L)
{
  CS_L;
  SPDR="Com"_H;
  while(!(SPSR&(1<   SPDR="Com"_L;
  while(!(SPSR&(1<}
/************************************************************/
/******************Disassemble the number to display****************************/
void data_to_buffer(uint a)
{
  uchar i;
  for(i=0;i<=4;i++)
  {
    buffer[i]=a%10;
 a=a/10;
  }
}
/******************************************************************/
/*********************Disassemble the number to display****************************/
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; //Send data and provide pulse to max1168
  while (!(SPSR &(1<   da ta_h=SPDR; //Get the high eight bits
  
  SPDR = 0xff; //Send data and provide pulses to max1168
  while (!(SPSR &(1<   ta_l =SPDR; //Get the lower eight bits
  
  ad_ta =(unsigned int)ta_h *256+ta_l ; //Merge the upper and lower eight bits
  ad_dat=ad_ta /16; //Convert to voltage value 
  
  sum+=ad_dat; //Sixteen sampling results cumulative sum
  count++;
  if(count>15)
  {
    count="0";
 ad_ta1 =sum/16; //sum divided by 16 to get the average value
    ad_ta2 =ad_ta1 *3;
    ta_to_buffer (ad_ta2 );  
 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);  //等待转换结束
    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);
  }
}

Keywords:ATmage128  MAX1168 Reference address:ATmage128 complete program to operate 16-bit AD chip MAX1168

Previous article:ATmega128 Model Identification Description
Next article:AVR internal EEPROM experiment

Latest Microcontroller Articles
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号