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 CCS Technical Support

Piezo sensors

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



Joined: 30 Nov 2009
Posts: 6

View user's profile Send private message

Piezo sensors
PostPosted: Tue Mar 16, 2010 7:40 pm     Reply with quote

Hi

I was building a drum kit using piezo sensors. I am using a pic 18f4420 with an analogue to digital input. The circuit is connected to the computer using a midi usb cable. I am using putc() to send signals to the computer. What I wanted to know was how the analogue signals are read into the pic as they are not just on(1) or off(0). If anyone has any sample code that shows how the signals are read and used I would appreciate it.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Tue Mar 16, 2010 11:51 pm     Reply with quote

Quote:

What I wanted to know was how the analogue signals are read into the pic.

How to use the ADC in 10-bit mode, over the full 0v to 5v input range:
http://www.ccsinfo.com/forum/viewtopic.php?t=40279&start=3

How to convert the 10-bit ADC value to a Voltage and display it:
http://www.ccsinfo.com/forum/viewtopic.php?t=32168&start=1

How to use Vref+ and Vref- with the ADC (if necessary):
http://www.ccsinfo.com/forum/viewtopic.php?t=39341&start=4
Ikk06



Joined: 30 Nov 2009
Posts: 6

View user's profile Send private message

PostPosted: Wed Mar 17, 2010 10:08 am     Reply with quote

This is the code that I currently have to setup the adc and read from it the digital value. Based on this value I want to send a diffrent output to the computer via the midi calbe. Have I set this up correctly??
Code:

#include <stdio.h>
#include <stdlib.h>

void main()
{
int x;

setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);
setup_timer_1(T1_DISABLED);
setup_timer_2(T2_DISABLED,0,1);
setup_comparator(NC_NC_NC_NC);
setup_vref(FALSE);

setup_adc(ADC_CLOCK_INTERNAL);   //enables the a/d module and sets the clock to internal adc clock
setup_adc_ports(ALL_ANALOG);     //sets all the adc pins to analog
set_adc_channel(0);              //the next read_adc call will read channel 0
delay_us(10);

While(1)
   {     
   x=read_adc(A0);                //starts the conversion and reads the result and store it in x
      If (x>100)
      else If (100<x<=200)   
         putc (0xFF);
      else If (200<x<=300)   
         putc (0xFF);
      else If (300<x<=400)   
         putc (0xFF);
      else If (400<x<=500)   
         putc (0xFF);
      else If (500<x<=600)   
         putc (0xFF);
      else if (600<x<=700)   
         putc (0xFF);
      else If (700<x<=800)   
         putc (0xFF);
      else If (800<x<=900)   
         putc (0xFF);
      else If (900<x<=1000)   
         putc (0xFF);
      else if (1000<x)   
         putc (0xFF);
      else
      {
         putc(0x80);   /* Transmits the midi signal for sound off */
      }
   }
}
bungee-



Joined: 27 Jun 2007
Posts: 206

View user's profile Send private message

PostPosted: Wed Mar 17, 2010 10:31 am     Reply with quote

Write what would you like to achieve with that code. On first glance, it will not work.

int x <== 8 bit value, there you can put only 255 not more.

Then if you want to send for values larger that 100 an 0xFF you can do it with one simple if sentence.

Code:

if (x>100) putc(0xff)
else putc(0x80);


And remember x must be declared as: int16 x;
Ikk06



Joined: 30 Nov 2009
Posts: 6

View user's profile Send private message

PostPosted: Wed Mar 17, 2010 10:46 am     Reply with quote

What I am basically building is a drum kit using a piezo sensor which is connected to the circuit and then onto the computer via a midi cable. What I want the code to do is when the sensor is hit it gives an analogue reading which should be converted to a 10 bit digital reading due to the ADC. The If functions are supposed to send out a midi signal to the computer via the midi usb cable based on the reading of the 10 bit digital signal. All the If functions show is what signal to send when the digital reading is in which range. All the midi signals are the same at the moment because I wanted to check if it was working. This is the code with the alterations.
Code:

#include <stdio.h>
#include <stdlib.h>

void main()
{
   int16 x;

   setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);
   setup_timer_1(T1_DISABLED);
   setup_timer_2(T2_DISABLED,0,1);
   setup_comparator(NC_NC_NC_NC);
   setup_vref(FALSE);

   setup_adc(ADC_CLOCK_INTERNAL);   //enables the a/d module and sets the clock to internal adc clock
   setup_adc_ports(ALL_ANALOG);     //sets all the adc pins to analog
   set_adc_channel(0);              //the next read_adc call will read channel 0
   delay_us(10);

   While(1)
   {     
   x=read_adc(AN0);                //starts the conversion and reads the result and store it in x
      If (x>100)
         putc (0xFF);
      else If (100<x<=200)   
         putc (0xEE);
      else If (200<x<=300)   
         putc (0xDD);
      else If (300<x<=400)   
         putc (0xCC);
      else If (400<x<=500)   
         putc (0xBB);
      else If (500<x<=600)   
         putc (0xAA);
      else if (600<x<=700)   
         putc (0x99);
      else If (700<x<=800)   
         putc (0x88);
      else If (800<x<=900)   
         putc (0x77);
      else If (900<x<=1000)   
         putc (0x66);
      else if (1000<x)   
         putc (0x55);
      else
      {
         putc(0x80);   /* Transmits the midi signal for sound off */
      }
   }
}

What do I need to change??
bungee-



Joined: 27 Jun 2007
Posts: 206

View user's profile Send private message

PostPosted: Wed Mar 17, 2010 1:39 pm     Reply with quote

With all that if else compiler get confused and in middle of all compiles your code in some strange manner.

And when I'm looking at your code, there is something wrong with the logic. If x is larger than 100 (x>100) then you put 0xff to output, and then you exit ...

try to code like that:

Code:
      if (20>x) putc(0x80); 
      If (20<x<=100)     putc (0xFF);
      If (100<x<=200)    putc (0xEE);
      If (200<x<=300)    putc (0xDD);
      If (300<x<=400)    putc (0xCC);
      If (400<x<=500)    putc (0xBB);
      If (500<x<=600)    putc (0xAA);
      if (600<x<=700)    putc (0x99);
      If (700<x<=800)    putc (0x88);
      If (800<x<=900)    putc (0x77);
      If (900<x<=1000)   putc (0x66);
      if (1000>x)        putc (0x55); 


It covers all the AD range, so you don't need the if else structure. Also you did not supply fuses etc, that we could see if something is wrong in there also.

Why do you need stdio and stdlib? habit from PC world, you can avoid them here if you don't need some function from there.
Ikk06



Joined: 30 Nov 2009
Posts: 6

View user's profile Send private message

PostPosted: Thu Mar 18, 2010 8:05 am     Reply with quote

The fuses are in a different file which is included into this one.
Code:

#include <18F4420.h>

#FUSES NOWDT                    //No Watch Dog Timer
#FUSES INTRC_IO                 //High speed Osc (> 4mhz for PCM/PCH) (>10mhz for PCD)
#FUSES NOPUT                    //No Power Up Timer
#FUSES NOPROTECT                //Code not protected from reading
#FUSES NOBROWNOUT               //No brownout reset
#FUSES NOMCLR                   //Master Clear pin used for I/O
#FUSES NOLVP                    //No low voltage prgming, B3(PIC16) or B5(PIC18) used for I/O
#FUSES NOCPD                    //No EE protection

#use delay(clock=4000000)
#use rs232(baud=3125,parity=N,xmit=PIN_C4,rcv=PIN_C5,bits=8)

The code with the alterations looks like this now:
Code:

#include "Y:\Year3\Project\Program\Project\Project2.h"
  #include <stdio.h>
  #include <stdlib.h>

void main()
{
   int16 x;

   setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);
   setup_timer_1(T1_DISABLED);
   setup_timer_2(T2_DISABLED,0,1);
   setup_comparator(NC_NC_NC_NC);
   setup_vref(FALSE);

   setup_adc(ADC_CLOCK_INTERNAL);   //enables the a/d module and sets the clock to internal adc clock
   setup_adc_ports(ALL_ANALOG);     //sets all the adc pins to analog
   set_adc_channel(0);              //the next read_adc call will read channel 0
   delay_us(10);

   While(1)
   {     
   x=read_adc(AN0);                //starts the conversion and reads the result from AN0 and stores it in x

     if (20>x) putc(0x80); 
         if (20<x<=100)     putc (0xFF);
         if (100<x<=200)    putc (0xEE);
         if (200<x<=300)    putc (0xDD);
         if (300<x<=400)    putc (0xCC);
         if (400<x<=500)    putc (0xBB);
         if (500<x<=600)    putc (0xAA);
         if (600<x<=700)    putc (0x99);
         if (700<x<=800)    putc (0x88);
         if (800<x<=900)    putc (0x77);
         if (900<x<=1000)   putc (0x66);
         if (1000>x)        putc (0x55);       
   }
}

Is this right??
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