View previous topic :: View next topic |
Author |
Message |
arunkish
Joined: 23 Dec 2008 Posts: 83
|
AD7528JN |
Posted: Tue May 03, 2011 3:19 am |
|
|
Deal All
I am using AD7528JN along with PIC to convert digital to analog outputs. However I want to confirm if I am using the chip in the right way. Here is my code.
Code: |
#include <18F4620.H>
#fuses HS,PROTECT,PUT,NOBROWNOUT,NOLVP,NOWDT
#use delay(clock=20000000)
#byte PORTB = 0xf81
#byte TRISB = 0xf93
#byte TRISC = 0xf94
#byte PORTC = 0xf82
#bit WR = PORTC.0
#bit CS = PORTC.1
#bit DAC = PORTC.2
void main(void)
{
int i;
int j;
set_tris_c(0x00);
set_tris_b(0x00);
i=128;
WR=0;delay_ms(100);
CS=0;delay_us(100);
DAC=0;delay_us(50);
PORTB=128;
DAC=1;delay_us(50);
PORTB=128;
// WR=1;
delay_ms(2000);
while(1)
{
for(i=100; i<=140; i=i+1)
{
DAC=0;PORTB=i;delay_us(10);
DAC=1;PORTB=128;delay_us(10);
delay_ms(250);
}
for(i=100; i<=140; i=i+1)
{
DAC=0;PORTB=128;delay_us(10);
DAC=1;PORTB=i; delay_us(10);
delay_ms(250);
}
}
}
|
Thanks |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Tue May 03, 2011 11:19 am |
|
|
I would have made it into a routine, called write_dac() or ad7528_write().
Then call the routine in main(). Pass the dac value as a parameter.
Example of how it would be called:
|
|
|
arunkish
Joined: 23 Dec 2008 Posts: 83
|
|
Posted: Tue May 03, 2011 10:35 pm |
|
|
Dear PCM Programmer
Thank you for your advice. I want to know if the delay and the way in which I am passing the value to the DAC port is right or not.
Thanks |
|
|
FvM
Joined: 27 Aug 2008 Posts: 2337 Location: Germany
|
|
Posted: Wed May 04, 2011 12:47 am |
|
|
Quote: | I want to know if the delay and the way in which I am passing the value to the DAC port is right or not. |
Not exactly. You have enabled /CS and /WR permanently and are only toggling the select line. So both DAC outputs will show glitches. You should preferably strobe the /WR line low after setting a new value. /CS can be hardwired and don't need a separate pin, as long as you don't intend to drive multiple devices.
Code: | DAC=0;PORTB=val_a;WR=0;WR=1;delay_us(10);
DAC=1;PORTB=val_b;WR=0;WR=1;delay_us(10); |
|
|
|
|