![](/media/cache/group/default_image.jpg.50x50_q85.jpg)
06/21/18
Fork of rtos_threading_with_callback by
I2C_write.cpp@4:61f1412310af, 2020-04-02 (annotated)
- Committer:
- CactusSemi
- Date:
- Thu Apr 02 17:44:59 2020 +0000
- Revision:
- 4:61f1412310af
- Parent:
- 3:3e8c16b6620c
Jacaranda Final
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
CactusSemi | 3:3e8c16b6620c | 1 | #include "mbed.h" |
CactusSemi | 3:3e8c16b6620c | 2 | |
CactusSemi | 3:3e8c16b6620c | 3 | extern Serial pc; |
CactusSemi | 3:3e8c16b6620c | 4 | extern int frequ; //unit kHz |
CactusSemi | 3:3e8c16b6620c | 5 | extern char dev_addr; //fixed |
CactusSemi | 3:3e8c16b6620c | 6 | |
CactusSemi | 3:3e8c16b6620c | 7 | char get_half_byte(int *); //defined in I2C_read.cpp |
CactusSemi | 3:3e8c16b6620c | 8 | |
CactusSemi | 3:3e8c16b6620c | 9 | void i2c_write() { |
CactusSemi | 3:3e8c16b6620c | 10 | char reg_ptr = 0x55; |
CactusSemi | 3:3e8c16b6620c | 11 | char reg_data = 0xaa; |
CactusSemi | 3:3e8c16b6620c | 12 | char temp; |
CactusSemi | 3:3e8c16b6620c | 13 | int flag1 = 0; |
CactusSemi | 3:3e8c16b6620c | 14 | I2C i2c(p9,p10); |
CactusSemi | 3:3e8c16b6620c | 15 | LPC_PINCON->PINMODE_OD0 = (LPC_PINCON->PINMODE_OD0 | 0x0003); // To make p9 & P10 open_drain |
CactusSemi | 3:3e8c16b6620c | 16 | //Read Register Pointer |
CactusSemi | 3:3e8c16b6620c | 17 | pc.printf("\n\n\n \r\tYahoooo! Lets program a Register"); |
CactusSemi | 3:3e8c16b6620c | 18 | while (!flag1) { |
CactusSemi | 3:3e8c16b6620c | 19 | pc.printf("\n\n\n \r\tEnter Register Address in hex (0 to f): "); |
CactusSemi | 3:3e8c16b6620c | 20 | reg_ptr = get_half_byte(&flag1); |
CactusSemi | 3:3e8c16b6620c | 21 | if (flag1==0) pc.printf(" \033[%dm Invalid Chracter!! No worries, let us try again \033[%dm",41,40); |
CactusSemi | 3:3e8c16b6620c | 22 | } |
CactusSemi | 3:3e8c16b6620c | 23 | //Read Register Data |
CactusSemi | 3:3e8c16b6620c | 24 | flag1 = 0; |
CactusSemi | 3:3e8c16b6620c | 25 | while (!flag1) { |
CactusSemi | 3:3e8c16b6620c | 26 | pc.printf("\n\n \r\tEnter Data in hex (00 to ff): "); |
CactusSemi | 3:3e8c16b6620c | 27 | reg_data = get_half_byte(&flag1); |
CactusSemi | 3:3e8c16b6620c | 28 | if (flag1 == 1) { |
CactusSemi | 3:3e8c16b6620c | 29 | reg_data = reg_data << 4; |
CactusSemi | 3:3e8c16b6620c | 30 | temp = get_half_byte(&flag1); |
CactusSemi | 3:3e8c16b6620c | 31 | reg_data = (reg_data | temp); |
CactusSemi | 3:3e8c16b6620c | 32 | } |
CactusSemi | 3:3e8c16b6620c | 33 | if (flag1==0) pc.printf(" \033[%dm Invalid Chracter!! No worries, let us try again \033[%dm",41,40); |
CactusSemi | 3:3e8c16b6620c | 34 | } |
CactusSemi | 3:3e8c16b6620c | 35 | |
CactusSemi | 3:3e8c16b6620c | 36 | pc.printf("\n\n\n\r\t\033[%dm", 44);//change backround to blue |
CactusSemi | 3:3e8c16b6620c | 37 | pc.printf(" Device Address (fixed) = 0x%2.2X ; Register Pointer = 0x%2.2X ; Register Data = 0x%2.2X ",dev_addr, reg_ptr, reg_data); |
CactusSemi | 3:3e8c16b6620c | 38 | pc.printf("\033[%dm", 40);//change backround to black |
CactusSemi | 3:3e8c16b6620c | 39 | |
CactusSemi | 3:3e8c16b6620c | 40 | pc.printf("\n\n\n \r\tContinue Write? (y/n) : "); |
CactusSemi | 3:3e8c16b6620c | 41 | temp = 0; |
CactusSemi | 3:3e8c16b6620c | 42 | while ( temp!='y' && temp!='n') { |
CactusSemi | 3:3e8c16b6620c | 43 | temp = pc.getc(); |
CactusSemi | 3:3e8c16b6620c | 44 | } |
CactusSemi | 3:3e8c16b6620c | 45 | if (temp == 'n') { |
CactusSemi | 3:3e8c16b6620c | 46 | pc.printf("\n\n \r\tAborting Register Write :("); |
CactusSemi | 3:3e8c16b6620c | 47 | return; |
CactusSemi | 3:3e8c16b6620c | 48 | } |
CactusSemi | 3:3e8c16b6620c | 49 | i2c.frequency(frequ*1000); |
CactusSemi | 3:3e8c16b6620c | 50 | i2c.stop(); //add a stop after hot-read |
CactusSemi | 3:3e8c16b6620c | 51 | wait_us(10); |
CactusSemi | 3:3e8c16b6620c | 52 | i2c.start(); |
CactusSemi | 3:3e8c16b6620c | 53 | flag1 = i2c.write(dev_addr); |
CactusSemi | 3:3e8c16b6620c | 54 | if (flag1 != 1) pc.printf("\n\n\n \r\tNo Ack for dev addr :("); |
CactusSemi | 3:3e8c16b6620c | 55 | flag1 = i2c.write(reg_ptr); |
CactusSemi | 3:3e8c16b6620c | 56 | if (flag1 != 1) pc.printf("\n\n \r\tNo Ack reg pointer :("); |
CactusSemi | 3:3e8c16b6620c | 57 | flag1 = i2c.write(reg_data); |
CactusSemi | 3:3e8c16b6620c | 58 | if (flag1 != 1) pc.printf("\n\n \r\tNo Ack data :("); |
CactusSemi | 3:3e8c16b6620c | 59 | wait_us(5); |
CactusSemi | 3:3e8c16b6620c | 60 | i2c.stop(); |
CactusSemi | 3:3e8c16b6620c | 61 | pc.printf("\n\n\n \r\tYeahhh! Register Write Complete"); |
CactusSemi | 3:3e8c16b6620c | 62 | if (flag1 != 1) pc.printf("\n\n\n\r\t\033[%dm But the Write operation might not be successful since no 'Ack' was received :( \033[%dm",45,40); |
CactusSemi | 3:3e8c16b6620c | 63 | pc.printf("\n\n\n\r"); |
CactusSemi | 3:3e8c16b6620c | 64 | wait_us(5); |
CactusSemi | 3:3e8c16b6620c | 65 | i2c.~I2C(); |
CactusSemi | 3:3e8c16b6620c | 66 | } |