📄 test-adc.c
字号:
/*********************************************************************
* File: test-adc.c
* Author:
* Desc:
* History:
*********************************************************************/
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
/* cpu main clock */
#define MCLK 64000000
/* ADC register */
#define rADCCON (*(volatile unsigned long *)0x01D40000)
#define rADCPSR (*(volatile unsigned long *)0x01D40004)
#define rADCDAT (*(volatile unsigned long *)0x01D40008)
/* bits definitions */
#define CON_FLAG (1<<6)
#define CON_SLEEP (1<<5)
#define CON_AIN0 (0<<2)
#define CON_AIN1 (1<<2)
#define CON_AIN2 (2<<2)
#define CON_AIN3 (3<<2)
#define CON_AIN4 (4<<2)
#define CON_AIN5 (5<<2)
#define CON_AIN6 (6<<2)
#define CON_AIN7 (7<<2)
#define CON_READ_START (1<<1)
#define CON_ENABLE_START (1<<0)
inline void start_adc(int ain)
{
/* Start A/D conversion */
rADCCON = CON_ENABLE_START|ain;
/* To avoid The first FLAG error case. */
while(rADCCON & CON_ENABLE_START);
}
inline void stop_adc(void)
{
rADCCON = CON_SLEEP;
}
inline void set_clock(unsigned char prescaler)
{
rADCPSR = (MCLK / 2 / 16 / (prescaler + 1));
printf("ADC conversion freq. = %dHz\n", (MCLK / 2 / 16 / (prescaler + 1)));
}
int main(int argc, char** argv)
{
int i;
printf("Test ADC, if AIN2 == AIN4 then exit...\n");
set_clock(20);
for(;;)
{
start_adc(CON_AIN2);
while(!(rADCCON & CON_FLAG));
//To avoid The second FLAG error case
for(i=0; i<rADCPSR; i++);
printf("AIN2 = 0x%03x\r", rADCDAT);
}
stop_adc();
printf("\n");
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -