473,666 Members | 2,065 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

C-code with I2C API implementation

1 New Member
hallo all,I need help and any tips shall be appreciated!
i want to implement a I2C API(from Beck SC12 microcontroller ) in a 1 wire search C-code(use to search devices on 1 wire bus) sothat that i can use Beck SC12 microcontroller to perform a 1 wire search command.
I tried to implement this I2C API in the search code but my programm this not workout because my programming power is weak

1) see communication diagram on page one under:
http://pdfserv.maxim-ic.com/en/an/AN3684.pdf or search AN3684 in the internet
2) see the 1 wire search C-code from page 12 to 16 under: http://pdfserv.maxim-ic.com/en/an/AN3684.pdf or search AN3684 in the internet.
3)see below for the I2C API decription and my failed 1 wire search program.
further explanation for the I2C API is found under:www.goblack.de.

Expand|Select|Wrap|Line Numbers
  1.  
  2. /****
  3. I2C API DEFINITIONS*************************************** ******************************/
  4.  
  5. void I2C_init (void);
  6. void I2C_release (void);
  7. void I2C_restart (void);
  8. unsigned char I2C_scan (unsigned char start_addr, unsigned char end_addr);
  9. int I2C_transmit_block (unsigned char slave, char far * buffer, int length);
  10. int I2C_receive_block (unsigned char slave, char far * buffer, int length);
  11. int I2C_transmit_char (unsigned char slave, char c);
  12. int I2C_receive_char (unsigned char slave, char * c, unsigned char lastchar);
  13. void I2C_select_clock_pin(unsigned char pio_no);
  14. void I2C_select_data_pin(unsigned char pio_no);
  15.  
  16. /************************************************** ***********************/
  17. //#endif /* __I2CAPI_H__*/
  18.  
  19.  
  20. /************************************************** **************************/
  21. //includes
  22. //I2C definitions
  23. /************************************************** **************************/
  24. #include <stdio.h>
  25. #include <stdlib.h>
  26. #include <dos.h>
  27. #include "declarations.h"
  28. #include "1wire.h"
  29.  
  30.  
  31.  
  32. /************************************************** ***********************/
  33. //Init the I2C Interface
  34. //I2C bus initialisation
  35. /************************************************** ***********************/
  36. uchar program = 0; // program-variable
  37. uchar ROM[8];
  38. uchar lastDevice=0;
  39.  
  40. uchar LastDeviceFlag, LastFamilyDiscrepancy;
  41. int LastDiscrepancy;
  42. uchar deviceAddress;
  43.  
  44. uchar resetSearch;
  45. uchar diir,firsstBit,seccondBit;
  46. //I2C bus initialisation
  47.  
  48. void I2C_init (void)
  49. {
  50. union REGS inregs;
  51. union REGS outregs;
  52.  
  53.  
  54. inregs.h.ah = I2C_INIT;
  55.  
  56. int86(I2CINT,&inregs,&outregs);
  57.  
  58. }
  59.  
  60. /************************************************** ***********************/
  61. //Release I2C bus
  62. //I2C bus sends stop condition
  63. /************************************************** ***********************/
  64. void I2C_release (void) //
  65. {
  66. union REGS inregs;
  67. union REGS outregs;
  68.  
  69.  
  70. inregs.h.ah = I2C_RELEASE;
  71. inregs.h.al = 0;
  72. int86(I2CINT,&inregs,&outregs);
  73.  
  74. }
  75.  
  76. /************************************************** ***********************/
  77. //Restart I2C
  78. //I2C bus sends start condition
  79. /************************************************** ***********************/
  80. void I2C_restart (void)
  81. {
  82. union REGS inregs;
  83. union REGS outregs;
  84.  
  85. inregs.h.ah = I2C_RESTART;
  86. inregs.h.al = 0;
  87. int86(I2CINT,&inregs,&outregs);
  88.  
  89. }
  90.  
  91.  
  92. /************************************************** ***********************/
  93. //Transmit Block
  94. // I2C bus transmits a number of bytes length from the buffer to DS2482
  95. //"slave" is the i2c adress of DS2482 ,7bits + 1bit(R/W)
  96. //transmission ends up with stop condition
  97. /************************************************** ***********************/
  98. int I2C_transmit_block (unsigned char slave, char far * buffer, int length)
  99. {
  100. union REGS inregs;
  101. union REGS outregs;
  102. struct SREGS sregs;
  103.  
  104. inregs.h.ah = I2C_TRANS_RECV_BLOCK;
  105. inregs.h.al = slave & 0xFE;
  106. inregs.x.cx = length;
  107. sregs.es = FP_SEG(buffer);
  108. inregs.x.bx = FP_OFF(buffer);
  109. int86x(I2CINT,&inregs,&outregs,&sregs);
  110.  
  111. if (outregs.x.flags & 0x01)
  112. {
  113. return outregs.h.al;
  114. }
  115.  
  116. return 0;
  117.  
  118. }
  119.  
  120. /************************************************** ***********************/
  121. //Receive Block
  122. //I2C bus recieve byte(s) sequentially
  123. //this is oppsite of transmit_block above!
  124. /************************************************** ***********************/
  125. int I2C_receive_block (unsigned char slave, char far * buffer, int length)
  126. {
  127. union REGS inregs;
  128. union REGS outregs;
  129. struct SREGS sregs;
  130.  
  131.  
  132. inregs.h.ah = I2C_TRANS_RECV_BLOCK;
  133. inregs.h.al = slave | 0x01;
  134. inregs.x.cx = length;
  135. sregs.es = FP_SEG(buffer);
  136. inregs.x.bx = FP_OFF(buffer);
  137. int86x(I2CINT,&inregs,&outregs,&sregs);
  138.  
  139. if (outregs.x.flags & 0x01)
  140. {
  141. return outregs.h.al;
  142. }
  143.  
  144. return 0;
  145.  
  146. }
  147.  
  148. /************************************************** ***********************/
  149. //Transmit one character
  150. //I2C bus transmits a byte from µC to DS2482
  151. //"slave" is DS2482 adress 7bits + 1 bit(R/W)
  152. /************************************************** ***********************/
  153. int I2C_transmit_char (unsigned char slave, char c)
  154. {
  155. union REGS inregs;
  156. union REGS outregs;
  157.  
  158. inregs.h.ah = I2C_TRANS_RECV_CHAR;
  159. inregs.h.al = slave & 0xFE;
  160. inregs.h.cl = c;
  161. int86(I2CINT,&inregs,&outregs);
  162.  
  163. if (outregs.x.flags & 0x01)
  164. {
  165. return (int)outregs.h.al & 0x00FF;
  166. }
  167.  
  168. return 0;
  169. }
  170.  
  171. /************************************************** ***********************/
  172. //Receive one Character
  173. //I2C recieve byte(s) from DS2482
  174. /************************************************** ***********************/
  175. int I2C_receive_char (unsigned char slave, char * c, unsigned char lastchar)
  176. {
  177. union REGS inregs;
  178. union REGS outregs;
  179.  
  180. inregs.h.ah = I2C_TRANS_RECV_CHAR;
  181. inregs.h.al = slave | 0x01;
  182. if (lastchar)
  183. {
  184. inregs.h.cl = 1; //want more characters
  185. }
  186. else
  187. {
  188. inregs.h.cl = 0; //only one character
  189. }
  190. int86(I2CINT,&inregs,&outregs);
  191.  
  192. if (outregs.x.flags & 0x01)
  193. {
  194. *c = 0;
  195. return (int)outregs.h.al & 0x00FF;
  196. }
  197.  
  198. *c = (char)outregs.h.ch;
  199. return 0;
  200. }
  201.  
  202. /************************************************** ***********************/
  203. //Select I2C Clock Pin from the µC
  204. /************************************************** ***********************/
  205. void I2C_select_clock_pin(unsigned char pio_no)
  206. {
  207. union REGS inregs;
  208. union REGS outregs;
  209.  
  210. inregs.h.ah = I2C_SELECT_CLK_PIN;
  211. inregs.h.al = pio_no;
  212. int86(I2CINT,&inregs,&outregs);
  213. }
  214.  
  215. /************************************************** ***********************/
  216. //Select I2C data Pin from the µC
  217. /************************************************** ***********************/
  218. void I2C_select_data_pin(unsigned char pio_no)
  219. {
  220. union REGS inregs;
  221. union REGS outregs;
  222.  
  223. inregs.h.ah = I2C_SELECT_DATA_PIN;
  224. inregs.h.al = pio_no;
  225. int86(I2CINT,&inregs,&outregs);
  226. }
  227.  
  228. void writeByte(unsigned char slave,unsigned char data)
  229. {
  230. I2C_init();
  231. I2C_transmit_char(slave,data);
  232. I2C_release();
  233. }
  234.  
  235.  
  236.  
  237. void readbyte();
  238. {
  239. }
  240.  
  241. void onewreset1()
  242. {
  243. I2C_init();
  244. I2C_transmit_char( 0x30, 0xB4);// A5h 1w-wire reset byte
  245. I2C_release(); // transmission stop condition
  246.  
  247.  
  248. }
  249.  
  250.  
  251. uchar onewreset()
  252. {
  253.  
  254. I2C_init();
  255. I2C_transmit_char( 0x30, 0xB4); // 1-wire reset byte
  256. I2C_release(); // transmission stop condition
  257. return SUCCESS;
  258. }
  259.  
  260.  
  261. void Dreset()
  262. {
  263.  
  264. I2C_init();
  265. I2C_transmit_char( 0x30,0xF0);// 1-wire device reset byte
  266. I2C_release(); // transmission stop condition
  267. }
  268.  
  269.  
  270. uchar OWFirst( uchar resetSearch,uchar lastDevice, uchar deviceAddress)
  271. {
  272. LastDiscrepancy=0;
  273. deviceAddress=0;
  274. OWSearch1( resetSearch, &lastDevice, &deviceAddress);
  275. }
  276.  
  277.  
  278. uchar OWNext(uchar resetSearch, uchar lastdevice, uchar deviceAddress) 
  279. {
  280. OWSearch1(resetSearch, &lastdevice, &deviceAddress);
  281.  
  282. }
  283.  
  284.  
  285.  
  286.  
  287. // OWSearch
  288.  
  289.  
  290. //
  291. uchar OWSearch1(uchar resetSearch, uchar *lastDevice, uchar *deviceAddress)
  292. {
  293. uchar bit_number,last_zero,serial_byte_number,serial_byt e_mask;
  294. uchar firstBit,secondBit,dir,i, buff[3];
  295. //initialisation for search
  296. uchar retVal = FAILURE;
  297. bit_number = 1;
  298. last_zero = 0;
  299. serial_byte_number = 0;
  300. serial_byte_mask = 1;
  301. firstBit, secondBit, dir;
  302. i = 0;
  303. deviceAddress = (unsigned char *)malloc(8*sizeof(char));
  304. I2C_init();
  305. I2C_transmit_char(0x30, 0xF0);// perform a global reset of device
  306. I2C_restart();
  307. I2C_transmit_block( 0x30, buff,0);
  308. buff[2]=I2C_receive_char(0x31,buff, 0);
  309. I2C_release();
  310.  
  311. // if the last call was not the last one
  312. if (!(*lastDevice))
  313. {
  314. // reset the 1-wire
  315. // if there are no parts on 1-wire, return FALSE
  316. if(!onewreset())
  317. {
  318. // reset the search
  319. *lastDevice = 0;
  320. LastDiscrepancy = 0;
  321. return FAILURE;
  322. }
  323.  
  324. // Issue the Search ROM command
  325.  
  326. writeByte(0x30,0xF0);
  327. I2C_restart();
  328. writeByte1(0x30,0xE1,0xF0); //slave adress+1w set pointer+save in read in status register
  329.  
  330. // loop to do the search
  331. do
  332. {
  333. if ( bit_number < LastDiscrepancy )
  334. {
  335. if( ROM[serial_byte_number] & serial_byte_mask )
  336. {
  337. dir = 1;
  338. }
  339. else
  340. {
  341. dir = 0;
  342. }
  343. }
  344. else
  345. {
  346. // if equal to last pick 1, if not then pick 0
  347.  
  348.  
  349. if(bit_number == LastDiscrepancy)
  350. {
  351.  
  352.  
  353. dir =1;
  354.  
  355. if(bit_number > LastDiscrepancy)
  356. {
  357.  
  358. dir =0;
  359.  
  360. }
  361.  
  362.  
  363.  
  364.  
  365. if(!owTriplet(&dir, &firstBit, &secondBit)) // genrate 3 time slotes..2 read slotes and 1 write slot
  366. {
  367. return FAILURE; 
  368. }
  369.  
  370.  
  371.  
  372. // if 0 was picked then record its position in LastZero
  373. if ( firstBit == 0 && secondBit == 0 && dir == 0 )
  374. {
  375. last_zero = bit_number;
  376.  
  377. }
  378.  
  379.  
  380. // check for no devices on 1-wire
  381. if ( firstBit == 1 && secondBit == 1 )
  382. break;
  383.  
  384. ****i deleted part of my program because of limited space to post message here!!
  385.  
  386.  
  387. }
  388.  
  389. return retVal;
  390.  
  391. }
  392.  
  393. // owTriplet
  394.  
  395. //
  396. // returns SUCCES or FAILURE
  397. //
  398. uchar owTriplet(uchar *diir, uchar *firsstBit, uchar *seccondBit)
  399.  
  400.  
small space here!!!refer to AN3684 in the internet,page 13 and 14!




once more i will be greatful for any help.
paul
Mar 10 '07 #1
0 5841

Sign in to post your reply or Sign up for a free account.

Similar topics

3
1805
by: jenniferyiu | last post by:
IMHO, simply NO. False actually, practically.
9
4637
by: Anon Email | last post by:
Hi people, I'm learning about header files in C++. The following is code from Bartosz Milewski: // Code const int maxStack = 16; class IStack
29
2240
by: Enrico `Trippo' Porreca | last post by:
Both K&R book and Steve Summit's tutorial define a getline() function correctly testing the return value of getchar() against EOF. I know that getchar() returns EOF or the character value cast to unsigned char. Since char may be signed (and if so, the return value of getchar() would be outside its range), doesn't the commented line in the following code produce implementation-defined behaviour?
52
3749
by: lovecreatesbeauty | last post by:
Why the C standard committee doesn't provide a standard implementation including the C compiler and library when the language standard document is published? C works on the abstract model of low level machine. C stands for portability and platform and machine independent. If the C compiler and C standard library are written in C itself, is it possible that one "standard" C compiler plus library is enough? The standard implementation is...
20
6073
by: Luc Kumps | last post by:
(Sorry about the previous post, it got transmitted before it was complete) We try to separate implementation and interface defintions, but we run into a problem. I hope the guru's can solve this, as we seem to lack only a single 'step' to have "full separation"... We have a first project, namespace Ninterface, that contains the interface definitions in class1_interface.cs, like this: namespace Ninterface { public interface IClass1{
7
2060
by: desktop | last post by:
I the C++ standard page 472 it says that an associative container can be constructed like X(i,j,c) where i and j are input iterators to elements. But in the implementation there is no constructor that matches this requirement, the only constructors are: public: // allocation/deallocation _Rb_tree() { }
6
3954
by: Ralph | last post by:
Hi, I was reading effictive C++ and some other books again and they all tell you about hiding implementation details (proxy/pimpl/inheritance) but they never really explain when to use it. I am starting on a new project which is part library so I think it would be good to hide the implementation for the public classes in the library but this seems a lot of overhead to me (both when developing and runtime overhead).
0
1984
by: anto.anish | last post by:
Hi , Since, i did not want to write instantiations in Source file of all template methods for various different datatypes that my client might use, i choose to write implementation of template methods along with their declarations in the header file. Well, there are also other files in the project, which include this header file as well, which all gets compiled, linked and tested well. #ifndef __ATT_H__
1
2770
by: anto.anish | last post by:
Hi , Since, i did not want to write explicit instantiations in Source file of all template methods for various different datatypes that my client might use, i choose to write implementation of template methods along with their declarations in the header file. Well, there are also other files in the project, which include this header file as well, which all gets compiled, linked and tested well. #ifndef __ATT_H__
173
13884
by: Ron Ford | last post by:
I'm looking for a freeware c99 compiler for windows. I had intended to use MS's Visual C++ Express and use its C capability. In the past with my MS products, I've simply needed to make .c the filetype to invoke the C compiler. Here's a link http://www.microsoft.com/express/download/#webInstall The download is 2.6 megs, which is near a reasonable size for a compiler, but then setup.exe wants to download 87 megs of dot net framework...
0
8356
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8869
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8551
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7386
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5664
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4368
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2771
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2011
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1775
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.