CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

Decode PWM data Signal [solved]

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
ishan



Joined: 11 Jun 2011
Posts: 3
Location: India

View user's profile Send private message

Decode PWM data Signal [solved]
PostPosted: Sat Jun 11, 2011 8:56 am     Reply with quote

I am writing a program to decode communication between Ultrasonic Parking Sensor and its display.

The data is PWM encoded with a 600 us (0) signal and 1200us (1) signal Ref.
http://www.mp3car.com/hardware-development/121995-custom-usb-parking-sensor-interface-formally-pdc-usb.html
I have written the following code and interrupts are being triggered. However I have realised that some bits are being skipped (bunch of 2 or 3). I guess the problem is in the way I have set up the interrupts and maybe the printf statement.
Request help.

Code:

#include <18F4550.H>
#fuses HSPLL, PLL5, CPUDIV1, NOWDT, PUT, BROWNOUT, NOLVP
#use delay(clock=48000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7)


int bit_val;short flag;
long rise,fall,pulse_width;

#int_ccp2
void isr()
{
 
  rise = CCP_1;
   fall = CCP_2;
   pulse_width = fall - rise;

   bit_val=3;
if (pulse_width <=3600 && pulse_width >=3598) {
   bit_val=0;
   } 
if (pulse_width <=7200 && pulse_width >=7197) {
   bit_val=1;
   }

   
}



void main()
{
   printf("\n\rPulse Width:\n\r");
   setup_ccp1(CCP_CAPTURE_RE);    // Configure CCP1 to capture rise
   setup_ccp2(CCP_CAPTURE_FE);    // Configure CCP2 to capture fall
   setup_timer_1(T1_INTERNAL);    // Start timer 1

   enable_interrupts(INT_CCP2);   // Setup interrupt on falling edge
   enable_interrupts(GLOBAL);

   while(TRUE) {
      if (bit_val<=1){
      //printf("\n\r%lu us ", pulse_width );
      printf("%u", bit_val);
         bit_val=4;
         
      
      }

     
   }
}
     


Following is the sample data that I have got using the above program
Code:
111100000000011111111011010010111011110001000110100100001001
101011001110000111101111000000011100001000001111111101100001
111101111000100011000011000011111011000111100000000011111111
011110000000001111111011010010000100110101100110100100101001
101011001110000100000111111110111000010000011111111011000011
010011111011000110000110101001101011101111000000000111111110
111100000000011111111011010010010100110101100110100100001001
101011001110000100000111111110111000010000011111111011000011
000100110101110110000110101001101011001111000000000111111110
111100000000011111111011010010010100110101100110100100101011
101011001110000100000111111110111000010000011111111011000011
010101110101100110000110001001101011001111000000000111111101
111000000000111111110110100100001001101011001101001001010011
010110011100001000001111111101110000100000111111110110000110
101001101011001100001101010011010111011110000000001111111101
111000000000111111110110100100101011101011001101001000010011
010110011100001000001111111101110000100000111111110110000110
001001101011101100001101010011010110011110000000001111111101
111000000000111111110110100100101001101011001101001000010011
010110011100001000001111111101110000100000111111110110000110
101011101011001100001100010011010110011110000000001111111101
111000000000111111101101001000010011010110011010010010100110
101110111000010000011111111011100001000001111111101100001101
010011010110011000011010100110101110111100000000011111111011
110000000001111111101101001001010011010111011010010000100110
101110111000010000011111111011100001000001111111101100001100
010011010111011000011010100110101100111100000000011111111011
110000000001111111101101001001010011010111011010010000100110
101100111000010000011111111011100001000001111111101100001100
010011010110011000011010100110101110111100000000011111111011
110000000001111111011010010000100110101100110100100101001101
011001110000100000111111110111000010000011111111011000011010
100110101110110000110001001101011001111000000000111111101111
000000000111111110110100100001001101011001101001001010011010
110011100001000001111111101110000100000111111110110000110101
001101011001100001101010011010111011110000000001111111101111
000000000111111110110100100101001101011001101001000010011010
110011100001000001111111101110000100000111111110110000110001
001101011101100001101010011010111011110000000001111111101111
000000000111111101101001001010011010110011010010000100110101
100111000010000011111111011100001000001111111101100001100010
011010111011000011010100110101110111100000000011111111011110
000000001111111011010010000100110101100110100100101011101011
001110000100000111111110111000010000011111111011000011010100
1101011101100001100


The data format is as follows
Total data packet of each sensor consists of 24 bits (3bytes x8)
The first byte is the sensor address and consists of 2 nibbles which are complementary of each other (1111 0000).
The second byte is the complement of third byte. and third byte contains the distance data

Except for the initial packet all sensor data is transmitted twice . So the sensor transmission is ABBCCDDAABBCCDD....with ABCD respectively being sensor names.

I will highlight the problem of missing bits in above data output. Lets consider first 24 bits
Sample data : 111100000000011111111011
11110000 - Sensor A Address
00000111 - Complement of 3rd byte
11111011 - Now you can see that there are 02 bits missing both of them 0's. Ideally the packet should have been 11111000 instead of 11111011

Help!
ishan



Joined: 11 Jun 2011
Posts: 3
Location: India

View user's profile Send private message

Stupid Oversight: Serial Baud Rate
PostPosted: Sat Jun 11, 2011 11:42 am     Reply with quote

Solution could not have been more simpler...
Increased the serial baudrate to 19200 and the missing bits became visible..

Will post the whole code shortly

Thanks
ishan



Joined: 11 Jun 2011
Posts: 3
Location: India

View user's profile Send private message

Opensource Code for PDC USB - Usb Ultrasonic Parking Sensor
PostPosted: Sun Jun 26, 2011 12:58 am     Reply with quote

As I mentioned in the above post..

Detailed Code for PDC USB Car is available at
http://ishankarve.blogspot.com/2011/06/decoding-ultrasonic-parking-sensor.html

Code as follows

Code:
/* 
 Copyright 2011 Ishan Anant Karve, India. All rights reserved. 
 Redistribution and use in source and binary forms, with or without modification, are 
 permitted provided that the following conditions are met: 
   1. Redistributions of source code must retain the above copyright notice, this list of 
    conditions and the following disclaimer. 
   2. Redistributions in binary form must reproduce the above copyright notice, this list 
    of conditions and the following disclaimer in the documentation and/or other materials 
    provided with the distribution. 
 THIS SOFTWARE IS PROVIDED BY ISHAN ANANT KARVE ``AS IS'' AND ANY EXPRESS OR IMPLIED 
 WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND 
 FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> OR 
 CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 
 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 
 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON 
 ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 
 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 
 ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
 The views and conclusions contained in the software and documentation are those of the 
 authors and should not be interpreted as representing official policies, either expressed 
 */ 
 #include <18F4550.h> 
 #fuses HSPLL,NOWDT,NOPROTECT,NOLVP,NODEBUG,USBDIV,PLL5,CPUDIV1,VREGEN 
 #use delay(clock=48000000) 
 #use rs232(baud=19200, xmit=PIN_C6, rcv=PIN_C7) 
 #DEFINE USB_HID_DEVICE TRUE 
 #define USB_EP1_TX_ENABLE USB_ENABLE_INTERRUPT  //turn on EP1 for IN bulk/interrupt transfers 
 #define USB_EP1_TX_SIZE  8 //allocate 8 bytes in the hardware for transmission 
 #define USB_EP1_RX_ENABLE USB_ENABLE_INTERRUPT  //turn on EP1 for OUT bulk/interrupt transfers 
 #define USB_EP1_RX_SIZE  8 //allocate 8 bytes in the hardware for reception 
 #define RAND_MAX 200   
 #define SEN_A     0b11110000 //SENSOR A Address 
 #define SEN_B     0b11010010     //SENSOR B Address 
 #define SEN_C     0b11100001     //SENSOR C Address 
 #define SEN_D     0b11000011     //SENSOR D Address 
 //change USB descriptors for custom use 
 #include <stdlib.h>           //required for rand() function 
 #include <pic18_usb.h>  //Microchip 18Fxx5x hardware layer for usb.c 
 #include <usb_desc_hid.h>     //USB Configuration and Device descriptors for this UBS device 
 #include <usb.c>    //handles usb setup tokens and get descriptor reports 
 short tx_flag; 
 int bit_val;int count; 
 short sensor_ready; //flags for correspond to 03 bytes of each sensor data 
 int sensor,Dist_A,Dist_B,Dist_C,Dist_D, nibble_byte,dist_byte; 
 short BUZZER; 
 long rise,fall,pulse_width;int16 temp; 
 #int_ccp2 
 void isr() 
 { 
  rise = CCP_1; 
   fall = CCP_2; 
   pulse_width = fall - rise; 
 bit_val=2; 
 if (pulse_width <=3600 && pulse_width >=3598) { 
      bit_val=0; count++;shift_left(&temp,1,bit_val); 
      }   
 if (pulse_width <=7200 && pulse_width >=7197) { 
      bit_val=1; count++;shift_left(&temp,1,bit_val); 
      } 
      if (temp==SEN_A){sensor=1;sensor_ready=1;count=0;nibble_byte=0;dist_byte=0;temp=0;} //distance DIST_A purposely not set to zero 
      if (temp==SEN_B){sensor=2;sensor_ready=1;count=0;nibble_byte=0;dist_byte=0;temp=0;} 
      if (temp==SEN_C){sensor=3;sensor_ready=1;count=0;nibble_byte=0;dist_byte=0;temp=0;} 
      if (temp==SEN_D){sensor=4;sensor_ready=1;count=0;nibble_byte=0;dist_byte=0;temp=0;} 
 } 
 void main() { 
 int8 out_data[20]; 
 int8 in_data[2]; 
 int8 send_timer=0; 
 count=0;temp=0;bit_val=2;tx_flag=0; 
 sensor=0;sensor_ready=0;count=0;nibble_byte=0;dist_byte=0;Dist_A=0;Dist_B=0;Dist_C=0;Dist_D=0; 
 //SETUP INTERRUPTS 
 setup_ccp1(CCP_CAPTURE_RE);  // Configure CCP1 to capture rise 
 setup_ccp2(CCP_CAPTURE_FE);  // Configure CCP2 to capture fall 
 setup_timer_1(T1_INTERNAL);  // Start timer 1 
 enable_interrupts(INT_CCP2);  // Setup interrupt on falling edge 
 enable_interrupts(GLOBAL); 
 delay_ms(1000); 
 printf("\r\n\nParking Sensor"); 
 usb_init_cs(); 
      while (TRUE) { 
   usb_task(); 
           if (sensor_ready && count==16){ 
                nibble_byte=make8(temp,0); dist_byte=~nibble_byte; 
                switch (sensor) { 
                  case 1:Dist_A=dist_byte; //Set data for Sensor A 
                break; 
                  case 2:Dist_B=dist_byte; //Set data for Sensor B 
                break; 
                  case 3:Dist_C=dist_byte; //Set data for Sensor C 
                break; 
                  case 4:Dist_D=dist_byte; //Set data for Sensor D 
                break; 
              } 
                //printf("%u --> %u\n\r"sensor,dist_byte); //for debug purposes 
                //reset all variables 
                sensor=0;sensor_ready=0;count=0;nibble_byte=0;dist_byte=0;temp=0; 
           }       
    if (usb_enumerated()) { 
      if (!send_timer) { 
       send_timer=250; 
       out_data[0]=Dist_A; 
       out_data[1]=Dist_B; 
       out_data[2]=Dist_C; 
       out_data[3]=Dist_D; 
       out_data[4]=0; 
            if (tx_flag){tx_flag=0; 
                 if (usb_put_packet(1, out_data,5, USB_DTS_TOGGLE)){ 
                   //printf("\r\n<-- Sending 2 bytes: 0x%X 0x%X 0x%X 0x%X 0x%X", out_data[0], out_data[1], out_data[2], out_data[3], out_data[4]); 
                          //printf("\r\n<-- Sending 2 bytes: %u %u %u %u %u", out_data[0], out_data[1], out_data[2], out_data[3], out_data[4]);// for debug purposes 
                          } 
                }           
                } 
      if (usb_kbhit(1)) { 
       usb_get_packet(1, in_data, 2); 
       { 
                //printf("\r\n--> Received data: 0x%X 0x%X",in_data[0],in_data[1]);//for debug purposes 
                } 
       if (in_data[0]==0x20) {tx_flag=1;}   
     } 
      send_timer--; 
      delay_ms(1); 
    } 
   } 
 } 
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group