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 support@ccsinfo.com

10 bit A/D on 12F675 not working

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







10 bit A/D on 12F675 not working
PostPosted: Thu Feb 20, 2003 1:05 pm     Reply with quote

I am using the PIC12F675 that is suppose to have a 10-bit A/D. So in the program I have a variable, Voltage, that is set up as a long. In the program I set the channel and do a "voltage=read_adc()". However, I do not get a 10-bit number, instead it only reads a 8-bit value. I have seen that there are some problems with the 12F675 and the CCS compiler. Does anyone know if this is a compiler problem or am I doing this wrong? See code below.

#include "12f675.h"
#use delay(clock=4000000)
#fuses INTRC,PUT,BROWNOUT,WDT,NOMCLR

#use fast_io(A)
#define vbat PIN_A0 //battery voltage input
#define isns PIN_A1 //current input
#define soc PIN_A2 //capacity output
#define on PIN_A3 //charge/discharge pin

#define caplo 0
#define caphi 1
#define mult 128 //charge multiplication factor
#define multd 130 //discharge multiplication factor

long on_time,capacity,sum; //variable setup
long prod,percent,crnt,voltage;
long design_cap=3200;
char loop,count;

#int_timer0 //timer 0 interrupt
timer0_isr() //this routine will toggle the
{ //output for the soc

restart_wdt();
count=count+1; //loop counter
if(!loop)
{
if(on_time==0)
{
loop=0;
count=count+1;
}
else
{
output_high(soc); //turn output high
set_timer0((2560-on_time)/10); //set timer
loop=1;
}
}
else
{
if(on_time>=2500)
loop=1;
else
{
output_low(soc); //set output low
set_timer0(on_time/10); //set timer
loop=0;
}
}
}

////////////////////////////////////////////////////////
void main()
{

setup_adc_ports(ALL_ANALOG);
setup_adc(ADC_CLOCK_DIV_2);
setup_vref(false);
set_timer0(0);
port_b_pullups(false);
setup_counters(RTCC_INTERNAL,WDT_2304MS);
setup_timer_0(RTCC_DIV_128);
setup_timer_1(T1_DISABLED);
setup_comparator(FALSE);
enable_interrupts(INT_TIMER0);
enable_interrupts(global);

set_tris_a(0b00101011);

loop=0;
count=0;
sum=0;
prod=0;
capacity=0;

while (true)
{
set_adc_channel(0);
delay_us(100);
restart_wdt();
voltage=read_adc(); //read battery voltage
set_adc_channel(1);
delay_us(100);
restart_wdt();
crnt=read_adc(); //read current
if(voltage<=174&&crnt<=4) //adjust capacity if wrong
capacity=0;
if (voltage>=210&&crnt<=4&&capacity<=875) //adjust capacity if wrong
capacity=875;
percent=((capacity*11)/design_cap); //calculate on time based on
on_time=percent*233; //current capacity and design capacity
while(input(on)) //cahrge accumulation
{
if(count>=55) //consitutes one second
{
count=0;
delay_us(100);
restart_wdt();
voltage=read_adc(); //read voltage
set_adc_channel(1);
delay_us(100);
restart_wdt();
crnt=read_adc(); //read current
delay_us(100);
restart_wdt();
sum=crnt*mult; //current reading times mult factor
prod=sum+prod; //add up the capacity
if (prod>=36000)
{
prod=prod-36000; //reset
capacity=capacity+1; //increase capacity by one
if(capacity>=3300)
capacity=3300;
}
if(voltage>=233&&(9>=crnt>=4)) //adjust capacity if wron
capacity=3210;
}
restart_wdt();
//write_eeprom(caplo,0x00);
//write_eeprom(caphi,0x00);
}
while(!input(on)&&crnt>4) //discharge subtraction
{
restart_wdt();
if(count>=55) //55 times through timer = 1 sec
{
count=0;
set_adc_channel(0);
delay_us(100);
restart_wdt();
voltage=read_adc(); //read voltage
set_adc_channel(1);
delay_us(100);
restart_wdt();
crnt=read_adc(); //read current
sum=crnt*multd;
prod=sum+prod; //add up current
if(prod>=36000)
{
prod=prod-36000; //reset
capacity=capacity-1; //decrement capacity
if(capacity==0 || capacity>=3305) //adjust capacity if wrong
capacity=0;
}
if(voltage<=170) //adjust capacity if wrong
capacity=0;
}
}

}
}
___________________________
This message was ported from CCS's old forum
Original Post ID: 11925
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

Re: 10 bit A/D on 12F675 not working
PostPosted: Thu Feb 20, 2003 1:33 pm     Reply with quote

:=I am using the PIC12F675 that is suppose to have a 10-bit A/D.
-------------------------------------------------------------

You need to tell the compiler that you want the 10 bits.
Do this with a #device statement. Put this line immediately
after the #include statement for your PIC.

#device adc=10

While looking at your code, I noticed some other problems.
You have this line:
setup_adc(ADC_CLOCK_DIV_2);
If you read the data sheet for the 12F675, in Table 7-1,
it shows a chart of the proper A/D clock divisor, based
on your crystal frequency. You're using a 4 MHz crystal,
so it shows that a divisor of 8 would be best. So do this:
setup_adc(ADC_CLOCK_DIV_8);


Also, some of your comparison code might not be correct.
I invite you to run the following test program.

<PRE>
#include "16F877.h"
#fuses HS, NOWDT,NOPROTECT,PUT,BROWNOUT, NOLVP
#use Delay(clock=8000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, ERRORS)

main()
{
char c;
char i;

printf("1st test:\n\r");

for(i = 0 ; i < 15; i++)
{
if(9 >= i >=4)
printf("i = \%d (within range)\n\r", i);
else
printf("i = \%d \n\r", i);
}

printf("\n\r");
printf("2nd test:\n\r");

for(i = 0 ; i < 15; i++)
{
if((9 >= i) && (i >=4))
printf("i = \%d (within range)\n\r", i);
else
printf("i = \%d \n\r", i);
}

while(1);

}

</PRE>
___________________________
This message was ported from CCS's old forum
Original Post ID: 11926
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