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

Switching random I/O.

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



Joined: 06 May 2008
Posts: 2

View user's profile Send private message

Switching random I/O.
PostPosted: Tue May 06, 2008 2:52 am     Reply with quote

Hi. I am using a 16F684 PIC to produce a random output when AN3 is below 300. I am using a 0-5v solar panel on the analog input and for some reason the panel is producing 0v in light when connected. If i directly connect AN3 to 5v it does work but draws 100mA. I am guessing it has something to do with output_a(r). But i dont know any other way to output a random pin, does anybody know a way that will work? My code is:


Code:
#define RAND_MAX 3 // Change to an appropriate number
#include <stdlib.h>
int16 adc;
int r;
void main()
{
 
   
   setup_adc_ports(sAN3|VSS_VDD);
   setup_adc(ADC_CLOCK_INTERNAL);
   set_adc_channel(3);
   setup_vref(FALSE);
   setup_comparator(NC_NC_NC_NC);
   
 while(1){

r=rand();
adc=Read_Adc();

if(adc<300)
output_a(r);

else
output_bit(PIN_A0,0);
output_bit(PIN_A1,0);
output_bit(PIN_A2,0);
output_bit(PIN_A3,0);

             
   }
   } 
}



Thanks.
Ttelmah
Guest







PostPosted: Tue May 06, 2008 3:03 am     Reply with quote

Add the line '#use fast_io(a)' below the stdlib include, and then at the start of your main, add 'set_tris_a(0b11110000);'.
The problem is that with 'standard_io' mode selected (the default), as soon as you output a full byte the port, the whole port is changed to operate as an output. The reprograms the tris for RA4, to an output...
The changes above, _fix_ the tris settings, so that this doesn't happen.

Best Wishes
GregN



Joined: 06 May 2008
Posts: 2

View user's profile Send private message

PostPosted: Tue May 06, 2008 3:18 am     Reply with quote

Ok great it works. Thanks.
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