JAVA PIC

Ficou para marco/abril de 2012
Estamos fazendo mais exemplos para o livro...
Colocamos seis threads extras
Estamos fazendo mais exemplos para o livro...
Colocamos seis threads extras

- Código: Selecionar todos
import com.muvium.UVMRunnable;
import com.muvium.driver.display.lcd.LCDPrinter;
import com.muvium.io.PortIO;
import com.muvium.io.PinIO;
import com.muvium.io.EEPROMStore;
import com.muvium.apt.PWM;
import com.muvium.apt.PeripheralUnavailableException;
import java.lang.Object;
import com.muvium.apt.TimerListener;
import com.muvium.apt.TimerEvent;
import com.muvium.apt.PeriodicTimer;
import com.muvium.apt.PeripheralFactory;
import com.muvium.apt.Time;
import com.muvium.apt.I2CMaster;
import com.muvium.apt.MultiServoController;
import com.muvium.apt.ADCReader;
import com.muvium.apt.PulseEvent;
import com.muvium.apt.PulseListener;
import com.muvium.apt.PulseMonitor;
import com.muvium.apt.PinListener;
import com.muvium.apt.PinEvent;
import com.muvium.apt.PinMonitor;
import com.muvium.apt.PortMonitor;
import com.muvium.apt.PortListener;
import com.muvium.apt.PortEvent;
public class p64 extends UVMRunnable implements TimerListener, PinListener, PortListener {
private ShowValueCount valorConta;
private LCD2Lines lcd;
private PWM pwm;
private PeriodicTimer timer;
private int count;
private Time tempo;
private ClasseThread thread;
private ClasseThread2 thread2;
private ClasseThread3 thread3;
private ClasseThread4 thread4;
private ClasseThread5 thread5;
private ClasseThread6 thread6;
private I2CMaster i2c;
private MultiServoController controllerDirect;
private ADCReader adc;
boolean restart;
private PinMonitor monitoraPino0;
private PortMonitor monitoraPortaB1;
//private PinMonitor monitoraPino1;
//private PinMonitor monitoraPino2;
PulseMonitor pulses;
//I2C
private static final int M24LC256_ADDRESS_WRITE = 0xA2;
private static final int M24LC256_ADDRESS_READ = 0xA3;
private static final int M24LC256_ADDRESS = 0x0006;
private static final byte M24LC256_DATA = 0x35;
//RTC DS1307
private static final int DS1307_ADDRESS_WRITE = 0xD0;
private static final int DS1307_ADDRESS_READ = 0xD1;
class PulseClass implements PulseListener {
long pulse = 0;
int i = 0;
public PulseClass() {
System.out.print("\rStarting Pulse");
}
public synchronized void pulseArrived(PulseEvent e) {
long p = e.getDurationMicroSeconds();
System.out.print("\rPulse ( ");
System.out.print(i++);
System.out.print(" ) = ");
System.out.print(p);
pulses.stop();
restart = true;
}
}
public synchronized void Timer(TimerEvent e){
Object src = e.getSource();
count++;
System.out.print("\r\nTIMER" + String.valueOf(count));
}
public synchronized void pinChanged(PinEvent event){
if( event.getSource() == monitoraPino0 )
System.out.print("\r\nTECLA RB0******************************************");
/* if( event.getSource() == monitoraPino1 )
System.out.print("\r\nTECLA RB1******************************************");
if( event.getSource() == monitoraPino2 )
System.out.print("\r\nTECLA RB2******************************************");
try{Thread.sleep(200);}catch(Exception i){}
*/
}
public synchronized void portChanged(PortEvent e){
if (e.getSource() == monitoraPortaB1)
System.out.print("\r\n TECLA B1");
System.out.print("\r\n TECLA B --------");
try{Thread.sleep(200);}catch(Exception i){}
}
public void run() {
PortIO.setTris(0, PortIO.PORTC);
PortIO.setTris(7, PortIO.PORTB);
PortIO.setProperty( PortIO.PORTB, PortIO.PROPERTY_PORT_PULLUP_ON );
monitoraPino0 = getPeripheralFactory().createPinMonitor(this, PinIO.PINB0, PinMonitor.IMPLEMENTATION_INTERRUPT,
PeripheralFactory.EVENT_PRIORITY_BACKGROUND);
monitoraPortaB1 = getPeripheralFactory().createPortMonitor(this,
PortMonitor.IMPLEMENTATION_POLLED + PortMonitor.MONITOR_PORTB,
PeripheralFactory.EVENT_PRIORITY_BACKGROUND);
/* monitoraPino1 = getPeripheralFactory().createPinMonitor(this, PinIO.PINB1, PinMonitor.IMPLEMENTATION_INTERRUPT,
PeripheralFactory.EVENT_PRIORITY_BACKGROUND);
monitoraPino2 = getPeripheralFactory().createPinMonitor(this, PinIO.PINB2, PinMonitor.IMPLEMENTATION_INTERRUPT,
PeripheralFactory.EVENT_PRIORITY_BACKGROUND);
*/
lcd = new LCD2Lines();
System.out.println("starting");
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Starting");
try{
Thread.sleep(1000);
}catch(Exception e){}
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("PIN MONITOR");
try{
monitoraPino0.start(PinMonitor.MONITOR_LOW_HIGH);
}catch(PeripheralUnavailableException e){
System.out.print("Tecla nao disponivel...");
}
try{
monitoraPortaB1.start();
}catch(Exception e){}
/* try{
monitoraPino1.start(PinMonitor.MONITOR_LOW_HIGH);
}catch(PeripheralUnavailableException e){
System.out.print("Tecla nao disponivel...");
}
try{
monitoraPino2.start(PinMonitor.MONITOR_LOW_HIGH);
}catch(PeripheralUnavailableException e){
System.out.print("Tecla nao disponivel...");
}
*/
try{
Thread.sleep(1000);
}catch(Exception e){}
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("PWM");
try{
Thread.sleep(1000);
}catch(Exception e){}
pwm();
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("EEPROM");
try{
Thread.sleep(1000);
}catch(Exception e){}
eeprom();
try{
Thread.sleep(1000);
}catch(Exception e){}
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("TIMER EVENT");
try{
Thread.sleep(1000);
}catch(Exception e){}
timer(); //Event
try{
Thread.sleep(1000);
}catch(Exception e){}
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Thread");
try{
Thread.sleep(1000);
}catch(Exception e){}
thread();
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("I2C");
try{
Thread.sleep(1000);
}catch(Exception e){}
i2c();
mostrarHora_Terminal_LCD();
try{
Thread.sleep(1000);
}catch(Exception e){}
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Servo Init");
try{
Thread.sleep(1000);
}catch(Exception e){}
servo();
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("ADC CONFIG");
try{
Thread.sleep(1000);
}catch(Exception e){}
adc();
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Init Captura");
try{
Thread.sleep(1000);
}catch(Exception e){}
inicializacaoCaptura();
int maxPos = controllerDirect.getMaxPosition();
while (true)
{
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("PWM...");
for (int duty=0; duty <= 10; duty++) {
pwm.setDuty(duty);
try{
Thread.sleep(100);
}catch(Exception e){}
}
mostrarHora_Terminal_LCD();
try{
Thread.sleep(1000);
} catch(Exception e) {};
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("ADC...");
leAdc();
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Time...");
time();
valorConta();
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Captura");
captura();
try{
Thread.sleep(1000);
} catch(Exception e) {};
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Servo...");
controlaServo();
}
}
private void inicializacaoCaptura(){
System.out.println("starting");
PulseClass p = new PulseClass ();
pulses = getPeripheralFactory().createPulseMonitor(p,
PulseMonitor.IMPLEMENTATION_CCP1 |
PulseMonitor.MONITOR_HIGH_PULSE,
(long) 5000,
PeripheralFactory.EVENT_PRIORITY_URGENT);
restart = true;
}
private void captura(){
if( restart ) {
try{
pulses.start();
restart = false;
}catch(Exception e){}
}
doEvents();
}
private void leAdc(){
System.out.print("Reading value of the AD0 " + String.valueOf(adc.getSample())
+ "\n\r");
try{
Thread.sleep(1000);
}catch(Exception e){};
}
private void adc(){
System.out.print("Starting ADC\r\n");
try{
Thread.sleep(200);
} catch(Exception e) {};
adc = getPeripheralFactory().createADCReader(0, ADCReader.READ_INT);
adc.setChannel(0);
// 1110 D D D D D D D A VDD VSS 1/0
// RA2,RA3,RA5 DIGITAL...
// RA0 = ANALOG
// VREF+ = 5V, VREF- = 0V
// Config all channel as digital, execpt AN0,AN1 E AN2 and
// make REF Internal
// Use XML
}
private void controlaServo(){
controllerDirect.setPosition(0, 0);
try{
Thread.sleep(500);
}catch(Exception e){};
controllerDirect.setPosition(78, 0);
try{
Thread.sleep(500);
}catch(Exception e){};
controllerDirect.setPosition(156, 0);
try{
Thread.sleep(500);
}catch(Exception e){};
controllerDirect.setPosition(234, 0);
try{
Thread.sleep(500);
}catch(Exception e){};
controllerDirect.setPosition(156, 0);
try{
Thread.sleep(500);
}catch(Exception e){};
controllerDirect.setPosition(78, 0);
try{
Thread.sleep(500);
}catch(Exception e){};
}
private void servo(){
controllerDirect = getPeripheralFactory().createMultiServoController(
MultiServoController.IMPLEMENTATION_DIRECT);
try{
controllerDirect.start();
}catch(PeripheralUnavailableException e){
System.out.print("ServoController unavailable");
}
}
private void mostrarHora_Terminal_LCD(){
i2c.generateStartBit();
i2c.sendByte((byte) DS1307_ADDRESS_WRITE);
i2c.sendByte((byte) 0x00);
i2c.generateStopBit();
i2c.generateStartBit();
i2c.sendByte((byte) DS1307_ADDRESS_READ);
byte segundos = i2c.receiveByte(false);
i2c.generateStopBit();
i2c.generateStartBit();
i2c.sendByte((byte) DS1307_ADDRESS_WRITE);
i2c.sendByte((byte) 0x01);
i2c.generateStopBit();
i2c.generateStartBit();
i2c.sendByte((byte) DS1307_ADDRESS_READ);
byte minutos = i2c.receiveByte(false);
i2c.generateStopBit();
i2c.generateStartBit();
i2c.sendByte((byte) DS1307_ADDRESS_WRITE);
i2c.sendByte((byte) 0x02);
i2c.generateStopBit();
i2c.generateStartBit();
i2c.sendByte((byte) DS1307_ADDRESS_READ);
byte horas = i2c.receiveByte(false);
i2c.generateStopBit();
System.out.print("\r\nHORA = " + bcdToBin(horas));
System.out.print(":" + bcdToBin(minutos));
System.out.print(":" + bcdToBin(segundos));
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Horas:");
lcd.setCursor(1, 0);
lcd.print(bcdToBin(horas));
lcd.print(":");
lcd.print(bcdToBin(minutos));
lcd.print(":");
lcd.print(bcdToBin(segundos));
}
private String bcdToBin(byte valor){
byte temp = ((byte) (valor & 0xF0));
temp >>= 4;
temp *= 10;
temp += ((byte) (valor & 0x0F));
if (String.valueOf(temp).length() == 1) {
return "0" + String.valueOf(temp);
} else {
return String.valueOf(temp);
}
}
private void i2c(){
i2c = getPeripheralFactory().createI2CMaster(I2CMaster.CLOCK_100KHZ);
//I2C
try{
i2c.start();
i2c.generateStartBit();
i2c.sendByte((byte) M24LC256_ADDRESS_WRITE);
byte lsb = M24LC256_ADDRESS / 256;
i2c.sendByte(lsb);
byte msb = M24LC256_ADDRESS % 256;
i2c.sendByte(msb);
i2c.sendByte(M24LC256_DATA);
i2c.generateStopBit();
Thread.sleep(10);
byte lsbr = M24LC256_ADDRESS / 256;
byte msbr = M24LC256_ADDRESS % 256;
i2c.generateStartBit();
i2c.sendByte((byte) M24LC256_ADDRESS_WRITE);
i2c.sendByte(lsbr);
i2c.sendByte(msbr);
i2c.generateStopBit();
i2c.generateStartBit();
i2c.sendByte((byte) M24LC256_ADDRESS_READ);
byte temp = i2c.receiveByte(true);
System.out.print("\r\n" + String.valueOf(temp));
i2c.generateStopBit();
System.out.print("\r\n***********************************");
i2c.generateStartBit();
i2c.sendByte((byte) DS1307_ADDRESS_WRITE);
i2c.sendByte((byte) 0x00); //COMEÇA APONTANDO PARA SEGUNDOS/START
i2c.sendByte((byte) 0x50); //ESCREVE 00 SEGUNDOS....
i2c.sendByte((byte) 0x11); //MINUTOS
i2c.sendByte((byte) 0x11); //HORAS
i2c.sendByte((byte) 0x01); //DIA DA SEMANA
i2c.sendByte((byte) 0x11); //DIA
i2c.sendByte((byte) 0x11); //MES
i2c.sendByte((byte) 0x11); //ANO
i2c.generateStopBit();
//i2c.stop();
System.out.print("\r\n***********************************");
} catch(Exception e){}
}
private void thread(){
thread = new ClasseThread();
thread2 = new ClasseThread2();
thread3 = new ClasseThread3();
thread4 = new ClasseThread4();
thread5 = new ClasseThread5();
thread6 = new ClasseThread6();
try{
((Thread) thread).start();
((Thread) thread2).start();
((Thread) thread3).start();
((Thread) thread4).start();
((Thread) thread5).start();
((Thread) thread6).start();
} catch (Exception ie){
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("ERRO THREAD");
}
try{
Thread.sleep(1000);
}catch(Exception e){
}
}
private void valorConta(){
if (valorConta == null) {
valorConta = new ShowValueCount(4, 6);
} else {
valorConta.setN1(valorConta.getN1() + 2);
valorConta.setN2(valorConta.getN1() + 20);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(String.valueOf(valorConta.getN1()) + " - " + String.valueOf(valorConta.getN2()));
try{ Thread.sleep(1000); } catch(Exception e) {}
}
System.out.print("\r\n" + String.valueOf(pwm.getDuty()));
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("SOMA = ");
lcd.setCursor(1, 0);
lcd.print(String.valueOf(valorConta.getSum()));
// try{ Thread.sleep(1000); } catch(Exception e) {}
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Subtract = ");
lcd.setCursor(1, 0);
lcd.print(String.valueOf(valorConta.getSubtract()));
// try{ Thread.sleep(1000); } catch(Exception e) {}
}
private void time(){
tempo = getPeripheralFactory().createTime();
System.out.print("\r\n Tempo (ms)= " + String.valueOf(tempo.currentTimeMillis()));
// try{
// Thread.sleep(200);
// }catch(Exception e){}
}
private void timer(){
timer = getPeripheralFactory().createPeriodicTimer(
this, 1000, PeripheralFactory.EVENT_PRIORITY_BACKGROUND);
try{
timer.start();
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Inicio NO Timer");
} catch (PeripheralUnavailableException pue){
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("ERRO NO Timer");
}
// try{
// Thread.sleep(500);
// }catch(Exception e){}
}
private void pwm(){
pwm = getPeripheralFactory().createPWM(PWM.PWM_CHANNEL2, 4000);
try{
pwm.start();
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("PWM INICIO");
} catch (PeripheralUnavailableException pue) {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("ERRO NO PWM");
}
// try{
// Thread.sleep(500);
//}catch(Exception e){}
}
private void eeprom(){
//EEPROM
String message = "WRITE TO EEPROM\r\n";
int err = 0;
EEPROMStore store = new EEPROMStore();
store.unlock();
store.pokeByte(0, (byte) message.length());
for( int i = 0; i < message.length(); i++ ) {
store.pokeByte(i + 1, (byte) message.charAt(i));
}
store.lock();
try {
Thread.sleep(500);
} catch(Exception e) {};
for (int i = 0; i < message.length(); i++) {
if (!(store.peekByte(i + 1) == message.charAt(i))){
err = 1;
}
}
lcd.clear();
if (err == 1){
lcd.setCursor(0, 0);
lcd.print("ERRO!!!");
} else {
lcd.setCursor(0, 0);
lcd.print("GRAVOU!!!");
}
// try{ Thread.sleep(1000); } catch(Exception e) {}
}
}
class ClasseThread extends Thread {
public void run(){
while(true){
PortIO.togglePin(0, PortIO.PORTC);
System.out.print("\r\nTHREAD 1");
try{
Thread.sleep(1500);
} catch (Exception e) {}
}
}
}
class ClasseThread2 extends Thread {
public void run(){
while(true){
PortIO.togglePin(7, PortIO.PORTB);
System.out.print("\r\nTHREAD 2");
try{
Thread.sleep(2500);
} catch (Exception e) {}
}
}
}
class ClasseThread3 extends Thread {
public void run(){
while(true){
System.out.print("\r\nTHREAD 3");
try{
Thread.sleep(3500);
} catch (Exception e) {}
}
}
}
class ClasseThread4 extends Thread {
public void run(){
while(true){
System.out.print("\r\nTHREAD 4");
try{
Thread.sleep(5000);
} catch (Exception e) {}
}
}
}
class ClasseThread5 extends Thread {
public void run(){
while(true){
System.out.print("\r\nTHREAD 5");
try{
Thread.sleep(6000);
} catch (Exception e) {}
}
}
}
class ClasseThread6 extends Thread {
public void run(){
while(true){
System.out.print("\r\nTHREAD 6");
try{
Thread.sleep(7000);
} catch (Exception e) {}
}
}
}
class LCD2Lines{
LCDPrinter lcd;
public LCD2Lines() {
lcd = new LCDPrinter();
}
public void printDisplay(String line1, String line2) {
clear();
setCursor(0, 0);
print(line1);
setCursor(1, 0);
print(line2);
}
public void clear() {
lcd.writeCommand(LCDPrinter.CLEAR);
}
public void setCursor(int line, int pos){
lcd.writeCommand((byte) ( 128 + line * 0x40 + pos));
}
public void print(String printMe){
int l = printMe.length();
for( int i = 0; i < l; i++ ){
lcd.write((byte) printMe.charAt(i));
}
}
}
abstract class Counts{
private int n1;
private int n2;
public Counts(int n1, int n2){
this.n1 = n1;
this.n2 = n2;
}
public void setN1(int n1){
this.n1 = n1;
}
public void setN2(int n2){
this.n2 = n2;
}
public int getN1(){
return this.n1;
}
public int getN2(){
return this.n2;
}
public int getSum(){
return (n1 + n2);
}
public int getSubtract(){
return (n1 - n2);
}
}
class ShowValueCount extends Counts{
public ShowValueCount(int number1, int number2) {
super(number1, number2);
}
}