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 CCS Technical Support

INT Analog to digital converter for pic16f676 not working

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



Joined: 15 Dec 2006
Posts: 109

View user's profile Send private message

INT Analog to digital converter for pic16f676 not working
PostPosted: Fri Dec 15, 2006 7:03 am     Reply with quote

Need help with this code. Its not working why?
compiler version 4.013

#include <16F676.h>
#DEVICE ADC=10
#USE Fast_io(A)
#FUSES NOPUT,INTRC_IO,NOWDT,NOBROWNOUT

#use delay(clock=4000000) //1us per instruction
int16 ch2_result;


#int_AD
void AD_isr()
{
ch2_result = read_adc(ADC_READ_ONLY);
read_adc(ADC_START_ONLY);

}
void main()
{

SET_TRIS_C( 0B00000000 );
SET_TRIS_A( 0B00000100 );
OUTPUT_LOW(PIN_C0);
setup_comparator(NC_NC_NC_NC);
setup_adc_ports( sAN2 | VSS_VDD);
setup_adc( ADC_CLOCK_DIV_8 );
set_adc_channel( 2 );
DELAY_US(100);
enable_interrupts(INT_AD);
enable_interrupts(GLOBAL);
read_adc(ADC_START_ONLY);
while (1)
{

if (ch2_result >= 0x79) {OUTPUT_HIGH(PIN_C0);}

}
}
Ttelmah
Guest







PostPosted: Fri Dec 15, 2006 9:02 am     Reply with quote

First, get rid of 4.013, and use 3.249, which has a hope of working.
Read the comments about the V4 compilers.

The main problem though, is that the code will probably spend just about for ever in the interrupt. It is really never worth using interrupts to service the ADC. The only time it is 'worth' doing, is if the ADC, is being triggered off the CCP. As it stands, the conversion will take 12 ADC clocks. Each ADC clock wth your settings, takes just two instruction times. Hence the conversion will take jut 24 instruction times from the point it is triggered. The act of returning from the interrupt handler, takes typically about 24 instructions, so as soon as the interrupt exits, it'll be called again...
It takes longer to call, and return from an interrupt, on a processor below about 8Mz, than it takes to actually just trigger and read the ADC.

Best Wishes
jaikumar



Joined: 15 Dec 2006
Posts: 109

View user's profile Send private message

Thanks for the info
PostPosted: Fri Dec 15, 2006 10:33 pm     Reply with quote

Thanks for the useful information Ttelmah. But i still need to know why this dosen't work. I was looking at the LIST file created by the compiler yesterday, and as per datasheet of the 16f676 it has a ANSEL select register which is used to select that a pin is a analog which is required for ADC operation So i will have to set this and try again. wish me luck.

jaikumar
jaikumar



Joined: 15 Dec 2006
Posts: 109

View user's profile Send private message

This Code WORKS! using int for ADC ccs compiler 4.13
PostPosted: Sat Dec 16, 2006 12:15 am     Reply with quote

#include <16F676.h>
#DEVICE ADC=10
#USE Fast_io(A)
#FUSES NOPUT,INTRC_IO,NOWDT,NOBROWNOUT

#use delay(clock=4000000) //1us per instruction
int16 ch2_result;

#int_AD
void AD_ISR()
{
set_adc_channel( 2 );
DELAY_US(10);
ch2_result = Read_ADC(ADC_READ_ONLY);
}
void main()
{

SET_TRIS_C( 0B00000000 );
SET_TRIS_A( 0B00000100 );
OUTPUT_LOW(PIN_C0);
setup_comparator(NC_NC_NC_NC);
setup_adc_ports( sAN2 | VSS_VDD);
setup_adc( ADC_CLOCK_DIV_8 );
enable_interrupts(INT_AD);
enable_interrupts(GLOBAL);
Read_ADC(ADC_START_ONLY);
while (1)
{
if (ch2_result >= 0x73) {OUTPUT_HIGH(PIN_C0);}
Read_ADC(ADC_START_ONLY);

}


}

Have Fun Everyone! Electronics is great when it works and also when it dosen't (you can learn a lot more by debugging).
helpme
Guest







PostPosted: Sat Dec 16, 2006 4:27 am     Reply with quote

thank u 4 the priceless input. i read and re-read the datasheet time and again. i ve written an ADC program . it is gettin compiled but the lcd is not workin when i did it practically.. have my doubts about adc code also. will pass on the code. make suggest changes pls..

#if defined(__PCM__)
#include <16f877.h>
#fuses HS,NOWDT,PUT
#use delay(clock=16000000)
#define use_portd_lcd TRUE // Using Port D for LCD Display
#include <lcd.c>
#define LCD_DB4 PIN_D4
#define LCD_DB5 PIN_D5
#define LCD_DB6 PIN_D6
#define LCD_DB7 PIN_D7

#define LCD_E PIN_D0
#define LCD_RS PIN_D1
#define LCD_RW PIN_D2
#include <stdlib.h>

#endif

void main()
{
int value;
setup_adc_ports( ANALOG_RA3_REF);

setup_adc( ADC_CLOCK_INTERNAL );

SET_TRIS_A( 0x01 );
SET_TRIS_D( 0x00 );
set_adc_channel(0);
while ( TRUE )
{

delay_ms( 5000 );

value = read_adc();



}

read_adc(ADC_START_ONLY);

sleep();

value=read_adc(ADC_READ_ONLY);





lcd_init();
printf(lcd_putc,"/f Pressure is %U Bar ",value);
}
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