Denver / Mbed 2 deprecated denver_train_proj

Dependencies:   mbed TextLCD

main.cpp

Committer:
carlosperales95
Date:
2018-06-07
Revision:
19:ff21ba3a4dc5
Parent:
17:0a657e338356
Child:
20:608ab9bf340d

File content as of revision 19:ff21ba3a4dc5:

#include "mbed.h"
#include "TextLCD.h"
//mbed DCC Model Train Demo
 
/******PINS AND DECLARATIONS*******/

///p5
///p6
///p7
///p8

//RAIL SENSORS - INT0,INT1
//INT0 - p9
InterruptIn sensors0(p9);
//INT1 - p10
InterruptIn sensors1(p10);

///p11
///p12

//M0 - p13
DigitalIn d21(p13);
//M1 - p14
DigitalIn d22(p14);
//M2 - p15
DigitalIn d23(p15);

//p16
//p17

//BUZZER - p18
DigitalOut buzz(p18); // buzz=0 doesn't beep, buzz=1 beeps

//POTENTIOMETER - p19
AnalogIn pot(p19);  //Gives float value pot.read(). Convert analog input to V with f*3.3

//DAT - p20
DigitalOut Track(p20); //Digital output bit used to drive track power via H-bridge

//LCD SCREEN - p21, p22, p23, p24, p25, p26
TextLCD lcd(p22,p21,p23,p24,p25,p26); // RS, E, A4, A5, A6, A7 // ldc.cls() to clear and printf(String up to 16char)

///p27
///p28

//LED1 - p29
DigitalOut redled(p29);
//LED2 - p30
DigitalOut greenled(p30);


//Rail sensors
InterruptIn int0(p9);
InterruptIn int1(p10);

//MCP
MCP23017 *mcp;

//**************** FUNCTIONS FOR DENVER TRAIN ****************//

void interrupt0(){
     int data = mcp->readRegister(GPIO);
     lcd.cls();
     lcd.printf("int0 %d",data);
     buzz = 1;
     wait(0.5);
     buzz = 0;
}

void interrupt1(){
    int data = mcp->readRegister(GPIO);
    lcd.cls();
    lcd.printf("int1 %d",data);
    buzz = 1;
    wait(0.5);
    buzz = 0;
}

void initalize_mcp(){
    mcp = new MPC23017(p28,p27,0x40); //Connect to SCL - p28 and SDA - p27 and MPC I2C address 0x40 
    
    mcp->reset();
    mcp->writeRegister(0x00, (unsigned char )0xff);
    mcp->writeRegister(0x01, (unsigned char )0xff);
    mcp->writeRegister(0x02, (unsigned char )0x00);
    mcp->writeRegister(0x03, (unsigned char )0x00);
    mcp->writeRegister(0x04, (unsigned char )0xff);
    mcp->writeRegister(0x05, (unsigned char )0xff);
    mcp->writeRegister(0x06, (unsigned char )0xff);
    mcp->writeRegister(0x07, (unsigned char )0xff);
    mcp->writeRegister(0x08, (unsigned char )0xff);
    mcp->writeRegister(0x09, (unsigned char )0xff);
    mcp->writeRegister(0x0a, (unsigned char )0x42);
    mcp->writeRegister(0x0b, (unsigned char )0x42);
    mcp->writeRegister(0x0c, (unsigned char )0x00);
    mcp->writeRegister(0x0d, (unsigned char )0x00);
}
 
 
void DCC_send_command(unsigned int address, unsigned int inst, unsigned int repeat_count)
{
    unsigned __int64 command = 0x0000000000000000; // __int64 is the 64-bit integer type
    unsigned __int64 temp_command = 0x0000000000000000;
    unsigned __int64 prefix = 0x3FFF; // 14 "1" bits needed at start
    unsigned int error = 0x00; //error byte
    //calculate error detection byte with xor
    error = address ^ inst;
    //combine packet bits in basic DCC format
    command = (prefix<<28)|(address<<19)|(inst<<10)|((error)<<1)|0x01;
    //printf("\n\r %llx \n\r",command);
    int i=0;
//repeat DCC command lots of times
    while(i < repeat_count) {
        temp_command = command;
//loops through packet bits encoding and sending out digital pulses for a DCC command
        for (int j=0; j<64; j++) {
            if((temp_command&0x8000000000000000)==0) { //test packet bit
                //send data for a "0" bit
                Track=0;
                wait_us(100);
                Track=1;
                wait_us(100);
                //printf("0011");
            } else {
                //send data for a "1"bit
                Track=0;
                wait_us(58);
                Track=1;
                wait_us(58);
                //printf("01");
            }
            // next bit in packet
            temp_command = temp_command<<1;
        }
        i++;
    }
}



//**************** MAIN PROGRAM FOR DENVER TRAIN ****************//

int main()
{
    led1 = 1;
    wait(0.5);
    led1 = 0;
    wait(0.5);
    led1 = 1;
    
    initialize_mcp();
    
    int0.rise($interrupt0);
    int1.rise($interrupt1);
    
    
    //typical out of box default engine DCC address is 3 (at least for Bachmann trains)
    //Note: A DCC controller can reprogram the address whenever needed
    unsigned int DCCaddress = 0x01;
    //see http://www.nmra.org/standards/DCC/standards_rps/RP-921%202006%20Aug%2021.pdf
    //01DCSSSS for speed, D is direction (fwd=1 and rev=0), C is speed(SSSSC) LSB
    unsigned int DCCinst_forward = 0x68; //forward half speed
    unsigned int DCCinst_reverse = 0x48; //reverse half speed
    unsigned int DCCinst_stop = 0x50;
    //100DDDDD for basic headlight functions
    unsigned int DCC_func_lighton = 0x90; //F0 turns on headlight function
    unsigned int DCC_func_dimlight = 0x91; //F0 + F1 dims headlight
    //
    //Basic DCC Demo Commands
    DCC_send_command(DCCaddress,DCC_func_lighton,200); // turn light on full
    DCC_send_command(DCCaddress,DCC_func_dimlight,200); //dim light
    DCC_send_command(DCCaddress,DCC_func_lighton,200);  //light full again
    led2 = 1;
    
    //Command variables for Switches using DCC
    
    unsigned int SWBaddress = 0x06; //address for switch box
    
    ////100DDDDD where DDDDD 
    ///00001 to flip the first switch SW1 (F1 active)
    ///00010 to flip the second switch SW2 (F2 active)
    ///00100 to flip the third switch SW3 (F3 active)
    ///01000 to flip the fourth switch SW4 (F4 active)
    //example - 111111 0 00000101 0 10000000 0 10000101 1 - idle
    
    unsigned int SWBflip_idle = 0x80;   //No switches -- idle
    unsigned int SWBflip_f1 = 0x81;     //FLIP 1
    unsigned int SWBflip_f2 = 0x82;     
    unsigned int SWBflip_f3 = 0x84;
    unsigned int SWBflip_f4 = 0x90;

    //Send using DCC_send_command()
    
    
    //Demo for stopping at the station
    while(1) {
        
        if(d21 == 1 || d22 == 1 || d23 == 1){
            
            lcd.cls();
            lcd.printf("Choo Choo station");
            DCC_send_command(DCCaddress,DCCinst_stop,400); // Stop train address 3 
            
        }else{
            DCC_send_command(DCCaddress,DCCinst_forward,1); // Forward half speed train address 3 
        }
        
        
        /*
        float f = pot.read();
        float vin = f * 3.3;
            
        led3 = 1;
        if(switch1 == 1){
            lcd.cls();
            lcd.printf("Going forward");
            greenled = 0;
            redled = 1;  
            DCC_send_command(DCCaddress,DCCinst_forward,400); // forward half speed train address 3 
            lcd.cls();
            
            lcd.printf("vin: %.4f",vin);
            wait(0.2);
            
        }else{
            lcd.cls();
            lcd.printf("Going backwards");
            greenled = 1;
            redled = 0; 
            DCC_send_command(DCCaddress,DCCinst_reverse,400); // reverse half speed train address 3
            lcd.cls();
            
            lcd.printf("vin: %.4f",vin);
            wait(0.2);
            
        }
        */
    }
}