自己做的一個仿真的單片機開發板,有完整的試驗步驟和仿真工程文件提供
板子上帶擴展ROM空間(i^2c 24c02)、adc(數字電壓表)、dac(信號發生器)、矩形按鍵(計算器),四個任務通過獨立按鍵進行切換
下面是At24c02讀寫部分的的單片機源程序
完整代碼和仿真工程文件請下載http://www.zg4o1577.cn/bbs/dpj-91558-1.html
- #include <reg51.h>
- #include "i2c.h"
- #include "lcd.h"
- //按鍵IO
- sbit K1=P3^0;
- sbit K2=P3^1;
- sbit K3=P3^2;
- sbit K4=P3^3;
- sbit key=P3^4;
- //--定義全局變量--//
- unsigned char wordCode0[6] = "write:";
- unsigned char wordCode1[6] = " read:";
- //--聲明全局函數--//
- void At24c02Write(unsigned char ,unsigned char );
- unsigned char At24c02Read(unsigned char );
- void Delay10ms(unsigned int c); //誤差 0us
- /*******************************************************************************
- * 函數名 : main
- * 函數功能 : 主函數
- *******************************************************************************/
- void num24c02()
- {
- unsigned int num0 = 0,num1 = 0,n;
- LcdInit();
- LcdWriteCom(0x80);
- for(n=0; n<6; n++)
- {
- LcdWriteData(wordCode0[n]);
- }
- LcdWriteCom(0x80 + 0x40);
- for(n=0; n<6; n++)
- {
- LcdWriteData(wordCode1[n]);
- }
- while(key)
- {
- if(K1 == 0) //按鍵1按下
- {
- Delay10ms(1);
- if(K1 == 0)
- At24c02Write(2,num0);
- while((n < 50)&&(K3==0))
- {
- n++;
- Delay10ms(1);
- }
- n=0;
- n=0;
- }
-
- if(K2 == 0) //按鍵2按下
- {
- Delay10ms(1);
- if(K2 == 0)
- num1 = At24c02Read(2);
- while((n < 50)&&(K2 == 0))
- {
- n++;
- Delay10ms(1);
- }
- n=0;
- }
- if(K3 == 0) //按鍵3按下
- {
- Delay10ms(1);
- if(K3 == 0)
- num0++;
- while((n < 50)&&(K3 == 0))
- {
- n++;
- Delay10ms(1);
- }
- n=0;
- if(num0==256)
- num0=0;
- }
-
- if(K4 == 0) //按鍵4按下
- {
- Delay10ms(1);
- if(K4 == 0)
- num0 = 0;
- while((n < 50) && (K4 == 0))
- {
- n++;
- Delay10ms(1);
- }
- n=0;
- }
- LcdWriteCom(0x86);
- LcdWriteData('0' + (num0%1000%100/10));//十位
- LcdWriteData('0' + (num0%1000%100%10));//個位
- LcdWriteCom(0x89);
- LcdWriteData('a');
- LcdWriteData('d');
- LcdWriteData('d');
- LcdWriteData('r');
- LcdWriteData('0' + (num0%1000%100/10));//十位
- LcdWriteData('0' + (num0%1000%100%10));//個位
- LcdWriteCom(0x86 + 0x40);
- LcdWriteData('0' + (num1%1000%100/10));//十位
- LcdWriteData('0' + (num1%1000%100%10));//個位
-
- }
- }
- /*******************************************************************************
- * 函 數 名 : Delay10ms
- * 函數功能 : 延時函數,延時10ms
- *******************************************************************************/
- void Delay10ms(unsigned int c) //誤差 0us
- {
- unsigned char a, b;
- for (;c>0;c--)
- {
- for (b=38;b>0;b--)
- {
- for (a=130;a>0;a--);
- }
- }
- }
- /*******************************************************************************
- * 函 數 名 : void At24c02Write(unsigned char addr,unsigned char dat)
- * 函數功能 : 往24c02的一個地址寫入一個數據
- *******************************************************************************/
- void At24c02Write(unsigned char addr,unsigned char dat)
- {
- I2C_Start();
- I2C_SendByte(0xa0, 1);//發送寫器件地址
- I2C_SendByte(addr, 1);//發送要寫入內存地址
- I2C_SendByte(dat, 0); //發送數據
- I2C_Stop();
- }
- /*******************************************************************************
- * 函 數 名 : unsigned char At24c02Read(unsigned char addr)
- * 函數功能 : 讀取24c02的一個地址的一個數據
- *******************************************************************************/
- unsigned char At24c02Read(unsigned char addr)
- {
- unsigned char num;
- I2C_Start();
- I2C_SendByte(0xa0, 1); //發送寫器件地址
- I2C_SendByte(addr, 1); //發送要讀取的地址
- I2C_Start();
- I2C_SendByte(0xa1, 1); //發送讀器件地址
- num=I2C_ReadByte(); //讀取數據
- I2C_Stop();
- return num;
- }
復制代碼
|