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

PIC12F675 AN0 acts as output

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



Joined: 26 Apr 2013
Posts: 11

View user's profile Send private message

PIC12F675 AN0 acts as output
PostPosted: Fri Apr 26, 2013 9:41 am     Reply with quote

Hello,
I've written this code for the following purpose: It reads an analog value on AN0 (pin 7) and according to the first digit of the adc value (0-10), switches on a particular pattern of LEDs. The problem is that pin AN0 seems to be acting as an output (it follows output_a() commands), although trisa is set to make it an input. I even tried it in an #asm block, but it still acts as an output. Because of this, the analog value (controlled by a 0-5v potentiometer) doesn't change linearly with pot variation, but almost just switches between 0 and 5v. Even stranger, the ADC does seem to be working too. As in, when the pot is turned all the way up, no LEDs glow, and when all the way down, all of them glow, which is as expected. I've looked all day but can't see the problem. Can you?
Thanks.
Code:
#device PIC12F675
#include<12f675.h>
#device ADC=10
//#fuses 1=0x3e44
#use delay(clock=4000000)
#define GP0 PIN_A0
#define GP1 PIN_A1
#define GP2 PIN_A2
#define GP3 PIN_A3
#define GP4 PIN_A4
#define GP5 PIN_A5
void init()
{
   
setup_comparator( NC_NC_NC_NC ); // disable comparators
setup_adc_ports(AN0_ANALOG); 
setup_adc(ADC_CLOCK_DIV_8);
set_adc_channel(0);
//set_tris_a( 0x01 ); // set GP0 input, all other outputs
}
void main()
{
   init();
   //set_tris_a(0x01); // set GP0 input, all other outputs
   #asm   //same as trisio=0x01, cmcon=0x07 and ansel.0=1
   BSF 03.5
   MOVLW 0F
   MOVWF 85
   BSF 9F.0
   BCF 03.5
   MOVLW 07
   MOVWF 19
   #endasm
   set_adc_channel(0);
   delay_us(100);
   byte kvar[11]={0x01,0x03,0x05,0x07,0x15,0x17,0x23,0x25,0x27,0x35,0x37};
   int16 value;
   int8 val,val2;   
   output_a(0xff);
   delay_ms(700);
   output_a(0x00);
   delay_ms(700);
   while(1)
   {
       delay_ms(100);       
       value=read_adc();
       delay_ms(30);
       val=value/100;
       val2=10-val;
       output_a(kvar[val2]);
       delay_ms(100);
   }
}
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Fri Apr 26, 2013 11:12 am     Reply with quote

Quote:

#asm //same as trisio=0x01, cmcon=0x07 and ansel.0=1
BSF 03.5
MOVLW 0F
MOVWF 85
BSF 9F.0
BCF 03.5
MOVLW 07
MOVWF 19
#endasm

You have apparently copied this code from the .LST file of a program.
But the .LST file uses Hex as the default format. In other words, the
values of 0F, 85, 9F and 19 are really 0x0F, 0x85, 0x9F and 0x19.
The .LST file doesn't put a prefix in front of the numbers.
But when you put it into a CCS program you do need to put in the "0x"
prefix. With your program above, it's using all the wrong values.

If you don't like the CCS functions, the correct way to do it is to
use #byte and #bit statements to declare the register addresses,
and then write directly to the registers in main().


Last edited by PCM programmer on Fri Apr 26, 2013 11:19 am; edited 1 time in total
samyg



Joined: 26 Apr 2013
Posts: 11

View user's profile Send private message

PostPosted: Fri Apr 26, 2013 11:19 am     Reply with quote

Thanks for the reply.
I understand your point.
But i did this #asm part only when using the set_tris_a() function gave me the error i talked about. You said there are many things wrong in my code. Please ignore the asm part, assume i'm using the ccs function, and point out what other mistakes there are.
Thanks again sir.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Fri Apr 26, 2013 11:49 am     Reply with quote

What's your CCS compiler version ? It's a 4-digit number given at the
top of the .LST file, which is in your project directory after a sucessful
compilation. Example of version numbers:
http://www.ccsinfo.com/devices.php?page=versioninfo
samyg



Joined: 26 Apr 2013
Posts: 11

View user's profile Send private message

PostPosted: Fri Apr 26, 2013 11:52 am     Reply with quote

It's version 4.140.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Fri Apr 26, 2013 12:10 pm     Reply with quote

Quote:
output_a(0xff);
delay_ms(700);
output_a(0x00);
delay_ms(700);

You have another problem. In the lines in bold, you are changing
the TRIS for all pins on PortA to be output pins. If you don't want
the compiler to change the TRIS, then you need to specify #fast io
mode for PortA. See the CCS manual on this.



Quote:
byte kvar[11]={0x01,0x03,0x05,0x07,0x15,0x17,0x23,0x25,0x27,0x35,0x37};
int16 value;
int8 val,val2;

Don't put variable declarations in mid-code. Put them at the start of
main(), or at the start of the function that they are in.
samyg



Joined: 26 Apr 2013
Posts: 11

View user's profile Send private message

Solved!
PostPosted: Fri Apr 26, 2013 12:35 pm     Reply with quote

The #use fast_io solved it.
Thanks a lot for your help. Smile
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