mini2440 ADC_poll实验

 


/*************************************************
Function name: 这是基础实验的一个模版
Parameter    : 无
Description  : 做基础实验,直接调用该模板即可
Return     : 无
Argument     : 无
Autor & date : Daniel
**************************************************/
#define GLOBAL_CLK 1
#include <stdlib.h>
#include <string.h>
#include "def.h"
#include "option.h"
#include "2440addr.h"
#include "2440lib.h"
#include "2440slib.h"
#include "mmu.h"
#include "profile.h"
#include "memtest.h"




#define ADC_FREQ 2500000


volatile U32 preScaler;
volatile U32 adc_value=0;


void adc_init(void);
int ReadAdc(int channel);
static void cal_cpu_bus_clk(void);
void Set_Clk(void);
void beep_init(void);
void beep_run(void);
/*************************************************
Function name: delay
Parameter    : times
Description : 延时函数
Return : void
Argument     : void
Autor & date : Daniel
**************************************************/
void delay(int times)
{
    int i,j;
    for(i=0;i<times;i++)
       for(j=0;j<400;j++);
}
/*************************************************
Function name: Main
Parameter    : void
Description : 主功能函数
Return : void
Argument     : void
Autor & date : Daniel
**************************************************/
void Main(void)
{
    /*时钟初始化*/
    Set_Clk();
    
    /*adc初始化*/
    adc_init();
    
    while(1)
    {
    /*读取转换值*/
        adc_value=ReadAdc(0);


delay(1000) ;
    }
}


/*************************************************
Function name: adc_init()
Parameter    : int channel
Description : adc初始化
Return : void
Argument     : void
Autor & date : Daniel
**************************************************/ 
void adc_init(void)
{

//选择输入通道,AIN0,对应开发板上W1可调电阻
int channel=0; 

/*设置分频时钟*/
preScaler = ADC_FREQ;
preScaler = 50000000/ADC_FREQ - 1; //PCLK=50M

/*AD转换频率设置,最大频率为2.5MHz*/
rADCCON = (1<<14)|(preScaler<<6)|(channel<<3); //setup channel
delay(1000);


}
    
/*************************************************
Function name: ReadAdc(int channel)
Parameter    : int channel
Description : 获取AD 转换后的值
Return : int
Argument     : void
Autor & date : Daniel
**************************************************/    
int ReadAdc(int channel)
{
   /*开始AD转换*/
   rADCCON |= 0x01; //start ADC

   while(rADCCON & 0x1); //check if Enable_start is low

   /*检查转换是否结束*/
   while(!(rADCCON & 0x8000)); //check if EC(End of Conversion) flag is high
    
   return ( (int)rADCDAT0 & 0x3ff );

/*************************************************
Function name: Set_Clk()
Parameter    : void
Description : 设置CPU的时钟频率
Return : void
Argument     : void
Autor & date : Daniel
**************************************************/
void Set_Clk(void)
{
int i;
U8 key;
U32 mpll_val = 0 ;
i = 2 ;             //don't use 100M!
                //boot_params.cpu_clk.val = 3;
switch ( i ) {
case 0: //200
key = 12;
mpll_val = (92<<12)|(4<<4)|(1);
break;
case 1: //300
key = 13;
mpll_val = (67<<12)|(1<<4)|(1);
break;
case 2: //400
key = 14;
mpll_val = (92<<12)|(1<<4)|(1);
break;
case 3: //440!!!
key = 14;
mpll_val = (102<<12)|(1<<4)|(1);
break;
default:
key = 14;
mpll_val = (92<<12)|(1<<4)|(1);
break;
}

//init FCLK=400M, so change MPLL first
ChangeMPllValue((mpll_val>>12)&0xff, (mpll_val>>4)&0x3f, mpll_val&3);   //set the register--rMPLLCON
ChangeClockDivider(key, 12);    //the result of rCLKDIVN [0:1:0:1] 3-0 bit
cal_cpu_bus_clk();    //HCLK=100M   PCLK=50M
}
/*************************************************
Function name: cal_cpu_bus_clk
Parameter    : void
Description : 设置PCLK\HCLK\FCLK的频率
Return : void
Argument     : void
Autor & date : Daniel
**************************************************/
static void cal_cpu_bus_clk(void)
{
static U32 cpu_freq;
    static U32 UPLL;

U32 val;
U8 m, p, s;

val = rMPLLCON;
m = (val>>12)&0xff;
p = (val>>4)&0x3f;
s = val&3;


//(m+8)*FIN*2 不要超出32位数!
FCLK = ((m+8)*(FIN/100)*2)/((p+2)*(1<<s))*100;     //FCLK=400M  FIN=12000000

val = rCLKDIVN;
m = (val>>1)&3;
p = val&1;
val = rCAMDIVN;
s = val>>8;

switch (m) {
case 0:
HCLK = FCLK;
break;
case 1:
HCLK = FCLK>>1;
break;
case 2:
if(s&2)
HCLK = FCLK>>3;
else
HCLK = FCLK>>2;
break;
case 3:
if(s&1)
HCLK = FCLK/6;
else
HCLK = FCLK/3;
break;
}

if(p)
PCLK = HCLK>>1;
else
PCLK = HCLK;

if(s&0x10)
cpu_freq = HCLK;
else
cpu_freq = FCLK;

val = rUPLLCON;
m = (val>>12)&0xff;
p = (val>>4)&0x3f;
s = val&3;
UPLL = ((m+8)*FIN)/((p+2)*(1<<s));
UCLK = (rCLKDIVN&8)?(UPLL>>1):UPLL;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值