|
|
View previous topic :: View next topic |
Author |
Message |
Guest
|
Can not get the ADC on pic 16f676 to work... |
Posted: Wed Mar 03, 2004 9:13 am |
|
|
I�m having trouble to get the ACD on pic16f676 to work. The code is supposed to generate an output if a value between two measurements is positive.
The code is:
#include <16F676.h>
#device adc=8
#use delay(clock=4000000)
#fuses HS, NOWDT, BROWNOUT, PUT
main()
{
setup_adc_ports(sAN4|sAN5|VSS_VDD);
setup_adc(ADC_CLOCK_INTERNAL);
While(TRUE)
{
set_adc_channel(0);
voltage1=read_adc(); //Read voltage nr. 1
delay_ms(1000);
voltage2=read_adc(); //Read voltage nr. 2
deltavoltage=( (voltage2-voltage1) & mask );
if( deltavoltage>0 ) //If voltage slope positive
{output_high(PIN_C5);}
else //If voltage slope zero or negative
{output_low(PIN_C5);}
}
}
I don�t think there is any obvious mistake in the code. It works on pic16f877a.
Since the conversion from pic to pic is rather simple in CCS, or it seems to be, so I thought it would work on the other pic as well (with some small changes for outputs, and adc setup).
Any help is appreciated. |
|
|
Ttelmah Guest
|
Re: Can not get the ADC on pic 16f676 to work... |
Posted: Wed Mar 03, 2004 9:30 am |
|
|
Anonymous wrote: | I�m having trouble to get the ACD on pic16f676 to work. The code is supposed to generate an output if a value between two measurements is positive.
The code is:
#include <16F676.h>
#device adc=8
#use delay(clock=4000000)
#fuses HS, NOWDT, BROWNOUT, PUT
main()
{
setup_adc_ports(sAN4|sAN5|VSS_VDD);
setup_adc(ADC_CLOCK_INTERNAL);
While(TRUE)
{
set_adc_channel(0);
voltage1=read_adc(); //Read voltage nr. 1
delay_ms(1000);
voltage2=read_adc(); //Read voltage nr. 2
deltavoltage=( (voltage2-voltage1) & mask );
if( deltavoltage>0 ) //If voltage slope positive
{output_high(PIN_C5);}
else //If voltage slope zero or negative
{output_low(PIN_C5);}
}
}
I don�t think there is any obvious mistake in the code. It works on pic16f877a.
Since the conversion from pic to pic is rather simple in CCS, or it seems to be, so I thought it would work on the other pic as well (with some small changes for outputs, and adc setup).
Any help is appreciated. |
The 676, has comparators as well as the ADC. These may need to be turned off (SETUP_COMPARATORS(NC_NC_NC_NC)).
There is a slight fault in the code, in that you should wait for about 10uSec (depending on the analog source impedance), between selecting the adc channel, and taking the reading. This won't matter on latter loops since the channel doesn't change, but will result in an incorrect result on the first pass. Personally, I'd simple put the set_adc_channel outside the loop, wait for a moment, then start the loop.
Best Wishes |
|
|
Guest
|
Using Pic16F676 wrting in C, which compiler are using |
Posted: Thu Oct 29, 2009 8:49 pm |
|
|
Hi All
I'm having the same trouble with the ADC.
I noticed that your pragmas are not compiling.
Code: | SETUP_COMPARATORS(NC_NC_NC_NC); |
The FUSE settings are not found.
I am using HTC which is going to expire soon.
So I have two questions. Which include file are you using to get these pragmas/functions.
Second is there a free C compiler that I can use.
Thanks a lot. |
|
|
Ttelmah Guest
|
|
Posted: Fri Oct 30, 2009 9:15 am |
|
|
The defines (not pragmas), are part of the processor include file for the CCS compiler, which is what this forum is for...
You say you are using 'HTC'. This sounds like possibly the HiTech C. If so, you are in the wrong forum.
Best Wishes |
|
|
Wayne_
Joined: 10 Oct 2007 Posts: 681
|
Re: Can not get the ADC on pic 16f676 to work... |
Posted: Fri Oct 30, 2009 9:31 am |
|
|
Anonymous wrote: | I�m having trouble to get the ACD on pic16f676 to work. The code is supposed to generate an output if a value between two measurements is positive.
The code is:
#include <16F676.h>
#device adc=8
#use delay(clock=4000000)
#fuses HS, NOWDT, BROWNOUT, PUT
main()
{
setup_adc_ports(sAN4|sAN5|VSS_VDD);
setup_adc(ADC_CLOCK_INTERNAL);
While(TRUE)
{
set_adc_channel(0);
voltage1=read_adc(); //Read voltage nr. 1
delay_ms(1000);
voltage2=read_adc(); //Read voltage nr. 2
deltavoltage=( (voltage2-voltage1) & mask );
if( deltavoltage>0 ) //If voltage slope positive
{output_high(PIN_C5);}
else //If voltage slope zero or negative
{output_low(PIN_C5);}
}
}
I don�t think there is any obvious mistake in the code. It works on pic16f877a.
Since the conversion from pic to pic is rather simple in CCS, or it seems to be, so I thought it would work on the other pic as well (with some small changes for outputs, and adc setup).
Any help is appreciated. |
There is a big fault in your code.
voltage1, voltage2, deltavoltage and mask are not defined.
I expect your problem lies with the equation.
If voltage1 and voltage2 are unsigned (which is the default for CCS) then your result will never be negative. You will need to cast to signed:-
Code: |
int16 voltage1, voltage2; // I presume!, both are unsigned
signed int16 deltavoltage; // I presume!
int16 mask; // ? This is very important as to what it is set to.
deltavoltage=( ((signed int32)voltage2-voltage1) & mask );
|
|
|
|
[email protected] Guest
|
How to use the CCS compiler |
Posted: Sat Oct 31, 2009 4:03 pm |
|
|
Can anyopne direct me to how to use the CCS compiler?
Where are the FAQS and how to docs?
Thanks |
|
|
dyeatman
Joined: 06 Sep 2003 Posts: 1934 Location: Norman, OK
|
|
|
|
|
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
|