📄 pic_hi-tech_c_ad7390_routines.c
字号:
/*
Freely distributable in this original form.
(c) Feb 2000 Shane Tolmie
(c) Working Technologies, producers of the KeyGhost - record keystrokes on PC with tiny device
Any questions or comments? shane@keyghost.com
Updates to FAQ available from http://www.keyghost.com/htpic
Analog Devices AD7390 micropower 12-bit DAC for Hi-Tech C v7.85 routines for generic
PIC micro.
Routines to use DAC (digital to analogue converter, creates an analogue voltage
from a digital bit stream)
For Hi-Tech C v7.85, on a generic PIC micro.
Tested with PIC16F84, PIC12CE674, and PIC16F876 @ 4Mhz, should work with 16Mhz
Code status: tested and works beautifully.
Sample C:
dac_setup(); //sets up port directions
dac_set(x); //sets voltage between 0V and 5V (or Vref),0000 and 4096 (12-bit)
May need delay routines
*/
#include <pic.h>
#define DAC_LD_BAR RB1 //load strobe. Transfers shift register to DAC register
#define DAC_LD_BAR_DIR TRISB1 // while active low. Equivalent to CS bar
#define DAC_CLK RB3 //clock. Positive, rising edge clocks data into shift register
#define DAC_CLK_DIR TRISB3
#define DAC_DATA RB2 //connects to SDI on DAC. Serial data output. Data loads
#define DAC_DATA_DIR TRISB2 // directly into the shift register of the DAC
void dac_setup(void)
{
DAC_LD_BAR=0;
DAC_CLK=0;
DAC_DATA=0;
DAC_LD_BAR_DIR=OUTPUT;
DAC_DATA_DIR=OUTPUT;
DAC_CLK_DIR=OUTPUT;
}
void dac_set(unsigned int dacval)
{
unsigned char i;
DAC_LD_BAR=LOW;
DelayUs(10);
DAC_CLK=HIGH;
DAC_LD_BAR=HIGH;
DelayUs(3);
dacval<<=4; //only need 12 bits, chop off top four
//clock lowest 12 bits into dac
for(i=1; i<=12; i++)
{
DelayUs(3);
dacval<<=1; //assembles to rotate left into carry
DAC_DATA=CARRY; //data clocked in msb first to lsb last
DAC_CLK=LOW; //pulse the clock, data read in on rising edge
DelayUs(3);
DAC_CLK=HIGH;
}
DelayUs(3);
DAC_LD_BAR=LOW; //latch serial data into dac
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -