|
|
View previous topic :: View next topic |
Author |
Message |
Saad khan
Joined: 29 Apr 2019 Posts: 3
|
PIC24FV16KM202 DAC |
Posted: Mon Apr 29, 2019 5:04 am |
|
|
Hello,
I am quite new to PIC but have worked on ATmel series.
I have several issues in using "PIC24FV16KM202" with CCS compiler, since there is not enough examples with this specific UC.
Can anyone help and point out all the mistakes, i am not reading any output at DAC.
I have connected an LED on PIN_RB14 (DAC2) and a variable voltage source at sANO (RA0).
Code: |
//#device *=24 ICD=TRUE
#device PIC24FV16KM202
#device ICD=TRUE
#device ADC=12
#include <24FV16KM202.h>
#fuses NOPROTECT, DEBUG
#use delay(clock=16M)
#use rs232(baud=9600,UART1)
void main() {
setup_dac(2, VREF_VSS_VDD | DAC_OUTPUT);
setup_dac(2, DAC_ON);
unsigned int8 value, min, max;
printf("Sampling:"); // Printf function included in RS232 library
setup_adc(ADC_CLOCK_INTERNAL); // Built-in A/D setup function
set_adc_channel(0);
value = read_adc();
do {
delay_ms(100); // Built-in delay function
read_adc(ADC_START_ONLY); // Built-in start A/D conversion
// sleep(); // Reduce power and noise during conversion
value = read_adc(ADC_READ_ONLY); // Built-in A/D read function
value = read_adc();
if(value>200)
{
output_high(PIN_B13);
min=value;}
if(value>50 && value<100)
{output_low(PIN_B13);
min=value;}
dac_write(2, value);
printf("\r\nMin: %2X Max: %2X\n\r",min,max);
} while (TRUE);
}
|
|
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9241 Location: Greensville,Ontario
|
|
Posted: Mon Apr 29, 2019 5:32 am |
|
|
I don't use that PIC but I'd start over doing the following.
Program #1, 1Hz LED. Attach an LED and 470r to an I/O pin, toggle that pin to confirm the PIC actually runs as expected.
Program #2, send a value to the DAC, then measure DAC pin.
Program #3, send 0 to DAC ,delay 1/2 second, send 1/4 of DAC FSR,delay 1/2 second, send 1/2 DAC FSR, delay 1/2 second, send 3/4 DAC FSR, delay 1/2 second, send DAS FSR, delay 1/2 second, loop back to start.
If that PIC has 'pin selectable peripherals', you need to assign them BEFORE accessing them. There is a 'sticky' at the top of the forum with notes.
Program 3 should 'step' the output of the DAC, so a DMM, DVM or scope will see it.
Jay |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19537
|
|
Posted: Mon Apr 29, 2019 6:38 am |
|
|
Comments inline:
Code: |
//#device *=24 ICD=TRUE
#include <24FV16KM202.h>
//This should always be first
//#device PIC24FV16KM202
//Not needed it is set in the include file
#device ICD=TRUE
#device ADC=12
#fuses NOPROTECT, DEBUG
#use delay(clock=16M)
//Where is this clock to come from?. You need to tell the compiler
//Either a source selection fuse, or: 'INTERNAL=16M' will say
//to use the internal clock.
#use rs232(baud=9600,UART1,ERRORS)
//Always use 'ERRORS' when using the hardware UART, unless you
//are ading your own error handling code
void main() {
setup_dac(2, DAC_REF_VDD | DAC_OUTPUT | DAC_ON);
//all one line - also your Vref setting was wrong.
unsigned int8 value, min, max;
printf("Sampling:"); // Printf function included in RS232 library
setup_adc(ADC_CLOCK_INTERNAL); // Built-in A/D setup function
set_adc_channel(0);
value = read_adc();
//You need to pause for Tacq between selecting a channel and
//taking a reading. Then you have declared 'value' as an int8, but
//setup read_adc to return an int12. You need to be using an int16
//for 'value'
do {
delay_ms(100); // Built-in delay function
read_adc(ADC_START_ONLY); // Built-in start A/D conversion
// sleep(); // Reduce power and noise during conversion
value = read_adc(ADC_READ_ONLY); // Built-in A/D read function
//You are starting, then reading without time for the ADC to acquire.
//Wrong.
//If you were wanting to use sleep, the sequence would be to call
//sleep, and _then_ start the adc. The sleep always executes the
//following instruction _before_ beginning. You would also need to
//enable INT_ADC so that the ADC completion can wake from the
//sleep.
value = read_adc();
//This is OK with the already made comment about the size of value...
if(value>200)
{
output_high(PIN_B13);
min=value;
}
if(value>50 && value<100)
{output_low(PIN_B13);
min=value;
}
dac_write(2, value);
//You need to scale this value before feeding it to the DAC.
//Much too large a number.
printf("\r\nMin: %2X Max: %2X\n\r",min,max);
} while (TRUE);
}
|
Then the DAC is a _low current_ output. Couldn't feed an LED without
a buffer amplifier.
Use ADC=8, then your ADC value will fit in the int8, and be a suitable
size to drive the DAC. |
|
|
Saad khan
Joined: 29 Apr 2019 Posts: 3
|
|
Posted: Mon Apr 29, 2019 7:03 am |
|
|
That seems to be a logical reason, but I tried to measure the output voltage with no load, still no response.
I believe I am missing some declaration in the code.
Also ADC = 8 still no output. |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19537
|
|
Posted: Mon Apr 29, 2019 8:30 am |
|
|
Have you updated the clock, and added acquisition times.
You also do not have the ADC setup correctly. Currently no connections
are made to the ADC multiplexer, and no Vref is selected, so add:
setup_adc_ports(sAN0, VSS_VDD);
As Temtronic says _start with one thing at a time_. Can you pulse an output
pin?. Is it pulsing at the right speed?. (proves the chip is actually working
and at the right clock rate). Then try generating a simple output on the DAC.
Only then move forward to doing multiple things. |
|
|
Saad khan
Joined: 29 Apr 2019 Posts: 3
|
|
Posted: Mon Apr 29, 2019 9:00 am |
|
|
Ttelmah wrote: | Have you updated the clock, and added acquisition times.
You also do not have the ADC setup correctly. Currently no connections
are made to the ADC multiplexer, and no Vref is selected, so add:
setup_adc_ports(sAN0, VSS_VDD);
As Temtronic says _start with one thing at a time_. Can you pulse an output
pin?. Is it pulsing at the right speed?. (proves the chip is actually working
and at the right clock rate). Then try generating a simple output on the DAC.
Only then move forward to doing multiple things. |
Thanks for the prompt response,
The ADC is working fine, even without adding setup_adc_ports(sAN0, VSS_VDD);
If you see the code there is another LED connected at RB_13, which toggles according to the IF condition.
Also, I tried the blink code as well, it works fine. but no DAC code is running, even individually.
Also i just realized my Serial port is also showing some garbage data, i have checked the baud rate on both sides also have verified the pin connection. |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19537
|
|
Posted: Mon Apr 29, 2019 10:32 am |
|
|
When you say 'checked the baud rate', have you actually measured with a
scope, rather than just setting 'to' 9600?.
The point is the baud rate will not be right, unless the clock is right.
This goes back to testing the one per second flash, with a stopwatch, and
verifying what clock rate the chip is actually running.
The Frc oscillator is meant to be +/-2% on that chip, so should be good
enough if the clock is being setup correctly, but you really do need to test what speed the chip is running.
Also, how is this connected to the PC?. Are you using a USB converter module?
Do you know what voltage this accepts as an input?. Remember this is
a 3.3v chip, so needs a unit that supports 3.3v inputs.
As I said, start by testing the DAC _on it's own'. Send a count from 0 to 255,
output a new value every 10mSec, so you should have a nice ramp, and see what it does. Always test _one thing at a time_.
Though it is an output, not an input, I'd also turn the analog select
bit on for your DAC output. So:
Code: |
#word ANSB=getenv("SFR:ANSB")
//then in the code
bit_set(ANSB, 14);
|
This is done automatically for the ADC, but is only 'recommended' for the
DAC, so may not be happening. |
|
|
|
|
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
|