iic.c
/* # I2C代码片段说明
1. 本文件夹中提供的驱动代码供参赛选手完成程序设计参考。
2. 参赛选手可以自行编写相关代码或以该代码为基础,根据所选单片机类型、运行速度和试题
中对单片机时钟频率的要求,进行代码调试和修改。
*/
#include <STC15F2K60S2.H>
#include "iic.h"
#include "intrins.h"
sbit scl = P2^0;
sbit sda = P2^1;
#define DELAY_TIME 5
//
static void I2C_Delay(unsigned char n)
{
do
{
_nop_();_nop_();_nop_();_nop_();_nop_();
_nop_();_nop_();_nop_();_nop_();_nop_();
_nop_();_nop_();_nop_();_nop_();_nop_();
}
while(n--);
}
//
void I2CStart(void)
{
sda = 1;
scl = 1;
I2C_Delay(DELAY_TIME);
sda = 0;
I2C_Delay(DELAY_TIME);
scl = 0;
}
//
void I2CStop(void)
{
sda = 0;
scl = 1;
I2C_Delay(DELAY_TIME);
sda = 1;
I2C_Delay(DELAY_TIME);
}
//
void I2CSendByte(unsigned char byt)
{
unsigned char i;
for(i=0; i<8; i++){
scl = 0;
I2C_Delay(DELAY_TIME);
if(byt & 0x80){
sda = 1;
}
else{
sda = 0;
}
I2C_Delay(DELAY_TIME);
scl = 1;
byt <<= 1;
I2C_Delay(DELAY_TIME);
}
scl = 0;
}
//
unsigned char I2CReceiveByte(void)
{
unsigned char da;
unsigned char i;
for(i=0;i<8;i++){
scl = 1;
I2C_Delay(DELAY_TIME);
da <<= 1;
if(sda)
da |= 0x01;
scl = 0;
I2C_Delay(DELAY_TIME);
}
return da;
}
//
unsigned char I2CWaitAck(void)
{
unsigned char ackbit;
scl = 1;
I2C_Delay(DELAY_TIME);
ackbit = sda;
scl = 0;
I2C_Delay(DELAY_TIME);
return ackbit;
}
//
void I2CSendAck(unsigned char ackbit)
{
scl = 0;
sda = ackbit;
I2C_Delay(DELAY_TIME);
scl = 1;
I2C_Delay(DELAY_TIME);
scl = 0;
sda = 1;
I2C_Delay(DELAY_TIME);
}
void Write_v(unsigned char dat)
{
I2CStart();
I2CSendByte(0x90);
I2CWaitAck();
I2CSendByte(0x03);
I2CWaitAck();
I2CSendByte(dat);
I2CWaitAck();
I2CStop();
}
void Write_AT24C02_Page(unsigned char *buf,unsigned char num)
{
I2CStart();
I2CSendByte(0xa0);
I2CWaitAck();
I2CSendByte(0x00);
I2CWaitAck();
while(num--)
{
I2CSendByte(*buf++);
I2CWaitAck();
// I2C_Delay(200);
// delay();
}
I2CStop();
}
void Read_AT24C02_Page(unsigned char *buf,unsigned char num)
{
I2CStart();
I2CSendByte(0xa0);
I2CWaitAck();
I2CSendByte(0x00);
I2CWaitAck();
I2CStart();
I2CSendByte(0xa1);
I2CWaitAck();
while(num--)
{
*buf++ = I2CReceiveByte();
if(num) I2CSendAck(0);
else I2CSendAck(1);
}
I2CStop();
}
void Write_AT24C02(unsigned char dat,addr)
{
I2CStart();
I2CSendByte(0xa0);
I2CWaitAck();
I2CSendByte(addr);
I2CWaitAck();
I2CSendByte(dat);
I2CWaitAck();
I2CStop();
}
unsigned char Read_AT24C02(unsigned char addr)
{
unsigned char temp;
I2CStart();
I2CSendByte(0xa0);
I2CWaitAck();
I2CSendByte(addr);
I2CWaitAck();
I2CStart();
I2CSendByte(0xa1);
I2CWaitAck();
temp = I2CReceiveByte();
I2CSendAck(1);
I2CStop();
return temp;
}
void delay()
{
char i = 112;
while(i--);
}
iic.h
#ifndef __iic_h
#define __iic_h
static void I2C_Delay(unsigned char n);
void I2CStart(void);
void I2CStop(void);
void I2CSendByte(unsigned char byt);
unsigned char I2CReceiveByte(void);
unsigned char I2CWaitAck(void);
void I2CSendAck(unsigned char ackbit);
void Write_v(unsigned char dat);
void Write_AT24C02_Page(unsigned char *buf,unsigned char num);
void Read_AT24C02_Page(unsigned char *buf,unsigned char num);
void Write_AT24C02(unsigned char dat,addr);
unsigned char Read_AT24C02(unsigned char addr);
void delay();
#endif
sys.c
#include <STC15F2K60S2.H>
#include "sys.h"
#include "intrins.h"
sbit TX = P1^0;
unsigned int distance;
void Delay12us() //@12.000MHz
{
unsigned char i;
_nop_();
_nop_();
i = 33;
while (--i);
}
void Delay_ms(unsigned int t) //@12.000MHz
{
while(t--)
{
unsigned char i, j;
i = 12;
j = 169;
do
{
while (--j);
} while (--i);
}
}
void Select_Hc573(char n)
{
switch(n)
{
case 4:P2 = P2 & 0x1f | 0x80;break;
case 5:P2 = P2 & 0x1f | 0xa0;break;
case 6:P2 = P2 & 0x1f | 0xc0;break;
case 7:P2 = P2 & 0x1f | 0xe0;break;
}
P2 = P2 & 0x1f;
}
void Sys_Init()
{
P0 = 0x00;
Select_Hc573(5);
P0 = 0xff;
Select_Hc573(4);
}
void Select_Bit(unsigned char pos,dat)
{
P0 = 0x01 << pos;
Select_Hc573(6);
P0 = dat;
Select_Hc573(7);
Delay_ms(1);
P0 = 0xff;
Select_Hc573(7);
}
void PCA_Init()
{
P_SW1 &= 0xcf;
CCON = 0;
CH = CL = 0;
CMOD = 0x01;
CCAPM0 = 0x10;
}
void Send()
{
char i;
for(i = 0;i < 8;i++)
{
TX = 1;
Delay12us();
TX = 0;
Delay12us();
}
CH = CL = 0;
CF = CCF0 = 0;
CCAPM0 |= 0x01;
CR = 1;
}
void PCA_isr() interrupt 7
{
if(CCF0)
{
distance = (CCAP0H << 8 | CCAP0L) * 0.017;
CCF0 = 0;
}
else if(CF)
{
distance = 999;
CF = 0;
}
CCAPM0 &= ~0x01;
CR = 0;
}
sys.h
#ifndef __sys_h
#define __sys_h
void Delay12us();
void Delay_ms(unsigned int t);
void Select_Hc573(char n);
void Sys_Init();
void Select_Bit(unsigned char pos,dat);
void PCA_Init();
void Send();
#endif
main.c
#include <STC15F2K60S2.H>
#include "iic.h"
#include "sys.h"
sbit S7 = P3^0;
sbit S6 = P3^1;
sbit S5 = P3^2;
sbit S4 = P3^3;
code unsigned char SMG[] = { ~0x3F,~0x06,~0x5B,~0x4F,~0x66,~0x6D,~0x7D,~0x07,~0x7F,~0x6F};
extern unsigned int distance;
unsigned int dis;
unsigned int dis_echo[11];
unsigned int index1 = 1,index2 = 1;//index1为回显界面的索引 2为测距时候的索引
unsigned int dis_old;
unsigned int dis_sum;
char param = 20;
unsigned char mode;//0-距离 1-回显 2-参数
bit mode_dis;// 0-无操作 1-加操作
bit flag_10ms;
unsigned char count1;
unsigned char key_val;
unsigned char S5_count;
unsigned char S6_count;
unsigned char save[13] = {0,0,0,0,0,0,0,0,0,0,0,20};//保存测量盲区和最近十次的测量结果
unsigned char rec[13];//初始化从E2PROM里读取10个距离和测量盲区
unsigned char index3 = 1;
bit L1_flag;
bit dis_ok;
unsigned char count2,count3;
char i;
unsigned char count4;
bit flag_20ms;
void Display_dis()//测距界面
{
Select_Bit(0,SMG[mode_dis]);
if(!mode_dis)
{
Select_Bit(2,SMG[dis_old / 100]);
Select_Bit(3,SMG[dis_old / 10 % 10]);
Select_Bit(4,SMG[dis_old % 10]);
}
else
{
Select_Bit(2,SMG[dis_sum / 100]);
Select_Bit(3,SMG[dis_sum / 10 % 10]);
Select_Bit(4,SMG[dis_sum % 10]);
}
Select_Bit(5,SMG[dis / 100]);
Select_Bit(6,SMG[dis / 10 % 10]);
Select_Bit(7,SMG[dis % 10]);
}
void Display_echo()//回显界面
{
Select_Bit(0,SMG[index1 / 10]);
Select_Bit(1,SMG[index1 % 10]);
Select_Bit(5,SMG[dis_echo[index1] / 100]);
Select_Bit(6,SMG[dis_echo[index1] / 10 % 10]);
Select_Bit(7,SMG[dis_echo[index1] % 10]);
}
void Display_param()//参数界面
{
Select_Bit(0,~0x71);
Select_Bit(5,SMG[param / 100]);
Select_Bit(6,SMG[param / 10 % 10]);
Select_Bit(7,SMG[param % 10]);
}
void Timer0Init(void) //10毫秒@12.000MHz
{
AUXR &= 0x7F; //定时器时钟12T模式
TMOD &= 0xF0; //设置定时器模式
TL0 = 0xF0; //设置定时初值
TH0 = 0xD8; //设置定时初值
TF0 = 0; //清除TF0标志
TR0 = 1; //定时器0开始计时
ET0 = 1;
}
void Timer0_isr() interrupt 1
{
flag_10ms = 1;
if(dis_ok)
{
if(++count2 > 20)
{
count2 = 0;
L1_flag ^= 1;
if(++count3 > 20)
{
count3 = 0;
L1_flag = 0;
dis_ok = 0;
}
}
}
}
unsigned char Key_Scan()
{
unsigned char key_temp = 0;
static unsigned char cnt4 = 0;
static unsigned char cnt5 = 0;
static unsigned char cnt6 = 0;
static unsigned char cnt7 = 0;
if(flag_10ms)
{
if(S7 == 0) cnt7++;
if(S7 == 1)
{
if(cnt7 > 2) key_temp = 7;
cnt7 = 0;
}
if(S6 == 0) cnt6++;
if(S6 == 1)
{
if(cnt6 > 2) key_temp = 6;
cnt6 = 0;
}
if(S5 == 0) cnt5++;
if(S5 == 1)
{
if(cnt5 > 2) key_temp = 5;
cnt5 = 0;
}
if(S4 == 0) cnt4++;
if(S4 == 1)
{
if(cnt4 > 2) key_temp = 4;
cnt4 = 0;
}
flag_10ms = 0;
}
return key_temp;
}
void Key_Pro()
{
switch(key_val)
{
case 4:
dis_old = dis;
dis = distance;
dis_sum = dis_old + dis;
dis_echo[index2] = dis;
if(++index2 > 10) index2 = 1;
save[index3] = dis;
if(++index3 > 10) index3 = 1;
dis_ok = 1;
break;
case 5:
if(++S5_count > 2) S5_count = 1;
if(S5_count == 1) mode = 1;
else mode = 0;
break;
case 6:
if(++S6_count > 2) S6_count = 1;
if(S6_count == 1) mode = 2;
else mode = 0;
break;
case 7:
if(mode == 0) mode_dis ^= 1;
if(mode == 1)
{
if(++index1 > 10) index1 = 1;
}
if(mode == 2)
{
param += 10;
if(param > 90) param = 0;
}
break;
}
}
void Dac_Pro()
{
if(dis <= param) Write_v(0);
else Write_v((dis - param) * 0.02);
}
void Led(unsigned char addr,enable)
{
static unsigned char temp = 0x00;
static unsigned char temp_old = 0xff;
if(enable) temp |= 0x01 << addr;
else temp &= ~(0x01 << addr);
if(temp != temp_old)
{
P0 = ~temp;
Select_Hc573(4);
temp_old = temp;
}
}
void Led_Pro()
{
if(L1_flag) Led(0,1);
else Led(0,0);
if(mode == 2) Led(6,1);
else Led(6,0);
if(mode == 1) Led(7,1);
else Led(7,0);
}
void E2PROM_Pro()
{
}
void main()
{
Sys_Init();
PCA_Init();
Timer0Init();
EA = 1;
Read_AT24C02_Page(rec,11);
for(i = 1;i < 11;i++)
dis_echo[i] = rec[i];
param = rec[0];
Delay_ms(10);
while(1)
{
Send();
key_val = Key_Scan();
Key_Pro();
Dac_Pro();
Led_Pro();
for(i = 1;i < 11;i++)
save[i] = dis_echo[i];
save[0] = param;
Write_AT24C02_Page(save,11);
switch(mode)
{
case 0:Display_dis();break;
case 1:Display_echo();break;
case 2:Display_param();break;
}
}
}