|
|
View previous topic :: View next topic |
Author |
Message |
newbieA2D Guest
|
sprintf(); writing arrays with a2d |
Posted: Wed Dec 13, 2006 11:23 pm |
|
|
I am trying to write an array based on light inputs. The code I was trying was this:
Code: |
// fortest.c
#include <16F690.h>
#include <STDLIB>
#fuses INTRC_IO,NOWDT,NOPROTECT,NOMCLR,NOBROWNOUT,NOIESO,NOFCMEN //uses built-in clock only!
#use delay(clock=8000000)
#use RS232(Baud=9600,Xmit=PIN_B7) //PIN_B7 sends to LCD
#define LENGTH 6
void main(void){
delay_ms(3000); //wait for display to wake up
printf("?B7F"); //set contrast adaptor
delay_ms(2000); //wait for config to take hold
output_high(PIN_A2); // turn on the led
printf("?f"); //Home and clear the display.
setup_adc_ports(sAN1); //setup analog port
setup_adc(ADC_CLOCK_INTERNAL);
while(1){
char sig[LENGTH];
int i;
int light;
light = read_adc();
set_adc_channel(1);
printf("?f");
delay_ms(1000);
printf("?f");
for (i=0; i<5; i++){
printf("i = %u",i); //display the value of i
delay_ms(2000);
printf("?f");
if(light<120){
sig[i]= '1';
}
else{
sig[i]= '0';
}
}
printf("%s",sig);
delay_ms(2000);
}
}
|
and it did not seem to work. I was wondering if sprintf() should be used and could be used to write specific parts of the array. Any suggestions would be helpful. |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Wed Dec 13, 2006 11:58 pm |
|
|
Quote: | light = read_adc();
set_adc_channel(1); |
Here, at least on the first pass, you're reading the A/D before you
set the channel. You should set the channel, delay for 20 us,
and then read the A/D.
Quote: |
if(light<120){
sig[i]= '1';
}
else{
sig[i]= '0';
}
}
printf("%s",sig);
|
In the code above, you're trying to build a string which consists of
ascii 1's and 0's. But it's not a string until you write a string terminator
byte of zero at the end of the ascii digits. After you've finished filling
the array, you need to write 0x00 in the next available location. |
|
|
|
|
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
|