Max Vasconcelos / Mbed 2 deprecated encoder_communication

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 
00003 
00004 Ticker tick;                                                                //
00005 
00006 AnalogIn linear_position(A0);                                               //sets an analog input to receive the linear position
00007 AnalogIn linear_velocity(A1);                                               //sets an analog input to receive the linear velocity
00008 
00009 Serial pc(USBTX, USBRX);                                                    //enable usb serial communication
00010 //Serial encoder(PTE0, PTE1);
00011 DigitalOut red(LED_RED);                                                    //set the red LED as red
00012 DigitalOut green(LED_GREEN);                                                //set the green LED as green
00013 DigitalOut blue(LED_BLUE);                                                  //set the blue LED as blue
00014 DigitalOut ms(PTD1);                                                        //set the transceiver mode selection (0=receiver mode and 1=transmiter mode) 
00015 
00016 volatile uint8_t received1, received2;                                      //angle components variables
00017 volatile uint16_t angle = 0;                                                //encoder angle
00018 
00019 void isr()                                                                  //interrupution routine
00020 {
00021     pc.printf("0x%04X",linear_position.read_u16());                         //send linear position, obs: 6 chars needed
00022     pc.printf("0x%04X",linear_velocity.read_u16());                         //send linear velocity
00023     pc.printf("0x%04X",angle);                                              //send angle
00024     pc.printf("OMEGA ");                                                    //send angular velocity 
00025 }
00026 
00027 int main()                                                                  //main function
00028 {
00029 
00030     //PC serial communication
00031     pc.baud(115200);                                                        //set baud rate
00032     //encoder.baud(115200);
00033     tick.attach(&isr, 1);                                                   //setup ticker to call isr every 0.1s seconds
00034     
00035     //Encoder serial communication
00036     SIM->SCGC5 |= 0x00002000;                                               //enable PORTE clock
00037     PORTE->PCR[0] |= 0x00000300;                                            //PTE0 as ALT3 (UART1) 
00038     PORTE->PCR[1] |= 0x00000300;                                            //PTE1 as ALT3 (UART1) 
00039     SIM->SCGC4 |= 0x00000800;                                               //enable UART1 clock
00040     SIM->SOPT5 &= ~0x00000070;                                              //select UART1 TX pin as UART1 TX source and UART1 RX pin as UART1 RX source
00041     
00042     
00043     /*baud rate divider setting:
00044      BAUD = Bus_Clock/(16 x BR) = 24M/ (16 x 13) ~ 115200
00045      baud rate values:       115200, 39400,   19200,   9600
00046      respective BDL values:    0x0D,  0x27,    0x4E,    0x9C*/
00047     
00048     UART1->BDH = 0x00;                                                      //set the highest bits of the baud rate divider
00049     UART1->BDL = 0x0D;                                                      //set the lowest bits of the baud rate divider
00050     UART1->C1  = 0x00;                                                      //sets a 8N1 configuration
00051     UART1->C2  = 0x0C;                                                      //enables Tx and Rx/ Interrupts disabled
00052     
00053     red = 1;                                                                //turn off the red LED
00054     green = 1;                                                              //turn off the green LED
00055     blue = 1;                                                               //turn off the blue LED
00056     while (true)                                                            //infinite loop
00057     {
00058         
00059         if (((UART1->S1&0xC0)>>6)==0x03)                                    //if the ucontroller transmiter buffer is free
00060         {
00061             ms = 1;                                                         //put the transceiver in the transmiter mode
00062             UART1->D = 0x54;                                                //send the encoder's address
00063             while(!(((UART1->S1&0xC0)>>6)==0x03));                          //wait 'til the message has been completely sent
00064             ms = 0;                                                         //put the transceiver in the receiver mode
00065         }
00066         
00067         wait_us(10);                                                        //turnaround time: 3us             10us                30us    56us    100us
00068                                                                             //baud rate:       115200(default) 115200(adjustable)  38400   19200   9600
00069 
00070         if(((UART1->S1&0x20)>>5)==0x01)                                     //if the ucontroller transmiter buffer is full
00071             {
00072                 received1 = UART1->D;                                       //stores the first Byte in the received1 variable
00073                 while(!(((UART1->S1&0x20)>>5)==0x01));                      //wait 'til the next has been received
00074                 if(((UART1->S1&0x20)>>5)==0x01)                             //if the ucontroller transmiter buffer is full
00075                 {
00076                     received2 = UART1->D;                                   //stores the second Byte in the received2 variable
00077                     angle = ((received1)|(((received2)&0x3F)<<8));          //assembly the angle using the components
00078                 }
00079             }
00080 /*
00081         if (((UART1->S1&0x20)>>5)==0x01)
00082             {
00083                 received = UART1->D;
00084                 angle = (received<<2);
00085                 red = !red;
00086             }
00087 */
00088     }
00089 }
00090 //TO DO: Program the readings