CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

DA conversion

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
Foppie



Joined: 16 Sep 2005
Posts: 138
Location: The Netherlands

View user's profile Send private message Send e-mail Visit poster's website MSN Messenger

DA conversion
PostPosted: Fri Sep 16, 2005 6:27 am     Reply with quote

Hello,

I coded a PIC16F877A so that it could make a Analog Output Signal using an AD5312 chip but it doesn't seem to work. Has anybody have any suggestions?

my code:
Code:
#device PIC16F877A *=16 ADC=8
#include <drivers/16f877.h>
#FUSES NOWDT       //No Watch Dog Timer
#FUSES HS       //High speed Osc (> 4mhz)
#FUSES NOPROTECT    //Code not protected from reading
#FUSES NOLVP       //Low Voltage Programming on B3
#FUSES NOCPD       //No EE protection
#FUSES NOWRT       //Program memory not write protected
#use delay(clock=20000000)
#use rs232(baud=9600, BRGH1OK, parity=n, bits=8, xmit=PIN_C6, rcv=PIN_C7)

//LED
#define LED      PIN_D7   // LED Output          (D1)
#define LED1      PIN_D0   // LED 1 Output      (D6)
#define LED2      PIN_D1   // LED 2 Output      (D5)
#define LED3      PIN_D2   // LED 3 Output      (D7)
#define LED4      PIN_D3   // LED 4 Output      (D8)

//DAC (U10, U9)
#define DAC_LDAC   PIN_B4   // Load DAC signal
#define CS_DAC      PIN_B2   // Chip select signal for DAC
#define SDI      PIN_C4   // SPI Data In
#define CLK      PIN_C3   // SPI Clock

void SPI_clockcycle()
// Version 1.0 12.9.2005
// Input  : void
// Output : void
// Action : takes clocksignal low and high
{
   output_low(CLK);
   delay_us(20);
   output_high(CLK);
   delay_us(20);
}

void SPI_writebit(short data)
// Version 1.0 12.9.2005
// Input  : void
// Output : void
// Action : puts a bit on the data in bus
{
   if(data)
      output_high(SDI);
   else
      output_low(SDI);
   delay_us(10);
   SPI_clockcycle();
}

void DAC_convert(short outputSelect, long data)
// Version 1.0 12.9.2005
// Version 1.1 13.9.2005
//    - revised parameters (from 1,1,1,1,8,1,1 to 1,16)
// Input  : outputSelect- select output A (0) or output B (1)
//        data - Data bytes (6 MSb get negated)
// Output : void
// Action : converts a digital (10 bits) value to a analog output
{
   long i;
   delay_us(20);
   output_high(CLK); //SCLK pulse signal
   delay_us(20);
   output_low(CS_DAC); //SYNC input signal
   delay_us(20);

   //write control bits
   SPI_writebit(outputSelect);
   SPI_writebit(1);
   SPI_writebit(0);
   SPI_writebit(0);

   output_low(DAC_LDAC);

   //write 10 databits
   for(i = 0b1000000000; i; i >>= 1)
   {
      SPI_writebit(i == (data & i));
   }

   //write last 2 bits to complete total of 16 (sending zeros)
   output_low(SDI);
   SPI_clockcycle();
   SPI_clockcycle();

   output_high(CS_DAC);

   //put value on line out
   delay_us(20);
   output_high(DAC_LDAC);
}

void init()
// Version 1.0 12.9.2005
// Input  : void
// Output : void
// Action : initializes program
{
   ADC_init(); // initializes ADC

   output_high(CS_DAC); // makes sure DAC is not selected
   output_high(DAC_LDAC);

   output_high(CS_USART); // makes sure USART is not selected
   output_low(IRQ_USART);
}

void testDAC()
{
   short ledon = TRUE;
   int i = 0;

   while(1)
   {
      ledon = !ledon;
      DAC_convert(0,i * 4000);
      DAC_convert(1,i * 4000);
      i++;
      if (i > 15)
      {
         i = 0;
      }
      delay_ms(500);
      ChangeLed(LED1, ledon);
      ChangeLed(LED2, ledon);
      ChangeLed(LED3, ledon);
      ChangeLed(LED4, ledon);
   }
}

void main()
{
   ChangeLed(LED, TRUE);
   
   init();   
   testDAC();
}


when I run the code the Main Led is continually on and the other 4 leds are blinking as expected but there doesn't seem to be any other activity. I can not even find any other changing signals on the board.

Please help
rwyoung



Joined: 12 Nov 2003
Posts: 563
Location: Lawrence, KS USA

View user's profile Send private message Send e-mail

PostPosted: Fri Sep 16, 2005 7:12 am     Reply with quote

In your main() you need to prevent the PIC from hitting the SLEEP instruction compiled after your last function call. Add
Code:
while(1);
as your last statement and try again.
_________________
Rob Young
The Screw-Up Fairy may just visit you but he has crashed on my couch for the last month!
Ttelmah
Guest







PostPosted: Fri Sep 16, 2005 7:34 am     Reply with quote

That shouldn't apply. His 'TestDAC' function contains an endless loop, so never returns.
However the code is not posted for 'ChangeLed', making working out what is going on hard.
One obvious question is how the activity is being 'looked' for, since with the long (500mSec) delay in this loop, the activity on the DAC lines, is going to be very hard to see, without a digital, or storage sope.

Best Wishes
rwyoung



Joined: 12 Nov 2003
Posts: 563
Location: Lawrence, KS USA

View user's profile Send private message Send e-mail

PostPosted: Fri Sep 16, 2005 1:31 pm     Reply with quote

Ttelmah wrote:
That shouldn't apply. His 'TestDAC' function contains an endless loop, so never returns.


Yep, shouldn't scroll through the listing so fast next time.

For 16F877 parts I generally enable the power up timer (PUT fuse).

If you look at the SPI lines, do you see a clock (use an oscilloscope, not a DMM)?

Are you sure you are intpreting the datasheet for the DAC correctly? You have a pin named "SDI" but generally that would be the INPUT data pin to the PIC's SPI port and the PIC would then have a pin called SDO which is its output bit. The PIC (master) connects its SDO to the DAC's (slave) SDI pin. Personally I prefer the Motorola terms MOSI (Master Out, Slave IN) and MISO (Master In, Slave Out) for labeling SPI pin eventhough they aren't what "true" SPI parts call the signals. MOSI/MISO are very descripting and you KNOW exactly where the signals are supposed to go.
_________________
Rob Young
The Screw-Up Fairy may just visit you but he has crashed on my couch for the last month!
Guest








PostPosted: Mon Sep 19, 2005 12:30 am     Reply with quote

Ttelmah wrote:
However the code is not posted for 'ChangeLed', making working out what is going on hard.

Sorry, forgot to post that code.
Code:
void ChangeLed(int channel, short led_on)
// Version 1.0 8.9.2005
// Input  : channel - address of led
//        led_on - state of led
// Output : void
// Action : Set or clear specified Leds.
// Using in main program like ex. ChangeLed(LED1,FALSE);  then led 1 is off
{
   if (led_on)
   {switch (channel)
      {
             case LED:  output_high(LED);
            break;
             case LED1: output_high(LED1);
           break;
             case LED2: output_high(LED2);
            break;
             case LED3: output_high(LED3);
            break;
             case LED4: output_high(LED4);
           break;
      }
   }
   else
   {
      switch (channel)
      {
             case LED:  output_low(LED);
            break;
             case LED1: output_low(LED1);
           break;
             case LED2: output_low(LED2);
            break;
             case LED3: output_low(LED3);
            break;
             case LED4: output_low(LED4);
           break;
      }
   }
}
Foppie



Joined: 16 Sep 2005
Posts: 138
Location: The Netherlands

View user's profile Send private message Send e-mail Visit poster's website MSN Messenger

PostPosted: Mon Sep 19, 2005 4:06 am     Reply with quote

Ttelmah wrote:
One obvious question is how the activity is being 'looked' for, since with the long (500mSec) delay in this loop, the activity on the DAC lines, is going to be very hard to see, without a digital, or storage scope.


I will do that, but I'll have to get one... But I was assuming I could just look at the output from my circuit, that should get higher (in steps) and after approximatly 7.5 seconds dropping low again.

I now have only a DMM but tomorrow I'll try to get my hands on a scope
Foppie



Joined: 16 Sep 2005
Posts: 138
Location: The Netherlands

View user's profile Send private message Send e-mail Visit poster's website MSN Messenger

PostPosted: Mon Sep 19, 2005 4:22 am     Reply with quote

rwyoung wrote:
Are you sure you are intpreting the datasheet for the DAC correctly? You have a pin named "SDI" but generally that would be the INPUT data pin to the PIC's SPI port and the PIC would then have a pin called SDO which is its output bit. The PIC (master) connects its SDO to the DAC's (slave) SDI pin. Personally I prefer the Motorola terms MOSI (Master Out, Slave IN) and MISO (Master In, Slave Out) for labeling SPI pin eventhough they aren't what "true" SPI parts call the signals. MOSI/MISO are very descripting and you KNOW exactly where the signals are supposed to go.


I changed SDI for SDO and now it seems to react! (I changed it earlier today but didn't realize my code was stuck on another place so I didn't see the result directly...) I'm still gonna check it with a scope as soon as I have one. I'll post if I know it is (not) working.

But so far, so good!

Ttelmah and rwyoung thanks for your help so far!
Foppie



Joined: 16 Sep 2005
Posts: 138
Location: The Netherlands

View user's profile Send private message Send e-mail Visit poster's website MSN Messenger

PostPosted: Tue Sep 20, 2005 2:06 am     Reply with quote

It is working Very Happy

Thanks again for all your help!

I'll never forget MOSI/MISO and a good scope!
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group