need help for PIC18f4550

Thread Starter

cyril666

Joined Feb 7, 2009
12
i need any code that working for adc PIC18F4550.
please help me...

only one analog input...in c language...

please help me..your help is greatly appreciated...
 

thatoneguy

Joined Feb 19, 2009
6,359
This was in my old snippets folder, For use with PicKit Demo Board.

Pretty straightforward. Mike Predko is the original author.

Rich (BB code):
 
1: #include <pic.h>
 2: 
 3: int i, j;                       //  Array Pointers
 4: 
 5: int ADCState = 0;               //  Keep Track of ADC Operation
 6: int ADCValue = 0;               //  Initialize to zero on declaration
 7: 
 8: int Dlay = 63;                  //  LED Time on Delay Variable
 9: 
10: //Pattern to display on LEDs
11: const char PORTAValue[8] =
12:   { 0b010000, 0b100000, 0b010000, 0b000100, 0b100000, 0b000100,
13: 0b000100, 0b000010 };
14: //  Force off Array
15: const char NOTPORTA[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
16: 
17: // IO Ports, 1 = Input, 0 = Output
18: const char TRISAValue[8] =
19:   { 0b001111, 0b001111, 0b101011, 0b101011, 0b011011, 0b011011,
20: 0b111001, 0b111001 };
21: 
22: main ()
23: {
24:   PORTA = 0;
25:   ANSEL = 1                                      //  Just RA0 is an Analog Input
26:   MCON0 = 7;                                     //  Turn off Comparators
27: 
28:   ADCON0 = 0b00000001;                           //  Turn on the ADC
29:                                                  //   Bit 7 - Left Justified Sample
30:                                                  //   Bit 6 - Use VDD
31:                                                  //   Bit 4:2 - Channel 0
32:                                                  //   Bit 1 - Do not Start
33:                                                  //   Bit 0 - Turn on ADC
34:   ADCON1 = 0b00010000;                           // Clock ADC from FOSC/8
35: 
36:   while (1 == 1)                                 //  Loop Forever
37:     {
38:       for (i = 0; i < 8; i++)
39:         {                                        //  Loop through Each of the 8 LEDS
40:           for (j = 0; j < Dlay; j++);           //  Display "On" Delay Loop
41:           if ((ADCValue & (1 << i)) == 0)
42:             PORTA = NOTPORTA[i];                 // Change lit LEDs
43:           else
44:             PORTA = PORTAValue[i];
45:           TRISA = TRISAValue[i];
46:         }
47:       switch (ADCState)                          //  ADC State Machine
48:         {
49:         case 0:                                  //  Finished, Start Next Sample
50:           GODONE = 1;
51:           ADCState++;
52:           break;
53:         case 1:                                  //  Wait for ADC to complete
54:           if (!GODONE)
55:             ADCState++;                          //  Sample Finished
56:           break;
57:         case 2:                                  //  Save Sample Value in "ADCValue"
58:           ADCValue = ADRESH;
59:           ADCState = 0;
60:           break;
61:         }
62:     }
63: }


Colorizing from: http://www.chamisplace.com/colorizer/cc.asp
 
Top