(adc.c- ADC 0808/0809 C Library)
Line 40: Line 40:
 
= adc.c- ADC 0808/0809 C Library=
 
= adc.c- ADC 0808/0809 C Library=
  
<syntaxhighlight lang="c">
+
<syntaxhighlight>
 
                                        
 
                                        
 
/*----------------------------------------------------------------------------------
 
/*----------------------------------------------------------------------------------

Revision as of 15:06, 22 October 2013


Introduction

This library can be used for anyone starting with 8051 family of microcontrollers. The code has been tested on Ultra_X51 development board by xplore labz. The schematic can be downloaded from product page. If you're testing it on any other platform please refer to its schematic.

Overview: This manual is designed to help embedded programmers and students, rapidly exploit the 8051-Controller for embedded applications. This manual has been targeted at embedded systems programmers and Students who have basic knowledge of 8051 architecture and C-Language.

This manual provides the reference to all the library functions which are grouped under respective .c file.

  1. The .c files convention is as per the peripherals. The peripherals (lcd, keypad..) are connected to default PORTs which can be connected to required PORTs by changing the #defines and sbit.

Reference:

It is recommended to go through the below reference documents and datasheets before interfacing any peripherals.

  1. .The 8051 Microcontroller and Embedded Systems by Muhammad Ali Mazidi.
  2. .The 8051 microcontroller by Kenneth J Ayela.
  3. .Embedded C by Michael J Pont .
  4. .Any of the 16x2 lcd datasheet.
  5. .ADC0808/ADC0809 datasheet from National Semiconductors.
  6. .RTC-DS1307 from Dallas Semiconductors.
  7. .Serial Eeprom AT24C04/02 from Atmel.


Feedback:

Suggestions for additions and improvements in code and documentation are always welcome. Please send your feedback via e-mail to feedback@xplorelabz.com To keep the libraries clean, only administrators are authorized to edit them. If you wish to contribute drop in a email with code changes for review.

Disclaimer:

The libraries have been tested for P89v51rd2/AT89s52 on different development boards. We strongly believe that the library works on any 8051 boards running at 11.0592 MHz. However, Xplore Labz disclaims any kind of hardware failure resulting out of usage of libraries, directly or indirectly. Documentation may be subject to change without prior notice.

The usage of tools and software demonstrated in the document are for educational purpose only, all rights pertaining to these belong to the respective owners. Users must ensure license terms are adhered to, for any use of the demonstrated software.

GNU GENERAL PUBLIC LICENSE: The library code in this document is licensed under GNU General Public License (GPL) Copyright (C) 2012.

  1. Everyone is permitted to copy and distribute verbatim copies of this license document, but changing it is not allowed.

Since the library is licensed free of charge, there is no warranty for the libraries and the entire risk of the quality and performance is with the user.

Errors and omissions should be reported to feedback@xplorelabz.com


adc.c- ADC 0808/0809 C Library

  1.  
  2. /*----------------------------------------------------------------------------------
  3. 8051 ADC0808/9 library.
  4.  
  5. Filename: adc.c
  6. Controller: P89V51RD2/89c51(8051 family)
  7. Oscillator: 11.0592 MHz
  8. Author: XploreLabz
  9. website: www.xplorelabz.com
  10.  
  11. Note:
  12. 1.The Databus and Control lines are connected as below
  13. 2.The code can be modified to connect the ADC to any of the PORTs by changing the
  14. "#define and sbit".
  15.  
  16.  
  17.  
  18. Databus ---> P1
  19. adc_A ---> P3.0
  20. adc_B ---> P3.1
  21. adc_C ---> P3.2
  22. adc_ALE ---> P3.3
  23. adc_Start---> P3.4
  24. adc_EOC ---> P3.5
  25. adc_OE ---> P3.6
  26.  
  27. ----------------------------------------------------------------------------------*/
  28.  
  29. #include<reg51.h>
  30. #include"delay.h"
  31.  
  32. #define adc_databus P1
  33.  
  34. sbit adc_A = P3^0;
  35. sbit adc_B = P3^1;
  36. sbit adc_C = P3^2;
  37. sbit adc_ALE = P3^3;
  38. sbit adc_Start= P3^4;
  39. sbit adc_EOC = P3^5;
  40. sbit adc_OE = P3^6;
  41.  
  42.  
  43.  
  44. /*----------------------------------------------------------------------------------
  45. void ADC_Init()
  46. -----------------------------------------------------------------------------------
  47. * I/P Arguments: none.
  48. * Return value : none
  49.  
  50. * description :This function initializes the ADC control lines and databus..
  51. -----------------------------------------------------------------------------------*/
  52. void ADC_Init()
  53. {
  54. adc_Start=0; //Initialize all the control lines to zero.
  55. adc_ALE=0;
  56. adc_OE=0;
  57. adc_EOC=1; //Configure the EOC pin as I/P
  58. adc_databus=0xff; //configure adc_databus as input
  59. }
  60.  
  61.  
  62.  
  63.  
  64. /*----------------------------------------------------------------------------------
  65. unsignaed char ADC_StartConversion(char channel)
  66. -----------------------------------------------------------------------------------
  67. * I/P Arguments: char(channel number).
  68. * Return value : char(8 bit ADC result)
  69.  
  70. * description  :This function does the ADC conversioin for the Selected Channel
  71. and returns the converted 8bit result.
  72.  
  73. -----------------------------------------------------------------------------------*/
  74. unsigned char ADC_StartConversion(char channel)
  75. {
  76. unsigned char adc_result;
  77.  
  78. adc_A=((channel>>0) & 0x01); //Selectthe channel
  79. adc_B=((channel>>1) & 0x01); //for which the conversion needs to be done
  80. adc_C=((channel>>2) & 0x01);
  81.  
  82. adc_ALE=1; // Latch the address by making the ALE high.
  83. delay_us(50);
  84. adc_Start=1; //Start the conversion after latching the channel address
  85. delay_us(25);
  86.  
  87. adc_ALE=0; //Pull ALE line to zero after starting the conversion.
  88. delay_us(50);
  89. adc_Start=0; //Pull Start line to zero after starting the conversion.
  90.  
  91. while(adc_EOC==0); // Wait till the ADC conversion is completed,
  92. // EOC will be pulled to HIGH by the hardware(ADC0809)
  93. // once conversion is completed.
  94.  
  95. adc_OE=1; //Make the Output Enable high
  96. //to bring the ADC data to port pins
  97. delay_us(25);
  98. adc_result=adc_databus; //Read the ADC data from ADC bus
  99. adc_OE=0; //After reading the data, disable th ADC output line.
  100.  
  101. return(adc_result) ;
  102.  
  103. }

Delay.c: 8051 delay C-Library

  1.  
  2.  
  3. /*----------------------------------------------------------------------------------
  4. 8051 delay library with the crystal frequency 11.0592MHz
  5. Filename: delay.c
  6. Controller: P89V51RD2/89c51(8051 family)
  7. Oscillator: 11.0592 MHz
  8. Author: XploreLabz
  9. website: www.xplorelabz.com
  10. ----------------------------------------------------------------------------------*/
  11.  
  12.  
  13. #include<reg51.h>
  14.  
  15.  
  16.  
  17.  
  18. /*----------------------------------------------------------------------------------
  19. void delay_us(unsigned int n)
  20. ----------------------------------------------------------------------------------
  21. * I/P Arguments: unsigned int.
  22. * Return value : none
  23.  
  24. * description : This function is used generate delay in us.
  25. It genarates a approximate delay of 10us for each count,
  26. if 5000 is passed as the argument then it generates a delay of apprx 50ms.
  27.  
  28. -----------------------------------------------------------------------------------*/
  29. void delay_us(unsigned int us_count)
  30. {
  31. while(us_count!=0)
  32. {
  33. us_count--;
  34. }
  35. }
  36.  
  37.  
  38.  
  39.  
  40.  
  41. /*----------------------------------------------------------------------------------
  42. void delay_ms(unsigned int n)
  43. -----------------------------------------------------------------------------------
  44. * I/P Arguments: unsigned int.
  45. * Return value : none
  46.  
  47. * description: This function is used generate delay in ms.
  48. It genarates a approximate delay of 1ms for each count,
  49. if 1000 is passed as the argument then it generates delay of apprx 1000ms(1sec)
  50. -----------------------------------------------------------------------------------*/
  51. void delay_ms(unsigned int ms_count)
  52. {
  53. while(ms_count!=0)
  54. {
  55. delay_us(112); //delay_us is called to generate 1ms delay
  56. ms_count--;
  57. }
  58. }
  59.  
  60.  
  61.  
  62.  
  63.  
  64.  
  65.  
  66. /*----------------------------------------------------------------------------------
  67. void delay_sec(unsigned char sec_count)
  68. -----------------------------------------------------------------------------------
  69. * I/P Arguments: unsigned char.
  70. * Return value  : none
  71.  
  72. * description:
  73. This function is used generate delay in sec .
  74. It genarates a approximate delay of 1sec for each count,
  75. if 10 is passed as the argument then it generates delay of apprx 10sec
  76.  
  77. note: A max of 255 sec delay can be generated using this function.
  78. -----------------------------------------------------------------------------------*/
  79. void delay_sec(unsigned char sec_count)
  80. {
  81.  
  82.  
  83. while(sec_count!=0)
  84. {
  85. delay_ms(1000); //delay_ms is called to generate 1sec delay
  86. sec_count--;
  87. }
  88. }
  89. /*-------------------------------------------------*/


LCD 16x2 8 bit mode

  1. /*---------------------------------------------------------------------------------
  2. 8051 LCD library for 8-bit mode
  3. Filename: lcd_8_bit.c
  4. Controller: P89V51RD2/89c51(8051 family)
  5. Oscillator: 11.0592 MHz
  6. Author: XploreLabz
  7. website: www.xplorelabz.com
  8. ----------------------------------------------------------------------------------
  9.  
  10. Note:
  11. 1.Pin connection for LCD display in 8-bit mode is as shown below.
  12. By default the LCD is connected to Port2.
  13. 2.The code can be modified to connect the LCD to any of the PORTs
  14. by changing the "#define databus P2".
  15.  
  16. -----------------------------------------------------------------------------------*/
  17.  
  18.  
  19. #include<reg51.h>
  20. #include "delay.h"
  21. #include "lcd.h"
  22.  
  23. #define databus P2 // LCD databus connected to PORT2
  24.  
  25. sbit rs= P0^0; // Register select pin connected to P0.0
  26. sbit rw= P0^1; // Read Write pin connected to P0.1
  27. sbit en= P0^2; // Enable pin connected to P0.2
  28.  
  29.  
  30. /* 16x2 LCD Specification */
  31. #define LCDMaxLines 2
  32. #define LCDMaxChars 16
  33. #define LineOne 0x80
  34. #define LineTwo 0xc0
  35.  
  36. #define BlankSpace ' '
  37.  
  38.  
  39.  
  40. /*---------------------------------------------------------------------------------
  41. void LCD_Init()
  42. ----------------------------------------------------------------------------------
  43. * I/P Arguments: none.
  44. * Return value : none
  45.  
  46. * description  :This function is used to initialize the lcd in 8-bit mode
  47. -----------------------------------------------------------------------------------*/
  48. void LCD_Init()
  49. {
  50. delay_us(5000);
  51. LCD_CmdWrite(0x38); // LCD 2lines, 5*7 matrix
  52. LCD_CmdWrite(0x0E); // Display ON cursor ON
  53. LCD_CmdWrite(0x01); // Clear the LCD
  54. LCD_CmdWrite(0x80); // Move the Cursor to First line First Position
  55.  
  56. }
  57.  
  58. /*---------------------------------------------------------------------------------
  59. void LCD_Clear()
  60. ----------------------------------------------------------------------------------
  61. * I/P Arguments: none.
  62. * Return value : none
  63.  
  64. * description  :This function clears the LCD and moves the cursor to first Position
  65. -----------------------------------------------------------------------------------*/
  66. void LCD_Clear()
  67. {
  68. LCD_CmdWrite(0x01); // Clear the LCD and go to First line First Position
  69. LCD_CmdWrite(LineOne);
  70. }
  71.  
  72.  
  73.  
  74. /*---------------------------------------------------------------------------------
  75. void LCD_GoToLineOne()
  76. ----------------------------------------------------------------------------------
  77. * I/P Arguments: none.
  78. * Return value : none
  79.  
  80. * description  :This function moves the Cursor to First line First Position
  81. -----------------------------------------------------------------------------------*/
  82. void LCD_GoToLineOne()
  83. {
  84. LCD_CmdWrite(LineOne); // Move the Cursor to First line First Position
  85. }
  86.  
  87.  
  88. /*---------------------------------------------------------------------------------
  89. void LCD_GoToLineTwo()
  90. ----------------------------------------------------------------------------------
  91. * I/P Arguments: none.
  92. * Return value : none
  93.  
  94. * description  :This function moves the Cursor to Second line First Position
  95. -----------------------------------------------------------------------------------*/
  96. void LCD_GoToLineTwo()
  97. {
  98. LCD_CmdWrite(LineTwo); // Move the Cursor to Second line First Position
  99. }
  100.  
  101.  
  102.  
  103. /*---------------------------------------------------------------------------------
  104. void LCD_GoToXY(char row,char col)
  105. ----------------------------------------------------------------------------------
  106. * I/P Arguments: char row,char col
  107. row -> line number(line1=0, line2=1),
  108. For 2line LCD the I/P argument should be either 0 or 1.
  109. col -> char number.
  110. For 16-char LCD the I/P argument should be betwen 0-15.
  111. * Return value : none
  112.  
  113. * description  :This function moves the Cursor to specified position
  114.  
  115. -----------------------------------------------------------------------------------*/
  116. void LCD_GoToXY(char row, char col)
  117. {
  118. char pos;
  119.  
  120. if(row<LCDMaxLines)
  121. {
  122. pos= LineOne | (row << 6); // take the line number
  123. //row0->pos=0x80 row1->pos=0xc0
  124.  
  125. if(col<LCDMaxChars)
  126. pos= pos+col; //take the char number
  127. //now pos points to the given XY pos
  128.  
  129. LCD_CmdWrite(pos); // Move the Cursor to specified Position
  130. }
  131. }
  132.  
  133.  
  134. /*---------------------------------------------------------------------------------
  135. void LCD_CmdWrite( char cmd)
  136. * ---------------------------------------------------------------------------------
  137. * I/P Arguments: 8-bit command supported by LCD.
  138. * Return value : none
  139.  
  140. * description :This function sends a command to LCD in the following steps.
  141. step1: Send the I/P command to LCD.
  142. step2: Select the Control Register by making RS low.
  143. step3: Select Write operation making RW low.
  144. step4: Send a High-to-Low pulse on Enable PIN with some delay_us.
  145. ----------------------------------------------------------------------------------*/
  146. void LCD_CmdWrite( char cmd)
  147. {
  148.  
  149. databus=cmd; // Send the command to LCD
  150. rs=0; // Select the Command Register by pulling RS LOW
  151. rw=0; // Select the Write Operation by pulling RW LOW
  152. en=1; // Send a High-to-Low Pusle at Enable Pin
  153. delay_us(10);
  154. en=0;
  155.  
  156. delay_ms(1);
  157. }
  158.  
  159.  
  160.  
  161.  
  162.  
  163. /*---------------------------------------------------------------------------------
  164. void LCD_DataWrite( char dat)
  165. ----------------------------------------------------------------------------------
  166. * I/P Arguments: ASCII value of the char to be displayed.
  167. * Return value : none
  168.  
  169. * description  :
  170. This function sends a character to be displayed on LCD in the following steps.
  171. step1: Send the character to LCD.
  172. step2: Select the Data Register by making RS high.
  173. step3: Select Write operation making RW low.
  174. step4: Send a High-to-Low pulse on Enable PIN with some delay_us.
  175.  
  176. -----------------------------------------------------------------------------------*/
  177. void LCD_DataWrite( char dat)
  178. {
  179.  
  180. databus=dat; // Send the data to LCD
  181. rs=1; // Select the Data Register by pulling RS HIGH
  182. rw=0; // Select the Write Operation by pulling RW LOW
  183. en=1; // Send a High-to-Low Pusle at Enable Pin
  184. delay_us(10);
  185. en=0;
  186.  
  187. delay_ms(1);
  188.  
  189. }
  190.  
  191.  
  192.  
  193.  
  194.  
  195. /*---------------------------------------------------------------------------------
  196. void LCD_DisplayString(char *string_ptr)
  197. ----------------------------------------------------------------------------------
  198. * I/P Arguments: String(Address of the string) to be displayed.
  199. * Return value : none
  200.  
  201. * description  :
  202. This function is used to display the ASCII string on the lcd.
  203. 1.The string_ptr points to the first char of the string
  204. and traverses till the end(NULL CHAR).
  205. 2.Each time a char is sent to LCD_DataWrite funtion to display.
  206. -----------------------------------------------------------------------------------*/
  207. void LCD_DisplayString(char *string_ptr)
  208. {
  209. while(*string_ptr)
  210. LCD_DataWrite(*string_ptr++);
  211. }
  212.  
  213.  
  214. /*---------------------------------------------------------------------------------
  215. void LCD_DisplayNumber(unsigned int num)
  216. ----------------------------------------------------------------------------------
  217. * Function name: LCD_DisplayNumber()
  218. * I/P Arguments: unsigned int.
  219. * Return value : none
  220.  
  221. * description  :This function is used to display a 5-digit integer(0-65535).
  222. ex: if the number is 12345 then 12345 is displayed.
  223. if the number is 123 then 00123 is displayed.
  224.  
  225.  
  226. ----------Take 1 by dividing by 10000 and add 0X30 to obtain the ASCII value,
  227. | then take the 4-digit remainder(2345).
  228. |
  229. | ---------Take 2 by dividing by 1000 and add 0X30 to obtain the ASCII value,
  230. || then take the 3-digit remainder(345)
  231. ||
  232. || --------Take 3 by dividing by 100 and add 0X30 to obtain the ASCII value,
  233. ||| then take the 2-digit remainder(45).
  234. |||
  235. ||| -------Take 4 by dividing by 10 and add 0X30 to obtain the ASCII value,
  236. |||| ------Take 5 the remainder of 45 and add 0X30 to obtain the ASCII value,.
  237. |||||
  238. 12345
  239. -----------------------------------------------------------------------------------*/
  240. void LCD_DisplayNumber(unsigned int num)
  241. {
  242. LCD_DataWrite((num/10000)+0x30);
  243. num=num%10000;
  244.  
  245. LCD_DataWrite((num/1000)+0x30);
  246. num=num%1000;
  247.  
  248. LCD_DataWrite((num/100)+0x30);
  249. num=num%100;
  250.  
  251. LCD_DataWrite((num/10)+0x30);
  252.  
  253. LCD_DataWrite((num%10)+0x30);
  254.  
  255. }
  256.  
  257.  
  258. /*---------------------------------------------------------------------------------
  259. void LCD_ScrollMessage(char *msg_ptr)
  260. ----------------------------------------------------------------------------------
  261. * I/P Arguments: char *msg_ptr
  262. msg_ptr -> pointer to the string to be scrolled
  263.  
  264. * Return value : none
  265.  
  266. * description  :This function scrolls the given message on the first line.
  267. 1.16 chars are displayed at atime.
  268. 2.Pointer is incremented to skip a char each time to give the illusion of
  269. moving chars
  270. 3.If the chars are less than 16, then the BlankSpaces are displayed.
  271.  
  272. -----------------------------------------------------------------------------------*/
  273.  
  274. void LCD_ScrollMessage(char *msg_ptr)
  275. {
  276. unsigned char i,j;
  277. LCD_CmdWrite(0x0c); //Disable the Cursor
  278. for(i=0;msg_ptr[i];i++) //Loop to display the complete string
  279. { //each time 16 chars are displayed and
  280. //pointer is incremented to point to next char
  281.  
  282. LCD_GoToLineOne(); //Move the Cursor to first line
  283.  
  284. for(j=0;j<LCDMaxChars && msg_ptr[i+j];j++) //loop to Display first 16 Chars
  285. LCD_DataWrite(msg_ptr[i+j]); //or till Null char
  286.  
  287. for(j=j; j<LCDMaxChars; j++) //If the chars are below 16
  288. LCD_DataWrite(BlankSpace); //then display blank spaces
  289.  
  290. delay_ms(125);
  291. }
  292. LCD_CmdWrite(0x0E); //Enable the Cursor
  293. }
  294.  
  295.  
  296. /*---------------------------------------------------------------------------------
  297. void LCD_DisplayRtcTime(char hour,char min,char sec)
  298. ----------------------------------------------------------------------------------
  299. * I/P Arguments: char hour,char min,char sec
  300. hour,min,sec should be packed BCD format, as read from DS1307
  301.  
  302. * Return value : none
  303.  
  304. * description  :This function display hour,min,sec read from DS1307.
  305.  
  306. ---------- Display the higher nibble of hour after adding 0x30(ASCII conversion)
  307. | Display the lower nibble of hour after adding 0x30(ASCII conversion)
  308. |
  309. | -------- Display the higher nibble of min after adding 0x30(ASCII conversion)
  310. | | Display the lower nibble of min after adding 0x30(ASCII conversion)
  311. | |
  312. | | -----Display the higher nibble of sec after adding 0x30(ASCII conversion)
  313. | | | Display the lower nibble of sec after adding 0x30(ASCII conversion)
  314. | | |
  315. 10;10;40
  316.  
  317. -----------------------------------------------------------------------------------*/
  318. void LCD_DisplayRtcTime(char hour,char min,char sec)
  319. {
  320. LCD_DataWrite(((hour>>4) & 0x0f) + 0x30);
  321. LCD_DataWrite((hour & 0x0f) + 0x30);
  322. LCD_DataWrite(':');
  323.  
  324. LCD_DataWrite(((min>>4) & 0x0f) + 0x30);
  325. LCD_DataWrite((min & 0x0f) + 0x30);
  326. LCD_DataWrite(':');
  327.  
  328. LCD_DataWrite(((sec>>4) & 0x0f) + 0x30);
  329. LCD_DataWrite((sec & 0x0f) + 0x30);
  330.  
  331. }
  332.  
  333.  
  334.  
  335.  
  336. /*---------------------------------------------------------------------------------
  337. LCD_DisplayRtcDate(char day,char month,char year)
  338. ----------------------------------------------------------------------------------
  339. * I/P Arguments: char day,char month,char year
  340. day,month,year should be packed BCD format, as read from DS1307
  341.  
  342. * Return value : none
  343.  
  344. * description  :This function display day,month,year read from DS1307.
  345.  
  346. ----------- Display the higher nibble of day after adding 0x30(ASCII conversion)
  347. | Display the lower nibble of day after adding 0x30(ASCII conversion)
  348. |
  349. | -------- Display the higher nibble of month after adding 0x30(ASCII conversion)
  350. | | Display the lower nibble of month after adding 0x30(ASCII conversion)
  351. | |
  352. | | ----- Display the higher nibble of year after adding 0x30(ASCII conversion)
  353. | | | Display the lower nibble of year after adding 0x30(ASCII conversion)
  354. | | |
  355. 01/01/12 (1st-Jan 2012)
  356.  
  357. -----------------------------------------------------------------------------------*/
  358. void LCD_DisplayRtcDate(char day,char month,char year)
  359. {
  360. LCD_DataWrite(((day>>4) & 0x0f) + 0x30);
  361. LCD_DataWrite((day & 0x0f) + 0x30);
  362. LCD_DataWrite('/');
  363.  
  364. LCD_DataWrite(((month>>4) & 0x0f) + 0x30);
  365. LCD_DataWrite((month & 0x0f) + 0x30);
  366. LCD_DataWrite('/');
  367.  
  368. LCD_DataWrite(((year>>4) & 0x0f) + 0x30);
  369. LCD_DataWrite((year & 0x0f) + 0x30);
  370.  
  371. }

LCD 4 bit mode

  1.  
  2.  
  3.  
  4. /*----------------------------------------------------------------------------------
  5. 8051 LCD library for 4-bit mode
  6. Filename: lcd_4_bit.c
  7. Controller: P89V51RD2/89c51(8051 family)
  8. Oscillator: 11.0592 MHz
  9. Author: XploreLabz
  10. website: www.xplorelabz.com
  11.  
  12. ----------------------------------------------------------------------------------
  13. Note:
  14. 1.Pin connection for LCD display in 4-bit mode.
  15. 2.By default the LCD is connected to Port0.
  16. 3.The code can be modified to connect the LCD to any of the PORTs by changing the
  17. "#define databus P0".
  18.  
  19.  
  20.  
  21. ----------------------------------------------------------------------------------*/
  22.  
  23.  
  24. #include<reg51.h>
  25. #include "delay.h"
  26.  
  27. #define databus P0 // LCD databus connected to PORT0
  28.  
  29. sbit rs= databus^0; // Register select pin connected to P0.0
  30. sbit rw= databus^1; // Read Write pin connected to P0.1
  31. sbit en= databus^2; // Enable pin connected to P0.2
  32.  
  33.  
  34.  
  35. /* 16x2 LCD Specification */
  36. #define LCDMaxLines 2
  37. #define LCDMaxChars 16
  38. #define LineOne 0x80
  39. #define LineTwo 0xc0
  40.  
  41. #define BlankSpace ' '
  42.  
  43.  
  44.  
  45.  
  46.  
  47.  
  48.  
  49. /*----------------------------------------------------------------------------------
  50. void LCD_Init()
  51. ----------------------------------------------------------------------------------
  52. * Function name: LCD_Init()
  53. * I/P Arguments: none.
  54. * Return value : none
  55.  
  56. * description  :This function is used to initialize the lcd in 4-bit mode
  57. ----------------------------------------------------------------------------------*/
  58. void LCD_Init()
  59. {
  60. delay_us(5000);
  61. LCD_CmdWrite(0x02); //Initilize the LCD in 4bit Mode
  62. LCD_CmdWrite(0x28);
  63. LCD_CmdWrite(0x0E); // Display ON cursor ON
  64. LCD_CmdWrite(0x01); // Clear the LCD
  65. LCD_CmdWrite(0x80); // Move the Cursor to First line First Position
  66.  
  67. }
  68.  
  69.  
  70. /*----------------------------------------------------------------------------------
  71. void LCD_CmdWrite( char cmd)
  72. ------------------------------------------------------------------------------------
  73. * I/P Arguments: 8-bit command supported by LCD.
  74. * Return value : none
  75.  
  76. * description  :This function sends a command to LCD in the following steps.
  77. step1: Send the Higher Nibble of the I/P command to LCD.
  78. step2: Select the Control Register by making RS low.
  79. step3: Select Write operation making RW low.
  80. step4: Send a High-to-Low pulse on Enable PIN with some delay_us.
  81.  
  82. step5: Send the Lower Nibble of the I/P command to LCD.
  83. step6: Select the Control Register by making RS low.
  84. step7: Select Write operation making RW low.
  85. step8: Send a High-to-Low pulse on Enable PIN with some delay_us.
  86. ----------------------------------------------------------------------------------*/
  87. void LCD_CmdWrite( char cmd)
  88. {
  89.  
  90. databus=(cmd & 0xf0); // Send the Higher Nibble of the command to LCD
  91. rs=0; // Select the Command Register by pulling RS LOW
  92. rw=0; // Select the Write Operation by pulling RW LOW
  93. en=1; // Send a High-to-Low Pusle at Enable Pin
  94. delay_us(1);
  95. en=0;
  96.  
  97. delay_us(10); // wait for some time
  98.  
  99. databus=((cmd<<4) & 0xf0); // Send the Lower Nibble of the command to LCD
  100. rs=0; // Select the Command Register by pulling RS LOW
  101. rw=0; // Select the Write Operation by pulling RW LOW
  102. en=1; // Send a High-to-Low Pusle at Enable Pin
  103. delay_us(1);
  104. en=0;
  105. delay_ms(1);
  106. }
  107.  
  108.  
  109.  
  110. /*---------------------------------------------------------------------------------
  111. void LCD_DataWrite( char dat)
  112. ----------------------------------------------------------------------------------
  113. * Function name: LCD_DataWrite()
  114. * I/P Arguments: ASCII value of the char to be displayed.
  115. * Return value : none
  116.  
  117. * description  :
  118. This function sends a character to be displayed on LCD in the following steps.
  119. step1: Send the higher nibble of the character to LCD.
  120. step2: Select the Data Register by making RS high.
  121. step3: Select Write operation making RW low.
  122. step4: Send a High-to-Low pulse on Enable PIN with some delay_us.
  123.  
  124. step5: wait for some time
  125.  
  126. step6: Send the lower nibble of the character to LCD.
  127. step7: Select the Data Register by making RS high.
  128. step8: Select Write operation making RW low.
  129. step9: Send a High-to-Low pulse on Enable PIN with some delay_us.
  130. ----------------------------------------------------------------------------------*/
  131. void LCD_DataWrite( char dat)
  132. {
  133.  
  134.  
  135. databus=(dat & 0xf0); // Send the Higher Nibble of the Data to LCD
  136. rs=1; // Select the Data Register by pulling RS HIGH
  137. rw=0; // Select the Write Operation by pulling RW LOW
  138. en=1; // Send a High-to-Low Pusle at Enable Pin
  139. delay_us(1);
  140. en=0;
  141.  
  142. delay_us(10); // wait for some time.
  143.  
  144. databus=((dat <<4) & 0xf0); // Send the Lower Nibble of the Data to LCD
  145. rs=1; // Select the Data Register by pulling RS HIGH
  146. rw=0; // Select the Write Operation by pulling RW LOW
  147. en=1; // Send a High-to-Low Pusle at Enable Pin
  148. delay_us(1);
  149. en=0;
  150.  
  151. delay_ms(1);
  152.  
  153. }

Keypad.c: 8051 Keypad C-library

  1. /*----------------------------------------------------------------------------------
  2. 8051 4x4 Keypad Library
  3. Filename: keypad.c
  4. Controller: P89V51RD2/89c51(8051 family)
  5. Oscillator: 11.0592 MHz
  6. Author: XploreLabz
  7. website: www.xplorelabz.com
  8. ----------------------------------------------------------------------------------
  9. Note:
  10. 1.Rows are connected to lower 4-bits of P1
  11. 1.Cols are connected to higher 4-bits of P1
  12.  
  13. ----------------------------------------------------------------------------------*/
  14.  
  15.  
  16. #include <reg51.h>
  17. #include "keypad.h"
  18. #include "delay.h"
  19.  
  20.  
  21. #define ROW P1 //Lower four bits of P1 are used as ROWs
  22. #define COL P1 //Higher four bits of P1 are used as COLs
  23.  
  24.  
  25.  
  26.  
  27. /*---------------------------------------------------------------------------------
  28. void KEYPAD_Init()
  29. ----------------------------------------------------------------------------------
  30. * I/P Arguments:none
  31. * Return value : none
  32.  
  33. * description  : This function the rows and colums for keypad scan
  34. 1.ROW lines are configured as Output.
  35. 2.Column Lines are configured as Input.
  36. -----------------------------------------------------------------------------------*/
  37. void KEYPAD_Init()
  38. {
  39. ROW=0xF0; // Configure Row lines as O/P and Column lines as I/P
  40. }
  41.  
  42.  
  43.  
  44.  
  45.  
  46.  
  47. /*---------------------------------------------------------------------------------
  48. void KEYPAD_WaitForKeyRelease()
  49. ----------------------------------------------------------------------------------
  50. * I/P Arguments:none
  51.  
  52. * Return value : none
  53.  
  54. * description  : This function waits till the previous key is released.
  55. 1.All the ROW lines are pulled low.
  56. 2.Column Lines are read to check the key press.
  57. 3.If all the Keys are released then Column lines will be High(0xF0)
  58. -----------------------------------------------------------------------------------*/
  59. void KEYPAD_WaitForKeyRelease()
  60. {
  61. unsigned char key;
  62. do
  63. {
  64. ROW=0xF0; // Pull the ROW lines to low and Column lines high.
  65. key=COL & 0xF0; // Read the Columns, to check the key press
  66. }while(key!=0xF0); // Wait till the Key is released,
  67. // If no Key is pressed, Column lines will be High(0xF0)
  68. }
  69.  
  70.  
  71.  
  72. /*---------------------------------------------------------------------------------
  73. void KEYPAD_WaitForKeyPress()
  74. ----------------------------------------------------------------------------------
  75. * I/P Arguments:none
  76. * Return value : none
  77.  
  78. * description  : This function waits till a new key is pressed.
  79. 1.All the ROW lines are pulled low.
  80. 2.Column Lines are read to check the key press.
  81. 3.If any Key is pressed then corresponding Column Line goes low.
  82. 4.Wait for Some time and perform the above operation to ensure
  83. the True Key Press before decoding the KEY.
  84. -----------------------------------------------------------------------------------*/
  85. void KEYPAD_WaitForKeyPress()
  86. {
  87. unsigned char key;
  88. do
  89. {
  90. do
  91. {
  92. ROW=0xF0; // Pull the ROW lines to low and Column lines high.
  93. key=COL & 0xF0; // Read the Columns, to check the key press
  94. }while(key==0xF0); // Wait till the Key is pressed,If no Key is pressed
  95. // if a Key is pressed the corresponding Column line go low
  96.  
  97. delay_ms(1); // Wait for some time(debounce Time);
  98.  
  99. ROW=0xF0; // After some time, perform the above action to ensure the
  100. key=COL & 0xF0; // the Key press.
  101. }while(key==0xF0);
  102.  
  103. }
  104.  
  105.  
  106.  
  107.  
  108.  
  109. /*---------------------------------------------------------------------------------
  110. unsigned char KEYPAD_ScanKey()
  111. ----------------------------------------------------------------------------------
  112. * I/P Arguments:none
  113.  
  114. * Return value : char--> Scancode of the Key Pressed
  115.  
  116. * description  : This function scans all the rows to decode the key pressed.
  117. 1.Each time a ROW line is pulled low to detect the KEY.
  118. 2.Column Lines are read to check the key press.
  119. 3.If any Key is pressed then corresponding Column Line goes low.
  120. 4.Return the ScanCode(Combination of ROW & COL) for decoding the key.
  121. -----------------------------------------------------------------------------------*/
  122. unsigned char KEYPAD_ScanKey()
  123. {
  124.  
  125. unsigned char ScanKey = 0xFE,i, key;
  126.  
  127. for(i=0;i<0x04;i++) // Scan All the 4-Rows for key press
  128. {
  129. ROW=ScanKey; // Select 1-Row at a time for Scanning the Key
  130. key=COL & 0xF0; // Read the Column, for key press
  131.  
  132. if(key!=0xf0) // If the KEY press is detected for the selected
  133. break; // ROW then stop Scanning,
  134.  
  135. ScanKey=((ScanKey<<1)+0x01); // Rotate the ScanKey to SCAN the remaining Rows
  136. }
  137.  
  138.  
  139. key = COL; // Retun the COL status to decode the key
  140. return(key);
  141. }
  142.  
  143.  
  144.  
  145.  
  146.  
  147.  
  148.  
  149.  
  150.  
  151.  
  152.  
  153.  
  154.  
  155.  
  156. /*---------------------------------------------------------------------------------
  157. unsigned char KEYPAD_GetKey()
  158. ----------------------------------------------------------------------------------
  159. * I/P Arguments:none
  160.  
  161. * Return value : char--> ASCII value of the Key Pressed
  162.  
  163. * description: This function waits till a key is pressed and retuns its ASCII Value
  164.  
  165. 1.Wait till the previous key is released..
  166. 2.Wait for the new key press.
  167. 3.Scan all the rows one at atime for the pressed key.
  168. 4.Decode the key pressed depending on ROW-COL combination and retuns its
  169. ASCII value.
  170. -----------------------------------------------------------------------------------*/
  171. unsigned char KEYPAD_GetKey()
  172. {
  173. unsigned char key;
  174.  
  175. KEYPAD_WaitForKeyRelease(); // Wait for the previous key release
  176. delay_ms(1);
  177.  
  178. KEYPAD_WaitForKeyPress(); // Wait for the new key press
  179. key = KEYPAD_ScanKey(); // Scan for the key pressed.
  180.  
  181. switch(key) // Decode the key
  182. {
  183. case 0xe7: key='0'; break;
  184. case 0xeb: key='1'; break;
  185. case 0xed: key='2'; break;
  186. case 0xee: key='3'; break;
  187. case 0xd7: key='4'; break;
  188. case 0xdb: key='5'; break;
  189. case 0xdd: key='6'; break;
  190. case 0xde: key='7'; break;
  191. case 0xb7: key='8'; break;
  192. case 0xbb: key='9'; break;
  193. case 0xbd: key='A'; break;
  194. case 0xbe: key='B'; break;
  195. case 0x77: key='C'; break;
  196. case 0x7b: key='D'; break;
  197. case 0x7d: key='E'; break;
  198. case 0x7e: key='F'; break;
  199. default: key='z';
  200. }
  201. return(key); // Return the key
  202. }

Uart.c: 8051 Uart C-Libraray

  1. /*--------------------------------------------------------------------------------
  2. 8051 UART library for Serial Communication at 9600 baud rate
  3. with the crystal frequency 11.0592MHz
  4.  
  5. Filename: uart.c
  6. Controller: P89V51RD2/89c51(8051 family)
  7. Oscillator: 11.0592 MHz
  8. Author: XploreLabz
  9. website: www.xplorelabz.com
  10. ---------------------------------------------------------------------------------*/
  11.  
  12.  
  13. #include<reg51.h>
  14.  
  15.  
  16.  
  17. /*-------------------------------------------------------------------------------
  18. void UART_Init()
  19. ----------------------------------------------------------------------------------
  20. * I/P Arguments: none.
  21. * Return value : none
  22.  
  23. * description  :This function is used to initialize the UART at 9600 baud rate
  24. by below configuration.
  25. 1.Timer 1 is run in Mode2(auto reload mode) to generate the required Baud Rate.
  26. 2.9600 baud rate is generate by loading -3(0XFD) in TH1 register.
  27. 3.SCON is configured in MODE1 ie. 8bit Data 1-Start and 1-Stop bit.
  28. 4.Finally the timer is turned ON by setting TR1 bit to generate the baud rate.
  29. ----------------------------------------------------------------------------------*/
  30. void UART_Init()
  31. {
  32. TMOD |=0x20; //Timer1 in Mode2.
  33. TH1=-3; // 9600 Baud rate at 11.0592MHz
  34. SCON=0x50; // Asynchronous mode 8-bit data and 1-stop bit
  35. TR1=1; //Turn ON the timer.
  36. }
  37.  
  38.  
  39.  
  40.  
  41.  
  42. /*----------------------------------------------------------------------------------
  43. char UART_RxChar()
  44. ----------------------------------------------------------------------------------
  45. * I/P Arguments: none.
  46. * Return value : char
  47.  
  48. * description :This function is used to receive a char from UART module.
  49. It waits till a char is received ie.till RI is set,
  50. RI will be set once a CHAR is received.
  51. Finally it clears the RI for next cycle and returns the received char.
  52. ----------------------------------------------------------------------------------_*/
  53. char UART_RxChar()
  54. {
  55. while(RI==0); // Wait till the data is received
  56. RI=0; // Clear Receive Interrupt Flag for next cycle
  57. return(SBUF); // return the received char
  58. }
  59.  
  60.  
  61.  
  62.  
  63.  
  64.  
  65.  
  66. /*----------------------------------------------------------------------------------
  67. void UART_TxChar(char ch)
  68. ------------------------------------------------------------------------------------
  69. * I/P Arguments: char--> data to be transmitted.
  70. * Return value : none.
  71.  
  72. * description  :This function is used to transmit a char through UART module.
  73. The Char to be transmitted is loaded into SBUF.
  74. It waits till char is transmitted ie.till TI is set.
  75. TI will be set once a CHAR is transmitted.
  76. Finally it clears the TI for next operation.
  77. ----------------------------------------------------------------------------------*/
  78. void UART_TxChar(char ch)
  79. {
  80. SBUF=ch; // Load the data to be transmitted
  81. while(TI==0); // Wait till the data is trasmitted
  82. TI=0; //Clear the flag for next cycle.
  83. }
  84.  
  85.  
  86.  
  87.  
  88.  
  89.  
  90.  
  91.  
  92.  
  93. /*----------------------------------------------------------------------------------
  94. void UART_TxString(char *string_ptr)
  95. -----------------------------------------------------------------------------------
  96. * I/P Arguments: String(Address of the string) to be transmitted.
  97. * Return value : none
  98.  
  99. * description :This function is used to transmit the ASCII string through UART..
  100. The string_ptr points to the first char of the string.
  101. And it is incremented each time to traverse till the end(NULL CHAR).
  102. Each time a char is sent to UART_TxChar() fun to transmit it through UART
  103. ----------------------------------------------------------------------------------_*/
  104. void UART_TxString(char *string_ptr)
  105. {
  106. while(*string_ptr)
  107. UART_TxChar(*string_ptr++);
  108. }
  109.  
  110.  
  111.  
  112.  
  113.  
  114.  
  115.  
  116.  
  117.  
  118.  
  119.  
  120.  
  121.  
  122.  
  123.  
  124.  
  125.  
  126.  
  127.  
  128.  
  129.  
  130.  
  131. /*---------------------------------------------------------------------------------
  132. void UART_RxString(char *string_ptr)
  133. ----------------------------------------------------------------------------------
  134. * I/P Arguments: *string_ptr
  135. Address of the string where the received data needs to be stored
  136. * Return value : none
  137.  
  138. * description  :
  139. 1.This function is used to receive a ASCII string through UART
  140. till the carriage_return/New_line
  141. 2.The string_ptr points to the begining of the string and each
  142. time UART_RxChar() function is called to receive a char and copy
  143. it into the buffer(STRING) and incrment string_ptr.
  144. 3.Once the carriage_return/New_line is encountered the loop
  145. is breaked and the String is NULL terminated.
  146.  
  147. *****NOTE*******:
  148. 1.The received char is ECHOED back,
  149. if not required then comment UART_TxChar(ch) in the code.
  150. 2.BackSlash is not taken care.
  151. ----------------------------------------------------------------------------------_*/
  152. void UART_RxString(char *string_ptr)
  153. {
  154. char ch;
  155. while(1)
  156. {
  157. ch=UART_RxChar(); //Reaceive a char
  158. UART_TxChar(ch); //Echo back the received char
  159.  
  160. if((ch=='\r') || (ch=='\n')) //read till enter key is pressed
  161. { //once enter key is pressed
  162. *string_ptr=0; //null terminate the string
  163. break; //and break the loop
  164. }
  165. *string_ptr=ch; //copy the char into string.
  166. string_ptr++; //and increment the pointer
  167. }
  168. }
  169.  
  170.  
  171.  
  172.  
  173.  
  174.  
  175.  
  176.  
  177.  
  178.  
  179.  
  180.  
  181.  
  182.  
  183.  
  184.  
  185.  
  186.  
  187.  
  188.  
  189.  
  190.  
  191.  
  192.  
  193. /*----------------------------------------------------------------------------------
  194. void UART_TxNumber(unsigned int num)
  195. ------------------------------------------------------------------------------------
  196. * I/P Arguments: unsigned int.
  197. * Return value : none
  198.  
  199. * description  :This function is used to transmit a 5-digit integer(0-65535).
  200. ex: if the number is 12345 then 12345 is transmitted.
  201. if the number is 123 then 00123 is transmitted.
  202.  
  203.  
  204. ----------Take 1 by dividing by 10000 and add 0X30 to obtain the ASCII value,
  205. | then take the 4-digit remainder(2345).
  206. |
  207. | ---------Take 2 by dividing by 1000 and add 0X30 to obtain the ASCII value,
  208. || then take the 3-digit remainder(345)
  209. ||
  210. || --------Take 3 by dividing by 100 and add 0X30 to obtain the ASCII value,
  211. ||| then take the 2-digit remainder(45).
  212. |||
  213. ||| -------Take 4 by dividing by 10 and add 0X30 to obtain the ASCII value,
  214. |||| ------Take 5 the remainder of 45 and add 0X30 to obtain the ASCII value,.
  215. |||||
  216. 12345
  217. ------------------------------------------------------------------------------------*/
  218.  
  219. void UART_TxNumber(unsigned int num)
  220. {
  221.  
  222. UART_TxChar((num/10000)+0x30);
  223. num=num%10000;
  224.  
  225. UART_TxChar((num/1000)+0x30);
  226. num=num%1000;
  227.  
  228. UART_TxChar((num/100)+0x30);
  229. num=num%100;
  230.  
  231. UART_TxChar((num/10)+0x30);
  232.  
  233. UART_TxChar((num%10)+0x30);
  234. }
  1. =I2c.c: 8051 I2c C-Library=
  1. /*---------------------------------------------------------------------------------*
  2. 8051 I2C library
  3. Filename: I2C.c
  4. Controller: P89V51RD2/89c51(8051 family)
  5. Oscillator: 11.0592 MHz
  6. Author: XploreLabz
  7. website: www.xplorelabz.com
  8.  
  9. Note:
  10. 1.The SDA and SCL lines are connected to P0.7 and P0.6
  11. 2.The code can be modified to connect the
  12. SDA and SCL to any of the PORTs by changing the "sbit".
  13. ----------------------------------------------------------------------------------*/
  14. #include<reg51.h>
  15. #include "delay.h"
  16. #include "i2c.h"
  17.  
  18. sbit SCL=P0^6; //SCL Connected to P0.6
  19. sbit SDA=P0^7; //SDA Connected to P0.7
  20.  
  21.  
  22.  
  23.  
  24.  
  25.  
  26.  
  27.  
  28. /*---------------------------------------------------------------------------------*
  29. void I2C_Clock()
  30. ----------------------------------------------------------------------------------*
  31. * I/P Arguments: none.
  32. * Return value : none
  33.  
  34. * description  :This function is used to generate a clock pulse on SCL line.
  35. -----------------------------------------------------------------------------------*/
  36. void I2C_Clock(void)
  37. {
  38. delay_us(1);
  39. SCL = 1; // Wait for Some time and Pull the SCL line High
  40.  
  41. delay_us(1); // Wait for Some time
  42. SCL = 0; // Pull back the SCL line low to Generate a clock pulse
  43. }
  44.  
  45.  
  46.  
  47.  
  48.  
  49.  
  50.  
  51.  
  52.  
  53.  
  54.  
  55.  
  56.  
  57.  
  58.  
  59.  
  60.  
  61.  
  62.  
  63. /*---------------------------------------------------------------------------------*
  64. void I2C_Start()
  65. ----------------------------------------------------------------------------------*
  66. * I/P Arguments: none.
  67. * Return value : none
  68. * description  :This function is used to generate I2C Start Condition.
  69. Start Condition: SDA goes low when SCL is High.
  70.  
  71. -----------------------------------------------------------------------------------*/
  72. void I2C_Start()
  73. {
  74. SCL = 0; // Pull SCL low
  75.  
  76. SDA = 1; // Pull SDA High
  77. delay_us(1);
  78.  
  79. SCL = 1; //Pull SCL high
  80. delay_us(1);
  81.  
  82. SDA = 0; //Now Pull SDA LOW, to generate the Start Condition
  83. delay_us(1);
  84.  
  85. SCL = 0; //Finally Clear the SCL to complete the cycle
  86. }
  87.  
  88.  
  89.  
  90.  
  91. /*-----------------------------------------------------------------------------------
  92. void I2C_Stop()
  93. ------------------------------------------------------------------------------------
  94. * I/P Arguments: none.
  95. * Return value : none
  96. * description  :This function is used to generate I2C Stop Condition.
  97. Stop Condition: SDA goes High when SCL is High.
  98.  
  99. ------------------------------------------------------------------------------------*/
  100.  
  101. void I2C_Stop(void)
  102. {
  103. SCL = 0; // Pull SCL low
  104. delay_us(1);
  105.  
  106. SDA = 0; // Pull SDA low
  107. delay_us(1);
  108.  
  109. SCL = 1; // Pull SCL High
  110. delay_us(1);
  111.  
  112. SDA = 1; // Now Pull SDA High, to generate the Stop Condition
  113. }
  114.  
  115.  
  116. /*---------------------------------------------------------------------------------*
  117. void I2C_Write(unsigned char dat)
  118. ----------------------------------------------------------------------------------*
  119. * I/P Arguments: unsigned char-->8bit data to be sent.
  120. * Return value : none
  121.  
  122. * description  :This function is used to send a byte on SDA line using I2C protocol
  123. 8bit data is sent bit-by-bit on each clock cycle.
  124. MSB(bit) is sent first and LSB(bit) is sent at last.
  125. Data is sent when SCL is low.
  126.  
  127. -----------------------------------------------------------------------------------*/
  128. void I2C_Write(unsigned char dat)
  129. {
  130. unsigned char i;
  131.  
  132. for(i=0;i<8;i++) // loop 8 times to send 1-byte of data
  133. {
  134. SDA = dat & 0x80; // Send Bit by Bit on SDA line
  135. I2C_Clock(); // Generate Clock at SCL
  136. dat = dat<<1;
  137. }
  138. SDA = 1; // Set SDA at last
  139. }
  140.  
  141. /*-----------------------------------------------------------------------------------*
  142. unsigned char I2C_Read()
  143. ------------------------------------------------------------------------------------*
  144. * I/P Arguments: none.
  145. * Return value : Unsigned char(received byte)
  146.  
  147. * description :This fun is used to receive a byte on SDA line using I2C protocol.
  148. 8bit data is received bit-by-bit each clock and finally packed into Byte.
  149. MSB(bit) is received first and LSB(bit) is received at last.
  150.  
  151. -------------------------------------------------------------------------------------*/
  152. unsigned char I2C_Read(void)
  153. {
  154. unsigned char i, dat=0x00;
  155.  
  156. SDA=1; //Make SDA as I/P
  157. for(i=0;i<8;i++) // loop 8times to read 1-byte of data
  158. {
  159. delay_us(1);
  160. SCL = 1; // Pull SCL High
  161. delay_us(1);
  162.  
  163. dat = dat<<1; //dat is Shifted each time and
  164. dat = dat | SDA; //ORed with the received bit to pack into byte
  165.  
  166. SCL = 0; // Clear SCL to complete the Clock
  167. }
  168. return dat; // Finally return the received Byte*
  169. }
  170.  
  171.  
  172.  
  173. /*---------------------------------------------------------------------------------*
  174. void I2C_Ack()
  175. ----------------------------------------------------------------------------------*
  176. * I/P Arguments: none.
  177. * Return value : none
  178.  
  179. * description  :This function is used to generate a the Positive ACK
  180. pulse on SDA after receiving a byte.
  181. -----------------------------------------------------------------------------------*/
  182. void I2C_Ack()
  183. {
  184. SDA = 0; //Pull SDA low to indicate Positive ACK
  185. I2C_Clock(); //Generate the Clock
  186. SDA = 1; // Pull SDA back to High(IDLE state)
  187. }
  188.  
  189.  
  190.  
  191.  
  192.  
  193. /*---------------------------------------------------------------------------------*
  194. void I2C_NoAck()
  195. ----------------------------------------------------------------------------------*
  196. * I/P Arguments: none.
  197. * Return value : none
  198.  
  199. * description  :This function is used to generate a the Negative/NO ACK
  200. pulse on SDA after receiving all bytes.
  201. -----------------------------------------------------------------------------------*/
  202. void I2C_NoAck()
  203. {
  204. SDA = 1; //Pull SDA high to indicate Negative/NO ACK
  205. I2C_Clock(); // Generate the Clock
  206. SCL = 1; // Set SCL */
  207. }


Ds1307.c: 8051 ds1307-RTC C-Library

  1. /*--------------------------------------------------------------------------------
  2. 8051 DS1307 library
  3. Filename: DS1307.c
  4. Controller: P89V51RD2/89c51(8051 family)
  5. Oscillator: 11.0592 MHz
  6. Author: XploreLabz
  7. website: www.xplorelabz.com
  8.  
  9. ---------------------------------------------------------------------------------*/
  10.  
  11. #include "ds1307.h"
  12. #include "i2c.h"
  13. #include "delay.h"
  14.  
  15.  
  16.  
  17. /* Below values are fixed and should not be changed.
  18. Refer Ds1307 DataSheet for more info*/
  19.  
  20. #define DS1307_ID 0xD0 // DS1307 ID
  21.  
  22. #define SEC_ADDRESS 0x00 // Address to access Ds1307 SEC register
  23. #define DATE_ADDRESS 0x04 // Address to access Ds1307 DATE register
  24. #define CONTROL 0x07 // Address to access Ds1307 CONTROL register
  25.  
  26.  
  27.  
  28.  
  29.  
  30.  
  31. /*---------------------------------------------------------------------------------
  32. void DS1307_Init()
  33. ----------------------------------------------------------------------------------
  34. * I/P Arguments: none.
  35. * Return value : none
  36.  
  37. * description :This function is used to initialize the Ds1307 RTC.
  38. Ds1307 ic is enabled by sending the DS1307 id on the I2C bus.
  39. After selecting DS1307, write 0x00 into Control register of Ds1307
  40.  
  41. ------------------------------------------------------------------------------------*/
  42. void DS1307_Init()
  43. {
  44. I2C_Start(); // Start I2C communication
  45.  
  46. DS1307_Write(DS1307_ID); // Connect to DS1307 by sending its ID on I2c Bus
  47. DS1307_Write(CONTROL); // Select the Ds1307 ControlRegister to configure Ds1307
  48.  
  49. DS1307_Write(0x00); // Write 0x00 to Control register to disable SQW-Out
  50.  
  51. I2C_Stop(); // Stop I2C communication after initilizing DS1307
  52.  
  53. }
  54.  
  55.  
  56.  
  57.  
  58.  
  59.  
  60.  
  61.  
  62.  
  63.  
  64.  
  65.  
  66. /*---------------------------------------------------------------------------------
  67. void DS1307_Write(unsigned char dat)
  68. ----------------------------------------------------------------------------------
  69. * I/P Arguments: char-> Data to be written into DS1307.
  70. * Return value : none
  71.  
  72. * description :This function is used to initialize the Ds1307 RTC.
  73. Ds1307 ic is enabled by sending the DS1307 id on the I2C bus.
  74. After selecting DS1307, write 0x00 into Control register of Ds1307
  75.  
  76. ------------------------------------------------------------------------------------*/
  77. void DS1307_Write(unsigned char dat)
  78. {
  79. I2C_Write(dat); // Connect to DS1307 by sending its ID on I2c Bus
  80. I2C_Clock();
  81. }
  82.  
  83.  
  84.  
  85.  
  86.  
  87.  
  88.  
  89.  
  90. /*---------------------------------------------------------------------------------
  91. unsigned char DS1307_Read()
  92. ----------------------------------------------------------------------------------
  93. * I/P Arguments: char-> Data to be written into DS1307.
  94. * Return value : none
  95.  
  96. * description :This function is used to initialize the Ds1307 RTC.
  97. Ds1307 ic is enabled by sending the DS1307 id on the I2C bus.
  98. After selecting DS1307, write 0x00 into Control register of Ds1307
  99.  
  100. ------------------------------------------------------------------------------------*/
  101. unsigned char DS1307_Read()
  102. {
  103. unsigned char dat;
  104. dat = I2C_Read(); // Connect to DS1307 by sending its ID on I2c Bus
  105. return(dat);
  106. }
  107.  
  108.  
  109.  
  110.  
  111.  
  112.  
  113.  
  114.  
  115.  
  116.  
  117.  
  118.  
  119.  
  120.  
  121.  
  122.  
  123.  
  124.  
  125.  
  126.  
  127.  
  128. /*----------------------------------------------------------------------------------
  129. void DS1307_SetTime(unsigned char hh, unsigned char mm, unsigned char ss)
  130. -----------------------------------------------------------------------------------
  131. * I/P Arguments: char,char,char-->hh,mm,ss to initilize the time into DS1307.
  132. * Return value : none
  133.  
  134. * description  :This function is used to set Time(hh,mm,ss) into the Ds1307 RTC.
  135. Ds1307 ic is enabled by sending the DS1307 id on the I2C bus.
  136. After selecting DS1307, select the RAM address 0x00 to point to sec
  137. Initilze Sec, MIN, Hour one after the other.
  138. Stop the I2c communication.
  139. -----------------------------------------------------------------------------------*/
  140. void DS1307_SetTime(unsigned char hh, unsigned char mm, unsigned char ss)
  141. {
  142. I2C_Start(); // Start I2C communication
  143.  
  144. DS1307_Write(DS1307_ID); // connect to DS1307 by sending its ID on I2c Bus
  145. DS1307_Write(SEC_ADDRESS); // Select the SEC RAM address
  146.  
  147. DS1307_Write(ss); // Write sec on RAM address 00H
  148. DS1307_Write(mm); // Write min on RAM address 01H
  149. DS1307_Write(hh); // Write hour on RAM address 02H
  150.  
  151. I2C_Stop(); // Stop I2C communication after Setting the Time
  152. }
  153.  
  154.  
  155.  
  156.  
  157.  
  158.  
  159. /*---------------------------------------------------------------------------------
  160. void DS1307_SetDate(unsigned char dd, unsigned char mm, unsigned char yy)
  161. ----------------------------------------------------------------------------------
  162. * I/P Arguments: char,char,char-->day,month,year to initilize the Date into DS1307.
  163. * Return value : none
  164.  
  165. * description  :This function is used to set Date(dd,mm,yy) into the Ds1307 RTC.
  166. Ds1307 ic is enabled by sending the DS1307 id on the I2C bus.
  167. After selecting DS1307, select the RAM address 0x04 to point to day
  168. Initilze Day,Month and Year one after the other.
  169. Stop the I2c communication.
  170. ----------------------------------------------------------------------------------*/
  171. void DS1307_SetDate(unsigned char dd, unsigned char mm, unsigned char yy)
  172. {
  173. I2C_Start(); // Start I2C communication
  174.  
  175. DS1307_Write(DS1307_ID); // connect to DS1307 by sending its ID on I2c Bus
  176. DS1307_Write(DATE_ADDRESS); // Request DAY RAM address at 04H
  177.  
  178. DS1307_Write(dd); // Write date on RAM address 04H
  179. DS1307_Write(mm); // Write month on RAM address 05H
  180. DS1307_Write(yy); // Write year on RAM address 06h
  181.  
  182. I2C_Stop(); // Stop I2C communication after Setting the Date
  183. }
  184.  
  185.  
  186.  
  187.  
  188.  
  189.  
  190.  
  191. /*----------------------------------------------------------------------------------
  192. void DS1307_GetTime(unsigned char *h_ptr,unsigned char *m_ptr,unsigned char *s_ptr)
  193. -----------------------------------------------------------------------------------
  194. * I/P Arguments: char *,char *,char *-->pointers to get the hh,mm,ss.
  195. * Return value : none
  196.  
  197. * description  :This function is used to get the Time(hh,mm,ss) from Ds1307 RTC.
  198. Ds1307 ic is enabled by sending the DS1307 id on the I2C bus.
  199. After selecting DS1307, select the RAM address 0x00 to point to sec
  200. Get Sec, MIN, Hour one after the other.
  201. Stop the I2c communication.
  202. -----------------------------------------------------------------------------------*/
  203. void DS1307_GetTime(unsigned char *h_ptr,unsigned char *m_ptr,unsigned char *s_ptr)
  204. {
  205. I2C_Start(); // Start I2C communication
  206.  
  207. DS1307_Write(DS1307_ID); // connect to DS1307 by sending its ID on I2c Bus
  208. DS1307_Write(SEC_ADDRESS); // Request Sec RAM address at 00H
  209.  
  210. I2C_Stop(); // Stop I2C communication after selecting Sec Register
  211.  
  212. I2C_Start(); // Start I2C communication
  213. DS1307_Write(0xD1); // connect to DS1307( under Read mode)
  214. //by sending its ID on I2c Bus
  215.  
  216. *s_ptr = DS1307_Read(); I2C_Ack(); // read second and return Positive ACK
  217. *m_ptr = DS1307_Read(); I2C_Ack(); // read minute and return Positive ACK
  218. *h_ptr = DS1307_Read(); I2C_NoAck(); // read hour and return Negative/No ACK
  219.  
  220. I2C_Stop(); // Stop I2C communication after reading the Time
  221. }
  222.  
  223.  
  224.  
  225.  
  226.  
  227.  
  228.  
  229.  
  230.  
  231.  
  232.  
  233. /*----------------------------------------------------------------------------------
  234. void DS1307_GetDate(unsigned char *y_ptr,unsigned char *m_ptr,unsigned char *d_ptr)
  235. -----------------------------------------------------------------------------------
  236. * I/P Arguments: char *,char *,char *-->pointers to get the y,m,d.
  237. * Return value : none
  238.  
  239. * description  :This function is used to get the Date(y,m,d) from Ds1307 RTC.
  240. Ds1307 ic is enabled by sending the DS1307 id on the I2C bus.
  241. After selecting DS1307, select the RAM address 0x00 to point to DAY
  242. Get Day, Month, Year one after the other.
  243. Stop the I2c communication.
  244. -----------------------------------------------------------------------------------*/
  245. void DS1307_GetDate(unsigned char *d_ptr,unsigned char *m_ptr,unsigned char *y_ptr)
  246. {
  247. I2C_Start(); // Start I2C communication
  248.  
  249. DS1307_Write(DS1307_ID); // connect to DS1307 by sending its ID on I2c Bus
  250. DS1307_Write(DATE_ADDRESS); // Request DAY RAM address at 04H
  251.  
  252. I2C_Stop(); // Stop I2C communication after selecting DAY Register
  253.  
  254.  
  255. I2C_Start(); // Start I2C communication
  256. DS1307_Write(0xD1); // connect to DS1307( under Read mode)
  257. // by sending its ID on I2c Bus
  258.  
  259. *d_ptr = DS1307_Read(); I2C_Ack(); // read Day and return Positive ACK
  260. *m_ptr = DS1307_Read(); I2C_Ack(); // read Month and return Positive ACK
  261. *y_ptr = DS1307_Read(); I2C_NoAck(); // read Year and return Negative/No ACK
  262.  
  263. I2C_Stop(); // Stop I2C communication after reading the Time
  264. }


Eeprom.c: 8051 At2404/02 C-Library

  1. /*---------------------------------------------------------------------------------------*
  2. 8051 At2404/02 library
  3. Filename: eeprom.c
  4. Controller: P89V51RD2/89c51(8051 family)
  5. Oscillator: 11.0592 MHz
  6. Author: XploreLabz
  7. website: www.xplorelabz.com
  8.  
  9. ----------------------------------------------------------------------------------------*/
  10.  
  11. #include<reg51.h>
  12. #include"i2c.h"
  13. #include"delay.h"
  14.  
  15.  
  16. /* EEPROM_ID is fixed and should not be changed,
  17. Refer At2404/02 DataSheet for more info*/
  18. #define EEPROM_ID 0xA0
  19.  
  20.  
  21.  
  22.  
  23.  
  24. /*---------------------------------------------------------------------------------------
  25. void EEPROM_WriteByte(unsigned char eeprom_Address, unsigned char eeprom_Data)
  26. ----------------------------------------------------------------------------------------
  27. * I/P Arguments: char,char-->eeprom_address at which eeprom_data is to be written.
  28. * Return value : none
  29.  
  30. * description:This function is used to write the data at specified EEPROM_address..
  31. At2404 ic is enabled by sending its ID on the i2c bus.
  32. After selecting At2404 ic,select the address where the data is to written
  33. Write the Data at selected EppromAddress
  34. Stop the I2c communication.
  35. ----------------------------------------------------------------------------------------*/
  36. void EEPROM_WriteByte(unsigned char eeprom_Address, unsigned char eeprom_Data)
  37. {
  38.  
  39. I2C_Start(); // Start i2c communication
  40. I2C_Write(EEPROM_ID); // connect to AT2404 by sending its ID on I2c Bus
  41. I2C_Ack();
  42. I2C_Write(eeprom_Address); // Select the Specified EEPROM address of AT2404
  43. I2C_Ack();
  44. I2C_Write(eeprom_Data); // Write the data at specified address
  45. I2C_Ack();
  46. I2C_Stop(); // Stop i2c communication after Writing the data
  47. delay_ms(1); // Write operation takes max 5ms, refer At2404 datasheet
  48. }
  49.  
  50.  
  51.  
  52.  
  53.  
  54.  
  55.  
  56.  
  57.  
  58.  
  59.  
  60.  
  61. /*-----------------------------------------------------------------------------------------
  62. unsigned char EEPROM_ReadByte(unsigned char eeprom_Address)
  63. ------------------------------------------------------------------------------------------
  64. * I/P Arguments: char-->eeprom_address from where eeprom_data is to be read.
  65. * Return value : char-->data read from Eeprom.
  66.  
  67. * description:
  68. This function is used to read the data from specified EEPROM_address.
  69. At2404 ic is enabled by sending its ID on the i2c bus.
  70. After selecting At2404 ic,select the address from where the data is to read
  71. Read the Data from selected EppromAddress
  72. Stop the I2c communication.
  73. Return the Data read from Eeprom
  74. -----------------------------------------------------------------------------------------*/
  75. unsigned char EEPROM_ReadByte(unsigned char eeprom_Address)
  76. {
  77. unsigned char eeprom_Data;
  78.  
  79. I2C_Start(); // Start i2c communication
  80. I2C_Write(EEPROM_ID); // connect to AT2404(write) by sending its ID on I2c Bus
  81. I2C_Ack();
  82. I2C_Write(eeprom_Address); // Select the Specified EEPROM address of AT2404
  83. I2C_Ack();
  84.  
  85. I2C_Start(); // Start i2c communication
  86. I2C_Write(0xA1); // connect to AT2404(read) by sending its ID on I2c Bus
  87. I2C_Ack();
  88. eeprom_Data = I2C_Read(); // Read the data from specified address
  89. I2C_NoAck();
  90. I2C_Stop(); // Stop i2c communication after Reading the data
  91. delay_us(10);
  92. return eeprom_Data; // Return the Read data
  93.  
  94. }
  95.  
  96.  
  97.  
  98.  
  99.  
  100.  
  101.  
  102.  
  103.  
  104.  
  105.  
  106.  
  107.  
  108.  
  109.  
  110.  
  111.  
  112.  
  113.  
  114.  
  115.  
  116.  
  117.  
  118.  
  119.  
  120.  
  121.  
  122.  
  123.  
  124.  
  125.  
  126. /*---------------------------------------------------------------------------------------
  127. void EEPROM_WriteNBytes(unsigned char EepromAddr, unsigned char *RamAddr, char NoOfBytes)
  128. ----------------------------------------------------------------------------------------
  129. * I/P Arguments: char,-->eeprom_address from where the N-bytes are to be written.
  130. char*-->Pointer to the N-bytes of data to be written.
  131. char --> Number of bytes to be written
  132.  
  133. * Return value : none
  134.  
  135. * description:
  136. This function is used to write N-bytes of data at specified EEPROM_address.
  137. EEPROM_WriteByte() function is called to write a byte at atime.
  138. Source(RAM) and destination(EEPROM) address are incremented after each write
  139. NoOfBytes is Decemented each time a byte is written.
  140. Above Operation is carried out till all the bytes are written(NoOfBytes!=0)
  141. ---------------------------------------------------------------------------------------*/
  142. void EEPROM_WriteNBytes(unsigned char EepromAddr, unsigned char *RamAddr, char NoOfBytes)
  143. {
  144. while(NoOfBytes != 0)
  145. {
  146. EEPROM_WriteByte(EepromAddr,*RamAddr); //Write a byte from RAM to EEPROM
  147. EepromAddr++; //Incerement the Eeprom Address
  148. RamAddr++; //Increment the RAM Address
  149. NoOfBytes--; //Decrement NoOfBytes after writing each Byte
  150. }
  151. }
  152.  
  153.  
  154.  
  155. /*---------------------------------------------------------------------------------------
  156. void EEPROM_ReadNBytes(unsigned char EepromAddr, unsigned char *RamAddr, char NoOfBytes)
  157. ----------------------------------------------------------------------------------------
  158. * I/P Arguments: char,-->eeprom_address from where the N-bytes is to be read.
  159. char*-->Pointer into which the N-bytes of data is to be read.
  160. char --> Number of bytes to be Read
  161.  
  162. * Return value : none
  163.  
  164. * description:
  165. This function is used to Read N-bytes of data from specified EEPROM_address.
  166. EEPROM_ReadByte() func is called to read a byte at a time.
  167. Source(RAM) and destination(EEPROM) address are incremented each time.
  168. NoOfBytes is Decemented after a byte is read.
  169. Above Operation is carried out till all the bytes are read(NoOfBytes!=0)
  170. ---------------------------------------------------------------------------------------*/
  171. void EEPROM_ReadNBytes(unsigned char EepromAddr, unsigned char *RamAddr, char NoOfBytes)
  172. {
  173. while(NoOfBytes != 0)
  174. {
  175. *RamAddr = EEPROM_ReadByte(EepromAddr);//Read a byte from EEPROM to RAM
  176. EepromAddr++; //Incerement the Eeprom Address
  177. RamAddr++; //Increment the RAM Address
  178. NoOfBytes--; //Decrement NoOfBytes after Reading each Byte
  179. }
  180. }
  181.  
  182.  
  183.  
  184.  
  185.  
  186.  
  187.  
  188. /*---------------------------------------------------------------------------------------
  189. void EEPROM_WriteString(unsigned char eeprom_address, unsigned char * source_address)
  190. ----------------------------------------------------------------------------------------
  191. * I/P Arguments: char,-->eeprom_address where the String is to be written.
  192. char*-->Pointer to String which has to be written.
  193.  
  194. * Return value : none
  195.  
  196. * description:This function is used to Write a String at specified EEPROM_address.
  197. EEPROM_WriteByte() function is called to write a byte at a time.
  198. Source(RAM) and destination(EEPOM) address are incremented each time.
  199. Above Operation is carried out till Null char is identified.
  200.  
  201. NOTE: Null char is also written into the eeprom.
  202. ---------------------------------------------------------------------------------------*/
  203. void EEPROM_WriteString(unsigned char eeprom_address, unsigned char * source_address)
  204. {
  205.  
  206. do
  207. {
  208. EEPROM_WriteByte(eeprom_address,*source_address); //Write a byte from RAM to EEPROM
  209. source_address++; //Incerement the RAM Address
  210. eeprom_address++; //Increment the Eeprom Address
  211. } while(*(source_address-1) !=0);
  212. }
  213.  
  214.  
  215.  
  216.  
  217. /*---------------------------------------------------------------------------------------
  218. void EEPROM_ReadString(unsigned char eeprom_address, unsigned char * destination_address)
  219. ----------------------------------------------------------------------------------------
  220. * I/P Arguments: char,-->eeprom_address from where the String is to be read.
  221. char*-->Pointer into which the String is to be read.
  222.  
  223. * Return value : none
  224.  
  225. * description:This function is used to Read a String from specified EEPROM_address.
  226. EEPROM_ReadByte() function is called to read a byte at a time.
  227. Source(EEPROM) and destination(RAM) address are incremented each time.
  228. Above Operation is carried out till Null char is identified.
  229. ---------------------------------------------------------------------------------------*/
  230. void EEPROM_ReadString(unsigned char eeprom_address, unsigned char * destination_address)
  231. {
  232. char eeprom_data;
  233.  
  234. do
  235. {
  236. eeprom_data = EEPROM_ReadByte(eeprom_address); //Read a byte from EEPROM to RAM
  237. *destination_address = eeprom_data; //Copy the data into String Buffer
  238. destination_address++; //Incerement the RAM Address
  239. eeprom_address++; //Increment the Eeprom Address
  240. }while(eeprom_data!=0);
  241. }
  242.  
  243.  
  244.  
  245.  
  246.  
  247.  
  248.  
  249.  
  250. /*-----------------------------------------------------------------------------------------
  251. void EEPROM_Erase()
  252. ------------------------------------------------------------------------------------------
  253. * I/P Arguments: none
  254.  
  255. * Return value : none
  256.  
  257. * description:This function is used to erase the entire Eeprom memory.
  258. Eeprom is filled with 0xFF to accomplish the Eeprom Erase.
  259. EEPROM_WriteByte() function is called to write a byte at a time.
  260. Whole memory(0-255) is traversed and filled with 0xFF
  261. -----------------------------------------------------------------------------------------*/
  262. void EEPROM_Erase()
  263. {
  264. unsigned char eeprom_address;
  265.  
  266. for(eeprom_address=0;eeprom_address<255;eeprom_address++)
  267. {
  268. EEPROM_WriteByte(eeprom_address,0xff); // Write Each memory location with OxFF
  269. }
  270. }