tiny6410 ad转换

#include "gpio.h"
#include "adcts.h"
#include "uart.h"
// adccon 

void AdInit(u32 freq)
{
	u32 TEMP;
	SYSC_GetClkInform(); 
	TEMP = g_PCLK/freq -1;
	ADCTS->rADCCON = (1<<14)|(TEMP<<6);
	return ;
}
//判断是否已经转换好
u32 ReadData(void)
{
	ADCTS->rADCCON |= 0x1;			//start ADC

	while (ADCTS->rADCCON & 0x1);	//check if Enable_start is low
	while (!(ADCTS->rADCCON & 0x8000));	//check if EC(End of Conversion) flag is high

	return ( (int)ADCTS->rADCDAT0 & 0x3ff );
	}

void OutRead(u32 x)
{
	u8 TEMP;
	
	TEMP = (u8)x/1000;
	Sendchar(TEMP);
	x = x%1000;
	TEMP = (u8)x/100;
	Sendchar(TEMP);
	x %= 100;
	TEMP = (u8)x/10;
	Sendchar(TEMP);
	
	x = (u8)x%10;
	Sendchar(x);
}

void delay(int times)
{
    int i;
    for(;times>0;times--)
      	for(i=0;i<3000;i++);
}	

int main()
{
	u32 temp;
	UartInit();
	BaudInit(9600);
	AdInit(2500000);
	Sendchar('r');
	do{
		temp = ReadData();
		OutRead(temp);
		delay(8000);
 } while(Getchar() != 'o');
 }

相关的register,ADCCON  ,ADCDAT0

adccon 设定ad channel 及转换频率,以及使能,还有转换的位数

adcdatx 就是存储的相关值,ADCTS->rADCCON |= 0x1; //start ADC
while (ADCTS->rADCCON & 0x1); //check if Enable_start is low
while (!(ADCTS->rADCCON & 0x8000)); //检测结束
 (int)ADCTS->rADCDAT0 & 0x3ff );  这是所得的值


很简单,不难

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值