pyrohaz

ADC Test Code

Mar 3rd, 2015
287
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.75 KB | None | 0 0
  1.  
  2. void ADC_Init(void){
  3.     //Configure Inputs
  4.     TRISA |= 0x01;
  5.     ANSEL |= 0x01;
  6.    
  7.     ADCON0 = (1<<0) | (0<<2); //Enable ADC and select channel 0
  8.     ADCON1 = (0<<4) | (0<<5); //Set both references to PIC supply
  9.     ADCON2 = (0<<0) | (7<<3) | (0<<7); //Use 20 acquisition cycles and FOsc/2, left justified
  10. }
  11.  
  12. void ADC_StartConv(void){
  13.     ADCON0 |= (1<<1);
  14. }
  15.  
  16. unsigned int ADC_CheckConv(void){
  17.     if(ADCON0 & 2) return 1;
  18.     else return 0;
  19. }
  20.  
  21. void ADC_WaitForConv(void){
  22.     while(ADC_CheckConv());
  23. }
  24.  
  25. unsigned char ADC_GetConv(void){
  26.     return ADRESH;
  27. }
  28.  
  29. void main(){
  30.     ADC_Init();
  31.    
  32.     unsigned char Conv = 0;
  33.    
  34.     //Continuously convert!
  35.     while(1){
  36.         ADC_StartConv();
  37.         ADC_WaitForConv();
  38.        
  39.         //Conv contains the converted value
  40.         Conv = ADC_GetConv();
  41.     }
  42. }
Advertisement
Add Comment
Please, Sign In to add comment