Seeker of Truth , / Mbed 2 deprecated freemaster_write2master_working

Dependencies:   mbed

Fork of freemaster_write2master_working by green rosh

main.cpp

Committer:
raizel_varun
Date:
2014-12-12
Revision:
4:0b714f858ed2
Parent:
3:c810e140a6bf

File content as of revision 4:0b714f858ed2:

#include "mbed.h"

const int addr = 0x20;                     //slave address 
I2C master (D14,D15);                      //configure pins p27,p28 as I2C master
Serial pc (USBTX,USBRX);
DigitalOut interrupt(D9);
InterruptIn data_ready(D10);
bool looper;


void FUNC_I2C_MASTER()
{
    //printf("press backspace for master to start \n\r");
    //while(1){
            //interrupt = 0;
      //  if(pc.getc()=='z'){
            /*while (looper){
                wait(0.5);
                break;}*/
            wait(0.5);
            interrupt = 1;
            printf("\nMaster executed\n");
            
          
            bool ack0 =true; 
            bool loopvariable0 =true;
            char *data =  new char;
            *data = 'b';
           
           
            
             master.frequency(100000);                       //set clock frequency
             //master.start();
            /*while(write2slave)
            {  *writedata = pc.getc();
            wait(0.5);
              
                printf("master clk freq setup and addressing slave\n\r");
                master.start();                                 //initiating the data transfer
                master_status_write = (bool) master.write(addr|0x00,writedata,1);
                if(master_status_write==0)
                {
                    printf("master has written %c to slave\n\r",*writedata);
                    write2slave=false;
                }
                    
                    
             }*/
             
            while(ack0)
            {
                //printf("2 master clk freq setup and addressing slave\n\r");
                
                master.start();                                 //initiating the data transfer
                ack0 =  master.read(addr,data,1);
            }
            if(!ack0)
            {
                    printf("\n master has read ");
                    loopvariable0=false;
            }
                //master.stop();
            
            //printf("done\n\r");
            delete data;
            interrupt=0;
            looper = false ;
            }
            

int main()
{   
    data_ready.rise(&FUNC_I2C_MASTER);
    looper = true;
    printf("\nstarted master\n");
    bool write2slave;
     char *writedata = new char;
       
   
     bool master_status_write = true;
    while(1)
    { write2slave=true;
    interrupt = 0;
    
      *writedata = pc.getc();
      
    interrupt = 1; 
                master.frequency(100000);                        //set clock frequency
                while(write2slave)
           {                
                printf("master clk freq setup and addressing slave\n\r");
                master.start();                                 //initiating the data transfer
                master_status_write = (bool) master.write(addr|0x00,writedata,1);
                if(master_status_write==0)
                    {
                    printf("master has written");
                    write2slave=false;
                    }
            }
        
    }
}