need help for PIC18f4550

Discussion in 'Programmer's Corner' started by cyril666, Mar 7, 2009.

  1. cyril666

    Thread Starter Member

    Feb 7, 2009
    12
    0
    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...
     
  2. thatoneguy

    AAC Fanatic!

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

    Pretty straightforward. Mike Predko is the original author.

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


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