[关闭]
@Mahdi 2017-08-10T15:27:16.000000Z 字数 12338 阅读 1087

源代码记录

code


  1. #include<reg52.h>
  2. #define uint unsigned int
  3. sbit P2_0 = P2^0;
  4. sbit P2_1 = P2^1;
  5. void delay(uint z)
  6. {
  7. uint i, j;
  8. for(i = z; i > 0; i--)
  9. for(j = 114; j>0; j--);
  10. }
  11. void main()
  12. {
  13. while(1)
  14. {
  15. P2_0 = 0;
  16. delay(10);
  17. P2_0 = 1;
  18. delay(10);
  19. P2_1 = 0;
  20. delay(1000);
  21. P2_1 = 1;
  22. delay(1000);
  23. }
  24. }
  25. //改变占空比 改变电流的强弱

  1. #include<reg52.h>
  2. #define uint unsigned int
  3. sbit P2_0 = P2^0;
  4. void delay(void)
  5. {
  6. uint i;
  7. for(i = 1000; i > 0; i--);
  8. }
  9. void main()
  10. { {
  11. P2_0 = 0;
  12. delay();
  13. P2_0 = 1;
  14. delay();
  15. }while(1);
  16. }

  1. /*2.35Khz*/
  2. #include<reg52.h>
  3. #define uint unsigned int
  4. sbit P2_0 = P2^0;
  5. sbit P2_1 = P2^1;
  6. void delay(uint z)
  7. {
  8. uint i, j;
  9. for(i = z; i > 0; i--)
  10. for(j = 50; j>0; j--);
  11. }
  12. void main()
  13. {
  14. while(1)
  15. {
  16. P2_0 = 0;
  17. delay(1);
  18. P2_0 = 1;
  19. delay(1);
  20. }
  21. }

  1. /*双波*/
  2. #include<reg52.h>
  3. #define uint unsigned int
  4. sbit P2_0 = P2^0; //highWave
  5. sbit P0_0 = P0^0; //lowWave
  6. uint m;
  7. void delay(uint z)
  8. {
  9. uint i, j;
  10. for(i = z; i > 0; i--)
  11. for(j = 50; j>0; j--);
  12. }
  13. void highWave(void)
  14. {
  15. P2_0 = 0;
  16. delay(1);
  17. P2_0 = 1;
  18. delay(1);
  19. }
  20. void lowWave(void)
  21. {
  22. P0_0 = 0;
  23. delay(100);
  24. P0_0 = 1;
  25. delay(1);
  26. }
  27. void main()
  28. {
  29. do
  30. {
  31. lowWave();
  32. for(m = 0; m < 10; m++)
  33. {
  34. highWave();
  35. }
  36. }while(1);
  37. }

  1. #include<reg52.h>
  2. typedef unsigned char u8;
  3. typedef unsigned int u16;
  4. sbit beep = P2^0;
  5. void delay(u16 i)
  6. {
  7. while(i--);//i = 1大约10um
  8. }
  9. void main()
  10. { while(1)
  11. {
  12. delay(10000);
  13. beep = ~beep;
  14. }
  15. }

  1. /*改进双波*/
  2. #include<reg52.h>
  3. #define uint unsigned int
  4. sbit P2_0 = P2^0; //highWave
  5. sbit P0_0 = P0^0; //lowWave
  6. uint m;
  7. void delay(uint z)
  8. {
  9. uint i, j;
  10. for(i = z; i > 0; i--)
  11. for(j = 50; j>0; j--);
  12. }
  13. void highWave(void)
  14. {
  15. P2_0 = 0;
  16. delay(1);
  17. P2_0 = 1;
  18. delay(1);
  19. }
  20. void lowWave(void)
  21. {
  22. P0_0 = 0;
  23. delay(100);
  24. P0_0 = 1;
  25. delay(1);
  26. }
  27. void main()
  28. {
  29. do
  30. { P0 = 0x00;
  31. P2 = 0x00;
  32. lowWave();
  33. for(m = 0; m < 10; m++)
  34. {
  35. highWave();
  36. }
  37. }while(1);
  38. }

**

  1. #include<reg52.h>
  2. #define uint unsigned int
  3. sbit P2_0 = P2^0; //highWave
  4. sbit P0_0 = P0^0; //lowWave
  5. uint m;
  6. void delay(uint z)
  7. {
  8. uint i, j;
  9. for(i = z; i > 0; i--)
  10. for(j = 50; j>0; j--);
  11. }
  12. void highWave(void)
  13. {
  14. P2_0 = 1;
  15. delay(1);
  16. P2_0 = 0;
  17. delay(1);
  18. }
  19. void lowWave(void)
  20. {
  21. P0_0 = 0;
  22. delay(100);
  23. P0_0 = 1;
  24. delay(1);
  25. }
  26. void main()
  27. {
  28. while(1)
  29. { P0 = 0x00;
  30. P2 = 0x00;
  31. lowWave();
  32. for(m = 0; m < 10; m++)
  33. {
  34. highWave();
  35. }
  36. }
  37. }

  1. /*条形流水灯*/
  2. #include<reg52.h>
  3. #define uint unsigned int
  4. void main()
  5. {
  6. uint i = 0;
  7. uint cnt1 = 0;
  8. while(1)
  9. {
  10. P2 = (0xfe<<cnt1);
  11. for(i = 0; i < 10000; i++);
  12. cnt1++;
  13. if(cnt1 >=8)
  14. { uint cnt2 = 0;
  15. for( ;cnt2 < 8; cnt2++)
  16. {
  17. P2 = (0x7f>>cnt2);
  18. for(i = 0; i < 10000; i++);
  19. cnt1--;
  20. }
  21. }
  22. }
  23. }

移位与数据类型的位数有关,取反也是

  1. ##左右移函数实现流水灯
  2. #include<reg52.h>
  3. #include<intrins.h> //intrinsic 固有的 本征的
  4. #define led P2
  5. typedef unsigned char u8;
  6. typedef unsigned int u16;
  7. void delay(u16 i)
  8. {
  9. while(i--);
  10. }
  11. void main()
  12. {
  13. u8 i = 0;
  14. led = 0xfe;
  15. delay(50000);
  16. while(1)
  17. {
  18. for(i = 0; i< 7; i++)
  19. {
  20. /*
  21. crol是字符循环左移
  22. irol是整数循环左移
  23. lrol是长整数循环左移
  24. <<不循环左移
  25. <<左移,最高位移出去了就丢弃,最低位补0.
  26. _crol_则是循环左移,最高位移出去后不丢弃
  27. 而是补到最低位,也就是说,最低位不补0.
  28. */
  29. led = _crol_(led,1);
  30. //_crol_(a,b) a需要左移的值 b左移的位数
  31. //0xfe 1111 1110 循环左移后 1111 1101, 1111 1011, 1111 0111
  32. delay(50000);
  33. }
  34. for(i = 0; i < 7; i++)
  35. {
  36. led = _cror_(led,1);
  37. delay(50000);
  38. }
  39. // led = 0;
  40. // delay(50000); //450ms
  41. // led = 1;
  42. // delay(50000);
  43. }
  44. }

  1. #include<reg52.h>
  2. #define uint unsigned int
  3. void main()
  4. {
  5. uint m=0,i = 0;
  6. char cnt1 = 0;
  7. while(1)
  8. {
  9. P2 = ~(~0xfe<<cnt1);
  10. for(i = 0; i < 30000; i++);
  11. cnt1++;
  12. if(cnt1 >=8)
  13. { unsigned char cnt2 = 0;
  14. for( ;cnt2 < 8; cnt2++)
  15. {
  16. P2 = ~(~0x7f>>cnt2);
  17. for(i = 0; i < 3000; i++);
  18. cnt1--;
  19. }
  20. }
  21. }
  22. }

  1. /*左移函数*/
  2. #include <reg52.h>
  3. #include <intrins.h>
  4. void delay(unsigned int z);
  5. void delay(unsigned int z)
  6. {
  7. unsigned int x,y;
  8. for(x = z;x > 0;x--)
  9. for(y = 114;y > 0;y--);
  10. }
  11. int main()
  12. {
  13. int i = 0;
  14. unsigned char t;
  15. t = 0xfe;
  16. P2 = t;
  17. while(1)
  18. {
  19. for(i = 0;i < 8;i++)
  20. {
  21. t = _crol_(t,1);
  22. P2 = t;
  23. delay(500);
  24. }
  25. }
  26. }

数码管的静态显示实现简单的秒表

  1. #include <reg52.h>
  2. sbit A1 = P2^2;
  3. sbit A2 = P2^3;
  4. sbit A3 = P2^4;
  5. unsigned char code LedChar[] = {0xC0, 0xF9, 0xA4, 0xB0,
  6. 0x99, 0x92, 0x82, 0xF8,
  7. 0x80, 0x90, 0x88, 0x83,
  8. 0xC6, 0xA1, 0x86, 0x8E};//将数组放入Flash中存储
  9. unsigned char code LedbitA1[] = {0,1};
  10. unsigned char code LedbitA2[] = {0,1};
  11. unsigned char code LedbitA3[] = {0,1};
  12. void main()
  13. { unsigned char cnt = 0; //记录中断次数
  14. unsigned char sec = 0; //记录经过的秒数
  15. TMOD = 0X01; //设置T0为模式1
  16. TH0 = 0XB8; //为T0赋值0xb800
  17. TL0 = 0X00;
  18. TR0 = 1; //启动T0 溢出后也在运行但需要重载
  19. while(1)
  20. {
  21. if(TF0 == 1)
  22. {
  23. TF0 = 0; //T0溢出后软件清0中断标志
  24. TH0 = 0xB8; //重载 重新对T0赋初值
  25. TL0 = 0x00;
  26. cnt++;
  27. if(cnt >= 50) //判断T0的溢出次数是否达到50次
  28. {
  29. cnt = 0;
  30. P0 = ~LedChar[sec]; //将当前时间对应在真值表中的值送入P0口
  31. A3 = LedbitA3[sec];
  32. A2 = LedbitA2[sec];
  33. A1 = LedbitA1[sec];
  34. sec++;
  35. if(sec >= 16)// 当前秒数超过0X0F后,重新计时
  36. {
  37. sec = 0;
  38. }
  39. }
  40. }
  41. }
  42. }

动态扫描实现秒表

  1. /***最大到3.17年***/
  2. /***138进行位选;245进行段选***/
  3. #include<reg52.h>
  4. #define uchar unsigned char
  5. #define uint unsigned int
  6. #define ulong unsigned long int
  7. sbit A1 = P2^2;
  8. sbit A2 = P2^3;
  9. sbit A3 = P2^4;
  10. ulong cnt = 0;
  11. uint sec = 0;
  12. unsigned char code LedChar[] = { //数码管显示字符转换表
  13. 0xC0, 0xF9, 0xA4, 0xB0, 0x99, 0x92, 0x82, 0xF8,
  14. 0x80, 0x90, 0x88, 0x83, 0xC6, 0xA1, 0x86, 0x8E
  15. };
  16. unsigned char LedBuff[8] = { //数码管显示缓冲区,共阴极数码管初值 0x00 确保启动时都不亮
  17. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  18. };
  19. void init()
  20. {
  21. TMOD = 0x01;
  22. TH0 = 0xDC; //初值越大到达溢出的时间越短,即周期越短,最短时间12/11059200 s = 1/921600 s d=1
  23. TL0 = 0x00;
  24. TR0 = 1;
  25. }
  26. void reinit()
  27. {
  28. TF0 = 0;
  29. TH0 = 0xDC; //刷新周期与初值有关 初值为0xB800溢出周期为20ms
  30. TL0 = 0x00; //刷新周期为(数码管个数*20ms),刷新速度太慢,要在10ms以内
  31. } //周期数 = 921600*溢出周期
  32. void main()
  33. { uchar i = 0;
  34. init();
  35. while(1)
  36. {
  37. if(TF0 == 1)
  38. {
  39. reinit();
  40. cnt++;
  41. if(cnt == 100)
  42. {
  43. cnt = 0;
  44. sec++;
  45. LedBuff[0] = ~LedChar[sec%10]; //第1位数码管
  46. LedBuff[1] = ~LedChar[sec/10%10]; //每点亮一个数码管的间隔时间是定时器的溢出周期
  47. LedBuff[2] = ~LedChar[sec/100%10]; //点亮8个数码管至少要在10ms以内 即8*溢出周期<10ms
  48. LedBuff[3] = ~LedChar[sec/1000%10];
  49. LedBuff[4] = ~LedChar[sec/10000%10];
  50. LedBuff[5] = ~LedChar[sec/100000%10];
  51. LedBuff[6] = ~LedChar[sec/1000000%10];
  52. LedBuff[7] = ~LedChar[sec/10000000%10]; //第8位数码管
  53. }
  54. //以下代码 进行数码管的位选 完成数码管动态扫描刷新
  55. if (i == 0)
  56. { A3=0; A2=0; A1=0; i++; P0=LedBuff[0]; }
  57. else if (i == 1)
  58. { A3=0; A2=0; A1=1; i++; P0=LedBuff[1]; }
  59. else if (i == 2)
  60. { A3=0; A2=1; A1=0; i++; P0=LedBuff[2]; }
  61. else if (i == 3)
  62. { A3=0; A2=1; A1=1; i++; P0=LedBuff[3]; }
  63. else if (i == 4)
  64. { A3=1; A2=0; A1=0; i++; P0=LedBuff[4]; }
  65. else if (i == 5)
  66. { A3=1; A2=0; A1=1; i++; P0=LedBuff[5]; }
  67. else if (i == 6)
  68. { A3=1; A2=1; A1=0; i++; P0=LedBuff[6]; }
  69. else if (i == 7)
  70. { A3=1; A2=1; A1=1; i=0; P0=LedBuff[7]; }
  71. }
  72. }
  73. }

  1. #include<reg52.h>
  2. #include<intrins.h>
  3. #define uint unsigned int
  4. uint cnt = 0;
  5. sbit wave1 = P0^0;
  6. sbit wave2 = P0^3;
  7. void delay20ms(void) //误差 0us
  8. {
  9. unsigned char a,b;
  10. for(b=215;b>0;b--)
  11. for(a=45;a>0;a--);
  12. _nop_(); //if Keil,require use intrins.h
  13. _nop_(); //if Keil,require use intrins.h
  14. }
  15. void main()
  16. {
  17. EA = 1;
  18. TMOD = 0x01;
  19. TH0 = 0x3C;
  20. TL0 = 0xB0;
  21. ET0 = 1;
  22. TR0 = 1;
  23. while(1)
  24. {
  25. wave1 = 0;
  26. delay20ms();
  27. wave2 = 1;
  28. delay20ms();
  29. }
  30. }
  31. void InterrupteWave2()interrupt 1
  32. {
  33. TH0 = 0x3C;
  34. TL0 = 0xB0;
  35. cnt++;
  36. if(cnt = 20)
  37. {
  38. wave2 = 0;
  39. delay20ms();
  40. wave2 = 1;
  41. delay20ms();
  42. cnt = 0;
  43. }
  44. }

LCD1602

  1. /*有鬼影版*/
  2. #include<reg52.h>
  3. #define LCD1602_DB P0//宏定义替换
  4. //类型别称定义
  5. typedef unsigned char uchar;
  6. typedef unsigned int uint;
  7. sbit LCD1602_RS = P2^6;
  8. sbit LCD1602_RW = P2^5;
  9. sbit LCD1602_E = P2^7;
  10. //读状态
  11. void read_sta()
  12. {
  13. uchar sta;
  14. LCD1602_DB = 0xff;
  15. LCD1602_RS = 0;
  16. LCD1602_RW = 1;
  17. do
  18. {
  19. LCD1602_E = 1;
  20. sta = LCD1602_DB;//输出状态字
  21. LCD1602_E = 0;
  22. }while(sta & 0x80);
  23. }
  24. //写指令
  25. void write_cmd(uchar cmd)
  26. {
  27. read_sta();
  28. LCD1602_RS = 0;
  29. LCD1602_RW = 0;
  30. LCD1602_DB = cmd;
  31. LCD1602_E = 1;
  32. LCD1602_E = 0;
  33. }
  34. //写数据
  35. void write_dat(uchar dat)
  36. {
  37. read_sta();
  38. LCD1602_RS = 1;
  39. LCD1602_RW = 0;
  40. LCD1602_DB = dat;
  41. LCD1602_E = 1;
  42. LCD1602_E = 0;
  43. }
  44. void main()
  45. {
  46. /*lcd 初始化*/
  47. write_cmd(0x38);//设置16*2显示
  48. write_cmd(0x0c);//显示开/关以及光标设置指令
  49. write_cmd(0x06);//地址指针加一
  50. write_cmd(0x01);//清屏
  51. write_cmd(0x80|0x02);//设置显示地址
  52. write_dat(0x58);//设置显示内容
  53. write_dat(0x75);
  54. write_dat(0x65);
  55. write_dat(0x20);
  56. write_dat(0x53);
  57. write_dat(0x6f);
  58. write_dat(0x6e);
  59. write_dat(0x67);
  60. write_dat(0x20);
  61. write_dat(0x4a);
  62. write_dat(0x75);
  63. write_dat(0x6e);
  64. write_cmd(0x80|0x42);
  65. write_dat(0x48);
  66. write_dat(0x65);
  67. write_dat(0x6c);
  68. write_dat(0x6c);
  69. write_dat(0x6f);
  70. write_dat(0x20);
  71. write_dat(0x57);
  72. write_dat(0x6f);
  73. write_dat(0x72);
  74. write_dat(0x6c);
  75. write_dat(0x64);
  76. write_dat(0x21);
  77. }

LCD1602.c

  1. #include<lcd1602.h>
  2. void ReadSta()
  3. {
  4. u8 sta;
  5. DB = 0xff;
  6. RS = 0;
  7. RW = 1;
  8. do
  9. {
  10. LCD_E = 1;
  11. sta = DB;
  12. LCD_E = 0;
  13. }while(sta & 0x80);
  14. }
  15. void WriteCmd(u8 cmd)
  16. {
  17. ReadSta();
  18. RS = 0;
  19. RW = 0;
  20. DB = cmd;
  21. LCD_E = 1;
  22. LCD_E = 0;
  23. }
  24. void WriteData(u8 dat)
  25. {
  26. ReadSta();
  27. RS = 1;
  28. RW = 0;
  29. DB = dat;
  30. LCD_E = 1;
  31. LCD_E = 0;
  32. }

无源蜂鸣器

  1. #include <reg52.h>
  2. sbit fmq=P1^5;
  3. void delay(unsigned int i)
  4. {
  5. unsigned char j;
  6. while(i--)
  7. {
  8. for(j=0;j<115;j++);
  9. }
  10. }
  11. void yanzou(unsigned char pinlv,unsigned int jiepai)
  12. {
  13. unsigned char pl;
  14. unsigned int jp;
  15. if(jiepai==1) delay(250);
  16. else if(jiepai==2) delay(500);
  17. else
  18. {
  19. for(jp=0;jp<jiepai;jp++)
  20. {
  21. fmq=0;
  22. for(pl=0;pl<pinlv;pl++);
  23. fmq=1;
  24. for(pl=0;pl<pinlv;pl++);
  25. }
  26. }
  27. }
  28. void main()
  29. {
  30. unsigned char i,x;
  31. unsigned char code pinlv[]={131,110,98, 87, 73, 87, 110,98, 131,0,110,98, 87, 73, 73, 65, 98, 87,87,87,73,65,73,65,55,58,65,73,65,87,110,98,87,73,110,131,110,98,87,65,73,73,0,87,65,65,73,82,87,98,87,73,131,110,98,0,110,98,87,73,65,55,58,65,73,87,65,65};
  32. unsigned int code jiepai[]={110,131,147,494,196,165,131,294,440,1,131,147,165,588,196,440,294,660,330,165,196,880,588,220,262,124,110,196,220,330,131,147,495,196,262,220,131,147,165,220,784,392,2,660,660,220,196,175,330,588,495,196,110,131,147,2,131,147,330,392,440,524,247,220,196,165,880,880};
  33. i=68;
  34. for(x=0;x<i;x++)
  35. {
  36. yanzou(pinlv[x],jiepai[x]);
  37. }
  38. }

电子设计大赛


光照传感器


main.c

  1. /**************************************************************************************
  2. * EEPROM-IIC实验 *
  3. 实现现象:下载程序后数码管后4位显示0,按K1保存显示的数据,按K2读取上次保存的数据,
  4. 按K3显示数据加一,按K4显示数据清零。最大能写入的数据是255.
  5. 注意事项:由于P3.2口跟红外线共用,所以做按键实验时为了不让红外线影响实验效果,最好把红外线先取下来。
  6. ***************************************************************************************/
  7. #include "reg52.h" //此文件中定义了单片机的一些特殊功能寄存器
  8. #include "i2c.h"
  9. #include "lcd.h"
  10. typedef unsigned int u16; //对数据类型进行声明定义
  11. typedef unsigned char u8;
  12. typedef unsigned long int u32;
  13. u32 num=0;
  14. /*******************************************************************************
  15. * 函 数 名 : delay
  16. * 函数功能 : 延时函数,i=1时,大约延时10us
  17. *******************************************************************************/
  18. void delay(u16 i)
  19. {
  20. while(i--);
  21. }
  22. /*******************************************************************************
  23. * 函数名 :Keypros()
  24. * 函数功能 :按键处理函数
  25. * 输入 : 无
  26. * 输出 : 无
  27. *******************************************************************************/
  28. void get_from_lux(void)
  29. {
  30. num = Lux_Read(0x00); //读取EEPROM地址1内的数据保存在num中
  31. num |= Lux_Read(0x01) << 8; //读取EEPROM地址1内的数据保存在num中
  32. num |= Lux_Read(0x02) << 16; //读取EEPROM地址1内的数据保存在num中
  33. num |= Lux_Read(0x03) << 24; //读取EEPROM地址1内的数据保存在num中
  34. num /= 1000;
  35. }
  36. //95
  37. void itoa(u32 n, char *buf)
  38. {
  39. int i = 0;
  40. do {
  41. buf[i] = n%10 + '0';
  42. n /= 10;
  43. i++;
  44. } while (n);
  45. buf[i] = '\0';
  46. }
  47. void lux_display(void)
  48. {
  49. int i;
  50. char str[16] = {0};
  51. get_from_lux();
  52. itoa(num, str);
  53. for (i = 0; '\0' != str[i]; i++);
  54. while (i--) {
  55. //LcdWriteCom(0x80 + i );
  56. LcdWriteData(str[i]);
  57. }
  58. }
  59. void delay500ms(void) //误差 0us
  60. {
  61. unsigned char a,b,c;
  62. for(c=23;c>0;c--)
  63. for(b=152;b>0;b--)
  64. for(a=70;a>0;a--);
  65. }
  66. void main()
  67. {
  68. LcdInit();
  69. while(1)
  70. {
  71. LcdWriteCom(0x80); //设置数据指针起点
  72. lux_display( );
  73. delay500ms();
  74. }
  75. }

i2c.c

  1. #include "i2c.h"
  2. /*******************************************************************************
  3. * 函数名 : Delay10us()
  4. * 函数功能 : 延时10us
  5. * 输入 : 无
  6. * 输出 : 无
  7. *******************************************************************************/
  8. void Delay10us()
  9. {
  10. unsigned char a,b;
  11. for(b=1;b>0;b--)
  12. for(a=2;a>0;a--);
  13. }
  14. /*******************************************************************************
  15. * 函数名 : I2cStart()
  16. * 函数功能 : 起始信号:在SCL时钟信号在高电平期间SDA信号产生一个下降沿
  17. * 输入 : 无
  18. * 输出 : 无
  19. * 备注 : 起始之后SDA和SCL都为0
  20. *******************************************************************************/
  21. void I2cStart()
  22. {
  23. SDA=1;
  24. Delay10us();
  25. SCL=1;
  26. Delay10us();//建立时间是SDA保持时间>4.7us
  27. SDA=0;
  28. Delay10us();//保持时间是>4us
  29. SCL=0;
  30. Delay10us();
  31. }
  32. /*******************************************************************************
  33. * 函数名 : I2cStop()
  34. * 函数功能 : 终止信号:在SCL时钟信号高电平期间SDA信号产生一个上升沿
  35. * 输入 : 无
  36. * 输出 : 无
  37. * 备注 : 结束之后保持SDA和SCL都为1;表示总线空闲
  38. *******************************************************************************/
  39. void I2cStop()
  40. {
  41. SDA=0;
  42. Delay10us();
  43. SCL=1;
  44. Delay10us();//建立时间大于4.7us
  45. SDA=1;
  46. Delay10us();
  47. }
  48. /*******************************************************************************
  49. * 函数名 : I2cSendByte(unsigned char dat)
  50. * 函数功能 : 通过I2C发送一个字节。在SCL时钟信号高电平期间,保持发送信号SDA保持稳定
  51. * 输入 : num
  52. * 输出 : 0或1。发送成功返回1,发送失败返回0
  53. * 备注 : 发送完一个字节SCL=0,SDA=1
  54. *******************************************************************************/
  55. unsigned char I2cSendByte(unsigned char dat)
  56. {
  57. unsigned char a=0,b=0;//最大255,一个机器周期为1us,最大延时255us。
  58. for(a=0;a<8;a++)//要发送8位,从最高位开始
  59. {
  60. SDA=dat>>7; //起始信号之后SCL=0,所以可以直接改变SDA信号
  61. dat=dat<<1;
  62. Delay10us();
  63. SCL=1;
  64. Delay10us();//建立时间>4.7us
  65. SCL=0;
  66. Delay10us();//时间大于4us
  67. }
  68. SDA=1;
  69. Delay10us();
  70. SCL=1;
  71. while(SDA)//等待应答,也就是等待从设备把SDA拉低
  72. {
  73. b++;
  74. if(b>200) //如果超过2000us没有应答发送失败,或者为非应答,表示接收结束
  75. {
  76. SCL=0;
  77. Delay10us();
  78. return 0;
  79. }
  80. }
  81. SCL=0;
  82. Delay10us();
  83. return 1;
  84. }
  85. /*******************************************************************************
  86. * 函数名 : I2cReadByte()
  87. * 函数功能 : 使用I2c读取一个字节
  88. * 输入 : 无
  89. * 输出 : dat
  90. * 备注 : 接收完一个字节SCL=0,SDA=1.
  91. *******************************************************************************/
  92. unsigned char I2cReadByte()
  93. {
  94. unsigned char a=0,dat=0;
  95. SDA=1; //起始和发送一个字节之后SCL都是0
  96. Delay10us();
  97. for(a=0;a<8;a++)//接收8个字节
  98. {
  99. SCL=1;
  100. Delay10us();
  101. dat<<=1;
  102. dat|=SDA;
  103. Delay10us();
  104. SCL=0;
  105. Delay10us();
  106. }
  107. return dat;
  108. }
  109. /*******************************************************************************
  110. * 函数名 : void Lux_Write(unsigned char addr,unsigned char dat)
  111. * 函数功能 : 往24c02的一个地址写入一个数据
  112. * 输入 : 无
  113. * 输出 : 无
  114. *******************************************************************************/
  115. void Lux_Write(unsigned char addr,unsigned char dat)
  116. {
  117. I2cStart();
  118. I2cSendByte(0xa0);//发送写器件地址
  119. I2cSendByte(addr);//发送要写入内存地址
  120. I2cSendByte(dat); //发送数据
  121. I2cStop();
  122. }
  123. /*******************************************************************************
  124. * 函数名 : unsigned char Lux_Read(unsigned char addr)
  125. * 函数功能 : 读取24c02的一个地址的一个数据
  126. * 输入 : 无
  127. * 输出 : 无
  128. *******************************************************************************/
  129. unsigned char Lux_Read(unsigned char addr)
  130. {
  131. unsigned char num;
  132. I2cStart();
  133. I2cSendByte(0x94); //发送写器件地址
  134. I2cSendByte(addr); //发送要读取的地址
  135. I2cStart();
  136. I2cSendByte(0x95); //发送读器件地址
  137. num=I2cReadByte(); //读取数据
  138. I2cStop();
  139. return num;
  140. }

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注