Advertisement
Guest User

OOK 8kHz, 100 B/s

a guest
Apr 28th, 2015
198
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 19.13 KB | None | 0 0
  1. /** C O N F I G U R A T I O N   B I T S ******************************/
  2.  
  3. #pragma config FOSC = INTIO67, FCMEN = OFF, IESO = OFF                       // CONFIG1H
  4. #pragma config PWRT = OFF, BOREN = SBORDIS, BORV = 30                        // CONFIG2L
  5. #pragma config WDTEN = OFF, WDTPS = 32768                                     // CONFIG2H
  6. #pragma config MCLRE = ON, LPT1OSC = OFF, PBADEN = ON, CCP2MX = PORTC       // CONFIG3H
  7. #pragma config STVREN = ON, LVP = OFF, XINST = OFF                          // CONFIG4L
  8. #pragma config CP0 = OFF, CP1 = OFF, CP2 = OFF, CP3 = OFF                   // CONFIG5L
  9. #pragma config CPB = OFF, CPD = OFF                                         // CONFIG5H
  10. #pragma config WRT0 = OFF, WRT1 = OFF, WRT2 = OFF, WRT3 = OFF               // CONFIG6L
  11. #pragma config WRTB = OFF, WRTC = OFF, WRTD = OFF                           // CONFIG6H
  12. #pragma config EBTR0 = OFF, EBTR1 = OFF, EBTR2 = OFF, EBTR3 = OFF           // CONFIG7L
  13. #pragma config EBTRB = OFF                                                  // CONFIG7H
  14.  
  15. /** I N C L U D E S **************************************************/
  16. #include "p18f46k20.h"
  17. #include "delays.h"
  18. #include "usart.h"
  19.  
  20. //OLED crap
  21. #include "stdio.h"
  22. #include "OLED/oled_interface.h"
  23. #include "OLED/oled_jib.h"
  24. #include "OLED/oled_cmd.h"
  25.  
  26. /** V A R I A B L E S *************************************************/
  27. #pragma udata   // declare statically allocated uinitialized variables
  28. volatile unsigned long int tick = 0;
  29. volatile unsigned char timer0intflag = 0;
  30. volatile unsigned char last_ADC_val = 0;
  31. char sendbuffer[127] = "Hello world";
  32.  
  33.  
  34.  
  35. /** D E C L A R A T I O N S *******************************************/
  36. // declare constant data in program memory starting at address 0x180
  37. const rom unsigned char sinevals[128] = {
  38.     32,33,35,36,38,39,41,42,44,45,47,48,49,51,52,53,
  39.     54,55,56,57,58,59,60,60,61,62,62,62,63,63,63,63,
  40.     63,63,63,63,63,62,62,62,61,60,60,59,58,57,56,55,
  41.     54,53,52,51,49,48,47,45,44,42,41,39,38,36,35,33,
  42.     32,30,28,27,25,24,22,21,19,18,16,15,14,12,11,10,
  43.      9, 8, 7, 6, 5, 4, 3, 3, 2, 1, 1, 0, 0, 0, 0, 0,
  44.      0, 0, 0, 0, 0, 0, 1, 1, 2, 3, 3, 4, 5, 6, 7, 8,
  45.      9,10,11,12,14,15,16,18,19,21,22,24,25,27,28,30};
  46.  
  47. unsigned char ADC_GetConv(void);
  48. void ADC_StartConv(void);
  49.  
  50. #pragma interruptlow InterruptServiceLow// "interruptlow" pragma for low priority
  51. void InterruptServiceLow(void) {
  52.     //This interrupt takes 25 instructions (approx 25 cycles = 1.6us @ 64MHz=16MIPS)
  53.     //Happens every 16us (1/(16MHz/256)), uses about 1.6us/16us = 10% of CPU time
  54.     //Interrupt frequency: 62.5kHz
  55.     LATD |= 0b00000001;
  56.     if  (INTCON & 0b00000100) { //TIMER0 Interrupt
  57.         INTCON &= 0b11111011;   //CLEAR Timer0 interrupt flag
  58.         tick++;
  59.         last_ADC_val = ADRESH;
  60.         ADCON0 |= (1<<1);;
  61.         timer0intflag = 1;
  62.     }
  63.     LATD &= 0b11111110;
  64. }
  65.                                      
  66. //----------------------------------------------------------------------------
  67. // High priority interrupt vector
  68. /*#pragma code InterruptVectorHigh = 0x08
  69. void InterruptVectorHigh (void) // NOT USED ATM
  70. {
  71.   _asm
  72.     goto InterruptServiceHigh //jump to interrupt routine
  73.   _endasm
  74. }*/
  75.  
  76. //----------------------------------------------------------------------------
  77. // Low priority interrupt vector
  78. #pragma code InterruptVectorLow = 0x18
  79. void InterruptVectorLow (void) {
  80.   _asm
  81.     goto InterruptServiceLow //jump to interrupt routine
  82.   _endasm
  83. }
  84.  
  85. #pragma code    // declare executable instructions
  86.  
  87. typedef struct {
  88.     unsigned char   frequency;
  89.     unsigned char   amplitude;
  90.     char            phase;
  91. } wave;
  92.  
  93. typedef struct {
  94.     wave low;
  95.     wave high;
  96.     unsigned int duration;
  97. } modulation;
  98.  
  99. void setup(void) {
  100.         //////// PORT SETUP ////////
  101.     TRISC  = 0b00000000;        //All PORTC pins are OUTPUTS
  102.     LATC = 0b00101000;          //All PORTC pins are HIGH
  103.     TRISD  = 0b00000000;        //All PORTC pins are OUTPUTS (LEDs 0 to 7)
  104.     LATD   = 0b00100000;        //All PORTC pins are (LED 5 is HIGH (PWM))
  105.         TRISE = 0b00000111;     // Needed for OLED Display
  106.         TRISA = 0;    
  107.         LATA = 0b11110000;      //Needed for OLED Display ( Don't use 1st bit) -TRIS A defined in adc_init
  108.        
  109.         TRISB = 0b00001111;             // All switches are inputs 0-3
  110.         ANSELH = 0x00;                  // RB0 - RB4 Digital input
  111.         WPUB   = 0b11111111;    //PULL UP PULL UP
  112.  
  113.  
  114.         //////// SYSTEM CLOCK to 64MHz //////// (Initially FOSC = 1MHz)
  115.     OSCCON       = 0b01110000;  //Clock = 16MHz (=FOSC), (internal oscillator, full speed)
  116.         OSCTUNE |= 0b01000000;  //Clock = 64MHz (=FOSC*4), (PLL enable bit set )
  117.  
  118.         //////// PWM SETUP ////////
  119.     PR2     = 0b00001111;       //Timer2 period = 15
  120.     CCPR1L  = 0b00000000;       //Duty cycle 8 HIGH bits CLEARED
  121.     CCP1CON = 0b00001100;       //SINGLE OUTPUT mode, Duty cycle 2 LOW bits CLEARED, PWM pins are LOW, PWM MODE, ACTIVE HIGH
  122.     T2CON   = 0b00000100;       //Timer 2 ENABLED, PRESCALER = 1:1, POSTSCALER: 1:1
  123.         PSTRCON = 0b00000011;   //PWM PULSE STEERING: PWM routed to P1A (RC2) and P1B (RD5, LED5)
  124.  
  125.         //////// TIMER0 INTERRUPT SETUP ////////
  126.         RCON   |= 0b10000000;   //Interrupt Priority Levels ENABLED (IPEN bit set)
  127.         INTCON  = 0b11100000;   //Global Interrupt L&H ENABLE, Timer0 int. ENABLE, other interrupts DISABLED, flags CLEARED
  128.         INTCON2 = 0b00000000;   //Timer0 Interrupt is LOW priority
  129.     TMR0H       = 0b00000000;   //Clear timer - always write upper byte first
  130.     TMR0L       = 0b00000000;
  131.     T0CON       = 0b11001000;   //Timer ENABLED, 8-BIT mode, clock source is FOSC (16MHz), NO PRESCALER
  132.         //T0CON         = 0b10001000;   // ! ! ! 16-BIT MODE ! ! !
  133.        
  134.         LATD   |= 0b10000000;   //Turn LED 7 ON to indicate program execution
  135.                                                         //We have 8 LEDs on the board, we might as well use them.
  136.         // Configure MSSP for SPI master mode
  137.         SSPCON1 = 0b00110010;                                   // clock idle high, Clk /64
  138.         SSPSTAT = 0b00000000;
  139.         oled_res = 1;                                                   // do not reset LCD yet
  140.         oled_cs = 1;                                                    // do not select the LCD
  141.         INTCONbits.PEIE = 1;
  142.         //INTCONbits.GIE = 1;
  143. }
  144.  
  145.  
  146.  
  147. void pwm(unsigned char level) {
  148.         CCP1CON &= 0b11001111; //Clear 2 LSBs
  149.         CCP1CON |= ((level & 0b00000011)<<4); //Set 2 LSBs
  150.         CCPR1L  = 0b00001111 & (level>>2);    //Write 4 MSBs
  151.         return;
  152. }
  153.  
  154. void sine(wave wav, unsigned int duration) {
  155.     int i = 0;
  156.     int j = wav.phase;
  157.     int nextpwm;
  158.     for (i=0; i<duration; i++) {
  159.         nextpwm = (wav.amplitude!=0)?sinevals[j]:32;
  160.         LATD |= 0b00000010;
  161.         while(!(timer0intflag)); //wait for interrupt to happen
  162.         LATD &= 0b11111101;
  163.         timer0intflag = 0;  //Reset interrupt flag
  164.         pwm(nextpwm);   //Next pwm
  165.         j+=wav.frequency;
  166.         j &= 127;
  167.     }
  168. }
  169.  
  170. /*void sendbyte(unsigned char byteToSend, modulation mod) {
  171.     unsigned char bitno;
  172.     sine(mod.low, mod.duration);    //Start bit
  173.     for (bitno=0; bitno<8; bitno++) {   //Send 8 bit,s LSB first
  174.         sine(((byteToSend&(1<<bitno))?mod.high:mod.low), mod.duration);
  175.     }
  176.     sine(mod.high, mod.duration);   //Two stop bits
  177.     sine(mod.high, mod.duration);
  178. }*/
  179.  
  180. void sendbyte(unsigned char byteToSend, modulation mod) {
  181.     unsigned char bitno;
  182.     sine(mod.low, mod.duration);    //Two stop bits
  183.     sine(mod.low, mod.duration);
  184.     sine(mod.high, mod.duration);
  185.     for (bitno=0; bitno<8; bitno++) {   //Send 8 bit,s LSB first
  186.         sine(((byteToSend&(1<<bitno))?mod.high:mod.low), mod.duration);
  187.     }
  188.     sine(mod.low, mod.duration);    //Two stop bits
  189.     sine(mod.low, mod.duration);
  190. }
  191.  
  192. void sendstring(char* charptr, modulation mod) {
  193.     while (*charptr != 0) {
  194.         sendbyte(*charptr, mod);
  195.         charptr++;
  196.     }
  197.     sendbyte(0x00, mod);
  198.     LATD |= 0b00000001;
  199. }
  200.  
  201.  
  202. void UART_ArraySend(char *array)
  203. {
  204.     int i=0;
  205.     while(*(array+i)!=0) {
  206.         WriteUSART(array[i]);
  207.         while(!(TXSTA & 0b10));
  208.         i++;
  209.     }
  210.  
  211. }
  212.  
  213. //////// ADC functions ////////
  214. void ADC_Init(void){
  215.         //Configure Inputs
  216.         TRISA |= 0x01;
  217.         ANSEL |= 0x01;
  218.        
  219.         ADCON0 = (1<<0) | (0<<2); //Enable ADC and select channel 0
  220.         ADCON1 = (0<<4) | (0<<5); //Set both references to PIC supply
  221.         ADCON2 = (4<<0) | (7<<3) | (0<<7); //Use 20 acquisition cycles and FOsc/4, left justified
  222. }
  223.  
  224. void ADC_StartConv(void){
  225.         ADCON0 |= (1<<1);
  226. }
  227.  
  228. unsigned int ADC_CheckConv(void){
  229.         if(ADCON0 & 2) return 1;
  230.         else return 0;
  231. }
  232.  
  233. void ADC_WaitForConv(void){
  234.         while(ADC_CheckConv());
  235. }
  236.  
  237. unsigned char ADC_GetConv(void){
  238.         return ADRESH;
  239. }
  240.  
  241. //////// Demodulation //////
  242.  
  243.  
  244.  
  245.  
  246. unsigned char demodulate(modulation mod) {
  247.     unsigned long int startTick = 0;
  248.     unsigned int freq = 0;
  249.     unsigned char prev_ADRESH = 0;
  250.     unsigned int tresholdfreq = ((mod.high.frequency)*(mod.duration/128) + (mod.low.frequency)*(mod.duration/128)) / 2;
  251.     unsigned char received_data = 0b00000000;
  252.     unsigned char bit_no;
  253.     unsigned long int lastZero = tick; //This used to be a CHAR because I'm retarded
  254.     unsigned int lastPeriod = 0;
  255.    
  256.     /*while(lastPeriod > tresholdfreq && startTick > 8) {
  257.         while(!(timer0intflag)); //wait for interrupt to happen
  258.         timer0intflag = 0;  //Reset interrupt flag
  259.         if (ADRESH > 127 && prev_ADRESH <= 127) {
  260.             lastPeriod = tick - lastZero;
  261.         }
  262.         startTick++;
  263.     }
  264.    
  265.     startTick = tick;
  266.     while(tick > startTick + mod.duration);*/
  267.    
  268.     while(!(timer0intflag));
  269.     timer0intflag = 0;
  270.     for (bit_no = 0; bit_no < 8; bit_no++) {
  271.         startTick = tick;
  272.         freq=0;
  273.         while (tick < (startTick + mod.duration)) {
  274.             /*if (timer0intflag == 1) {
  275.                 LATD=0b10101010;
  276.                 while(1);
  277.             }*/
  278.             LATD |= 0b00000010;
  279.             while(!(timer0intflag)); //wait for interrupt to happen
  280.             LATD &= 0b11111101;
  281.             timer0intflag = 0;  //Reset interrupt flag
  282.             if (last_ADC_val > 127 && prev_ADRESH <= 127) freq++;
  283.             prev_ADRESH = last_ADC_val;
  284.             //LATD=(startTick + mod.duration)-tick;
  285.            
  286.         };
  287.        
  288.         //LATD=tresholdfreq;
  289.         received_data |= ((freq>tresholdfreq)?1:0)<<bit_no;
  290.    
  291.     }
  292.     return (mod.high.frequency < mod.low.frequency)? ~received_data : received_data;
  293. }
  294.  
  295. unsigned char demodulatePSK(modulation mod) {
  296.     unsigned long int startTick = 0;
  297.     unsigned long int lastZero = 0;
  298.     unsigned char prev_ADRESH = 0;
  299.     unsigned char received_data = 0b00000000;
  300.     unsigned char bit_no;
  301.     unsigned char wave_period_count = 0;
  302.     unsigned int tresholdfreq = 0;
  303.     unsigned int freq=0;
  304.     unsigned int reference_wave_offset = 0;
  305.     unsigned int current_bit_wave_offset = 0;
  306.     unsigned int period = 128/mod.low.frequency;
  307.     int shift_score = 0;
  308.    
  309.     // Wait for start condition
  310.     LATD |= 0b00001000; //LED4: waiting for start condition
  311.     while (1 /*loop left with BREAK*/) {
  312.  
  313.             LATD |= 0b00000010;
  314.             while(!(timer0intflag)); //wait for interrupt to happen
  315.             LATD &= 0b11111101;
  316.             timer0intflag = 0;  //Reset interrupt flag
  317.             if (last_ADC_val > 127 && prev_ADRESH <= 127) {
  318.                 prev_ADRESH = last_ADC_val;
  319.                 if (tick-lastZero < 2*period) {
  320.                     startTick=tick;
  321.                     LATD &= 0b11110111; //Clear LED4 (waiting for start cond.)
  322.                     break; //exit while(1) and start demodulation
  323.                 } else {
  324.                     //No start condition
  325.                     //Update lastZero
  326.                     lastZero = tick;
  327.                 }
  328.             }
  329.             prev_ADRESH = last_ADC_val;
  330.     }
  331.    
  332.     LATD |= 0b00000100; //START CONDITION, CALIBRATING
  333.     while (tick < (startTick + mod.duration)) {
  334.         //Fuck calibration, Occam's razor, bitch!
  335.        
  336.             /*LATD |= 0b00000010;
  337.             while(!(timer0intflag)); //wait for interrupt to happen
  338.             LATD &= 0b11111101;
  339.             timer0intflag = 0;  //Reset interrupt flag
  340.             if (last_ADC_val > 127 && prev_ADRESH <= 127) {
  341.                 reference_wave_offset += (tick-startTick - mod.duration/mod.low.frequency*wave_period_count);
  342.                 wave_period_count++;
  343.             }
  344.             prev_ADRESH = last_ADC_val;*/
  345.     }
  346.     //reference_wave_offset /= wave_period_count;
  347.     LATD &= 0b11111011;
  348.    
  349.     timer0intflag = 0;
  350.     for (bit_no = 0; bit_no < 8; bit_no++) {
  351.         shift_score =0;
  352.         while (tick < (startTick + mod.duration*(bit_no+1))) {
  353.             LATD |= 0b00000010;
  354.             while(!(timer0intflag)); //wait for interrupt to happen
  355.             LATD &= 0b11111101;
  356.             timer0intflag = 0;  //Reset interrupt flag
  357.             if (last_ADC_val > 127 && prev_ADRESH <= 127) {
  358.                 if ((tick-startTick)%period < period/4 || (tick-startTick)%period > 3*period/4) {
  359.                     shift_score--;
  360.                 } else {
  361.                     shift_score++;
  362.                 }
  363.             }
  364.             prev_ADRESH = last_ADC_val;
  365.         }
  366.                    
  367.         received_data |= ((shift_score>0)?1:0)<<bit_no;
  368.         //LATD=11;
  369.    
  370.     }
  371.     return (mod.high.frequency < mod.low.frequency)? ~received_data : received_data;
  372. }
  373.  
  374. unsigned char max_QPSK_score(int ss00, int ss01, int ss10, int ss11) {
  375.     int temp = ss00;
  376.     if (ss01>temp) temp = ss01;
  377.     if (ss10>temp) temp = ss10;
  378.     if (ss11>temp) temp = ss11;
  379.    
  380.     return  (ss00==temp) ? 0b00 :
  381.             (ss01==temp) ? 0b01 :
  382.             (ss10==temp) ? 0b10 :
  383.             (ss11==temp) ? 0b11 :
  384.             0b00;
  385. }
  386.  
  387. unsigned char demodulateQPSK(modulation mod) {
  388.     unsigned long int startTick = 0;
  389.     unsigned long int lastZero = 0;
  390.     unsigned char prev_ADRESH = 0;
  391.     unsigned char received_data = 0b00000000;
  392.     unsigned char bit_no;
  393.     unsigned char wave_period_count = 0;
  394.     unsigned int tresholdfreq = 0;
  395.     unsigned int freq=0;
  396.     unsigned int reference_wave_offset = 0;
  397.     unsigned int current_bit_wave_offset = 0;
  398.     unsigned int period = 128/mod.low.frequency;
  399.     int shift_score_00 = 0;
  400.     int shift_score_01 = 0;
  401.     int shift_score_10 = 0;
  402.     int shift_score_11 = 0;
  403.     unsigned char qpsk_temp = 0;
  404.    
  405.     // Wait for start condition
  406.     LATD |= 0b00001000; //LED4: waiting for start condition
  407.     while (1 /*loop left with BREAK*/) {
  408.  
  409.             LATD |= 0b00000010;
  410.             while(!(timer0intflag)); //wait for interrupt to happen
  411.             LATD &= 0b11111101;
  412.             timer0intflag = 0;  //Reset interrupt flag
  413.             if (last_ADC_val > 127 && prev_ADRESH <= 127) {
  414.                 prev_ADRESH = last_ADC_val;
  415.                 if (tick-lastZero < 2*period) {
  416.                     startTick=tick;
  417.                     LATD &= 0b11110111; //Clear LED4 (waiting for start cond.)
  418.                     break; //exit while(1) and start demodulation
  419.                 } else {
  420.                     //No start condition
  421.                     //Update lastZero
  422.                     lastZero = tick;
  423.                 }
  424.             }
  425.             prev_ADRESH = last_ADC_val;
  426.     }
  427.    
  428.     LATD |= 0b00000100; //START CONDITION, CALIBRATING
  429.     while (tick < (startTick + mod.duration)) {
  430.         //Fuck calibration, Occam's razor, bitch!
  431.        
  432.             /*LATD |= 0b00000010;
  433.             while(!(timer0intflag)); //wait for interrupt to happen
  434.             LATD &= 0b11111101;
  435.             timer0intflag = 0;  //Reset interrupt flag
  436.             if (last_ADC_val > 127 && prev_ADRESH <= 127) {
  437.                 reference_wave_offset += (tick-startTick - mod.duration/mod.low.frequency*wave_period_count);
  438.                 wave_period_count++;
  439.             }
  440.             prev_ADRESH = last_ADC_val;*/
  441.     }
  442.     //reference_wave_offset /= wave_period_count;
  443.     LATD &= 0b11111011;
  444.    
  445.     timer0intflag = 0;
  446.     for (bit_no = 0; bit_no < 4; bit_no++) {
  447.         shift_score_00 = shift_score_01 = shift_score_10 = shift_score_11 = 0;
  448.         while (tick < (startTick + mod.duration*(bit_no+1))) {
  449.             LATD |= 0b00000010;
  450.             while(!(timer0intflag)); //wait for interrupt to happen
  451.             LATD &= 0b11111101;
  452.             timer0intflag = 0;  //Reset interrupt flag
  453.             if (last_ADC_val > 127 && prev_ADRESH <= 127) {
  454.                 if ((tick-startTick)%period < period/8 || (tick-startTick)%period > 7*period/8) {
  455.                     shift_score_00++;
  456.                 } else if ((tick-startTick)%period > period/8 && (tick-startTick)%period < 3*period/8){
  457.                     shift_score_01++;
  458.                 } else if ((tick-startTick)%period > 3*period/8 && (tick-startTick)%period < 5*period/8){
  459.                     shift_score_10++;
  460.                 } else if ((tick-startTick)%period > 5*period/8 && (tick-startTick)%period < 7*period/8){
  461.                     shift_score_11++;
  462.                 }
  463.             }
  464.             prev_ADRESH = last_ADC_val;
  465.         }
  466.        
  467.        
  468.        
  469.         received_data |= (max_QPSK_score(shift_score_00, shift_score_01, shift_score_10, shift_score_11))<<bit_no*2;
  470.         //LATD=11;
  471.    
  472.     }
  473.     return (mod.high.frequency < mod.low.frequency)? ~received_data : received_data;
  474. }
  475.  
  476. unsigned char demodulateOOK(modulation mod) {
  477.     unsigned long int startTick = 0;
  478.     unsigned long int lastZero = 0;
  479.     unsigned char prev_ADRESH = 0;
  480.     unsigned char received_data = 0b00000000;
  481.     unsigned char bit_no;
  482.     unsigned int tresholdfreq = 0;
  483.     unsigned int freq=0;
  484.    
  485.     // Wait for start condition
  486.     LATD |= 0b00001000; //LED4: waiting for start condition
  487.     while (1 /*loop left with BREAK*/) {
  488.  
  489.             LATD |= 0b00000010;
  490.             while(!(timer0intflag)); //wait for interrupt to happen
  491.             LATD &= 0b11111101;
  492.             timer0intflag = 0;  //Reset interrupt flag
  493.             if (last_ADC_val > 127 && prev_ADRESH <= 127) {
  494.                 if (tick-lastZero < 2*128/mod.low.frequency) {
  495.                     startTick=tick;
  496.                     LATD &= 0b11110111; //Clear LED4 (waiting for start cond.)
  497.                     LATD |= 0b00000100; //LED3: start condition detected
  498.                     while (tick < (startTick + mod.duration)); //wait for start cond. to end
  499.                     LATD &= 0b11111011;
  500.                     break; //exit while(1) and start demodulation
  501.                 } else {
  502.                     //No start condition
  503.                     //Update lastZero
  504.                     lastZero = tick;
  505.                 }
  506.             }
  507.             prev_ADRESH = last_ADC_val;
  508.     }
  509.        
  510.     timer0intflag = 0;
  511.     for (bit_no = 0; bit_no < 8; bit_no++) {
  512.         startTick = tick;
  513.         freq=0;
  514.         while (tick < (startTick + mod.duration)) {
  515.  
  516.             LATD |= 0b00000010;
  517.             while(!(timer0intflag)); //wait for interrupt to happen
  518.             LATD &= 0b11111101;
  519.             timer0intflag = 0;  //Reset interrupt flag
  520.             if (last_ADC_val > 127 && prev_ADRESH <= 127) freq++;
  521.             prev_ADRESH = last_ADC_val;
  522.         }
  523.        
  524.        
  525.         received_data |= ((freq>(mod.low.frequency*mod.duration/256))?1:0)<<bit_no;
  526.         //LATD=11;
  527.    
  528.     }
  529.     return (mod.high.frequency < mod.low.frequency)? ~received_data : received_data;
  530. }
  531.  
  532. void receiveStringOOK(modulation mod, unsigned char *buffer) {
  533.         int i = 0;
  534.         unsigned char receivedByte;
  535.         for(i=0; i<255 && receivedByte != 0x00; i++) {
  536.             buffer[i] = receivedByte = demodulateOOK(mod);;
  537.         }
  538.         if (i>=255) buffer[255] = 0x00;
  539.         return;
  540.     }
  541.  
  542. unsigned char oledbuffer[256];
  543.  
  544. void main (void) {
  545.     unsigned int C=0;
  546.     unsigned char next;
  547.     unsigned char config = 0;
  548.     char helloworld[] = "Hello World!";
  549.  
  550.     int baud;
  551.     modulation ask;
  552.     modulation fsk;
  553.     modulation psk;
  554.     modulation qpsk;
  555.     wave start;
  556.  
  557.     setup();
  558.     Delay10KTCYx(64);
  559.  
  560.     // These are the functions you need to use to initialise the display
  561.     oled_init();
  562.     oled_clear();
  563.     oled_refresh();
  564.     ADC_Init();
  565.     Delay10KTCYx(64);
  566.  
  567.     fsk.high.frequency  = 2;
  568.     fsk.high.amplitude  = 128;
  569.     fsk.high.phase      = 0;
  570.     fsk.low.frequency   = 4;
  571.     fsk.low.amplitude   = 128;
  572.     fsk.low.phase       = 0;
  573.     fsk.duration        = 256;
  574.  
  575.  
  576.     psk.high.frequency  = 4;
  577.     psk.high.amplitude  = 128;
  578.     psk.high.phase      = 32;
  579.     psk.low.frequency   = 4;
  580.     psk.low.amplitude   = 128;
  581.     psk.low.phase       = 96;
  582.     psk.duration        = 128;
  583.  
  584.     ask.high.frequency  = 16;
  585.     ask.high.amplitude  = 128;
  586.     ask.high.phase      = 0;
  587.     ask.low.frequency   = 16;
  588.     ask.low.amplitude   = 0;
  589.     ask.low.phase       = 0;
  590.     ask.duration        = 48;
  591.  
  592.  
  593.     start.frequency=1;
  594.     start.amplitude=128;
  595.     start.phase=0;
  596.    
  597.     while (1) {
  598.         oled_clear();
  599.         receiveStringOOK(ask, oledbuffer);
  600.         oled_puts_2x (oledbuffer);
  601.         oled_refresh();
  602.         while(1);
  603.     }
  604.    
  605.  
  606.  
  607.     LATD=demodulateOOK(ask);
  608.     while(1);
  609.     //LATD=8;
  610.     while(1) {
  611.         //sendbyte(0b11110000, fsk);
  612.         //LATD=(ADRESH>127)?3:0;
  613.         LATD|=128;
  614.         //for (baud=0; baud<4; baud++)
  615.         //  demodulate(fsk);
  616.         LATD&= ~128;
  617.         LATD|=64;
  618.         for (baud=0; baud<32; baud++)
  619.             sine(fsk.low,256);
  620.         LATD&= ~64;
  621.     }
  622.  
  623.    
  624. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement