📄 adc_186.c
字号:
// ANALOGUE TO DIGITAL CONVERTER HARDWARE INTERFACE PROGRAM
// COPYRIGHT PROPERTY OF ALPHADATA DESIGNS LIMITED (c) 1999
// WRITTEN FOR THE MAX186 12 BIT 8-CHANNEL MICRO-WIRE A TO D
// published by permission of Alphadata designs on
// Hi-Tech C website, http://www.workingtex.com/htpic. Thanks!
//------------------------------------------------------
// Version History
//------------------------------------------------------
// Issue 1.0 : 21/12/1999 : First Officially Released
//------------------------------------------------------
#include "ioh8314.h"
#include "h8genlib.h"
#include "spi.h"
extern byte p4dr;
/* call this to set up the hardware ports */
void adc_initialise(void)
{
}
/* this table makes up the muddled way the device is addressed */
/* see the data sheet for the MAX186 to see why ! */
const byte command_table[]={ 0b10001111,
0b11001111,
0b10011111,
0b11011111,
0b10101111,
0b11101111,
0b10111111,
0b11111111};
/* call this to read one channel of data from the a to d */
word adc_read_channel(byte channel_number)
{
byte bits;
byte command;
int data;
spi_adc_select();
/* Start by sending command byte */
command = command_table[channel_number];
for (bits=0;bits<8;bits++) {
if ((command & 0x80) == 0) spi_lodata(); else spi_hidata();
command = command << 1;
spi_hiclock();
spi_loclock();
}
spi_lodata();
spi_hiclock();
spi_loclock();
data = 0;
for (bits=0;bits<12;bits++) {
data = (data << 1);
if (P4DR & 0b10000000) data = data | 0x0001;
spi_hiclock();
spi_loclock();
}
spi_adc_deselect();
return (data);
}
// does an average reading
word adc_filtered_read(byte channel,int readings)
{
long av;
word lp;
av = 0;
for (lp=0;lp<readings;lp++) {
av = av + adc_read_channel(channel);
}
return (word)(av / readings);
}
// end
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -