Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Revision 0:51c43836c1d7, committed 2019-08-14
- Comitter:
- GiJeongKim
- Date:
- Wed Aug 14 08:13:36 2019 +0000
- Child:
- 1:e04e563be5ce
- Commit message:
- cocoa;
Changed in this revision
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/FastPWM.lib Wed Aug 14 08:13:36 2019 +0000 @@ -0,0 +1,1 @@ +https://os.mbed.com/users/Sissors/code/FastPWM/#c0b2265cff9c
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/I2C_AS5510/I2C_AS5510.cpp Wed Aug 14 08:13:36 2019 +0000
@@ -0,0 +1,99 @@
+#include "mbed.h"
+#include "setting.h"
+
+void look_for_hardware_i2c()
+{
+ pc.printf("\r\n\n\n");
+ pc.printf("Note I2C address 0x1C used by FXOS8700CQ 3-axis accelerometer and 3-axis magetometer\r\n");
+ pc.printf("Start hardware search..... \r\n");
+
+ int count = 0;
+ for (int address=12; address<256; address+=2) {
+ if (!i2c.write(address, NULL, 0)) { // 0 returned is OK
+ pc.printf(" - I2C device found at address 0x%02X\n\r", address >>1);
+ count++;
+ }
+ }
+ pc.printf("%d devices found \n\r", count);
+}
+
+void init_as5510(int i2c_address)
+{
+ int i2c_adrs=0;
+ char idata[2];
+ int result=0;
+
+ pc.printf("\r\n");
+ pc.printf("Start AS5510 init.. \r\n");
+
+ i2c_adrs= (i2c_address << 1); // AS5510 Slave address lsb= 0 for write
+
+ //---------- Magnet selection --------------------------------
+ //----0x00= <50mT-----------Strong magnet
+ //----0x01= <25mT
+ //----0x02= <18.7mT
+ //----0x03= <12.5mT---------Weak magnet
+ //-----------------------------------------------------------
+ idata[0]=0x0B; // Register for Sensitivity
+ idata[1]=0x00; // Byte
+ result= i2c.write(i2c_adrs, idata, 2, 0); // Now write_sensitivity
+ if (result != 0) pc.printf("No ACK bit! (09)\n\r");
+
+ //----------- Operation mode selection------------------------
+ idata[0]=0x02; // 0x02 address setup register for operation, speed, polarity
+ idata[1]=0x04; // Normal Operation, Slow mode (1), NORMAL Polarity (0), Power Up (0)
+ result= i2c.write(i2c_adrs, idata, 2, 0); // Now write_operation
+ if (result != 0) pc.printf("No ACK bit! (11)\n\r");
+
+ pc.printf("AS5510 init done\r\n");
+}
+
+
+int offset_comp(int i2c_address)
+{
+ int adrss=0;
+ int oresult=0;
+ char off_data[2];
+ int ocf_done=0;
+
+ // First, now Write pointer to register 0x00----------------------------
+ adrss= (i2c_address << 1); // AS5510 Slave address lsb= 0 for write
+ oresult= i2c.write(adrss, 0x00, 1, 0); // write one byte
+ if (oresult != 0) pc.printf("No ACK bit! (33)\n\r");
+
+ // Second, now Read register 0x00 and 0x01--------------------------------
+ memset(off_data, 0, sizeof(off_data));
+ adrss= (i2c_address << 1) | 0x01; // AS5510 address lsb= 1 for read
+ oresult= i2c.read(adrss, off_data, 2, 0); // read two bytes
+
+ // Now analyse register 0x01 ----------------------------------------------
+ ocf_done= off_data[1] & 0x08; // mask off bits, 1= done
+ if (ocf_done== 0) return(0); // return(0)= compensation process is pending
+ else return(1); // return(1)= compensation process is completed
+}
+
+
+void read_field(int i2c_address)
+{
+ int adr=0;
+ char rx_data[2];
+ int rresult=0;
+ char lsb, msb;
+
+ // First, now Write pointer to register 0x00----------------------------
+ adr= (i2c_address << 1); // AS5510 address lsb= 0 for write
+ rresult= i2c.write(adr, 0x00, 1, 0); // write one byte to register 0x00 for magnetic field strength
+// if (rresult != 0) pc.printf("No ACK bit! (22)\n\r");
+
+ // Second, now Read register 0x00 and 0x01--------------------------------
+ memset(rx_data, 0, sizeof(rx_data));
+ adr= (i2c_address << 1) | 0x01; // AS5510 address lsb= 1 for read
+ rresult= i2c.read(adr, rx_data, 2, 0); // read two bytes
+
+
+ // Now analyse register 0x01 ----------------------------------------------
+ lsb= rx_data[0]; // get LSB
+ msb= rx_data[1]&0x03; // need only 2 low bits og MSB
+ value = ((msb & 0x03)<<8) + lsb;
+// pc.printf("I2C adres= 0x%02X, Magnetic Field => msb= 0x%02X, lsb= 0x%02X, decimal 10-bit value = %u \r\n ", i2c_address, rx_data[0],rx_data[1], value);
+}
\ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/I2C_AS5510/I2C_AS5510.h Wed Aug 14 08:13:36 2019 +0000 @@ -0,0 +1,12 @@ +#ifndef _I2C_AS5510_H_ +#define _I2C_AS5510_H_ + +#include "mbed.h" +#include "setting.h" + +void look_for_hardware_i2c(); +void init_as5510(int i2c_address); +int offset_comp(int i2c_address); +void read_field(int i2c_address); + +#endif
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/INIT_HW/INIT_HW.cpp Wed Aug 14 08:13:36 2019 +0000
@@ -0,0 +1,56 @@
+#include "mbed.h"
+#include "FastPWM.h"
+#include "setting.h"
+
+void Init_ADC(void){
+ // ADC Setup
+ RCC->APB2ENR |= RCC_APB2ENR_ADC3EN; // clock for ADC3
+ RCC->APB2ENR |= RCC_APB2ENR_ADC2EN; // clock for ADC2
+ RCC->APB2ENR |= RCC_APB2ENR_ADC1EN; // clock for ADC1
+
+ RCC->AHB1ENR |= RCC_AHB1ENR_GPIOCEN; // Enable clock for GPIOC
+ RCC->AHB1ENR |= RCC_AHB1ENR_GPIOBEN; // Enable clock for GPIOA
+
+ ADC->CCR = 0x00000016; // Regular simultaneous mode only
+ ADC1->CR2 |= ADC_CR2_ADON;//0x00000001; // ADC1 ON
+ ADC1->SQR3 = 0x0000000E; //channel // use PC_4 as input- ADC1_IN14
+ ADC2->CR2 |= ADC_CR2_ADON;//0x00000001; // ADC2 ON
+ ADC2->SQR3 = 0x00000008; // use PB_0 as input - ADC2_IN8
+ ADC3->CR2 |= ADC_CR2_ADON; // ADC3 ON
+ ADC3->SQR3 = 0x0000000B; // use PC_1, - ADC3_IN11
+ GPIOC->MODER |= 0b1100001100; //each channel // PC_4, PC_1 are analog inputs
+ GPIOB->MODER |= 0x3; // PB_0 as analog input
+
+ ADC1->SMPR1 |= 0x1000; // 15 cycles on CH_14, 0b 001000000000000
+ ADC2->SMPR1 |= 0x1000000; // 15 cycles on CH_8, 2^24 = 16^6
+ ADC3->SMPR2 |= 0b1000; // 15 cycles on CH_11, 0b 001000;
+
+ }
+
+
+
+void Init_PWM(){
+
+ RCC->APB1ENR |= RCC_APB1ENR_TIM4EN; // enable TIM4 clock
+ FastPWM pwm_v(PIN_V);
+ FastPWM pwm_w(PIN_W);
+
+ //ISR Setup
+
+ NVIC_EnableIRQ(TIM4_IRQn); //Enable TIM4 IRQ
+
+ TIM4->DIER |= TIM_DIER_UIE; // enable update interrupt
+ TIM4->CR1 = 0x40; // CMS = 10, interrupt only when counting up // Center-aligned mode
+ TIM4->CR1 |= TIM_CR1_UDIS;
+ TIM4->CR1 |= TIM_CR1_ARPE; // autoreload on,
+ TIM4->RCR |= 0x001; // update event once per up/down count of TIM4
+ TIM4->EGR |= TIM_EGR_UG;
+
+ //PWM Setup
+
+ TIM4->PSC = 0x0; // no prescaler, timer counts up in sync with the peripheral clock
+ TIM4->ARR = PWM_ARR; // set auto reload, 40 khz
+ TIM4->CCER |= ~(TIM_CCER_CC1NP); // Interupt when low side is on.
+ TIM4->CR1 |= TIM_CR1_CEN; // enable TIM4
+
+}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/INIT_HW/INIT_HW.h Wed Aug 14 08:13:36 2019 +0000 @@ -0,0 +1,10 @@ +#ifndef _INIT_HW_H_ +#define _INIT_HW_H_ + +#include "mbed.h" +#include "FastPWM.h" + +void Init_ADC(void); +void Init_PWM(); + +#endif
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/SPI_EEP_ENC/SPI_EEP_ENC.cpp Wed Aug 14 08:13:36 2019 +0000
@@ -0,0 +1,90 @@
+#include "mbed.h"
+#include "setting.h"
+#include "SPI_EEP_ENC.h"
+
+// EEPROM
+void spi_eeprom_ready(void){
+ int temp1, temp2;
+ do{
+ eeprom_cs=0;
+ eeprom.write(0x06); //write enable
+ eeprom_cs=1;
+
+ eeprom_cs=0;
+ temp1 = eeprom.write(0x05);
+ temp2 = eeprom.write(0x00);
+ eeprom_cs=1;
+ temp2=(temp2&(0x03))!= 0x02;
+ } while(temp2); // before writing or reading
+ }
+
+ void spi_eeprom_write(unsigned short int add, unsigned int data){
+ eeprom_cs=0;
+ eeprom.write(0x02);
+ eeprom.write(0xff&(add>>8));
+ eeprom.write(0xff&add);
+ eeprom.write(0xff&data);
+ eeprom.write(0xff&(data>>8));
+ eeprom.write(0xff&(data>>16));
+ eeprom.write(0xff&(data>>24));
+ eeprom_cs=1;
+}
+
+unsigned int spi_eeprom_read(unsigned short int add){
+ eeprom_cs=0;
+ eeprom.write(0x03);
+ eeprom.write(0xff&(add>>8));
+ eeprom.write(0xff&add);
+ int a1 = eeprom.write(0x00);
+ int a2 = eeprom.write(0x00);
+ int a3 = eeprom.write(0x00);
+ int a4 = eeprom.write(0x00);
+ eeprom_cs=1;
+ unsigned int final = (a4<<24)+(a3<<16) + (a2<<8) + a1;
+ return final;
+ }
+
+
+ // ENCODER
+void spi_enc_set_clear(void){
+ unsigned int temp;
+ enc_cs = 0;
+ temp = enc.write(0b00100000);
+ enc_cs = 1;
+}
+
+void spi_enc_set_init(void){
+ unsigned int temp, i, temp1, temp2;
+
+ // write MDR0 -> 0b11 -> x4 quadrature count mode
+ enc_cs = 0;
+ temp = enc.write(0b10001000); // WR + MDR0
+ temp = enc.write(0b00000011); // quadratue mode
+ enc_cs = 1;
+
+ // write MDR1 -> 0b10 -> 2-byte counter mode
+ for(i=0;i<100;i++);
+ enc_cs = 0;
+ temp = enc.write(0b10010000); // WR + MDR1
+ //temp = enc.write(0b00000010); // 2 byte mode
+ temp = enc.write(0b00000000); // 4 byte mode
+ enc_cs = 1;
+
+ // clear
+ spi_enc_set_clear();
+}
+
+
+int spi_enc_read(void){
+ //for(t_i=0;t_i<100;t_i++);
+ unsigned int t_dummy, t_b1, t_b2, t_b3, t_b4, t_i;
+ enc_cs = 0;
+ t_dummy = enc.write(0b01100000); // Read Commend
+ t_b1 = enc.write(0x00); // Dummy data for clock
+ t_b2 = enc.write(0x00); // Dummy data for clock
+ t_b3 = enc.write(0x00); // Dummy data for clock
+ t_b4 = enc.write(0x00); // Dummy data for clock
+ enc_cs = 1;
+
+ return((t_b1<<24) + (t_b2<<16) + (t_b3<<8) + t_b4);
+}
\ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/SPI_EEP_ENC/SPI_EEP_ENC.h Wed Aug 14 08:13:36 2019 +0000 @@ -0,0 +1,15 @@ +#ifndef _SPI_EEP_ENC_H_ +#define _SPI_EEP_ENC_H_ + +#include "mbed.h" + + +void spi_eeprom_ready(void); +void spi_eeprom_write(unsigned short int add, unsigned int data); +unsigned int spi_eeprom_read(unsigned short int add); + +void spi_enc_set_clear(void); +void spi_enc_set_init(void); +int spi_enc_read(void); + +#endif //_SPI_H_ \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp Wed Aug 14 08:13:36 2019 +0000
@@ -0,0 +1,150 @@
+#include "mbed.h"
+#include "FastPWM.h"
+#include "INIT_HW.h"
+#include "SPI_EEP_ENC.h"
+#include "I2C_AS5510.h"
+#include "setting.h"
+
+// dac & check
+DigitalOut check(PC_2);
+DigitalOut check_2(PC_3);
+AnalogOut dac_1(PA_4);
+AnalogOut dac_2(PA_5);
+
+// pwm
+float dtc_v=0;
+float dtc_w=0;
+
+// I2C
+I2C i2c(PC_9,PA_8); // SDA, SCL (for K22F)
+const int i2c_slave_addr1 = 0x56;
+unsigned int value; // 10bit output of reading sensor AS5510
+
+// SPI
+SPI eeprom(PB_15, PB_14, PB_13); // EEPROM //(SPI_MOSI, SPI_MISO, SPI_SCK);
+DigitalOut eeprom_cs(PB_12);
+SPI enc(PC_12,PC_11,PC_10);
+DigitalOut enc_cs(PD_2);
+
+// UART
+Serial pc(PA_9,PA_10); // _ UART
+
+//CAN
+CAN can(PB_8, PB_9, 1000000);
+CANMessage msg;
+void onMsgReceived()
+{
+ can.read(msg);
+ pc.printf("Message received: %d\n", msg.data[0]);
+}
+
+
+Timer t;
+//t.start();
+//t.stop();
+//pc.printf("The time taken was %f seconds\n",t.read());
+int a1;
+int i=0;
+float y;
+
+extern "C" void TIM4_IRQHandler(void)
+{
+ if (TIM4->SR & TIM_SR_UIF ) {
+// y=0.05*sin(i*0.00001);
+// y= 0.1;
+// if (i>10000){i=0;}
+// y=0.01*i/100000;
+ y=-0.04683;
+ //
+// dtc_w = 0.1;
+// dtc_v = 1;
+ //
+
+ //PIN_V dir
+
+ if (y>0) {
+ dtc_v=0;
+ dtc_w=y;
+ } else {
+ dtc_v=0;
+ dtc_w=-y;
+ }
+ i++;
+// check = 1;
+// //spi
+// eeprom.write(0xff);
+// eeprom.write(0xff);
+//// ready();
+//// read(1);
+// //i2c
+//// read_field(i2c_slave_addr1);
+ //ADC
+ ADC1->CR2 |= 0x40000000; // adc _ 12bit
+// a1=ADC1->DR;
+// a1=ADC2->DR;
+ a1=ADC3->DR;
+// //계산
+// //
+// //
+//
+// //DAC
+// dac_1 = ADC1->DR;
+// dac_2 = ADC2->DR;
+// //pwm
+ TIM4->CCR2 = (PWM_ARR)*(1.0f-dtc_v);
+ TIM4->CCR1 = (PWM_ARR)*(1.0f-dtc_w);
+// check = 0;
+ }
+ TIM4->SR = 0x0; // reset the status register
+}
+
+
+
+int main()
+{
+
+ // i2c init
+// i2c.frequency(400 * 1000); // 0.4 mHz
+// wait_ms(2); // Power Up wait
+// look_for_hardware_i2c(); // Hardware present
+// init_as5510(i2c_slave_addr1);
+// // spi init
+ eeprom.format(8,3);
+ eeprom.frequency(5000000); //5M
+ enc.format(8,0);
+ enc.frequency(5000000); //5M
+ // ADC init
+ Init_ADC();
+ // Pwm init
+ Init_PWM();
+ TIM4->CR1 ^= TIM_CR1_UDIS;
+// //SPI
+// spi_eeprom_ready();
+// spi_eeprom_write(0x1,0x112);
+// spi_eeprom_ready();
+// int i = spi_eeprom_read(0x1);
+ // CAN
+ can.attach(&onMsgReceived);
+ // spi _ enc
+ spi_enc_set_init();
+ msg.len=2;
+ while(1) {
+// dac_1=0.5;
+// dac_2=0.1;
+ check_2 = 1;
+ //spi _ eeprom
+// spi_eeprom_ready();
+// spi_eeprom_write(0x0001,0xFFFFFFFF);
+// spi_eeprom_ready();
+// int a=spi_eeprom_read(0x0001);
+ //spi _ enc
+ int a = spi_enc_read();
+// read_field(i2c_slave_addr1);
+ pc.printf("%d\n",a1);
+// msg.data[0]=0xFF&a1;
+// msg.data[1]=0xFF&(a1>>8);
+// can.write(msg);
+ check_2=0;
+// wait(0.00005f);
+ }
+}
\ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed.bld Wed Aug 14 08:13:36 2019 +0000 @@ -0,0 +1,1 @@ +https://os.mbed.com/users/mbed_official/code/mbed/builds/65be27845400 \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/setting.h Wed Aug 14 08:13:36 2019 +0000 @@ -0,0 +1,29 @@ +#include "mbed.h" +#include "FastPWM.h" + +extern DigitalOut check; +extern DigitalOut check_2; +extern AnalogOut dac_1; +extern AnalogOut dac_2; + +// pwm +#define PIN_V PB_7 +#define PIN_W PB_6 +//#define PWM_ARR 0x465 // loop 80k, pwm 40k +#define PWM_ARR 0x8CA // loop 40k, pwm 20k +extern float dtc_v; +extern float dtc_w; + +// I2C +extern I2C i2c; // SDA, SCL (for K22F) +extern const int i2c_slave_addr1; +extern unsigned int value; // 10bit output of reading sensor AS5510 + +// SPI +extern SPI eeprom; //(SPI_MOSI, SPI_MISO, SPI_SCK); +extern DigitalOut eeprom_cs; +extern SPI enc; +extern DigitalOut enc_cs; + +// UART +extern Serial pc; //Serial pc(PA_9,PA_10); _ UART \ No newline at end of file