Kim GiJeong
/
cocoa_STM_ver_BIPOLAR
cocoa_STM_ver_BIPOLAR
main.cpp@1:be9b2f6d39c2, 2019-08-20 (annotated)
- Committer:
- GiJeongKim
- Date:
- Tue Aug 20 12:35:26 2019 +0000
- Revision:
- 1:be9b2f6d39c2
- Parent:
- 0:51c43836c1d7
abc;
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 | 1:be9b2f6d39c2 | 13 | DigitalIn a(PB_7); |
GiJeongKim | 1:be9b2f6d39c2 | 14 | DigitalIn b(PB_6); |
GiJeongKim | 1:be9b2f6d39c2 | 15 | DigitalIn c(PA_6); |
GiJeongKim | 1:be9b2f6d39c2 | 16 | DigitalIn d(PC_9); |
GiJeongKim | 0:51c43836c1d7 | 17 | |
GiJeongKim | 0:51c43836c1d7 | 18 | // pwm |
GiJeongKim | 1:be9b2f6d39c2 | 19 | float dtc=0; |
GiJeongKim | 0:51c43836c1d7 | 20 | |
GiJeongKim | 0:51c43836c1d7 | 21 | // I2C |
GiJeongKim | 1:be9b2f6d39c2 | 22 | I2C i2c(PB_3,PB_10); // SDA, SCL (for K22F) |
GiJeongKim | 0:51c43836c1d7 | 23 | const int i2c_slave_addr1 = 0x56; |
GiJeongKim | 0:51c43836c1d7 | 24 | unsigned int value; // 10bit output of reading sensor AS5510 |
GiJeongKim | 0:51c43836c1d7 | 25 | |
GiJeongKim | 0:51c43836c1d7 | 26 | // SPI |
GiJeongKim | 0:51c43836c1d7 | 27 | SPI eeprom(PB_15, PB_14, PB_13); // EEPROM //(SPI_MOSI, SPI_MISO, SPI_SCK); |
GiJeongKim | 0:51c43836c1d7 | 28 | DigitalOut eeprom_cs(PB_12); |
GiJeongKim | 0:51c43836c1d7 | 29 | SPI enc(PC_12,PC_11,PC_10); |
GiJeongKim | 0:51c43836c1d7 | 30 | DigitalOut enc_cs(PD_2); |
GiJeongKim | 0:51c43836c1d7 | 31 | |
GiJeongKim | 0:51c43836c1d7 | 32 | // UART |
GiJeongKim | 0:51c43836c1d7 | 33 | Serial pc(PA_9,PA_10); // _ UART |
GiJeongKim | 0:51c43836c1d7 | 34 | |
GiJeongKim | 0:51c43836c1d7 | 35 | //CAN |
GiJeongKim | 0:51c43836c1d7 | 36 | CAN can(PB_8, PB_9, 1000000); |
GiJeongKim | 0:51c43836c1d7 | 37 | CANMessage msg; |
GiJeongKim | 0:51c43836c1d7 | 38 | void onMsgReceived() |
GiJeongKim | 0:51c43836c1d7 | 39 | { |
GiJeongKim | 0:51c43836c1d7 | 40 | can.read(msg); |
GiJeongKim | 0:51c43836c1d7 | 41 | pc.printf("Message received: %d\n", msg.data[0]); |
GiJeongKim | 0:51c43836c1d7 | 42 | } |
GiJeongKim | 0:51c43836c1d7 | 43 | |
GiJeongKim | 0:51c43836c1d7 | 44 | |
GiJeongKim | 0:51c43836c1d7 | 45 | Timer t; |
GiJeongKim | 0:51c43836c1d7 | 46 | //t.start(); |
GiJeongKim | 0:51c43836c1d7 | 47 | //t.stop(); |
GiJeongKim | 0:51c43836c1d7 | 48 | //pc.printf("The time taken was %f seconds\n",t.read()); |
GiJeongKim | 0:51c43836c1d7 | 49 | int a1; |
GiJeongKim | 0:51c43836c1d7 | 50 | int i=0; |
GiJeongKim | 0:51c43836c1d7 | 51 | float y; |
GiJeongKim | 0:51c43836c1d7 | 52 | |
GiJeongKim | 1:be9b2f6d39c2 | 53 | extern "C" void TIM1_UP_TIM10_IRQHandler(void) |
GiJeongKim | 0:51c43836c1d7 | 54 | { |
GiJeongKim | 1:be9b2f6d39c2 | 55 | if (TIM1->SR & TIM_SR_UIF ) { |
GiJeongKim | 0:51c43836c1d7 | 56 | // y=0.05*sin(i*0.00001); |
GiJeongKim | 0:51c43836c1d7 | 57 | // y= 0.1; |
GiJeongKim | 0:51c43836c1d7 | 58 | // if (i>10000){i=0;} |
GiJeongKim | 0:51c43836c1d7 | 59 | // y=0.01*i/100000; |
GiJeongKim | 0:51c43836c1d7 | 60 | y=-0.04683; |
GiJeongKim | 0:51c43836c1d7 | 61 | // |
GiJeongKim | 0:51c43836c1d7 | 62 | // dtc_w = 0.1; |
GiJeongKim | 0:51c43836c1d7 | 63 | // dtc_v = 1; |
GiJeongKim | 0:51c43836c1d7 | 64 | // |
GiJeongKim | 0:51c43836c1d7 | 65 | |
GiJeongKim | 0:51c43836c1d7 | 66 | //PIN_V dir |
GiJeongKim | 1:be9b2f6d39c2 | 67 | dtc=0.5; |
GiJeongKim | 0:51c43836c1d7 | 68 | i++; |
GiJeongKim | 0:51c43836c1d7 | 69 | //ADC |
GiJeongKim | 0:51c43836c1d7 | 70 | ADC1->CR2 |= 0x40000000; // adc _ 12bit |
GiJeongKim | 0:51c43836c1d7 | 71 | // a1=ADC1->DR; |
GiJeongKim | 0:51c43836c1d7 | 72 | // a1=ADC2->DR; |
GiJeongKim | 0:51c43836c1d7 | 73 | a1=ADC3->DR; |
GiJeongKim | 1:be9b2f6d39c2 | 74 | |
GiJeongKim | 0:51c43836c1d7 | 75 | // //pwm |
GiJeongKim | 1:be9b2f6d39c2 | 76 | TIM1->CCR1 = (PWM_ARR)*(1.0f-dtc); |
GiJeongKim | 0:51c43836c1d7 | 77 | // check = 0; |
GiJeongKim | 0:51c43836c1d7 | 78 | } |
GiJeongKim | 1:be9b2f6d39c2 | 79 | TIM1->SR = 0x0; // reset the status register |
GiJeongKim | 0:51c43836c1d7 | 80 | } |
GiJeongKim | 0:51c43836c1d7 | 81 | |
GiJeongKim | 0:51c43836c1d7 | 82 | |
GiJeongKim | 0:51c43836c1d7 | 83 | |
GiJeongKim | 0:51c43836c1d7 | 84 | int main() |
GiJeongKim | 0:51c43836c1d7 | 85 | { |
GiJeongKim | 0:51c43836c1d7 | 86 | |
GiJeongKim | 0:51c43836c1d7 | 87 | // ADC init |
GiJeongKim | 0:51c43836c1d7 | 88 | Init_ADC(); |
GiJeongKim | 0:51c43836c1d7 | 89 | // Pwm init |
GiJeongKim | 0:51c43836c1d7 | 90 | Init_PWM(); |
GiJeongKim | 1:be9b2f6d39c2 | 91 | TIM1->CR1 ^= TIM_CR1_UDIS; |
GiJeongKim | 0:51c43836c1d7 | 92 | // CAN |
GiJeongKim | 0:51c43836c1d7 | 93 | can.attach(&onMsgReceived); |
GiJeongKim | 0:51c43836c1d7 | 94 | while(1) { |
GiJeongKim | 0:51c43836c1d7 | 95 | pc.printf("%d\n",a1); |
GiJeongKim | 0:51c43836c1d7 | 96 | // msg.data[0]=0xFF&a1; |
GiJeongKim | 0:51c43836c1d7 | 97 | // msg.data[1]=0xFF&(a1>>8); |
GiJeongKim | 0:51c43836c1d7 | 98 | // can.write(msg); |
GiJeongKim | 0:51c43836c1d7 | 99 | check_2=0; |
GiJeongKim | 0:51c43836c1d7 | 100 | // wait(0.00005f); |
GiJeongKim | 0:51c43836c1d7 | 101 | } |
GiJeongKim | 0:51c43836c1d7 | 102 | } |