![](/media/cache/group/default_image.jpg.50x50_q85.jpg)
06/21/18
Fork of rtos_threading_with_callback by
main.cpp
- Committer:
- CactusSemi
- Date:
- 2018-05-25
- Revision:
- 2:c7229eac380b
- Parent:
- 0:d4b2a035ffe3
- Child:
- 3:3e8c16b6620c
File content as of revision 2:c7229eac380b:
#include "mbed.h" Serial pc(USBTX, USBRX); const int frequ = 500; //unit kHz const char dev_addr = 0x00; //1u char get_half_byte(int *flag) { char cur_char; cur_char = pc.getc(); if ( cur_char >= '0' && cur_char <= '9' ) { cur_char = cur_char - '0'; *flag = 1; } else if ( cur_char >= 'a' && cur_char <= 'f') { cur_char = cur_char - 'a'+10; *flag = 1; } else if( cur_char >= 'A' && cur_char <= 'F') { cur_char = cur_char - 'A'+10; *flag = 1; } else *flag = 0; return cur_char; } void i2c_write() { char reg_ptr = 0x55; char reg_data = 0xaa; char temp; int flag1 = 0; I2C i2c(p9,p10); //Read Register Pointer pc.printf("\n\n\r----------------------\n\n\rYahoooo! Lets program a Register"); while (!flag1) { pc.printf("\n\n\rEnter Register Address in hex (0 to f): "); reg_ptr = get_half_byte(&flag1); if (flag1==0) pc.printf(" Invalid Chracter!! No worries, let us try again"); } //Read Register Data flag1 = 0; while (!flag1) { pc.printf("\n\n\rEnter Data in hex (00 to ff): "); reg_data = get_half_byte(&flag1); if (flag1 == 1) { reg_data = reg_data << 4; temp = get_half_byte(&flag1); reg_data = (reg_data | temp); } if (flag1==0) pc.printf(" Invalid Chracter!! No worries, let us try again"); } pc.printf("\n\n\rDevice Address (fixed) = 0x%2.2X ; Register Pointer = 0x%2.2X ; Register Data = 0x%2.2X ",dev_addr, reg_ptr, reg_data); pc.printf("\n\n\rContinue Write? (y/n) : "); temp = 0; while ( temp!='y' && temp!='n') { temp = pc.getc(); } if (temp == 'n') { pc.printf("\n\n\rAborting Register Write :("); return; } i2c.frequency(frequ*100); i2c.stop(); //add a stop after hot-read i2c.start(); flag1 = i2c.write(dev_addr); if (flag1 != 1) pc.printf("\n\n\rNo Ack for dev addr :("); flag1 = i2c.write(reg_ptr); if (flag1 != 1) pc.printf("\n\n\rNo Ack reg pointer :("); flag1 = i2c.write(reg_data); if (flag1 != 1) pc.printf("\n\n\rNo Ack data :("); wait_us(5); i2c.stop(); pc.printf("\n\n\rYeahhh! Register Write Complete\n\n\r---------- "); wait_us(5); i2c.~I2C(); } int main() { char current_char=0; pc.printf("\n\r---------------------------------\n\n\rReady to have fun!!\n\n\rEnter a character- 'w' to perform write operation: "); while(current_char!='w') { current_char=pc.getc(); //pc.putc(current_char); } current_char=0; while(1) { i2c_write(); wait_us(5); } }