📄 f330_ad.cpp
字号:
No.1
--------------------------------------------------------------------------------
已测试通过新华龙写的F330_AD转换程序 .
//-----------------------------------------------------------------------------
// 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] ;
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();
ADCRESULT=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 + -