| 
	
	|  |  |  
	
		| View previous topic :: View next topic |  
		| Author | Message |  
		| Akhila Guest
 
 
 
 
 
 
 
			
			
			
			
			
			
			
			
			
 
 | 
			
				| using the A/D converter in the PIC16f874 |  
				|  Posted: Sun Apr 20, 2003 4:53 pm |   |  
				| 
 |  
				| The value I'm getting for the read_adc() is not what it is supposed to be.  The range of Voltage inputing into PIN 2(A0) is 0v--5v. input   expected output   actual output
 0           0              0
 0.5         26             103
 1           51             209
 1.5         77             52
 2           102            161
 2.5         128            5
 3           154            113
 3.5         179            210
 4           205            64
 4.5         230            154
 5           256            255
 
 My code also includes an 8bit counter 0--255, at 1 sec delay. It is supposed to print the correct value from read_adc(). Here is my code.
 /*********************************************************/
 #include <16F874.h>
 #use delay(CLOCK = 20000000)
 #use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7)
 
 
 int P7 = 0;                    // output most significant bit
 int P6 = 0;
 int P5 = 0;
 int P4 = 0;
 int P3 = 0;
 int P2 = 0;
 int P1 = 0;
 int P0 = 0;                    // output least significant bit
 byte value;
 int ScanNumber =0;                 // declaration for number of scan times
 int counter = 0;                   // counter for ten-minute time delay
 
 
 void nextloop(void);               // declaration for the 4 least significant bits
 void delay(void);                  // declaration for ten-minute time delay
 void Scan(void);                   // declaration for the 4 most significant bits
 void dcvol(void);		   // declaration for printing the dc voltage
 
 main ()
 {
 setup_adc(ADC_CLOCK_INTERNAL);     // set clock for PIC
 set_adc_channel(0);                // set analog-to-digital input to pin2
 setup_adc_ports(ALL_ANALOG);
 
 for(ScanNumber=0; ScanNumber<9; ScanNumber++)
 {
 Scan();
 delay();
 
 }
 }
 void Scan(void)
 {
 
 for (P7=0; P7<2; P7++)
 {
 output_low(PIN_B7);
 for (P6=0; P6<2; P6++)
 {
 output_low(PIN_B6);
 for (P5=0; P5<2; P5++)
 {
 output_low(PIN_B5);
 for(P4=0; P4<2; P4++)
 {
 
 output_low(PIN_B4);
 if (P4 == 1) { output_high(PIN_B4); }
 if (P5 == 1) { output_high(PIN_B5); }
 if (P6 == 1) { output_high(PIN_B6); }
 if (P7 == 1) { output_high(PIN_B7); }
 nextloop();
 }
 }
 }
 }
 output_low(PIN_B4);
 output_low(PIN_B5);
 output_low(PIN_B6);
 output_low(PIN_B7);
 }
 
 
 void nextloop(void)
 {
 for (P3=0; P3<2; P3++)
 {
 output_low(PIN_B3);
 for (P2=0; P2<2; P2++)
 {
 output_low(PIN_B2);
 for (P1=0; P1<2; P1++)
 {
 output_low(PIN_B1);
 for (P0=0; P0<2; P0++)
 {
 
 output_low(PIN_B0);
 if (P0 == 1) { output_high(PIN_B0); }
 if (P1 == 1) { output_high(PIN_B1); }
 if (P2 == 1) { output_high(PIN_B2); }
 if (P3 == 1) { output_high(PIN_B3); }
 dcvol();
 delay_ms(1000);
 
 }
 }
 }
 }
 output_low(PIN_B0);
 output_low(PIN_B1);
 output_low(PIN_B2);
 output_low(PIN_B3);
 }
 
 void delay(void)
 {
 for (counter = 0; counter < 120; counter++)
 {
 delay_ms(5000);
 }
 }
 void dcvol(void)
 {
 value = 0;
 value = Read_ADC();
 printf("\%u\n\r", value);
 }
 ___________________________
 This message was ported from CCS's old forum
 Original Post ID: 13841
 |  |  
		|  |  
		| PCM programmer 
 
 
 Joined: 06 Sep 2003
 Posts: 21708
 
 
 
			    
 
 | 
			
				| Re: using the A/D converter in the PIC16f874 |  
				|  Posted: Sun Apr 20, 2003 5:36 pm |   |  
				| 
 |  
				| :=The value I'm getting for the read_adc() is not what it is supposed to be.  The range of Voltage inputing into PIN 2(A0) is 0v--5v. <PRE>
 input   expected output   actual output
 0           0              0
 0.5         26             103
 1           51             209
 1.5         77             52
 </PRE>
 ------------------------------------------------------------
 
 Based on the number series above, you're getting 4x your
 expected value, and it's being truncated to the lower 8 bits.
 For example, 4 x 77 = 308.
 and 308 - 256 = 52.
 
 So you need to tell the compiler to return an 8-bit value from
 read_adc(), instead of a 10-bit value.
 So modify your pre-processor statements like this:
 
 #include <16F874.h>
 #device adc=8     // Add this line
 
 If that doesn't fix it, then post the version of your compiler.
 ___________________________
 This message was ported from CCS's old forum
 Original Post ID: 13846
 |  |  
		|  |  
		|  |  
  
	| 
 
 | 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
 
 |