sp3ctrm5tr

PIC16F877A, Phase Test

Mar 2nd, 2012
231
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.13 KB | None | 0 0
  1. #include <htc.h>
  2. #include <pic16f877a.h>
  3. #include <stdio.h>
  4. #define _XTAL_FREQ 20000000
  5.  
  6. void wait_for_one_press( )
  7. {  
  8.     while(!RB5);
  9.     __delay_ms(250);
  10.     while(RB5); //wait for one press, detect Pin B5
  11.     __delay_ms(250);
  12. }
  13.  
  14. //--Delay Function--
  15. void delay_ms (unsigned long int ms)
  16. {  
  17.     do
  18.     {  
  19.         __delay_ms(1); //Delay for 1ms
  20.     }
  21.     while (ms--);
  22. }
  23.  
  24. //--Main Funtion --
  25. void main (void)
  26. {  
  27.     unsigned int delay;
  28.     TRISA=0x01; // Set PORT A0 as an INPUT
  29.     TRISB=0x20; // Set PORT B5 as an INPUT
  30.     TRISC=0xF0; // Set PORT C7-C4 as INPUT / PORT C3-C0 as OUTPUT
  31.     ADCON1=0b01001110;  //Set ADFM (bit 7 of ADCON1) to 0 for left justified
  32.                         //Only RA0 is selected as analog input
  33.     ADCON0=0b11000001;
  34.    
  35.     wait_for_one_press( );
  36.    
  37.     while (1)
  38.     {  
  39.         ADCON0=ADCON0|1;
  40.         GO = 1; //Start A/D Conversion
  41.         while(GO==1); //Wait end of conversion (conversion complete)
  42.         delay = ADRESH;
  43.        
  44.         PORTC=2;
  45.         delay_ms(delay);
  46.         PORTC=4;
  47.         delay_ms(delay);
  48.         PORTC=6;
  49.         delay_ms(delay);
  50.         PORTC=8;
  51.         delay_ms(delay);
  52.         PORTC=6;
  53.         delay_ms(delay);
  54.         PORTC=4;
  55.         delay_ms(delay);
  56.         PORTC=2;
  57.         delay_ms(delay);
  58.     }
  59. } //End of Program
Advertisement
Add Comment
Please, Sign In to add comment