2014年3月6日木曜日

Raspberry Pi LCD Module (Manual Mode with wiringPi)

Raspberry Pi LCD Module (Manual Mode with wiringPi)

You should initialize LCD device after connect to device.

compile
 $g++ -Wall -o main main.c MCUGearM.cpp MCUGearBaseM.cpp -lwiringPi -lwiringPiDev


Don't forget "-lwiringPiDev".

#include 

.......

 MCUGear LcdM = MCUGear(N_VDD_VSS_VSS);
    initBase(); //initialize Baseboard
    LcdM.detectModule();    //save location data in MCUGear class and you can check return data.Baseboard has 0-47 pin.
    
    startReg(0);
    setIOLcd(&LcdM);//make schematic Bank0
    endReg(0);
    
    changeBank(0);//select Bank(schematic 0 to 7)

 LcdM.connectModule();   //connect---(open module gate)

 //same as normal circuit 
 int lcd = lcdInit(2,16,4, 2,3,4,5,6,7,0,0,0,0);//must be same pin assign with setIOLcd() function.
 delay(1);
 lcdPosition(lcd, 0,0);
 lcdPuts(lcd,"test");
 

void setIOLcd(MCUGear *mcugear);

void setIOLcd(MCUGear *mcugear){
 //rs
 //e
 //0-4bit

    mcugear->setWire(IO_2, IO_REG_OUT_DIR, 0);
    mcugear->setWire(IO_3, IO_REG_OUT_DIR, 1);
    mcugear->setWire(IO_4, IO_REG_OUT_DIR, 2);
    mcugear->setWire(IO_5, IO_REG_OUT_DIR, 3);
    mcugear->setWire(IO_6, IO_REG_OUT_DIR, 4);
    mcugear->setWire(IO_7, IO_REG_OUT_DIR, 5);
}

Raspberry Pi Multifunction Module (Manual Mode with wiringPi)

Raspberry Pi Multifunction Module (Manual Mode with wiringPi)

Instruction about multifunction module.

PWM you can use normal PWM IO but if you want to HOLD after closed module gate, the signal

You should write in main function like this:

ex) RC servo
initPWM(spiChan, 24, 40950, 0, 0, 0); //frequency = 40950/4095 = 10msec
LPC1110 clock = 48MHz
clock/divider = 48MHz/24 = 2MHz
t =Frequency/2MHz = 0.02sec = 20msec
40950 : 20mse = X : Ymsec <-You can calucurate from this in the ratio.

PWMDuty(spiChan, ch, duty);
The duty is available from 1msec(0x7FF) to 2msec(0xFFF) for RC servo


void setIOSimpleSPI(MCUGear *mcugear);
void initPWM(int spichan,  unsigned int Divider, unsigned int friquency, unsigned int duty0, unsigned int duty1, unsigned int duty2);
void PWMfriq(int spichan, unsigned int friquency);
void PWMDuty(int spichan, int ch, unsigned int Duty);
void stopPWM(int spichan);

void setIOSimpleSPI(MCUGear *mcugear){
 //for AD 2ch, AD 8ch and multifunction module
 //1:MISO 13
 //2:MOSI 12
 //3:SCK  14
 //4:CE0  10
    mcugear->setWire(IO_13, IO_REG_IN_DIR, 0);
    mcugear->setWire(IO_12, IO_REG_OUT_DIR, 1);
    mcugear->setWire(IO_14, IO_REG_OUT_DIR, 2);
    mcugear->setWire(IO_10, IO_REG_OUT_DIR, 3);
 
}
void initPWM(int spichan,  unsigned int Divider, unsigned int friquency, unsigned int duty0, unsigned int duty1, unsigned int duty2){
 
 unsigned char spiBuff[2];
 
 spiBuff[0] = (Divider >> 8);
 spiBuff[1] = (0x00FF & Divider);
 wiringPiSPIDataRW(spichan,spiBuff,2);

 spiBuff[0] = (friquency >> 8);
 spiBuff[1] = (0x00FF & friquency);
 wiringPiSPIDataRW(spichan,spiBuff,2);

 spiBuff[0] = (duty0 >> 8);
 spiBuff[1] = (0x00FF & duty0);
 wiringPiSPIDataRW(spichan,spiBuff,2);

 spiBuff[0] = (duty1 >> 8);
 spiBuff[1] = (0x00FF & duty1);
 wiringPiSPIDataRW(spichan,spiBuff,2);

 spiBuff[0] = (duty2>> 8);
 spiBuff[1] = (0x00FF & duty2);
 wiringPiSPIDataRW(spichan,spiBuff,2);

}

void PWMfriq(int spichan, unsigned int friquency){
 
 unsigned char spiBuff[2];
 
 spiBuff[0] = (friquency >> 8);
 spiBuff[1] = (0x00FF & friquency);
 wiringPiSPIDataRW(spichan,spiBuff,2);
 
}

void PWMDuty(int spichan, int ch, unsigned int duty){
 
 unsigned char spiBuff[2];
 
 switch(ch){
  case 1:
   spiBuff[0] = ((0x3000 + duty)>> 8);
   spiBuff[1] = (0x00FF & duty);
   wiringPiSPIDataRW(spichan,spiBuff,2);
  
  case 2:
   spiBuff[0] = ((0x4000 + duty)>> 8);
   spiBuff[1] = (0x00FF & duty);
   wiringPiSPIDataRW(spichan,spiBuff,2);
  
  case 3:
   spiBuff[0] = ((0x5000 + duty)>> 8);
   spiBuff[1] = (0x00FF & duty);
   wiringPiSPIDataRW(spichan,spiBuff,2);
   
  default:
   break;
 }
}

void stopPWM(nt spichan){
 
 unsigned char spiBuff[2];
 
 spiBuff[0] = 0x10;
 spiBuff[1] = 0x00;
 wiringPiSPIDataRW(spichan,spiBuff,2);
}


Raspberry Pi 12bit 1ch DA Module (Manual Mode with wiringPi)

Raspberry Pi 12bit 1ch DA Module (Manual Mode with wiringPi)

AD 8ch module has MCP4921.
You can use it easily, just put these functions on your main.c file or make header file and link these.
void setIOSimpleSPI(MCUGear *mcugear);
void write12bitDA(int spichan, int ratchPin, int data);

void setIOSimpleSPI(MCUGear *mcugear){
 //for AD 2ch, AD 8ch and multifunction module
 //1:MISO 13
 //2:MOSI 12
 //3:SCK  14
 //4:CE0  10
    mcugear->setWire(IO_13, IO_REG_IN_DIR, 0);
    mcugear->setWire(IO_12, IO_REG_OUT_DIR, 1);
    mcugear->setWire(IO_14, IO_REG_OUT_DIR, 2);
    mcugear->setWire(IO_10, IO_REG_OUT_DIR, 3);
 
}

void write12bitDA(int spichan, int ratchPin, int data){  //ratchPin must be same as setIODA()
 
 unsigned char spiBuff[2];
 
 digitalWrite(ratchPin,1); //for DA ratch
  
 spiBuff[0] = (0x30 + ((data>>8) & 0x0f));
 spiBuff[1] = (data& 0xff);
 digitalWrite(ratchPin,0); //for DA ratch
 digitalWrite(ratchPin,1); //for DA ratch
 
 //printf("data = %d : %x %x",data, spiBuff[0], spiBuff[1]);
 
 wiringPiSPIDataRW(spichan,spiBuff,2);
  
}

Raspberry Pi 12bit 8ch AD Module (Manual Mode with wiringPi)

Raspberry Pi 12bit 8ch AD Module (Manual Mode with wiringPi)

AD 8ch module has MCP3208.
You can use it easily, just put these functions on your main.c file or make header file and link these.
void setIOSimpleSPI(MCUGear *mcugear);
int read12bit8chAD(int spichan,char ch);

void setIOSimpleSPI(MCUGear *mcugear){
 //for AD 2ch, AD 8ch and multifunction module
 //1:MISO 13
 //2:MOSI 12
 //3:SCK  14
 //4:CE0  10
    mcugear->setWire(IO_13, IO_REG_IN_DIR, 0);
    mcugear->setWire(IO_12, IO_REG_OUT_DIR, 1);
    mcugear->setWire(IO_14, IO_REG_OUT_DIR, 2);
    mcugear->setWire(IO_10, IO_REG_OUT_DIR, 3);
 
}

int read12bit8chAD(int spichan,char ch){
 
 int value = 0;
 unsigned char spiBuff[3];
 
 if(ch < 8){
  spiBuff[0] = 0x18 + ch;
  spiBuff[0] = (spiBuff[0] << 2);
  spiBuff[1] = 0;
  spiBuff[2] = 0;
   
 }else{
  return -1;
 }
 
 wiringPiSPIDataRW(spichan,spiBuff,3);
 value = spiBuff[1] << 8;
 value = spiBuff[2] + value;
 value = value >> 4;
 
 return value; 
}

Raspberry Pi 12bit 2ch AD Module (Manual Mode with wiringPi)

Raspberry Pi 12bit 2c AD Module (Manual Mode with wiringPi)

AD 2ch module has MCP3202.
You can use it easily, just put these functions on your main.c file or make header file and link these.
void setIOSimpleSPI(MCUGear *mcugear);
int read12bit2chAD(int spichan,char ch);

void setIOSimpleSPI(MCUGear *mcugear){
 //for AD 2ch, AD 8ch and multifunction module
 //1:MISO 13
 //2:MOSI 12
 //3:SCK  14
 //4:CE0  10
    mcugear->setWire(IO_13, IO_REG_IN_DIR, 0);
    mcugear->setWire(IO_12, IO_REG_OUT_DIR, 1);
    mcugear->setWire(IO_14, IO_REG_OUT_DIR, 2);
    mcugear->setWire(IO_10, IO_REG_OUT_DIR, 3);
 
}
int read12bit2chAD(int spichan,char ch){
 
 int value = 0;
 unsigned char spiBuff[3];
 
 if(ch == 0)
  spiBuff[0] = 0x18;
 else if(ch == 1)
  spiBuff[0] = 0x1C;
 else
  return -1;
 
 spiBuff[1] = 0x00;
 spiBuff[2] = 0x00;
 
 wiringPiSPIDataRW(spichan,spiBuff,3);
 //printf("%x %x %x\n",spiBuff[0],spiBuff[1],spiBuff[2]);
 
 value = (spiBuff[1] <<4 data-blogger-escaped-spibuff="">>4);
 //printf("%d\n", value);
 return value;
 
}

2014年3月5日水曜日

Raspberry Pi Universal Module (Manual Mode with wiringPi)

Raspberry Pi Universal Module (Manual Mode with wiringPi)


//compile
// $g++ -Wall -o main main.c MCUGearM.cpp MCUGearBaseM.cpp -lwiringPi
// $sudo ./main

#include "commonM.h"
#include "MCUGearM.h"
#include "MCUGearBaseM.h"
#include 
#include 
#include 
#include 

void setIOUni(MCUGear *mcugear);

int main(void){
 
 //define IOs
 int inpin[2] = {0,1}; //output pins
 int outpin[6] = {2,3,4,5,6,7}; //output pins
 
 
 if(wiringPiSetup() == -1)
  return 1;

 int spiSpeed = 1000000; //1MHz
 int spiChan = 0; //CE0
 
 if(wiringPiSPISetup(spiChan, spiSpeed)== -1) //set CE0 and 1MHz
  return 1;
  

 //set IO direction
 pinMode(inpin[0], INPUT);
 pinMode(inpin[1], INPUT);
 
 pinMode(outpin[0], OUTPUT);
 pinMode(outpin[1], OUTPUT);
 pinMode(outpin[2], OUTPUT);
 pinMode(outpin[3], OUTPUT);
 pinMode(outpin[4], OUTPUT);
 pinMode(outpin[5], OUTPUT);
 
 
 initBase(); //initialize Baseboard   
   
 //set Address and number of pins that you want to use on this module
 MCUGear UniM = MCUGear(N_VDD_VDD_VDD); 
 MCUGear AD2M = MCUGear(N_SCL_SCL_VSS); 
 
    int sw1 = 0;
    int sw2 = 0;

    initBase(); //initialize Baseboard
    
    //set schematic select 0-7
    int location = UniM.detectModule();    //save location data in MCUGear class and you can check return data.Baseboard has 0-47 pin.
    printf("UniM location = %d\n", location);
    

   //Save wiring data on Bank0 --------------------------
    startReg(0);
    setIOUni(&UniM);//make schematic Bank0
    endReg(0);

//---------------------------------------------------------------------
//If you want to make other Bank(schematic 0 to 7) write like that.
//    startReg(1);
//    setIOBank1(&UniM);//make schematic Bank1
//    endReg(1);
//
//delete function is 
//void deleteBank(unsigned char bank);
//---------------------------------------------------------------------

    changeBank(0);//select Bank(schematic 0 to 7)
    
    UniM.connectModule();   //connect---(open module gate)
  
  
 while(1){
  
  //UniM.connectModule();   //connect---(open module gate)
  
  sw1 = digitalRead(inpin[0]);
  sw2 = digitalRead(inpin[1]);
  printf("sw1 = %d  sw2 = %d\r\n",sw1,sw2);
  
  digitalWrite(outpin[0],0);
  digitalWrite(outpin[1],1);
  digitalWrite(outpin[2],1);
  delay(300); //set your favorite time
  
  digitalWrite(outpin[0],1);
  digitalWrite(outpin[1],0);
  digitalWrite(outpin[2],1);
  delay(300); //set your favorite time
  
  digitalWrite(outpin[0],1);
  digitalWrite(outpin[1],1);
  digitalWrite(outpin[2],0);
  delay(300); //set your favorite time
  
  //UniM.disconnectModule();   //disconnect---(close module gate)
  //changeBank(nowBank);  //you can change the Bank after close all module gate. It is safe.
        
 }
 
 return 0;
}


void setIOUni(MCUGear *mcugear){
    mcugear->setWire(IO_0, IO_REG_IN_DIR, 0);
    mcugear->setWire(IO_1, IO_REG_IN_DIR, 1);
    mcugear->setWire(IO_2, IO_REG_OUT_DIR, 2);
    mcugear->setWire(IO_3, IO_REG_OUT_DIR, 3);
    mcugear->setWire(IO_4, IO_REG_OUT_DIR, 4);
}

2014年2月21日金曜日

MCU Gear Movies


Controlling Simple arm robot
I used Raspberry Pi,  iPad mini and PHP.


MCU Gear test with Raspberry Pi




Zigbee controled LPC 1114FN28 Tank with MCU Gear.
I try to use Lua script (loop) from my windows note PC.



This is a funny movie, to hack PS VITA.
My friend asked me "FF Thunder Plains (雷平原) are quite difficult to avoid thunder 200 times!".So, she try to make "Automatic Thunder Refuger" with Raspberry Pi , Python and MCU Gear(Board level reconfigurable circuit).
I just teach how to use photo sensor and small RC servo motor with these.

2014年2月19日水曜日

Web GPIO controller with PHP

INDEX
Set up Raspberry Pi Noobs
Install wiringPi
GPIO setting for Python.
(Python is the simplest sample code,.You don't need to check the number of GPIO. )
Web controller with PHP for MCU Gear
                                      

You can make GUI application easily.



                                      

Step 1: Install Apache and PHP

$sudo apt-get update
$sudo apt-get upgradesudo apt-get install apache2 php5
$sudo chown pi -R /var/www
$sudo updata-rc.d apache2 defaults

You can use HTML on /var/www/ folder.
                                      
Step 2 : Edit sudoers file

$sudo nano /etc/sudoers

Write "www-data ALL=(root) NOPASSWD: ALL" to the end of file.



Then you can use sudo command on PHP.

                                      

Step 3 : Download Sample program

$wget http://mcugear.org/RPitest/MUGear.tar
$tar xvf MCUGear.tar
$sudo mv -f MCUGear /usr/lib/cgi-bin

Then reboot RPi.
$sudo reboot

                                      

Step 4 : Test from you PC or Tablet.
Open browser and connect to 
http://raspberrypi.local/cgi-bin/MCUGear/index.php
or
http://[IP Address]/cgi-bin/MCUGear/index.php

You can see this page.



                                      

Step 5 :  How use it?

Push "Init Baseboard" botton that initialize baseboard and enable.

Read Input Pins : Read Input signal from Universal Module.
Write Output Pins : Write DA data to DA Module.
Read AD 2ch : Read AD data from 12bit AD 2ch module.
Read AD 8ch : Read AD data from 12bit AD 8ch module.
Write DA : Write DA data to DA Module.
Move RC Servo : Multifunction module 3ch PWM mode.

"Shutdown" is sudo halt command.
You do not need remote login for shutdown RPi.

PHP use System command format is
sudo python ***.py [module address] [data0] [data1] [data2] ...



GPIO setting for Python.

INDEX
Set up Raspberry Pi Noobs
Install wiringPi
GPIO setting for Python.
(Python is the simplest sample code,.You don't need to check the number of GPIO. )
Web GPIO controller with PHP
                                      

If you want to control GPIO with Python, you need this setting.




                                       Step 1 : Delete black list. And add "i2c-dev".

Open terminal, and type

$sudo nano /etc/modprobe.d/raspi-blacklist.conf

Type "#" to comment out bladk list. Put [ctrl]+[x] keys to save file.



$sudo nano /etc/modules
Write 
i2c-dev
at end of file. And  put [ctrl]+[x] keys to save file.



Step 2 : Install python-smbus

$sudo apt-get update

$sudo apt-get upgrade
$sudo apt-get install python-smbus
$sudo adduser pi i2c
$sudo reboot


Step 3 : I2C Test
Turn on MCU Gear. And type
$i2cdetect -y 0
or
$i2cdetect -y 1

It's depend on your Raspberry Pi version.



Step 4 : Setup SPI bus.
$sudo apt-get install python-dev
$sudo reboot

After reboot, check installed SPI.
$lsmod
Then you can see spi-bcm2708.





$mkdir python-spi 
$cd python-spi 
$wget https://raw.github.com/doceme/py-spidev/master/setup.py 
$wget https://raw.github.com/doceme/py-spidev/master/spidev_module.c 
$sudo python setup.py install 
$cd /home/pi/Desktop/
$wget http://mcugear.org/RPitest/MGPyVer1_1.tar
$tar xvf MGPyVer1_1.tar

You can see many sample program for each module.

$sudo python sample.py
is run command.

ex)LED blink test
This sample program apply to universal module.


N_VDD_VDD_VDD is address setting the reverse side of a module.
It means AD2 = VDD(+3V3) AD1 = VDD(+3V3)  AD0 = VDD(+3V3)
You can see detail on PCA9674 and PCA9674A datasheet.






You can simply check the sample program with LED and resister.



You can automatic wiring these pins on module.

p.NC : No connect c
p.outpin[0-5] : output pin
p.inpin[0-1] : input pin


Others are
SPI : MISO, MOSI, SCK, CE0 :
UART : TX, RX

You can check pin assigne on p.py.


! Option function is little bit difficult. You need to change sample program. (bank parameters)

Install wiringPi

INDEX
Set up Raspberry Pi Noobs
Install wiringPi
GPIO setting for Python.
(Python is the simplest sample code,.You don't need to check the number of GPIO. )
Web controller with PHP for MCU Gear
                                      

WiringPi is a GPIO access library written in C.



                                      

Step 1: Install
Type these command on your Pi's terminal.

$sudo apt-get install git-core

$sudo apt-get update
$sudo apt-get upgrade
$git clone git://git.drogon.net/wiringPi
$cd wiringPi
$git pull origin
$./build

test operation after build

$gpio -v
$gpio readall

You can see all IO setting like a picture.



Then reboot.
$sudo reboot

                                      
Step 2 : Download sample files, and compile.

$cd /home/pi/Desktop/
$wget http://www.mcugear.org/RPitest/MCUGearRPwp.tar
$tar xvf MCUGearRPwp.tar

Connect MCU Gear to Raspberry Pi and turn on.

You always need type these command for using I2C and SPI. 
$gpio load i2c 1000
$gpio load spi

Move directry.
$cd MCUGearRPwp

Compile it.

$g++ -Wall -o main main.c MCUGear.cpp MCUGearBase.cpp -lwiringPi
(MCU Gear sample program is C++ but main.c is C language.)

                                      

Step 3 : My favorite C & C++ editor is geany, It is very easy to use.
$sudo apt-get install geany

! Option function is little bit difficult. You need to change sample program. (bank parameters)

Set up Raspberry pi with NOOBS

INDEX
Set up Raspberry Pi Noobs
Install wiringPi
GPIO setting for Python.(Python is the simplest sample code,.You don't need to check the number of GPIO. 
)
Web controller with PHP for MCU Gear
                                      

Step 1 : Setup Raspbian on your SD card

Download NOOBS (offline and network install) from HERE.
Decompressed Zip file and move all files to SD card.

                                      

Step 2 :

Connect LAN cable, USB keyboard, USB mouse, HDMI cable, and SD card. Power on Raspberry Pi.
Select OS [Raspbian] and language.






                                      

Step 3 :

You can see dialog "succss" when finish the setting up the OS.



Push OK.

You can change password.
If you do not need to change, you ignore the setting.
default ID and Password is
ID : pi
Password : raspberry



                                      

Step 4 : Setup remote desktop appricatin




Type in these command for install remote desktop apprication. (If you do not need remote desktop, just type "$startx".)

$sudo apt-get update
$sudo apt-get upgrade
$sudo apt-get install xrdp
$ifconfig

Now, you can use remote desktop from windows with IP address (You can see it after ifconfig.).
Also, you can install avahi-deamon that allow you can access with NAME "raspberrypi.local" instead of the IP address.

$sudo apt-get install avahi-daemon

then reboot.

$sudo reboot

                                      

Step 5 : Try to accsess Raspberry Pi through the 


You can use remote desktop.
Input "rasberrypi.local"





username:pi
password:raspberry



You do not need check IP address.




                                      

If you can not remote login. Maybe, there is similar server (you can accsess with NAME like raspberrypi.local.)
Some router has such a NAME.

Try to connect other router and type

$ sudo rm /var/run/avahi-daemon/disabled-for-unicast-local
$ sudo /etc/init.d/avahi-daemon restart

or try access with IP addresss.