Encoder with UART0

Dependencies:   USBDevice mbed

Committer:
maxaa1998
Date:
Wed Nov 16 22:46:11 2022 +0000
Revision:
0:90116cd08554
Encoder with UART0

Who changed what in which revision?

UserRevisionLine numberNew contents of line
maxaa1998 0:90116cd08554 1 #include "mbed.h"
maxaa1998 0:90116cd08554 2 #include "USBSerial.h"
maxaa1998 0:90116cd08554 3 Ticker tick; //
maxaa1998 0:90116cd08554 4
maxaa1998 0:90116cd08554 5 AnalogIn linear_position(A0); //sets an analog input to receive the linear position
maxaa1998 0:90116cd08554 6 AnalogIn linear_velocity(A1); //sets an analog input to receive the linear velocity
maxaa1998 0:90116cd08554 7
maxaa1998 0:90116cd08554 8 USBSerial u_s_b;
maxaa1998 0:90116cd08554 9 //Serial pc(USBTX, USBRX); //enable usb serial communication
maxaa1998 0:90116cd08554 10 //Serial encoder(PTE0, PTE1);
maxaa1998 0:90116cd08554 11 DigitalOut red(LED_RED); //set the red LED as red
maxaa1998 0:90116cd08554 12 DigitalOut green(LED_GREEN); //set the green LED as green
maxaa1998 0:90116cd08554 13 DigitalOut blue(LED_BLUE); //set the blue LED as blue
maxaa1998 0:90116cd08554 14 DigitalOut ms(PTD1); //set the transceiver mode selection (0=receiver mode and 1=transmiter mode)
maxaa1998 0:90116cd08554 15
maxaa1998 0:90116cd08554 16 volatile uint8_t received1, received2, address = 0x54; //angle components variables
maxaa1998 0:90116cd08554 17 volatile uint16_t angle = 0; //encoder angle
maxaa1998 0:90116cd08554 18
maxaa1998 0:90116cd08554 19 void isr() //interrupution routine
maxaa1998 0:90116cd08554 20 {/*
maxaa1998 0:90116cd08554 21 u_s_b.printf("0x%04X",linear_position.read_u16()); //send linear position, obs: 6 chars needed
maxaa1998 0:90116cd08554 22 u_s_b.printf("0x%04X",linear_velocity.read_u16()); //send linear velocity
maxaa1998 0:90116cd08554 23 u_s_b.printf("0x%04X",angle); //send angle
maxaa1998 0:90116cd08554 24 u_s_b.printf("OMEGA "); //send angular velocity
maxaa1998 0:90116cd08554 25 */}
maxaa1998 0:90116cd08554 26
maxaa1998 0:90116cd08554 27 int main() //main function
maxaa1998 0:90116cd08554 28 {
maxaa1998 0:90116cd08554 29
maxaa1998 0:90116cd08554 30 //PC serial communication
maxaa1998 0:90116cd08554 31 // pc.baud(115200); //set baud rate
maxaa1998 0:90116cd08554 32 //encoder.baud(115200);
maxaa1998 0:90116cd08554 33 // tick.attach(&isr, 1); //setup ticker to call isr every 0.1s seconds
maxaa1998 0:90116cd08554 34
maxaa1998 0:90116cd08554 35
maxaa1998 0:90116cd08554 36 //Encoder serial communication
maxaa1998 0:90116cd08554 37 SIM->SCGC5 |= 0x00000200; //enable PORTA clock
maxaa1998 0:90116cd08554 38 PORTA->PCR[1] |= 0x00000200; //PTA0 as ALT2 (UART0)
maxaa1998 0:90116cd08554 39 PORTA->PCR[2] |= 0x00000200; //PTA1 as ALT2 (UART0)
maxaa1998 0:90116cd08554 40 SIM->SOPT2 |= 0x04000000; //select MCGFLLCLK as the UART0 clock source (24M by default)
maxaa1998 0:90116cd08554 41 SIM->SOPT5 &= 0x00000000; //select UART0 TX pin as UART0 TX source and UART0 RX pin as UART0 RX source
maxaa1998 0:90116cd08554 42 SIM->SCGC4 |= 0x00000400; //enable UART0 clock
maxaa1998 0:90116cd08554 43
maxaa1998 0:90116cd08554 44
maxaa1998 0:90116cd08554 45 //baud rate divider setting:
maxaa1998 0:90116cd08554 46 //BAUD = Bus_Clock/((OSR+1) x BR) = 24M/ ((3+1) x 3) = 2M
maxaa1998 0:90116cd08554 47
maxaa1998 0:90116cd08554 48 UART0->BDH = 0x00; //set the highest bits of the baud rate divider
maxaa1998 0:90116cd08554 49 UART0->BDL = 0x06; //set the lowest bits of the baud rate divider
maxaa1998 0:90116cd08554 50 UART0->C4 = 0x03; //sets the Over Sampling Ratio (OSR) - MUST BE SET BEFORE C2
maxaa1998 0:90116cd08554 51 UART0->C5 = 0x00; //sets both edges sampling - MUST BE SET BEFORE C2 AND IF OSR>7
maxaa1998 0:90116cd08554 52 UART0->C1 = 0x00; //sets a 8N1 configuration
maxaa1998 0:90116cd08554 53 UART0->C2 = 0x0C; //enables Tx and Rx/ Interrupts disabled
maxaa1998 0:90116cd08554 54
maxaa1998 0:90116cd08554 55 red = 1; //turn off the red LED
maxaa1998 0:90116cd08554 56 green = 1; //turn off the green LED
maxaa1998 0:90116cd08554 57 blue = 1; //turn off the blue LED
maxaa1998 0:90116cd08554 58 while (true) //infinite loop
maxaa1998 0:90116cd08554 59 {
maxaa1998 0:90116cd08554 60
maxaa1998 0:90116cd08554 61 if (((UART0->S1&0xC0)>>6)==0x03) //if the ucontroller transmiter buffer is free
maxaa1998 0:90116cd08554 62 {
maxaa1998 0:90116cd08554 63 ms = 1; //put the transceiver in the transmiter mode
maxaa1998 0:90116cd08554 64 UART0->D = 0x54; //send the encoder's address
maxaa1998 0:90116cd08554 65 while(!(((UART0->S1&0xC0)>>6)==0x03)); //wait 'til the message has been completely sent
maxaa1998 0:90116cd08554 66 ms = 0; //put the transceiver in the receiver mode
maxaa1998 0:90116cd08554 67 }
maxaa1998 0:90116cd08554 68
maxaa1998 0:90116cd08554 69 red = 1;
maxaa1998 0:90116cd08554 70 //wait_us(1);
maxaa1998 0:90116cd08554 71 //while(!(((UART0->S1&0x20)>>5)==0x01));
maxaa1998 0:90116cd08554 72
maxaa1998 0:90116cd08554 73 if(((UART0->S1&0x20)>>5)==0x01) //if the ucontroller receiver buffer is full
maxaa1998 0:90116cd08554 74 {
maxaa1998 0:90116cd08554 75 received1 = UART0->D;
maxaa1998 0:90116cd08554 76 if (((UART0->S1&0x08)>>3)==0x01) //if an overflow happened
maxaa1998 0:90116cd08554 77 {
maxaa1998 0:90116cd08554 78 received2 = 0x63;
maxaa1998 0:90116cd08554 79 UART0->S1=1;
maxaa1998 0:90116cd08554 80 }
maxaa1998 0:90116cd08554 81 else
maxaa1998 0:90116cd08554 82 received2 = 0x64; //stores the first Byte in the received1 variable
maxaa1998 0:90116cd08554 83 // //- NÃO SAI DO LOOP -wait 'til the next has been received
maxaa1998 0:90116cd08554 84 //if(((UART0->S1&0x20)>>5)==0x01)
maxaa1998 0:90116cd08554 85 //wait_us(6);
maxaa1998 0:90116cd08554 86 //if(((UART0->S1&0x20)>>5)==0x01)
maxaa1998 0:90116cd08554 87 // received2 = UART0->D;
maxaa1998 0:90116cd08554 88 //else if (((UART0->S1&0x08)>>3)==0x01) //if an overflow happened
maxaa1998 0:90116cd08554 89 // {
maxaa1998 0:90116cd08554 90 // received2 = 0x63;
maxaa1998 0:90116cd08554 91 // UART0->S1=1;
maxaa1998 0:90116cd08554 92 // }
maxaa1998 0:90116cd08554 93 // else
maxaa1998 0:90116cd08554 94 // received2 = 0x64;
maxaa1998 0:90116cd08554 95 //received2 = UART0->D;
maxaa1998 0:90116cd08554 96 //stores the second Byte in the received2 variable
maxaa1998 0:90116cd08554 97
maxaa1998 0:90116cd08554 98 angle = ((((received2)&0x3F)<<8)|(received1)); //assembly the angle using the components
maxaa1998 0:90116cd08554 99 }
maxaa1998 0:90116cd08554 100
maxaa1998 0:90116cd08554 101 if (((UART0->S1&0x08)>>3)==0x01) //if an overflow happened
maxaa1998 0:90116cd08554 102 {
maxaa1998 0:90116cd08554 103 UART0->S1=1;
maxaa1998 0:90116cd08554 104 }
maxaa1998 0:90116cd08554 105
maxaa1998 0:90116cd08554 106 u_s_b.printf("0x%04X",linear_position.read_u16()); //send linear position, obs: 6 chars needed
maxaa1998 0:90116cd08554 107 u_s_b.printf("0x%04X",linear_velocity.read_u16()); //send linear velocity
maxaa1998 0:90116cd08554 108 u_s_b.printf("0x%04X",angle); //send angle
maxaa1998 0:90116cd08554 109 u_s_b.printf("OMEGA "); //send angular velocity
maxaa1998 0:90116cd08554 110 }
maxaa1998 0:90116cd08554 111 }
maxaa1998 0:90116cd08554 112 //TO DO: verificar se ocorr overflow do buffer