Max Vasconcelos
/
encoder_communication
Encoder reading
main.cpp@0:e51ae6d8c5c8, 22 months ago (annotated)
- Committer:
- maxaa1998
- Date:
- Wed Nov 16 22:42:29 2022 +0000
- Revision:
- 0:e51ae6d8c5c8
Encoder reading
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
maxaa1998 | 0:e51ae6d8c5c8 | 1 | #include "mbed.h" |
maxaa1998 | 0:e51ae6d8c5c8 | 2 | |
maxaa1998 | 0:e51ae6d8c5c8 | 3 | |
maxaa1998 | 0:e51ae6d8c5c8 | 4 | Ticker tick; // |
maxaa1998 | 0:e51ae6d8c5c8 | 5 | |
maxaa1998 | 0:e51ae6d8c5c8 | 6 | AnalogIn linear_position(A0); //sets an analog input to receive the linear position |
maxaa1998 | 0:e51ae6d8c5c8 | 7 | AnalogIn linear_velocity(A1); //sets an analog input to receive the linear velocity |
maxaa1998 | 0:e51ae6d8c5c8 | 8 | |
maxaa1998 | 0:e51ae6d8c5c8 | 9 | Serial pc(USBTX, USBRX); //enable usb serial communication |
maxaa1998 | 0:e51ae6d8c5c8 | 10 | //Serial encoder(PTE0, PTE1); |
maxaa1998 | 0:e51ae6d8c5c8 | 11 | DigitalOut red(LED_RED); //set the red LED as red |
maxaa1998 | 0:e51ae6d8c5c8 | 12 | DigitalOut green(LED_GREEN); //set the green LED as green |
maxaa1998 | 0:e51ae6d8c5c8 | 13 | DigitalOut blue(LED_BLUE); //set the blue LED as blue |
maxaa1998 | 0:e51ae6d8c5c8 | 14 | DigitalOut ms(PTD1); //set the transceiver mode selection (0=receiver mode and 1=transmiter mode) |
maxaa1998 | 0:e51ae6d8c5c8 | 15 | |
maxaa1998 | 0:e51ae6d8c5c8 | 16 | volatile uint8_t received1, received2; //angle components variables |
maxaa1998 | 0:e51ae6d8c5c8 | 17 | volatile uint16_t angle = 0; //encoder angle |
maxaa1998 | 0:e51ae6d8c5c8 | 18 | |
maxaa1998 | 0:e51ae6d8c5c8 | 19 | void isr() //interrupution routine |
maxaa1998 | 0:e51ae6d8c5c8 | 20 | { |
maxaa1998 | 0:e51ae6d8c5c8 | 21 | pc.printf("0x%04X",linear_position.read_u16()); //send linear position, obs: 6 chars needed |
maxaa1998 | 0:e51ae6d8c5c8 | 22 | pc.printf("0x%04X",linear_velocity.read_u16()); //send linear velocity |
maxaa1998 | 0:e51ae6d8c5c8 | 23 | pc.printf("0x%04X",angle); //send angle |
maxaa1998 | 0:e51ae6d8c5c8 | 24 | pc.printf("OMEGA "); //send angular velocity |
maxaa1998 | 0:e51ae6d8c5c8 | 25 | } |
maxaa1998 | 0:e51ae6d8c5c8 | 26 | |
maxaa1998 | 0:e51ae6d8c5c8 | 27 | int main() //main function |
maxaa1998 | 0:e51ae6d8c5c8 | 28 | { |
maxaa1998 | 0:e51ae6d8c5c8 | 29 | |
maxaa1998 | 0:e51ae6d8c5c8 | 30 | //PC serial communication |
maxaa1998 | 0:e51ae6d8c5c8 | 31 | pc.baud(115200); //set baud rate |
maxaa1998 | 0:e51ae6d8c5c8 | 32 | //encoder.baud(115200); |
maxaa1998 | 0:e51ae6d8c5c8 | 33 | tick.attach(&isr, 1); //setup ticker to call isr every 0.1s seconds |
maxaa1998 | 0:e51ae6d8c5c8 | 34 | |
maxaa1998 | 0:e51ae6d8c5c8 | 35 | //Encoder serial communication |
maxaa1998 | 0:e51ae6d8c5c8 | 36 | SIM->SCGC5 |= 0x00002000; //enable PORTE clock |
maxaa1998 | 0:e51ae6d8c5c8 | 37 | PORTE->PCR[0] |= 0x00000300; //PTE0 as ALT3 (UART1) |
maxaa1998 | 0:e51ae6d8c5c8 | 38 | PORTE->PCR[1] |= 0x00000300; //PTE1 as ALT3 (UART1) |
maxaa1998 | 0:e51ae6d8c5c8 | 39 | SIM->SCGC4 |= 0x00000800; //enable UART1 clock |
maxaa1998 | 0:e51ae6d8c5c8 | 40 | SIM->SOPT5 &= ~0x00000070; //select UART1 TX pin as UART1 TX source and UART1 RX pin as UART1 RX source |
maxaa1998 | 0:e51ae6d8c5c8 | 41 | |
maxaa1998 | 0:e51ae6d8c5c8 | 42 | |
maxaa1998 | 0:e51ae6d8c5c8 | 43 | /*baud rate divider setting: |
maxaa1998 | 0:e51ae6d8c5c8 | 44 | BAUD = Bus_Clock/(16 x BR) = 24M/ (16 x 13) ~ 115200 |
maxaa1998 | 0:e51ae6d8c5c8 | 45 | baud rate values: 115200, 39400, 19200, 9600 |
maxaa1998 | 0:e51ae6d8c5c8 | 46 | respective BDL values: 0x0D, 0x27, 0x4E, 0x9C*/ |
maxaa1998 | 0:e51ae6d8c5c8 | 47 | |
maxaa1998 | 0:e51ae6d8c5c8 | 48 | UART1->BDH = 0x00; //set the highest bits of the baud rate divider |
maxaa1998 | 0:e51ae6d8c5c8 | 49 | UART1->BDL = 0x0D; //set the lowest bits of the baud rate divider |
maxaa1998 | 0:e51ae6d8c5c8 | 50 | UART1->C1 = 0x00; //sets a 8N1 configuration |
maxaa1998 | 0:e51ae6d8c5c8 | 51 | UART1->C2 = 0x0C; //enables Tx and Rx/ Interrupts disabled |
maxaa1998 | 0:e51ae6d8c5c8 | 52 | |
maxaa1998 | 0:e51ae6d8c5c8 | 53 | red = 1; //turn off the red LED |
maxaa1998 | 0:e51ae6d8c5c8 | 54 | green = 1; //turn off the green LED |
maxaa1998 | 0:e51ae6d8c5c8 | 55 | blue = 1; //turn off the blue LED |
maxaa1998 | 0:e51ae6d8c5c8 | 56 | while (true) //infinite loop |
maxaa1998 | 0:e51ae6d8c5c8 | 57 | { |
maxaa1998 | 0:e51ae6d8c5c8 | 58 | |
maxaa1998 | 0:e51ae6d8c5c8 | 59 | if (((UART1->S1&0xC0)>>6)==0x03) //if the ucontroller transmiter buffer is free |
maxaa1998 | 0:e51ae6d8c5c8 | 60 | { |
maxaa1998 | 0:e51ae6d8c5c8 | 61 | ms = 1; //put the transceiver in the transmiter mode |
maxaa1998 | 0:e51ae6d8c5c8 | 62 | UART1->D = 0x54; //send the encoder's address |
maxaa1998 | 0:e51ae6d8c5c8 | 63 | while(!(((UART1->S1&0xC0)>>6)==0x03)); //wait 'til the message has been completely sent |
maxaa1998 | 0:e51ae6d8c5c8 | 64 | ms = 0; //put the transceiver in the receiver mode |
maxaa1998 | 0:e51ae6d8c5c8 | 65 | } |
maxaa1998 | 0:e51ae6d8c5c8 | 66 | |
maxaa1998 | 0:e51ae6d8c5c8 | 67 | wait_us(10); //turnaround time: 3us 10us 30us 56us 100us |
maxaa1998 | 0:e51ae6d8c5c8 | 68 | //baud rate: 115200(default) 115200(adjustable) 38400 19200 9600 |
maxaa1998 | 0:e51ae6d8c5c8 | 69 | |
maxaa1998 | 0:e51ae6d8c5c8 | 70 | if(((UART1->S1&0x20)>>5)==0x01) //if the ucontroller transmiter buffer is full |
maxaa1998 | 0:e51ae6d8c5c8 | 71 | { |
maxaa1998 | 0:e51ae6d8c5c8 | 72 | received1 = UART1->D; //stores the first Byte in the received1 variable |
maxaa1998 | 0:e51ae6d8c5c8 | 73 | while(!(((UART1->S1&0x20)>>5)==0x01)); //wait 'til the next has been received |
maxaa1998 | 0:e51ae6d8c5c8 | 74 | if(((UART1->S1&0x20)>>5)==0x01) //if the ucontroller transmiter buffer is full |
maxaa1998 | 0:e51ae6d8c5c8 | 75 | { |
maxaa1998 | 0:e51ae6d8c5c8 | 76 | received2 = UART1->D; //stores the second Byte in the received2 variable |
maxaa1998 | 0:e51ae6d8c5c8 | 77 | angle = ((received1)|(((received2)&0x3F)<<8)); //assembly the angle using the components |
maxaa1998 | 0:e51ae6d8c5c8 | 78 | } |
maxaa1998 | 0:e51ae6d8c5c8 | 79 | } |
maxaa1998 | 0:e51ae6d8c5c8 | 80 | /* |
maxaa1998 | 0:e51ae6d8c5c8 | 81 | if (((UART1->S1&0x20)>>5)==0x01) |
maxaa1998 | 0:e51ae6d8c5c8 | 82 | { |
maxaa1998 | 0:e51ae6d8c5c8 | 83 | received = UART1->D; |
maxaa1998 | 0:e51ae6d8c5c8 | 84 | angle = (received<<2); |
maxaa1998 | 0:e51ae6d8c5c8 | 85 | red = !red; |
maxaa1998 | 0:e51ae6d8c5c8 | 86 | } |
maxaa1998 | 0:e51ae6d8c5c8 | 87 | */ |
maxaa1998 | 0:e51ae6d8c5c8 | 88 | } |
maxaa1998 | 0:e51ae6d8c5c8 | 89 | } |
maxaa1998 | 0:e51ae6d8c5c8 | 90 | //TO DO: Program the readings |