![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
asdgas
Dependencies: mbed Eigen FastPWM
main.cpp@1:e04e563be5ce, 2019-08-19 (annotated)
- Committer:
- jobuuu
- Date:
- Mon Aug 19 08:05:09 2019 +0000
- Revision:
- 1:e04e563be5ce
- Parent:
- 0:51c43836c1d7
- Child:
- 2:a1c0a37df760
HCB
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 | |
jobuuu | 1:e04e563be5ce | 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; |
jobuuu | 1:e04e563be5ce | 47 | float PWM_out; |
GiJeongKim | 0:51c43836c1d7 | 48 | |
GiJeongKim | 0:51c43836c1d7 | 49 | int main() |
GiJeongKim | 0:51c43836c1d7 | 50 | { |
jobuuu | 1:e04e563be5ce | 51 | /******************************************************* |
jobuuu | 1:e04e563be5ce | 52 | *** Initialization |
jobuuu | 1:e04e563be5ce | 53 | ********************************************************/ |
jobuuu | 1:e04e563be5ce | 54 | |
jobuuu | 1:e04e563be5ce | 55 | pc.baud(9600); |
GiJeongKim | 0:51c43836c1d7 | 56 | // i2c init |
GiJeongKim | 0:51c43836c1d7 | 57 | // i2c.frequency(400 * 1000); // 0.4 mHz |
GiJeongKim | 0:51c43836c1d7 | 58 | // wait_ms(2); // Power Up wait |
GiJeongKim | 0:51c43836c1d7 | 59 | // look_for_hardware_i2c(); // Hardware present |
GiJeongKim | 0:51c43836c1d7 | 60 | // init_as5510(i2c_slave_addr1); |
GiJeongKim | 0:51c43836c1d7 | 61 | // // spi init |
GiJeongKim | 0:51c43836c1d7 | 62 | eeprom.format(8,3); |
GiJeongKim | 0:51c43836c1d7 | 63 | eeprom.frequency(5000000); //5M |
GiJeongKim | 0:51c43836c1d7 | 64 | enc.format(8,0); |
GiJeongKim | 0:51c43836c1d7 | 65 | enc.frequency(5000000); //5M |
GiJeongKim | 0:51c43836c1d7 | 66 | // ADC init |
GiJeongKim | 0:51c43836c1d7 | 67 | Init_ADC(); |
GiJeongKim | 0:51c43836c1d7 | 68 | // Pwm init |
GiJeongKim | 0:51c43836c1d7 | 69 | Init_PWM(); |
GiJeongKim | 0:51c43836c1d7 | 70 | TIM4->CR1 ^= TIM_CR1_UDIS; |
GiJeongKim | 0:51c43836c1d7 | 71 | // //SPI |
GiJeongKim | 0:51c43836c1d7 | 72 | // spi_eeprom_ready(); |
GiJeongKim | 0:51c43836c1d7 | 73 | // spi_eeprom_write(0x1,0x112); |
GiJeongKim | 0:51c43836c1d7 | 74 | // spi_eeprom_ready(); |
GiJeongKim | 0:51c43836c1d7 | 75 | // int i = spi_eeprom_read(0x1); |
GiJeongKim | 0:51c43836c1d7 | 76 | // CAN |
GiJeongKim | 0:51c43836c1d7 | 77 | can.attach(&onMsgReceived); |
GiJeongKim | 0:51c43836c1d7 | 78 | // spi _ enc |
GiJeongKim | 0:51c43836c1d7 | 79 | spi_enc_set_init(); |
GiJeongKim | 0:51c43836c1d7 | 80 | msg.len=2; |
jobuuu | 1:e04e563be5ce | 81 | |
jobuuu | 1:e04e563be5ce | 82 | |
jobuuu | 1:e04e563be5ce | 83 | /******************************************************* |
jobuuu | 1:e04e563be5ce | 84 | *** Program is operating! |
jobuuu | 1:e04e563be5ce | 85 | ********************************************************/ |
GiJeongKim | 0:51c43836c1d7 | 86 | while(1) { |
GiJeongKim | 0:51c43836c1d7 | 87 | // dac_1=0.5; |
GiJeongKim | 0:51c43836c1d7 | 88 | // dac_2=0.1; |
GiJeongKim | 0:51c43836c1d7 | 89 | check_2 = 1; |
GiJeongKim | 0:51c43836c1d7 | 90 | //spi _ eeprom |
GiJeongKim | 0:51c43836c1d7 | 91 | // spi_eeprom_ready(); |
GiJeongKim | 0:51c43836c1d7 | 92 | // spi_eeprom_write(0x0001,0xFFFFFFFF); |
GiJeongKim | 0:51c43836c1d7 | 93 | // spi_eeprom_ready(); |
GiJeongKim | 0:51c43836c1d7 | 94 | // int a=spi_eeprom_read(0x0001); |
GiJeongKim | 0:51c43836c1d7 | 95 | //spi _ enc |
GiJeongKim | 0:51c43836c1d7 | 96 | int a = spi_enc_read(); |
GiJeongKim | 0:51c43836c1d7 | 97 | // read_field(i2c_slave_addr1); |
jobuuu | 1:e04e563be5ce | 98 | pc.printf("7 \n"); |
GiJeongKim | 0:51c43836c1d7 | 99 | // msg.data[0]=0xFF&a1; |
GiJeongKim | 0:51c43836c1d7 | 100 | // msg.data[1]=0xFF&(a1>>8); |
GiJeongKim | 0:51c43836c1d7 | 101 | // can.write(msg); |
GiJeongKim | 0:51c43836c1d7 | 102 | check_2=0; |
GiJeongKim | 0:51c43836c1d7 | 103 | // wait(0.00005f); |
GiJeongKim | 0:51c43836c1d7 | 104 | } |
jobuuu | 1:e04e563be5ce | 105 | } |
jobuuu | 1:e04e563be5ce | 106 | |
jobuuu | 1:e04e563be5ce | 107 | unsigned int CNT_TMR4 = 0; |
jobuuu | 1:e04e563be5ce | 108 | extern "C" void TIM4_IRQHandler(void) |
jobuuu | 1:e04e563be5ce | 109 | { |
jobuuu | 1:e04e563be5ce | 110 | if (TIM4->SR & TIM_SR_UIF ) { |
jobuuu | 1:e04e563be5ce | 111 | /******************************************************* |
jobuuu | 1:e04e563be5ce | 112 | *** Sensor Read & Data Handling |
jobuuu | 1:e04e563be5ce | 113 | ********************************************************/ |
jobuuu | 1:e04e563be5ce | 114 | |
jobuuu | 1:e04e563be5ce | 115 | //spi |
jobuuu | 1:e04e563be5ce | 116 | // eeprom.write(0xff); |
jobuuu | 1:e04e563be5ce | 117 | // eeprom.write(0xff); |
jobuuu | 1:e04e563be5ce | 118 | // ready(); |
jobuuu | 1:e04e563be5ce | 119 | // read(1); |
jobuuu | 1:e04e563be5ce | 120 | |
jobuuu | 1:e04e563be5ce | 121 | //i2c |
jobuuu | 1:e04e563be5ce | 122 | //// read_field(i2c_slave_addr1); |
jobuuu | 1:e04e563be5ce | 123 | |
jobuuu | 1:e04e563be5ce | 124 | //ADC |
jobuuu | 1:e04e563be5ce | 125 | ADC1->CR2 |= 0x40000000; // adc _ 12bit |
jobuuu | 1:e04e563be5ce | 126 | // a1=ADC1->DR; |
jobuuu | 1:e04e563be5ce | 127 | // a1=ADC2->DR; |
jobuuu | 1:e04e563be5ce | 128 | a1=ADC3->DR; |
jobuuu | 1:e04e563be5ce | 129 | |
jobuuu | 1:e04e563be5ce | 130 | //DAC |
jobuuu | 1:e04e563be5ce | 131 | // dac_1 = ADC1->DR; |
jobuuu | 1:e04e563be5ce | 132 | // dac_2 = ADC2->DR; |
jobuuu | 1:e04e563be5ce | 133 | |
jobuuu | 1:e04e563be5ce | 134 | /******************************************************* |
jobuuu | 1:e04e563be5ce | 135 | *** Valve Control |
jobuuu | 1:e04e563be5ce | 136 | ********************************************************/ |
jobuuu | 1:e04e563be5ce | 137 | |
jobuuu | 1:e04e563be5ce | 138 | // PWM_out=-0.04683; |
jobuuu | 1:e04e563be5ce | 139 | PWM_out=-0.95; |
jobuuu | 1:e04e563be5ce | 140 | |
jobuuu | 1:e04e563be5ce | 141 | if (PWM_out>0) { |
jobuuu | 1:e04e563be5ce | 142 | dtc_v=0; |
jobuuu | 1:e04e563be5ce | 143 | dtc_w=PWM_out; |
jobuuu | 1:e04e563be5ce | 144 | } else { |
jobuuu | 1:e04e563be5ce | 145 | dtc_v=0; |
jobuuu | 1:e04e563be5ce | 146 | dtc_w=-PWM_out; |
jobuuu | 1:e04e563be5ce | 147 | } |
jobuuu | 1:e04e563be5ce | 148 | |
jobuuu | 1:e04e563be5ce | 149 | //pwm |
jobuuu | 1:e04e563be5ce | 150 | TIM4->CCR2 = (PWM_ARR)*(1.0f-dtc_v); |
jobuuu | 1:e04e563be5ce | 151 | TIM4->CCR1 = (PWM_ARR)*(1.0f-dtc_w); |
jobuuu | 1:e04e563be5ce | 152 | |
jobuuu | 1:e04e563be5ce | 153 | /******************************************************* |
jobuuu | 1:e04e563be5ce | 154 | *** Data Send (CAN) |
jobuuu | 1:e04e563be5ce | 155 | ********************************************************/ |
jobuuu | 1:e04e563be5ce | 156 | |
jobuuu | 1:e04e563be5ce | 157 | |
jobuuu | 1:e04e563be5ce | 158 | |
jobuuu | 1:e04e563be5ce | 159 | |
jobuuu | 1:e04e563be5ce | 160 | /******************************************************* |
jobuuu | 1:e04e563be5ce | 161 | *** Timer Counting & etc. |
jobuuu | 1:e04e563be5ce | 162 | ********************************************************/ |
jobuuu | 1:e04e563be5ce | 163 | if(CNT_TMR4 == 100000) CNT_TMR4 = 0; |
jobuuu | 1:e04e563be5ce | 164 | else CNT_TMR4++; |
jobuuu | 1:e04e563be5ce | 165 | |
jobuuu | 1:e04e563be5ce | 166 | } |
jobuuu | 1:e04e563be5ce | 167 | TIM4->SR = 0x0; // reset the status register |
GiJeongKim | 0:51c43836c1d7 | 168 | } |