源代码及注释
#define GLOBAL_CLK 1
#include
#include
#include "def.h"
#include "option.h"
#include "2440addr.h"
#include "2440lib.h"
#include "2440slib.h"
#include "mmu.h"
#include "memtest.h"
#include "Mylib.h"
#define LED1 (1 << (5 * 2))
#define LED2 (1 << (6 * 2))
#define LED3 (1 << (7 * 2))
#define LED4 (1 << (8 * 2))
#define LEDS (LED1 | LED2 | LED3 | LED4)
#define LED1_ON ~(1<<5)
#define LED2_ON ~(1<<6)
#define LED3_ON ~(1<<7)
#define LED4_ON ~(1<<8)
#define LED1_OFF (1<<5)
#define LED2_OFF (1<<6)
#define LED3_OFF (1<<7)
#define LED4_OFF (1<<8)
void init()
{
cal_cpu_bus_clk();//在MyLib.h中有定义
Uart_Init(0,115200);//PCLK、波特率为115200
Uart_Select(0);//选择串口0
My_Logo();//在MyLib.h中有定义
}
int ADC_Test()
{
int temp;
rADCCON = (1<<14)|(0x31<<6)|(0<<3);//预分频器使能、预分频值为49、通道0
rADCCON |=0x1;// A/D开始转换,开始之后,此位被清除
while(rADCCON&0x1);//检测有没有开始
while(!(rADCCON&0x8000));//检测转换是否结束
temp = rADCDAT0&0x3ff;//返回转换值,正常A/D转换的数值只能通过ADCDAT0寄存器的[9:0]位读取
return temp;
}
void Main(void)
{
int temp,Value;
U8 key;
init();
MMU_Init();//内存初始化
rGPBCON = LEDS; //初始化LED相应引脚为输出
rGPBDAT = rGPBDAT|(LED1_OFF)|(LED2_OFF)|(LED3_OFF)|(LED4_OFF); // LED 灭
rGPBDAT &= ~(0x1);
Uart_Printf("nnnPress 'Enter' to refresh the Screenn");//终端提示
while(1)
{
temp = ADC_Test();//调用A/D转换函数
Value = ((3300/1024)*temp)*1000;//转换成电压值,单位mV,Value值比实际值放大了1000倍
key=Uart_Getch();//等待从键盘接收按键
Beep(2000,100);//蜂鸣器发声函数
// 刷新A/D转换,原因很简单,我们往往在等待按键时调整电位器
temp = ADC_Test();
Value = ((3300/1024)*temp)*1000;
rGPBCON = LEDS; //还原GPB的配置,原因调用了蜂鸣器发声函数,改变了GPB原先的配置
if(key==ENTER_KEY)//判断是否按了Enter键
{
key=0;//按键清零
rGPBDAT = (~temp)&(~0x1);//输出到LED显示,务必把GPB0清零,否则你将会有麻烦 ^|^
Uart_Printf("nnn%d %% 1000000 %(V%)n",Value);//把电压值输出到终端显示
}
else
{
key=0;
Uart_Printf("nnnPress the 'Enter' key!n");//按键非Enter键,提示按键
}
}
}
程序结果
声明:本人将不会再发表其它相关ARM裸机源代码,包括上传共享资料,望见谅!
|