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.