📄 adc_portd.c
字号:
#include <stdio.h>
#include <pic.h>
#include "usart.h"
__CONFIG(WDTDIS & HS & LVPDIS & DEBUGDIS);//配置位
void DELAY1(int time);
void USART(int a);
void INITIAL(void);
void init_a2d(void);
unsigned char read_a2d(unsigned char channel);
void main(void)
{
unsigned char i,j,press1,press2;
init_comms(); //串口初始化
init_a2d(); //AD转换初始化
ADCON1=0X07;
for(i=0;i<1000;i++)
{
press1=read_a2d(0);
press2=read_a2d(1);
USART(press1);//发送的是8位的二进制数据
USART(press2);
}
}
void DELAY1(int time)
{
int x,y;
for(x=0;x<1000;x++)
{
for(y=0;y<time;y++);
}
}
void USART(int a)//发送一个字节
{ TXSTA_5=0X01;
while(!TXIF)
continue;
TXREG=a;
}
void INITIAL(void)//波特率设置
{ SPBRG=23;//波特率9600//
TRISC=0XFF;//设置端口c//
TXSTA=0XA6;//10100110//
RCSTA=0XB0;//10110000//
PIR1=0X00;//第一外设中断标志寄存器//
}
void init_a2d(void)//AD初始化
{
ADCON0=0; // select Fosc/8//
ADCON1=0; // select left justify result. A/D port configuration 0//
ADON=1; // turn on the A2D conversion module//
ADIE=0;//关ad中断
}
unsigned char read_a2d(unsigned char channel)//读取AD值
{
channel&=0x07; //00000111 truncate channel to 3 bits
ADCON0&=0x85; //11000101 clear current channel select
ADCON0|=(channel<<3); // apply the new channel select
DELAY1(1);
ADGO=1; // initiate conversion on the selected channel
while(ADGO)continue;
return(ADRESH); //这里只返回了转换值的高8bit,能满足一般精度并简化程序
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -