pyrohaz

Example sampling code for FSK

Mar 10th, 2015
273
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.23 KB | None | 0 0
  1. void ADC_Init(void){
  2.     //Configure Inputs
  3.     TRISA |= 0x01;
  4.     ANSEL |= 0x01;
  5.    
  6.     ADCON0 = (1<<0) | (0<<2); //Enable ADC and select channel 0
  7.     ADCON1 = (0<<4) | (0<<5); //Set both references to PIC supply
  8.     ADCON2 = (0<<0) | (7<<3) | (0<<7); //Use 20 acquisition cycles and FOsc/2, left justified
  9. }
  10.  
  11. void ADC_StartConv(void){
  12.     ADCON0 |= (1<<1);
  13. }
  14.  
  15. unsigned int ADC_CheckConv(void){
  16.     if(ADCON0 & 2) return 1;
  17.     else return 0;
  18. }
  19.  
  20. void ADC_WaitForConv(void){
  21.     while(ADC_CheckConv());
  22. }
  23.  
  24. unsigned char ADC_GetConv(void){
  25.     return ADRESH;
  26. }
  27.  
  28. volatile unsigned int ISample = 0, AmntSamples = 0;
  29. volatile int LPF = 0, HSample = 0;
  30. volatile int SZCrossP = 0;
  31. volatile unsigned char IZC = 0;
  32.  
  33. //Long is 32bit?
  34. volatile unsigned long Frequency = 0;
  35.  
  36. void InterruptServiceLow(void) {
  37.         //This interrupt takes 25 instructions (approx 25 cycles = 1.6us @ 64MHz=16MIPS)
  38.         //Happens every 16us (1/(16MHz/256)), uses about 1.6us/16us = 10% of CPU time
  39.         //Interrupt frequency: 62.5kHz
  40.    
  41.      if  (INTCON & 0b00000100) {     //TIMER0 Interrupt
  42.         INTCON &= 0b11111011;   //CLEAR Timer0 interrupt flag
  43.         tick++;
  44.         timer0intflag = 1;
  45.        
  46.         ISample = ADC_GetConv(); //Get previously converted value
  47.         ADC_StartConv(); //Start conversion in the background
  48.        
  49.         //High pass the sample to get rid of DC offset, cutoff determined through Matlab
  50.         //as 40Hz
  51.         LPF += (ISample - LPF)>>7;
  52.        
  53.         AmntSamples++;
  54.        
  55.         //Set the last sample
  56.         LSample = HSample;
  57.         HSample = ISample - LPF;
  58.        
  59.         //Detect zero crossing point   
  60.         //If current sample is positive and last sample was negative
  61.         //Therefore, this finds the rising edge of a signal
  62.         if(HSample>0 && LSample <=0){
  63.             if(!IZC){
  64.                 AmntSamples = 0;
  65.                 IZC = 1;
  66.             }
  67.             //Second rising edge
  68.             else{
  69.                 IZC = 0;
  70.                 SZCrossP = AmntSamples;
  71.                
  72.                 //The whole period took 'SZCrossP' samples. Knowing one tick is 16us
  73.                 //The frequency will be equal to 1/(SZCrossP*16us) e.g. if there were 100
  74.                 //samples for the whole period, the frequency would be 625Hz (1/(100*16e-6).
  75.                 //The equation could also be 1e6/(SZCrossP*16).
  76.                
  77.                 //Multiplication by 16 is the same as upshifting by 4.
  78.                 Frequency = 1000000ul/((unsigned long)SZCrossP<<4);
  79.             }
  80.         }
  81.     }
  82. }
Advertisement
Add Comment
Please, Sign In to add comment