PIC16F877A 模数转换Demo

#include <htc.h>
#include <stdio.h>

__CONFIG(0xFF32);

#define u8 unsigned char
#define u16 unsigned int

void delayus()
{
	int i;
	for(i=200;i--;);
}

/********延时程序********/
void Delay_ms(u16 xms)
{
 	int i,j;
 	for(i=0;i<xms;i++)
 		{ for(j=0;j<71;j++) ; }
}

void port_init(void)
{
	PORTA=0xFF;
	TRISA=0x01;		//RA0输入
	ADCON1=0x8e;	//右对齐,RA0做模拟输入,其他做普通IO
	ADCON0=0x41;	//0:01 000 001	fosc/8,RA0,使能AD
	delayus();		//配置完后需要延时一会
}

void putch(u8 c)
{
	TXREG = c;
	while(TRMT==0);
}


void usart_init(void)
{
	TXSTA=0x24;	//开启发射使能位、高波特率,TRMT初始值可0可1
	RCSTA=0x80;
	SPBRG=0x19;	//4M晶振,波特率9600,则SPBRG初值为25	
}

void main()
{
	int result=0;
	port_init();
	usart_init();
	printf("hello!\n");
	
	while(1)
	{
		GO_DONE=1;
		while(GO_DONE);
		result=ADRESL+(ADRESH<<8);
		printf("result=%d\n",result);
		Delay_ms(500);
	}
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值