Has anyone tried the DAC on a pic16f18855

Status
Not open for further replies.
I tried this the outcome not what i was thinking it's just doing the the last statement.
C:
 while (1)
    {
      for(count=0; count<=32; count++)
        {
            DAC_SetOutput(count);
            __delay_ms(10);

        }
       for(count=32; count>=0; count--)
        {
            DAC_SetOutput(count);
            __delay_ms(10);

        }
    }
so I tried this
Code:
while (1)
    {
      do
        {
            DAC_SetOutput(count);
            __delay_ms(10);
            count = count++;
        }while (count < 32);
     
      do
        {
            DAC_SetOutput(count);
            __delay_ms(10);
            count = count--;
       
        }while (count > 0);
       count = 0;
    }
which is kind of what im looking for but it puts a line in middle going from 0 strait up too 5 volts.
Well i figured that out no more line. Can't use 32 it's 0 to 31 for the 32 bits.
 
Last edited:
Now to make it look like sine wave I no it not be a good one but I figure Id go from 0 to 31 in steps
 
I got bored so made a table in excel,
Code:
const char table[128]={
    16,16,17,18,19,19,20,21,22,22,23,24,24,25,26,26,
    27,27,28,28,29,29,30,30,30,31,31,31,31,31,32,32,
    32,32,32,31,31,31,31,31,30,30,30,29,29,28,28,27,
    27,26,26,25,24,24,23,22,22,21,20,19,19,18,17,16,
    16,15,14,13,12,12,11,10, 9, 9, 8, 7, 7, 6, 5, 5,
     4, 4, 3, 3, 2, 2, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0,
     0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 3, 3, 4,
     4, 5, 5, 6, 7, 7, 8, 9, 9,10,11,12,12,13,14,15
};

Mike.
Edit and a 64 byte table.
Code:
const char table[128]={
    16,17,19,20,22,23,24,26,27,28,29,30,30,31,31,32,
    32,32,31,31,30,30,29,28,27,26,24,23,22,20,19,17,
    16,14,12,11, 9, 8, 7, 5, 4, 3, 2, 1, 1, 0, 0, 0,
     0, 0, 0, 0, 1, 1, 2, 3, 4, 5, 7, 8, 9,11,12,14
};
 
Last edited:
Can't use 32 that's out range it makes a flat line going strait in the middle up. 0 to 31 works But thanks tho
how would i change the above just get rid of the 32 i guess and call it 60 byte table
 
Whoops, here's both tables adjusted.
Code:
const char table[64]={
    16,17,19,20,22,23,24,25,27,28,28,29,30,30,31,31,
    31,31,31,30,30,29,28,28,27,25,24,23,22,20,19,17,
    16,14,13,11,10, 8, 7, 6, 5, 4, 3, 2, 1, 1, 0, 0,
     0, 0, 0, 1, 1, 2, 3, 4, 5, 6, 7, 8,10,11,13,14
};

const char table[128]={
    16,16,17,18,19,19,20,21,22,22,23,24,24,25,25,26,
    27,27,28,28,28,29,29,30,30,30,30,31,31,31,31,31,
    31,31,31,31,31,31,30,30,30,30,29,29,28,28,28,27,
    27,26,25,25,24,24,23,22,22,21,20,19,19,18,17,16,
    16,15,14,13,13,12,11,10,10, 9, 8, 8, 7, 6, 6, 5,
     5, 4, 4, 3, 3, 2, 2, 2, 1, 1, 1, 1, 0, 0, 0, 0,
     0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 3, 3, 4, 4,
     5, 5, 6, 6, 7, 8, 8, 9,10,10,11,12,13,13,14,15
};

Mike.
 
Well I tried this out on ardiuno so i could see the output guess i done it wrong just getting 0 lol
Code:
const char table[128]={
    16,17,19,20,22,23,24,26,27,28,29,30,30,31,31,32,
    32,32,31,31,30,30,29,28,27,26,24,23,22,20,19,17,
    16,14,12,11, 9, 8, 7, 5, 4, 3, 2, 1, 1, 0, 0, 0,
     0, 0, 0, 0, 1, 1, 2, 3, 4, 5, 7, 8, 9,11,12,14
};
void setup() {
  Serial.begin(9600);

}
void loop() {
for (byte i = 0; i < 128; i = i + 1) {
  Serial.println(table[i]);
  delay(10);
}

}
 
Try this,
Code:
const char table[128]={
    16,16,17,18,19,19,20,21,22,22,23,24,24,25,25,26,
    27,27,28,28,28,29,29,30,30,30,30,31,31,31,31,31,
    31,31,31,31,31,31,30,30,30,30,29,29,28,28,28,27,
    27,26,25,25,24,24,23,22,22,21,20,19,19,18,17,16,
    16,15,14,13,13,12,11,10,10, 9, 8, 8, 7, 6, 6, 5,
     5, 4, 4, 3, 3, 2, 2, 2, 1, 1, 1, 1, 0, 0, 0, 0,
     0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 3, 3, 4, 4,
     5, 5, 6, 6, 7, 8, 8, 9,10,10,11,12,13,13,14,15
};

void setup(){
  Serial.begin(9600);
}

void loop(){
  for (byte i = 0; i < 128; i = i + 1) {
    Serial.println(table[i],DEC);
    delay(10);
  }
}

Note the DEC added to the print and changed the table to the 128 entry one.

Mike.
 
Well i got back to playing with this for some reason you can't use 0 to 128 as the count it has to be 0 to 127.
here a picture of 0 to 128 not bad.

shows the spike from count being 128
And here count is 127
 
Here my code i'm using now
Code:
 uint8_t count=0;

 DAC_Initialize();
 const char table[128]={
    16,16,17,18,19,19,20,21,22,22,23,24,24,25,25,26,
    27,27,28,28,28,29,29,30,30,30,30,31,31,31,31,31,
    31,31,31,31,31,31,30,30,30,30,29,29,28,28,28,27,
    27,26,25,25,24,24,23,22,22,21,20,19,19,18,17,16,
    16,15,14,13,13,12,11,10,10, 9, 8, 8, 7, 6, 6, 5,
     5, 4, 4, 3, 3, 2, 2, 2, 1, 1, 1, 1, 0, 0, 0, 0,
     0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 3, 3, 4, 4,
     5, 5, 6, 6, 7, 8, 8, 9,10,10,11,12,13,13,14,15
};

    while (1)
    {
       for(count=0; count<=127; count++)
       {
          DAC_SetOutput(table[count]);
        __delay_ms(10);
  }
}
}
 
Well i got back to playing with this for some reason you can't use 0 to 128 as the count it has to be 0 to 127.

You must be having a bad day

0-127 is a count of 128, 0-128 is a count of 129 - so exceeding the limit of the 128 bytes of data.

For a 5 bit D2A it's not looking bad though.
 
Yep I kind of thought of that after I posted but it really not bad for a 5 bit D2A
I got get my hands on a 8 bit or maybe a 10
 
Have a look at such as the dsPIC33FJ128GP802-I/SP

That has a 16 bit audio DAC, plus a 10 / 12 bit ADC.
Also 128K program FLASH and 16K RAM, among other things.
Available in a 28 pin DIP package for around £4.00

 
Note that <128 is the same as <=127 unless the variable is signed.

Mike.
 
In the first code posted the type was byte and in the second uint8_t. So maybe byte is signed - unit8_t (which I much prefer) isn't.

Mike.
 
Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…