ADC_MCP3008
Notes for MCP3008 usage
CS* pin is active low. Must by high initially for conversion to start.
INPUT Data: Set while clock is low. Read by ADC when clock goes high.
OUTPUT Data: Read after clock goes high.
Input data stream: <start=1> <single=1> <d2> <d1> <d0>
The start bit will be the first time Din==1 on a high clock, this is followed by 4 data bits.
The First of the 4 data bits should be 1 to designate single ended A/D conversion mode.
The next 3 bits (MSB first) designate the channel to be selected.
Once the above sequence is clocked in, another clock is issued for the sample/hold period.
Then another clock will start the null bit.
Finally, the next 10 clocks will clock out the data (MSB first) from the A/D chip.
If clocking continues, the data will clock out a second time LSB first, followed by all zero's as long as the clock runs.
Getting the command line in a Perl script:
use strict;
use FindBin;
use Cwd 'abs_path';
my $cmdfile = abs_path(__FILE__);
my $scriptname;
$scriptname=$cmdfile;
$scriptname=~s/.*\///; # chomp off path
my $cpath=$cmdfile;
$cpath=~s/\/[^\/]+$//; # get just the path to the command file
Perl timer functions with higher resolution than 1 second:
#!/usr/bin/perl
use strict;
use warnings;
use Time::HiRes qw(usleep nanosleep);
# 1 millisecond == 1000 microseconds
usleep(1000);
# 1 microsecond == 1000 nanoseconds
nanosleep(1000000);
Socket Communications over TCP:
http://xmodulo.com/how-to-write-simple-tcp-server-and-client-in-perl.html
In this example, client connects to a server with well-known IP address and port. Once client is connected to the server, it sends data to the server, and waits for a reply. Upon receiving data from client, server responds with an acknowledgement message.
The purpose of shutdown(SOCKET, 1) in the example is to tell the other end that you have finished sending data, without closing SOCKET. Note that you can still receive data afterward.
TCP server example in Perl
use IO::Socket::INET;
# auto-flush on socket
$| = 1;
# creating a listening socket
my $socket = new IO::Socket::INET (
LocalHost => '0.0.0.0',
LocalPort => '7777',
Proto => 'tcp',
Listen => 5,
Reuse => 1
);
die "cannot create socket $!\n" unless $socket;
print "server waiting for client connection on port 7777\n";
while(1)
{
# waiting for a new client connection
my $client_socket = $socket->accept();
# get information about a newly connected client
my $client_address = $client_socket->peerhost();
my $client_port = $client_socket->peerport();
print "connection from $client_address:$client_port\n";
# read up to 1024 characters from the connected client
my $data = "";
$client_socket->recv($data, 1024);
print "received data: $data\n";
# write response data to the connected client
$data = "ok";
$client_socket->send($data);
# notify client that response has been sent
shutdown($client_socket, 1);
}
$socket->close();
TCP client example in Perl
use IO::Socket::INET;
# auto-flush on socket
$| = 1;
# create a connecting socket
my $socket = new IO::Socket::INET (
PeerHost => '192.168.1.10',
PeerPort => '7777',
Proto => 'tcp',
);
die "cannot connect to the server $!\n" unless $socket;
print "connected to the server\n";
# data to send to a server
my $req = 'hello world';
my $size = $socket->send($req);
print "sent data of length $size\n";
# notify server that request has been sent
shutdown($socket, 1);
# receive a response of up to 1024 characters from server
my $response = "";
$socket->recv($response, 1024);
print "received response: $response\n";
$socket->close();