【ARM】2410裸机系列-ADC数模转换

时间:2021-10-23 23:09:08

开发环境

 

1、硬件平台:FS2410

2、主机:Ubuntu 12.04


ADC寄存器配置

      1、初始化ADC(ADCCON)

【ARM】2410裸机系列-ADC数模转换

设置预分频,预分频因子,选择A/D转换通道,并选择正常模式且启动转换

      2、判断转换是否结束(ADCCON[5])

      3、读取转换结果(ADCDATn)

【ARM】2410裸机系列-ADC数模转换


串口的初始化

见《串口通信·FS2410》:http://infohacker.blog.51cto.com/6751239/1223137


源代码

      //adc.c

#include "2410addr.h"      //头文件,包含寄存器的设置

void putc(char ch)      //发送字符
{
while(!(rUTRSTAT0 & 0X2)); //判断发送缓冲区是否为空 rUTXH0 = ch; //发送
} char getc(void) //接收字符
{
while(!(rUTRSTAT0 & 0X1)); //判断接收缓冲区是否为空 return rURXH0; //接收
} void putstr(char *str) //发送字符串
{
while(*str != '\0')
{
if(*str == '\n')
{
putc('\n');
putc('\r');
}
else
putc(*str); str++;
}
} void wait(long long max) //延迟函数
{
for(; max > 0; max--)
;
} void itoa(unsigned long value) //进行字符转换
{
unsigned long temp;
int i, flag = 0;
char data[4]; for(i = 0; i < 4; i++) //清空数组
data[i] = '\0'; for(i = 0; i < 3; i++)
{
temp = (value >> 4 * i) & 0xf; if(temp < 10)
data[2 - i] = '0' + temp;
else
{
if(temp <= 0xf)
data[2 - i] = 'a' + temp - 10;
}
} putstr(" Value of AIN0 is 0x");
putstr(data); putstr("\r");
putstr("\n");
} int main(void)
{
rGPHCON = rGPHCON & ~(0xf << 4) | (0xa << 4); //配置RDX0与TDX0
rGPHUP = 0X0c; //设置上拉电阻 rUFCON0 = 0; //不用FIFO</span></span>
rUMCON0 = 0; //不用流控</span></span>
rULCON0 = 0X3; //8位数据,1个停止位,无校验,正常模式
rUCON0 = 0X5; //时钟源位PCLK,中断方式为查询
rUBRDIV0 = ((int)(50000000 / 115200 / 16) - 1); //设置波特率为115200
rADCCON = 0x4c41; //初始化ADC putstr("\nThis is ADC test:\n"); unsigned long temp = 0; while(1)
{
while((rADCCON & (1 << 15)) == 0); //判断是否转换结束 temp = rADCDAT0; //读取转换值
itoa(temp); rADCCON = 0x4c41; //再启动ADC
wait(200000);
} return 0;
}

     //start.S

.text
.global _start
_start:
#define WATCHDOG 0x53000000
ldr r0, =WATCHDOG
mov r1, #0
str r1, [r0] ldr sp, =1024*4
bl main loop:
b loop

     //Makefile

adc.bin: start.S adc.c
arm-linux-gcc -c start.S -o start.o
arm-linux-gcc -c adc.c -o adc.o
arm-linux-ld -Ttext 0x30008000 start.o adc.o -o adc
arm-linux-objcopy -O binary -S adc adc.bin clean:
rm -f *.o adc.bin

 


编译链接

【ARM】2410裸机系列-ADC数模转换

 

     OK,成功!