|
|
View previous topic :: View next topic |
Author |
Message |
GregN
Joined: 06 May 2008 Posts: 2
|
Switching random I/O. |
Posted: Tue May 06, 2008 2:52 am |
|
|
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
|
|
Posted: Tue May 06, 2008 3:03 am |
|
|
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
|
|
Posted: Tue May 06, 2008 3:18 am |
|
|
Ok great it works. Thanks. |
|
|
|
|
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
|