|
|
View previous topic :: View next topic |
Author |
Message |
Richi Guest
|
A/D and PIC16F676 does not work |
Posted: Wed Feb 23, 2005 2:08 pm |
|
|
Hi all,
after 10 or 15 hours troubleshooting, testing and 5 killed PICs, I�m at the end.
Why does the ADC of the 16F676 not work? The code is perfect running at the 16F877.
All other functions are working at the 16F676.When I give a fixed number for the ADC value- the LED�s are working.
Is it a bug in the CCS-compiler?
Here is the code:
#if defined(_PCM_)
#endif
#include <16F676.h>
#device adc=10
#fuses intrc_io,NOWDT,PROTECT,NOBROWNOUT,PUT,MCLR,CPD
#use delay (clock=4000000)
#use standard_io(A)
#use standard_io(C)
//#ROM 0x3ff = {0x3480} // set osccal
// Pins
#define TAN PIN_A5
#define TSCHARF PIN_A2
#define LED1 PIN_C5
#define LED2 PIN_C4
#define LED3 PIN_C3
#define LEDAN PIN_C1
#define LEDSCHARF PIN_C2
int1 ADLESEN = 0;
int1 Blinkerled4 = 0;
int1 tansetzen = 0;
int1 ledscharfspeicher = 0;
int1 schalteran = 0;
int1 zustandalt = 0;
int1 zustandneu = 0;
int16 izeit2s = 0;
int8 iSmoothing = 0;
int8 iLedzaehler = 0;
int8 iTimertakt = 0;
int8 ieinzaehler = 1;
int16 ianalogwert1 = 1023;
int16 iwert_1 = 0;
int16 igrenzeled1 = 1023;
int16 igrenzeled1_2 = 392;
int16 igrenzeled2_3 = 381;
int16 igrenzeled3_4 = 368;
int16 ianalogwertalt1 = 0;
#int_timer1
void timer1_isr()
{
ADLESEN = 1;
set_timer1(15536); // (100ms)
++iLEDZAEHLER;
if ((input(TSCHARF)) && (izeit2s <= 9) )
{
++izeit2s;
}
if (izeit2s == 10)
{
output_HIGH(LEDSCHARF);
ledscharfspeicher = 1;
}
else
{
output_LOW(LEDSCHARF);
ledscharfspeicher = 0;
}
if (!input(TSCHARF) )
{
izeit2s = 0;
}
}
#INT_EXT
void ext_isr()
{
set_timer1(15536);
izeit2s = 0;
}
main()
{
setup_comparator(NC_NC_NC_NC);
setup_adc( ADC_CLOCK_INTERNAL );
setup_adc_ports( sAN3 | VSS_VDD);
setup_timer_1 ( T1_INTERNAL | T1_DIV_BY_2 );
enable_interrupts(GLOBAL);
enable_interrupts(INT_TIMER1);
enable_interrupts(INT_EXT);
ext_int_edge(L_TO_H);
ianalogwertalt1 = 0;
ianalogwert1 = 1023;
ADLESEN = 1;
iLEDZAEHLER = 0;
schalteran = 1;
for(;;)
{
output_LOW(PIN_A0);
output_LOW(PIN_A1);
output_LOW(PIN_C0);
igrenzeled1 = 1023;
igrenzeled1_2 = 430;
igrenzeled2_3 = 410;
igrenzeled3_4 = 368;
//Analog/Digitalwandlung
set_adc_channel(sAN3);
delay_us(10);
iwert_1 = read_adc();
{
ianalogwert1 = (iwert_1);
tansetzen = 0;
zustandneu = input(TAN);
if ((zustandneu == 1) && (zustandalt == 0))
{
tansetzen = 1;
}
zustandalt = zustandneu;
if (( tansetzen == 1) && (ledscharfspeicher == 1 ))
{
schalteran = 1;
}
else schalteran = 0;
if ( schalteran == 1 )
{
++ieinzaehler;
}
if ( ieinzaehler == 1 )
{
output_HIGH(LEDAN);
}
if ( ieinzaehler == 2 )
{
output_LOW(LEDAN), output_LOW(LED1), output_LOW(LED2), output_LOW(LED3);
}
if ( ((ieinzaehler == 0) && (!input(TSCHARF))) || ((ieinzaehler == 2) && (input(TSCHARF))) )
{
ieinzaehler = 0, output_LOW(LEDSCHARF), sleep();
}
if ( iLEDZAEHLER == 3)
{
blinkerled4 = 1;
}
if ( iLEDZAEHLER >= 4)
{
blinkerled4 = 0;
iLEDZAEHLER = 0;
}
if ( ieinzaehler == 1 )
{
if ( (ianalogwert1<=igrenzeled1 && ianalogwert1>=igrenzeled1_2) )
{
output_HIGH(LED1);
}
else
{
output_LOW(LED1);
}
if ( (ianalogwert1<igrenzeled1_2 && ianalogwert1>=igrenzeled2_3) )
{
output_HIGH(LED2);
}
else
{
output_LOW(LED2);
}
if ( (ianalogwert1<igrenzeled3_4 && blinkerled4 == 1 && ianalogwert1>=20) || (ianalogwert1<igrenzeled2_3 && ianalogwert1>=igrenzeled3_4) )
{
output_HIGH(LED3);
}
else
{
output_LOW(LED3);
}
}
}
} |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Wed Feb 23, 2005 2:38 pm |
|
|
This line is not correct:
Code: | set_adc_channel(sAN3); |
The parameters for set_adc_channel() are 0, 1, 2, 3, etc., and not
sANx. Since you're new to the compiler, whenever something doesn't
work, you should check the manual and verify that you're using the CCS
functions correctly.
If you're using channel 3, the code should be:
Code: | set_adc_channel(3); |
|
|
|
Richi Guest
|
|
Posted: Thu Feb 24, 2005 2:51 pm |
|
|
Thank you very much, but it also doesn�t work. Something changed: The value now was read from ADC I think is now 1024 - the first LED is lighting, before it must have been 0 cause no lights were burning. Im not sure - my ICD2 module also doesn�t work with this (f...) device...
The reason for using sAN3 was: I took the parameters from the CCS header file:
setup_adc_ports(sAN3 | VSS_VDD ); -- is that o.k.? But also no change when writing (3 | VSS_VDD )!
////////////////////////////////////////////////////////////////// ADC
// ADC Functions: SETUP_ADC(), SETUP_ADC_PORTS() (aka SETUP_PORT_A),
// SET_ADC_CHANNEL(), READ_ADC()
// Constants used in SETUP_ADC_PORTS() are:
// or together any combination
// of the sANx constants with one
// of the others.
#define sAN0 1 //| A0
#define sAN1 2 //| A1
#define sAN2 4 //| A2
#define sAN3 8 //| A4
#define sAN4 16 //| C0
#define sAN5 32 //| C1
#define sAN6 64 //| C2
#define sAN7 128 //| C3
#define NO_ANALOGS 0 // None
// The following may be OR'ed in with the above using |
#define VSS_VDD 0x8000 // Range 0-Vdd
#define VREF_VREF 0x9000 // Range VrefL-VrefH
#define VRL_VRH 0xA000 // Range Vrl-Vrh
#define VSS_VREF 0xB000 // Range 0-VrefH
#define VSS_VRH 0xC000 // Range 0-Vrl
#define VREF_VDD 0xD000 // Range VrefL-Vdd
#define VRL_VDD 0xE000 // Range Vrl-Vdd
#define VSS_VRL 0xF000 // Range 0-Vrl
// Constants used for SETUP_ADC() are:
#define ADC_OFF 0 // ADC Off
#define ADC_CLOCK_DIV_2 1
#define ADC_CLOCK_DIV_4 0x41
#define ADC_CLOCK_DIV_8 0x11
#define ADC_CLOCK_DIV_32 0x21
#define ADC_CLOCK_DIV_16 0x51
#define ADC_CLOCK_DIV_64 0x61
#define ADC_CLOCK_INTERNAL 0x31 // Internal 2-6us
// Constants used in READ_ADC() are:
#define ADC_START_AND_READ 7 // This is the default if nothing is specified
#define ADC_START_ONLY 1
#define ADC_READ_ONLY 6 |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Thu Feb 24, 2005 3:03 pm |
|
|
Quote: | setup_adc_ports(sAN3 | VSS_VDD ); -- is that o.k.? |
Yes.
Post your version of the compiler. I'll look at the .LST file to
see if it's generating the correct code for the A/D functions. |
|
|
Mark
Joined: 07 Sep 2003 Posts: 2838 Location: Atlanta, GA
|
|
Posted: Thu Feb 24, 2005 6:24 pm |
|
|
5 killed PICs and now the ICD. Can't blame the compiler for all that. You must have problems without your hardware. |
|
|
Richi Guest
|
|
Posted: Fri Feb 25, 2005 10:34 am |
|
|
No Problem with hardware... the pics where killed cause of soldering/desoldering. My ICD is o.k. but doesn�t work with THIS device.
Compilerversion is 3.180
Thanks for help... |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Fri Feb 25, 2005 12:40 pm |
|
|
I compiled the following program with PCM vs. 3.180 and looked at
the .LST file. I think it should work OK.
Also note that A/D channel 3 is on pin RA4, which is pin 3 on the chip.
Make sure your A/D signal is going to pin 3 on the PIC.
Try the following program and see if it works. Display the results using
printf. Make sure you use "%LX" to display a 16-bit result with printf.
Code: | #include <16F676.h>
#device adc=10
#fuses INTRC_IO,NOWDT,PROTECT,NOBROWNOUT,PUT,MCLR
#use delay (clock=4000000)
int16 result;
main()
{
setup_comparator(NC_NC_NC_NC);
setup_adc( ADC_CLOCK_INTERNAL );
setup_adc_ports( sAN3 | VSS_VDD);
set_adc_channel(3);
delay_us(10);
result = read_adc();
while(1);
} |
|
|
|
|
|
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
|