專注電子技術(shù)學習與研究
當前位置:單片機教程網(wǎng) >> MCU設計實例 >> 瀏覽文章

MEGA16單片機_AD轉(zhuǎn)換程序

作者:佚名   來源:本站原創(chuàng)   點擊數(shù):  更新時間:2014年09月01日   【字體:

 

 
AD轉(zhuǎn)換,編程的寄存器有:
ADMUX:多工選擇寄存器。參考電壓、對齊方式、模擬通道及增益選擇位
ADCSR(A):使能、中斷標志、中斷使能、預分頻值等
ADCL、ADCH:數(shù)據(jù)寄存器
SFI0R:特殊功能寄存器
////////////////////////////////////////////////////////////////////////////////////////////////////
#ifndef __AD_H__
#define __AD_H__
#include <iom16v.h>
#include <macros.h>
#define  uchar unsigned char
#define  uint  unsigned int
extern uint mega16_ad(void);
#endif
////////////////////////////////////////////////////////////////////////////////////////////////////
#include "ad.h"
uint mega16_ad(void)
{
    uint addata;
  DDRA&=~BIT(PA0);
  PORTA&=~BIT(PA0);
  ADMUX=0x00;
  ADCSR=0X80;
  ADCSR|=BIT(ADSC);
  while(!(ADCSR&(BIT(ADIF))));//即:while(!(ADCSR&0x10));
    addata=ADCL;
  addata=addata+ADCH*256;
  return addata;
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////
#include "ad.h"
#include "smg.h" 
void main(void)
{
  DDRA&=~BIT(PA0);//選定模擬通道0
  PORTA&=~BIT(PA0);
  SystemInit();
  SystemInit();
   while(1)
   {
   Show(mega16_ad()/1000,0);
   Show(mega16_ad()%1000/100,1);
   Show(mega16_ad()%100/10,2);
   Show(mega16_ad()%100%10,3);
   }
}
////////////////////////////////////////////////////////////////////////////////////////////////////////
/*MCU:ATMEGA16
晶振頻率:8MHZ
功能:數(shù)碼管動態(tài)掃描程序
*/
#ifndef __SMG_H__
#define __SMG_H__
#include <iom16v.h>
#include <macros.h>
#define  uchar unsigned char
#define  uint  unsigned int
extern const  table[];
//**********1ms基準延時程序**********************************   
void DelayMs(unsigned int ms);
void SystemInit(void);
void Show(unsigned char ddata,unsigned char num);
#endif
///////////////////////////////////////////////////////////////////////////////////////////////////////
/*MCU:ATMEGA16
晶振頻率:8MHZ
功能:數(shù)碼管動態(tài)掃描程序
*/
#include "smg.h"
#pragma data:code //共陽數(shù)碼管斷碼表
const  table[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,
                0x90,0x88,0x83,0xc6,0xa1,0x86,0x8e,0xb6};
//**********1ms基準延時程序**********************************   
void DelayMs(unsigned int ms)
{
    unsigned int j;
 while(--ms)
 {
    for(j=1141;j>0;--j);//1141
    }
}
void SystemInit(void)
{
  DDRA |= (1<<2)|(1<<3)|(1<<4)|(1<<6);
 DDRB |= 0xFF;
 
 PORTA |= (1<<2)|(1<<6);
 PORTB = 0xFF; 
 PORTA &= (~(1<<2))&(~(1<<6));
}
void Show(unsigned char ddata,unsigned char num)
{
  PORTA |= (1<<3);
 PORTB = table[ddata];
 PORTA &= ~(1<<3);
  
 PORTB = 0x00;
 PORTA |= (1<<4);
 PORTB = (1<<num);   
 PORTA &= ~(1<<4); 
 DelayMs(2);   
}
 
關閉窗口