![](/media/cache/group/default_image.jpg.50x50_q85.jpg)
06/21/18
Fork of rtos_threading_with_callback by
main.cpp@2:c7229eac380b, 2018-05-25 (annotated)
- Committer:
- CactusSemi
- Date:
- Fri May 25 17:11:59 2018 +0000
- Revision:
- 2:c7229eac380b
- Parent:
- 0:d4b2a035ffe3
- Child:
- 3:3e8c16b6620c
05?24/2018 final
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mab5449 | 0:d4b2a035ffe3 | 1 | #include "mbed.h" |
mab5449 | 0:d4b2a035ffe3 | 2 | |
CactusSemi | 2:c7229eac380b | 3 | Serial pc(USBTX, USBRX); |
CactusSemi | 2:c7229eac380b | 4 | |
CactusSemi | 2:c7229eac380b | 5 | |
CactusSemi | 2:c7229eac380b | 6 | const int frequ = 500; //unit kHz |
CactusSemi | 2:c7229eac380b | 7 | const char dev_addr = 0x00; //1u |
mab5449 | 0:d4b2a035ffe3 | 8 | |
CactusSemi | 2:c7229eac380b | 9 | char get_half_byte(int *flag) { |
CactusSemi | 2:c7229eac380b | 10 | char cur_char; |
CactusSemi | 2:c7229eac380b | 11 | cur_char = pc.getc(); |
CactusSemi | 2:c7229eac380b | 12 | if ( cur_char >= '0' && cur_char <= '9' ) { |
CactusSemi | 2:c7229eac380b | 13 | cur_char = cur_char - '0'; |
CactusSemi | 2:c7229eac380b | 14 | *flag = 1; |
CactusSemi | 2:c7229eac380b | 15 | } |
CactusSemi | 2:c7229eac380b | 16 | else if ( cur_char >= 'a' && cur_char <= 'f') { |
CactusSemi | 2:c7229eac380b | 17 | cur_char = cur_char - 'a'+10; |
CactusSemi | 2:c7229eac380b | 18 | *flag = 1; |
CactusSemi | 2:c7229eac380b | 19 | } |
CactusSemi | 2:c7229eac380b | 20 | else if( cur_char >= 'A' && cur_char <= 'F') { |
CactusSemi | 2:c7229eac380b | 21 | cur_char = cur_char - 'A'+10; |
CactusSemi | 2:c7229eac380b | 22 | *flag = 1; |
mab5449 | 0:d4b2a035ffe3 | 23 | } |
CactusSemi | 2:c7229eac380b | 24 | else *flag = 0; |
CactusSemi | 2:c7229eac380b | 25 | return cur_char; |
CactusSemi | 2:c7229eac380b | 26 | } |
mab5449 | 0:d4b2a035ffe3 | 27 | |
CactusSemi | 2:c7229eac380b | 28 | void i2c_write() { |
CactusSemi | 2:c7229eac380b | 29 | char reg_ptr = 0x55; |
CactusSemi | 2:c7229eac380b | 30 | char reg_data = 0xaa; |
CactusSemi | 2:c7229eac380b | 31 | char temp; |
CactusSemi | 2:c7229eac380b | 32 | int flag1 = 0; |
CactusSemi | 2:c7229eac380b | 33 | I2C i2c(p9,p10); |
CactusSemi | 2:c7229eac380b | 34 | //Read Register Pointer |
CactusSemi | 2:c7229eac380b | 35 | pc.printf("\n\n\r----------------------\n\n\rYahoooo! Lets program a Register"); |
CactusSemi | 2:c7229eac380b | 36 | while (!flag1) { |
CactusSemi | 2:c7229eac380b | 37 | pc.printf("\n\n\rEnter Register Address in hex (0 to f): "); |
CactusSemi | 2:c7229eac380b | 38 | reg_ptr = get_half_byte(&flag1); |
CactusSemi | 2:c7229eac380b | 39 | if (flag1==0) pc.printf(" Invalid Chracter!! No worries, let us try again"); |
CactusSemi | 2:c7229eac380b | 40 | } |
CactusSemi | 2:c7229eac380b | 41 | //Read Register Data |
CactusSemi | 2:c7229eac380b | 42 | flag1 = 0; |
CactusSemi | 2:c7229eac380b | 43 | while (!flag1) { |
CactusSemi | 2:c7229eac380b | 44 | pc.printf("\n\n\rEnter Data in hex (00 to ff): "); |
CactusSemi | 2:c7229eac380b | 45 | reg_data = get_half_byte(&flag1); |
CactusSemi | 2:c7229eac380b | 46 | if (flag1 == 1) { |
CactusSemi | 2:c7229eac380b | 47 | reg_data = reg_data << 4; |
CactusSemi | 2:c7229eac380b | 48 | temp = get_half_byte(&flag1); |
CactusSemi | 2:c7229eac380b | 49 | reg_data = (reg_data | temp); |
CactusSemi | 2:c7229eac380b | 50 | } |
CactusSemi | 2:c7229eac380b | 51 | if (flag1==0) pc.printf(" Invalid Chracter!! No worries, let us try again"); |
CactusSemi | 2:c7229eac380b | 52 | } |
CactusSemi | 2:c7229eac380b | 53 | 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); |
CactusSemi | 2:c7229eac380b | 54 | pc.printf("\n\n\rContinue Write? (y/n) : "); |
CactusSemi | 2:c7229eac380b | 55 | temp = 0; |
CactusSemi | 2:c7229eac380b | 56 | while ( temp!='y' && temp!='n') { |
CactusSemi | 2:c7229eac380b | 57 | temp = pc.getc(); |
CactusSemi | 2:c7229eac380b | 58 | } |
CactusSemi | 2:c7229eac380b | 59 | if (temp == 'n') { |
CactusSemi | 2:c7229eac380b | 60 | pc.printf("\n\n\rAborting Register Write :("); |
CactusSemi | 2:c7229eac380b | 61 | return; |
CactusSemi | 2:c7229eac380b | 62 | } |
CactusSemi | 2:c7229eac380b | 63 | i2c.frequency(frequ*100); |
CactusSemi | 2:c7229eac380b | 64 | i2c.stop(); //add a stop after hot-read |
CactusSemi | 2:c7229eac380b | 65 | i2c.start(); |
CactusSemi | 2:c7229eac380b | 66 | flag1 = i2c.write(dev_addr); |
CactusSemi | 2:c7229eac380b | 67 | if (flag1 != 1) pc.printf("\n\n\rNo Ack for dev addr :("); |
CactusSemi | 2:c7229eac380b | 68 | flag1 = i2c.write(reg_ptr); |
CactusSemi | 2:c7229eac380b | 69 | if (flag1 != 1) pc.printf("\n\n\rNo Ack reg pointer :("); |
CactusSemi | 2:c7229eac380b | 70 | flag1 = i2c.write(reg_data); |
CactusSemi | 2:c7229eac380b | 71 | if (flag1 != 1) pc.printf("\n\n\rNo Ack data :("); |
CactusSemi | 2:c7229eac380b | 72 | wait_us(5); |
CactusSemi | 2:c7229eac380b | 73 | i2c.stop(); |
CactusSemi | 2:c7229eac380b | 74 | pc.printf("\n\n\rYeahhh! Register Write Complete\n\n\r---------- "); |
CactusSemi | 2:c7229eac380b | 75 | wait_us(5); |
CactusSemi | 2:c7229eac380b | 76 | i2c.~I2C(); |
CactusSemi | 2:c7229eac380b | 77 | } |
CactusSemi | 2:c7229eac380b | 78 | |
mab5449 | 0:d4b2a035ffe3 | 79 | int main() { |
CactusSemi | 2:c7229eac380b | 80 | char current_char=0; |
CactusSemi | 2:c7229eac380b | 81 | pc.printf("\n\r---------------------------------\n\n\rReady to have fun!!\n\n\rEnter a character- 'w' to perform write operation: "); |
CactusSemi | 2:c7229eac380b | 82 | while(current_char!='w') { |
CactusSemi | 2:c7229eac380b | 83 | current_char=pc.getc(); |
CactusSemi | 2:c7229eac380b | 84 | //pc.putc(current_char); |
CactusSemi | 2:c7229eac380b | 85 | } |
CactusSemi | 2:c7229eac380b | 86 | current_char=0; |
CactusSemi | 2:c7229eac380b | 87 | while(1) { |
CactusSemi | 2:c7229eac380b | 88 | i2c_write(); |
CactusSemi | 2:c7229eac380b | 89 | wait_us(5); |
CactusSemi | 2:c7229eac380b | 90 | } |
mab5449 | 0:d4b2a035ffe3 | 91 | } |