FM-test

Dependencies:   MODSERIAL mbed-rtos mbed

Fork of Master by Ohnishi_Gundan

Files at this revision

API Documentation at this revision

Comitter:
9uS7
Date:
Thu Sep 11 10:15:34 2014 +0000
Child:
1:e1cfb5850088
Commit message:
main_program

Changed in this revision

bluetooth.cpp Show annotated file Show diff for this revision Revisions of this file
bluetooth.h Show annotated file Show diff for this revision Revisions of this file
control.cpp Show annotated file Show diff for this revision Revisions of this file
control.h Show annotated file Show diff for this revision Revisions of this file
fm.cpp Show annotated file Show diff for this revision Revisions of this file
fm.h Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
mbed.bld Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/bluetooth.cpp	Thu Sep 11 10:15:34 2014 +0000
@@ -0,0 +1,16 @@
+#include "mbed.h"
+#include "bluetooth.h"
+
+
+void btSetupMaster(void)        //setup master
+{
+    btMaster.baud(9600);
+    btMaster.printf("$$$");
+    wait(0.5);
+    btMaster.printf("C\r");
+    wait(0.5);
+}
+
+void btSetupSlave(void)
+{
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/bluetooth.h	Thu Sep 11 10:15:34 2014 +0000
@@ -0,0 +1,17 @@
+#ifndef _INC_MY_BLUETOOTH
+#define _INC_MY_BLUETOOTH
+
+#include "mbed.h"
+
+typedef union _cvt{
+    char byte[4];
+    int in;
+    float fl;    
+} Cvt;
+
+Serial btMaster(p13, p14);  // tx, rx
+
+void btSetupMaster(void);
+void btSetupSlave(void);
+
+#endif
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/control.cpp	Thu Sep 11 10:15:34 2014 +0000
@@ -0,0 +1,53 @@
+#include "mbed.h"
+#include "control.h"
+
+void pull(float buf){
+    Sig1 = 0;
+    Sig2 = 1;
+    Pwm = zeroPWM+buf/3;
+}
+
+void loose(float buf){
+    Sig1 = 1;
+    Sig2 = 0;
+    Pwm = zeroPWM+buf/3;
+}
+
+void brake(void){
+    Sig1 = 0;
+    Sig2 = 0;
+    Pwm = 1;
+}
+
+void open(void){
+    Sig1 = 0;
+    Sig2 = 0;
+    Pwm = 0;
+}
+
+void ResetPos(){
+    int pos_flag=0;
+    while(1){
+        if(Red1>center_pos){
+            pull(0.3);
+        }else if(Red1<center_pos){
+            loose(0.3);
+        }
+        if(abs(Red1-center_pos)<0.1){
+            Open1();
+            pos1_flag=1;
+        }
+    
+        if(Red2>center_pos){
+            Back2();
+        }else if(Red2<center_pos){
+            Forward2();
+        }
+        if(abs(Red2-center_pos)<0.1){
+            Open2();
+            pos2_flag=1;
+        }
+    
+        if(pos1_flag == 1 && pos2_flag ==1) break;
+    }
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/control.h	Thu Sep 11 10:15:34 2014 +0000
@@ -0,0 +1,16 @@
+#ifndef _INC_MY_CONTROL
+#define _INC_MY_CONTROL
+
+#include "mbed.h"
+
+//motor
+DigitalOut Sig1(p21);
+DigitalOut Sig2(p23);
+PwmOut Pwm(p22);
+
+void pull(float);   //pull motor(power)   power:0~1
+void loose(float);  //loose motor(power)  power:0~1
+void brake(void);   //brake motor
+void open(void);    //open motor
+
+#endif
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/fm.cpp	Thu Sep 11 10:15:34 2014 +0000
@@ -0,0 +1,71 @@
+#include "mbed.h"
+#include "fm.h"
+
+
+void i2cSetup(unsigned int _f)
+{
+    freq = _f;         //freq/10.MHz   760~910
+    if(freq > 870){
+        freqB = freq - 870;
+        freqH = freqB>>2;
+        freqL = (freqB&3)<<6;                  // Shift channel selection for matching register 0x03
+    }else{
+        freqB = freq-760;
+        freqH = freqB>>2;
+        freqL = (freqB&3)<<6;
+    }
+   
+    i2c.start();
+    i2c.write(addr);
+    i2c.write(0x02);
+    i2c.write(0xC0);
+    i2c.write(0x03);
+    i2c.stop();
+    wait(0.5);                        // wait 500ms
+   
+    i2c.start();
+    i2c.write(addr);
+    i2c.write(0x02);
+    i2c.write(0xC0);
+    i2c.write(0x0D);
+    i2c.stop();
+    wait(0.5);   
+   
+    if(freq > 870){
+        freqL=freqL | 0x10;
+    }else{ 
+        freqL=freqL | 0x14;
+    }
+    i2c.start();
+    i2c.write(addr);
+    i2c.write(0x03);
+    i2c.write(freqH);
+    i2c.write(freqL);
+    i2c.stop();
+    wait(0.5);
+   
+    char temp0 = 0x00;
+    char temp1 = 0x00;
+    i2c.read(addr,&temp0,1);
+    i2c.read(addr,&temp1,1);
+    temp0 &= 0xff;
+    temp1 &= 0xf0;
+    temp0 |= 0x00;
+    temp1 |= 0x01;
+    i2c.start();
+    i2c.write(addr);
+    i2c.write(0x40);
+    i2c.write(temp0);
+    i2c.write(temp1);
+    i2c.stop();
+    wait(0.5);
+ 
+    i2c.start();
+    i2c.write(addr);
+    i2c.write(0x42);
+    i2c.write(0x03);
+    i2c.write(0x0F);
+    i2c.stop();
+   
+    wait(0.5);
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/fm.h	Thu Sep 11 10:15:34 2014 +0000
@@ -0,0 +1,14 @@
+#ifndef _INC_MY_FMI2C
+#define _INC_MY_FMI2C
+
+#include "mbed.h"
+
+I2C i2c(p9,p10);    // SDA,SCI
+
+char freqH = 0, freqL = 0, c, i;
+unsigned int frequencyB, freq, freqB;
+const int addr = 0x11 << 1;
+
+void i2cSetup(unsigned int);     //setup i2c (int frequency) -frequency:760~910
+
+#endif
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Thu Sep 11 10:15:34 2014 +0000
@@ -0,0 +1,57 @@
+//Master mbed Program
+
+#include "mbed.h"
+#include "fm.h"
+#include "bluetooth.h"
+
+#define red_v 0.01
+#define center_pos 0.80
+#define late_time 0.3
+#define zeroPWM 0.17
+
+//port
+Serial pc(USBTX, USBRX);   // tx, rx
+
+//sensor
+AnalogIn Red1(p17);
+AnalogIn FSR1(p18);
+AnalogIn Red2(p19);
+AnalogIn FSR2(p20);
+ 
+//debug
+DigitalOut led1(LED1);
+DigitalOut led2(LED2);
+DigitalOut led4(LED4);
+ 
+//motor
+DigitalOut Sig11(p24);
+DigitalOut Sig12(p26);
+PwmOut Pwm1(p25);
+DigitalOut Sig21(p21);
+DigitalOut Sig22(p23);
+PwmOut Pwm2(p22);
+ 
+DigitalOut myled1(LED1);
+DigitalOut myled2(LED2);
+
+//byte converter
+Cvt cvt;
+
+void btRead(void);
+
+int main()
+{
+    i2cSetup( 780 );
+    btSetupMaster();
+    while(1){
+        /*i2c.start();
+        i2c.write(0x11);
+        i2c.write(0x42);
+        i2c.write(0x07);
+        i2c.write(0x0F);
+        i2c.stop();
+        wait(0.05);*/
+    }
+ }
+
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed.bld	Thu Sep 11 10:15:34 2014 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/mbed_official/code/mbed/builds/9327015d4013
\ No newline at end of file