?? 33xadc.c
字號:
//-----------------------------------------------------------------------------
// ADC.c
//-----------------------------------------------------------------------------
// 版權歸新華龍電子有限公司所有
//
// 作者:Robi Ken
// 日期: 2004.12.28
//
// 功能:ADC采樣
// 目標板: C8051F33x
// 開發工具: Silicon Laboratories IDE
//
//-------------------------------------------------------------------------------------
//程序功能
//------------------------------------------------------------------------------------
//此程序為ADC轉換程序,可以選擇向ADC0BUSY寫1或用定時器0,1,2,3作為ADC的啟動信號。
//
//------------------------------------------------------------------------------------
//頭文件定義
//------------------------------------------------------------------------------------
//
#include <c8051f330.h>
#include <stdio.h>
//-----------------------------------------------------------------------------
// 定義16位特殊功能寄存器
//-----------------------------------------------------------------------------
sfr16 ADC0 = 0xbd;
sfr16 TMR0RL = 0xca;
sfr16 TMR1RL = 0xca;
sfr16 TMR2RL =0xca;
sfr16 TMR3RL =0xca;
sfr16 TMR0 = 0xCC;
sfr16 TMR1 = 0xCC;
sfr16 TMR2 = 0xcc;
sfr16 TMR3 = 0xcc;
//-----------------------------------------------------------------------------
// 全局變量定義
//-----------------------------------------------------------------------------
char i;
int result;
//-----------------------------------------------------------------------------
//定義常量
//-----------------------------------------------------------------------------
#define SYSCLK 49000000
#define SAMPLE_RATE 50000
//------------------------------------------------------------------------------------
// 定義函數
//------------------------------------------------------------------------------------
void SYSCLK_Init (void);
void PORT_Init (void);
void Timer0_Init (int counts);
void Timer1_Init (int counts);
void Timer2_Init (int counts);
void Timer3_Init (int counts);
void ADC0_Init(void);
void ADC0_ISR (void);
void ADC0_CNVS_ADC0h(void);
//------------------------------------------------------------------------------------
// 主程序
//------------------------------------------------------------------------------------
void main (void)
{
int ADCRESULT[50] ;
int k;
PCA0MD &= ~0x40; // 禁止看門狗
SYSCLK_Init ();
PORT_Init ();
Timer0_Init (SYSCLK/SAMPLE_RATE);
//Timer1_Init (SYSCLK/SAMPLE_RATE); //選擇相應的啟動方式
//Timer2_Init (SYSCLK/SAMPLE_RATE);
//Timer3_Init (SYSCLK/SAMPLE_RATE);
ADC0_Init();
EA=1;
while(1)
{
//ADC0_CNVS_ADC0h();
k=ADC0;
ADCRESULT[i]=result; //此處設斷點,觀察ADCRESULT的結果
}
}
//------------------------------------------------------------------------------------
// 端口初始化
//------------------------------------------------------------------------------------
void PORT_Init (void)
{
//#pragma asm
// MOV P0MDOUT,#01H
//#pragma endasm
P0MDIN = 0x00;
P0SKIP = 0x03;
P0MDOUT = 0x01;
P1SKIP = 0xff;
XBR1 = 0x40;
}
//-----------------------------------------------------------------------------
// 系統時鐘初始化
//-----------------------------------------------------------------------------
void SYSCLK_Init (void)
{
OSCICN |= 0x03;
RSTSRC = 0x04;
}
//-----------------------------------------------------------------------------
// 定時器0初始化
//-----------------------------------------------------------------------------
void Timer0_Init (int counts)
{
TMOD=0x11;
CKCON |= 0x08;
TMR0RL = -counts;
TMR0 = TMR0RL;
ET0=0;
TR0=1;
}
//-----------------------------------------------------------------------------
// 定時器1初始化
//-----------------------------------------------------------------------------
void Timer1_Init (int counts)
{
TMOD=0x11;
CKCON |= 0x04;
TMR1RL = -counts;
TMR1 = TMR1RL;
ET1= 0;
TR1=1;
}
//-----------------------------------------------------------------------------
//定時器2初始化
//-----------------------------------------------------------------------------
void Timer2_Init (int counts)
{
CKCON |= 0x10;
TMR2RL = -counts;
TMR2 = TMR2RL;
ET2= 0;
TR2=1;
}
//-----------------------------------------------------------------------------
// 定時器3初始化
//-----------------------------------------------------------------------------
void Timer3_Init (int counts)
{
TMR3CN = 0x00;
CKCON |= 0x40;
TMR3RL = -counts;
TMR3 = TMR3RL;
EIE1 &= ~0x80;
TMR3CN |= 0x04;
}
//-----------------------------------------------------------------------------
// ADC0初始化
//-----------------------------------------------------------------------------
void ADC0_Init (void)
{
ADC0CN =0x81; //ADCOCN中的值為80,81,83,82,85時分別為向AD0BYSY寫1,
AMX0P = 0x01; //定時器0,1,2,3溢出作為啟動信號
AMX0N = 0x11; //AMX0P為通道選擇
ADC0CF = (SYSCLK/3000000) << 3;
ADC0CF &=~0x04;
REF0CN = 0x03;
EIE1 |= 0x08;
}
//-----------------------------------------------------------------------------
// 向ADC0寫1將啟動轉換
//-----------------------------------------------------------------------------
void ADC0_CNVS_ADC0h(void)
{
AD0BUSY = 1;
}
//-----------------------------------------------------------------------------
// ADC中斷處理
//-----------------------------------------------------------------------------
void ADC0_ISR (void) interrupt 10
{
AD0INT =0;
result= ADC0H;
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -