![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
distribution-201229
main.cpp@0:51c43836c1d7, 2019-08-14 (annotated)
- Committer:
- GiJeongKim
- Date:
- Wed Aug 14 08:13:36 2019 +0000
- Revision:
- 0:51c43836c1d7
- Child:
- 1:e04e563be5ce
cocoa;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
GiJeongKim | 0:51c43836c1d7 | 1 | #include "mbed.h" |
GiJeongKim | 0:51c43836c1d7 | 2 | #include "FastPWM.h" |
GiJeongKim | 0:51c43836c1d7 | 3 | #include "INIT_HW.h" |
GiJeongKim | 0:51c43836c1d7 | 4 | #include "SPI_EEP_ENC.h" |
GiJeongKim | 0:51c43836c1d7 | 5 | #include "I2C_AS5510.h" |
GiJeongKim | 0:51c43836c1d7 | 6 | #include "setting.h" |
GiJeongKim | 0:51c43836c1d7 | 7 | |
GiJeongKim | 0:51c43836c1d7 | 8 | // dac & check |
GiJeongKim | 0:51c43836c1d7 | 9 | DigitalOut check(PC_2); |
GiJeongKim | 0:51c43836c1d7 | 10 | DigitalOut check_2(PC_3); |
GiJeongKim | 0:51c43836c1d7 | 11 | AnalogOut dac_1(PA_4); |
GiJeongKim | 0:51c43836c1d7 | 12 | AnalogOut dac_2(PA_5); |
GiJeongKim | 0:51c43836c1d7 | 13 | |
GiJeongKim | 0:51c43836c1d7 | 14 | // pwm |
GiJeongKim | 0:51c43836c1d7 | 15 | float dtc_v=0; |
GiJeongKim | 0:51c43836c1d7 | 16 | float dtc_w=0; |
GiJeongKim | 0:51c43836c1d7 | 17 | |
GiJeongKim | 0:51c43836c1d7 | 18 | // I2C |
GiJeongKim | 0:51c43836c1d7 | 19 | I2C i2c(PC_9,PA_8); // SDA, SCL (for K22F) |
GiJeongKim | 0:51c43836c1d7 | 20 | const int i2c_slave_addr1 = 0x56; |
GiJeongKim | 0:51c43836c1d7 | 21 | unsigned int value; // 10bit output of reading sensor AS5510 |
GiJeongKim | 0:51c43836c1d7 | 22 | |
GiJeongKim | 0:51c43836c1d7 | 23 | // SPI |
GiJeongKim | 0:51c43836c1d7 | 24 | SPI eeprom(PB_15, PB_14, PB_13); // EEPROM //(SPI_MOSI, SPI_MISO, SPI_SCK); |
GiJeongKim | 0:51c43836c1d7 | 25 | DigitalOut eeprom_cs(PB_12); |
GiJeongKim | 0:51c43836c1d7 | 26 | SPI enc(PC_12,PC_11,PC_10); |
GiJeongKim | 0:51c43836c1d7 | 27 | DigitalOut enc_cs(PD_2); |
GiJeongKim | 0:51c43836c1d7 | 28 | |
GiJeongKim | 0:51c43836c1d7 | 29 | // UART |
GiJeongKim | 0:51c43836c1d7 | 30 | Serial pc(PA_9,PA_10); // _ UART |
GiJeongKim | 0:51c43836c1d7 | 31 | |
GiJeongKim | 0:51c43836c1d7 | 32 | //CAN |
GiJeongKim | 0:51c43836c1d7 | 33 | CAN can(PB_8, PB_9, 1000000); |
GiJeongKim | 0:51c43836c1d7 | 34 | CANMessage msg; |
GiJeongKim | 0:51c43836c1d7 | 35 | void onMsgReceived() |
GiJeongKim | 0:51c43836c1d7 | 36 | { |
GiJeongKim | 0:51c43836c1d7 | 37 | can.read(msg); |
GiJeongKim | 0:51c43836c1d7 | 38 | pc.printf("Message received: %d\n", msg.data[0]); |
GiJeongKim | 0:51c43836c1d7 | 39 | } |
GiJeongKim | 0:51c43836c1d7 | 40 | |
GiJeongKim | 0:51c43836c1d7 | 41 | |
GiJeongKim | 0:51c43836c1d7 | 42 | Timer t; |
GiJeongKim | 0:51c43836c1d7 | 43 | //t.start(); |
GiJeongKim | 0:51c43836c1d7 | 44 | //t.stop(); |
GiJeongKim | 0:51c43836c1d7 | 45 | //pc.printf("The time taken was %f seconds\n",t.read()); |
GiJeongKim | 0:51c43836c1d7 | 46 | int a1; |
GiJeongKim | 0:51c43836c1d7 | 47 | int i=0; |
GiJeongKim | 0:51c43836c1d7 | 48 | float y; |
GiJeongKim | 0:51c43836c1d7 | 49 | |
GiJeongKim | 0:51c43836c1d7 | 50 | extern "C" void TIM4_IRQHandler(void) |
GiJeongKim | 0:51c43836c1d7 | 51 | { |
GiJeongKim | 0:51c43836c1d7 | 52 | if (TIM4->SR & TIM_SR_UIF ) { |
GiJeongKim | 0:51c43836c1d7 | 53 | // y=0.05*sin(i*0.00001); |
GiJeongKim | 0:51c43836c1d7 | 54 | // y= 0.1; |
GiJeongKim | 0:51c43836c1d7 | 55 | // if (i>10000){i=0;} |
GiJeongKim | 0:51c43836c1d7 | 56 | // y=0.01*i/100000; |
GiJeongKim | 0:51c43836c1d7 | 57 | y=-0.04683; |
GiJeongKim | 0:51c43836c1d7 | 58 | // |
GiJeongKim | 0:51c43836c1d7 | 59 | // dtc_w = 0.1; |
GiJeongKim | 0:51c43836c1d7 | 60 | // dtc_v = 1; |
GiJeongKim | 0:51c43836c1d7 | 61 | // |
GiJeongKim | 0:51c43836c1d7 | 62 | |
GiJeongKim | 0:51c43836c1d7 | 63 | //PIN_V dir |
GiJeongKim | 0:51c43836c1d7 | 64 | |
GiJeongKim | 0:51c43836c1d7 | 65 | if (y>0) { |
GiJeongKim | 0:51c43836c1d7 | 66 | dtc_v=0; |
GiJeongKim | 0:51c43836c1d7 | 67 | dtc_w=y; |
GiJeongKim | 0:51c43836c1d7 | 68 | } else { |
GiJeongKim | 0:51c43836c1d7 | 69 | dtc_v=0; |
GiJeongKim | 0:51c43836c1d7 | 70 | dtc_w=-y; |
GiJeongKim | 0:51c43836c1d7 | 71 | } |
GiJeongKim | 0:51c43836c1d7 | 72 | i++; |
GiJeongKim | 0:51c43836c1d7 | 73 | // check = 1; |
GiJeongKim | 0:51c43836c1d7 | 74 | // //spi |
GiJeongKim | 0:51c43836c1d7 | 75 | // eeprom.write(0xff); |
GiJeongKim | 0:51c43836c1d7 | 76 | // eeprom.write(0xff); |
GiJeongKim | 0:51c43836c1d7 | 77 | //// ready(); |
GiJeongKim | 0:51c43836c1d7 | 78 | //// read(1); |
GiJeongKim | 0:51c43836c1d7 | 79 | // //i2c |
GiJeongKim | 0:51c43836c1d7 | 80 | //// read_field(i2c_slave_addr1); |
GiJeongKim | 0:51c43836c1d7 | 81 | //ADC |
GiJeongKim | 0:51c43836c1d7 | 82 | ADC1->CR2 |= 0x40000000; // adc _ 12bit |
GiJeongKim | 0:51c43836c1d7 | 83 | // a1=ADC1->DR; |
GiJeongKim | 0:51c43836c1d7 | 84 | // a1=ADC2->DR; |
GiJeongKim | 0:51c43836c1d7 | 85 | a1=ADC3->DR; |
GiJeongKim | 0:51c43836c1d7 | 86 | // //계산 |
GiJeongKim | 0:51c43836c1d7 | 87 | // // |
GiJeongKim | 0:51c43836c1d7 | 88 | // // |
GiJeongKim | 0:51c43836c1d7 | 89 | // |
GiJeongKim | 0:51c43836c1d7 | 90 | // //DAC |
GiJeongKim | 0:51c43836c1d7 | 91 | // dac_1 = ADC1->DR; |
GiJeongKim | 0:51c43836c1d7 | 92 | // dac_2 = ADC2->DR; |
GiJeongKim | 0:51c43836c1d7 | 93 | // //pwm |
GiJeongKim | 0:51c43836c1d7 | 94 | TIM4->CCR2 = (PWM_ARR)*(1.0f-dtc_v); |
GiJeongKim | 0:51c43836c1d7 | 95 | TIM4->CCR1 = (PWM_ARR)*(1.0f-dtc_w); |
GiJeongKim | 0:51c43836c1d7 | 96 | // check = 0; |
GiJeongKim | 0:51c43836c1d7 | 97 | } |
GiJeongKim | 0:51c43836c1d7 | 98 | TIM4->SR = 0x0; // reset the status register |
GiJeongKim | 0:51c43836c1d7 | 99 | } |
GiJeongKim | 0:51c43836c1d7 | 100 | |
GiJeongKim | 0:51c43836c1d7 | 101 | |
GiJeongKim | 0:51c43836c1d7 | 102 | |
GiJeongKim | 0:51c43836c1d7 | 103 | int main() |
GiJeongKim | 0:51c43836c1d7 | 104 | { |
GiJeongKim | 0:51c43836c1d7 | 105 | |
GiJeongKim | 0:51c43836c1d7 | 106 | // i2c init |
GiJeongKim | 0:51c43836c1d7 | 107 | // i2c.frequency(400 * 1000); // 0.4 mHz |
GiJeongKim | 0:51c43836c1d7 | 108 | // wait_ms(2); // Power Up wait |
GiJeongKim | 0:51c43836c1d7 | 109 | // look_for_hardware_i2c(); // Hardware present |
GiJeongKim | 0:51c43836c1d7 | 110 | // init_as5510(i2c_slave_addr1); |
GiJeongKim | 0:51c43836c1d7 | 111 | // // spi init |
GiJeongKim | 0:51c43836c1d7 | 112 | eeprom.format(8,3); |
GiJeongKim | 0:51c43836c1d7 | 113 | eeprom.frequency(5000000); //5M |
GiJeongKim | 0:51c43836c1d7 | 114 | enc.format(8,0); |
GiJeongKim | 0:51c43836c1d7 | 115 | enc.frequency(5000000); //5M |
GiJeongKim | 0:51c43836c1d7 | 116 | // ADC init |
GiJeongKim | 0:51c43836c1d7 | 117 | Init_ADC(); |
GiJeongKim | 0:51c43836c1d7 | 118 | // Pwm init |
GiJeongKim | 0:51c43836c1d7 | 119 | Init_PWM(); |
GiJeongKim | 0:51c43836c1d7 | 120 | TIM4->CR1 ^= TIM_CR1_UDIS; |
GiJeongKim | 0:51c43836c1d7 | 121 | // //SPI |
GiJeongKim | 0:51c43836c1d7 | 122 | // spi_eeprom_ready(); |
GiJeongKim | 0:51c43836c1d7 | 123 | // spi_eeprom_write(0x1,0x112); |
GiJeongKim | 0:51c43836c1d7 | 124 | // spi_eeprom_ready(); |
GiJeongKim | 0:51c43836c1d7 | 125 | // int i = spi_eeprom_read(0x1); |
GiJeongKim | 0:51c43836c1d7 | 126 | // CAN |
GiJeongKim | 0:51c43836c1d7 | 127 | can.attach(&onMsgReceived); |
GiJeongKim | 0:51c43836c1d7 | 128 | // spi _ enc |
GiJeongKim | 0:51c43836c1d7 | 129 | spi_enc_set_init(); |
GiJeongKim | 0:51c43836c1d7 | 130 | msg.len=2; |
GiJeongKim | 0:51c43836c1d7 | 131 | while(1) { |
GiJeongKim | 0:51c43836c1d7 | 132 | // dac_1=0.5; |
GiJeongKim | 0:51c43836c1d7 | 133 | // dac_2=0.1; |
GiJeongKim | 0:51c43836c1d7 | 134 | check_2 = 1; |
GiJeongKim | 0:51c43836c1d7 | 135 | //spi _ eeprom |
GiJeongKim | 0:51c43836c1d7 | 136 | // spi_eeprom_ready(); |
GiJeongKim | 0:51c43836c1d7 | 137 | // spi_eeprom_write(0x0001,0xFFFFFFFF); |
GiJeongKim | 0:51c43836c1d7 | 138 | // spi_eeprom_ready(); |
GiJeongKim | 0:51c43836c1d7 | 139 | // int a=spi_eeprom_read(0x0001); |
GiJeongKim | 0:51c43836c1d7 | 140 | //spi _ enc |
GiJeongKim | 0:51c43836c1d7 | 141 | int a = spi_enc_read(); |
GiJeongKim | 0:51c43836c1d7 | 142 | // read_field(i2c_slave_addr1); |
GiJeongKim | 0:51c43836c1d7 | 143 | pc.printf("%d\n",a1); |
GiJeongKim | 0:51c43836c1d7 | 144 | // msg.data[0]=0xFF&a1; |
GiJeongKim | 0:51c43836c1d7 | 145 | // msg.data[1]=0xFF&(a1>>8); |
GiJeongKim | 0:51c43836c1d7 | 146 | // can.write(msg); |
GiJeongKim | 0:51c43836c1d7 | 147 | check_2=0; |
GiJeongKim | 0:51c43836c1d7 | 148 | // wait(0.00005f); |
GiJeongKim | 0:51c43836c1d7 | 149 | } |
GiJeongKim | 0:51c43836c1d7 | 150 | } |