注:PWM没搞出来
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(0x40);
I2CWaitAck();
I2CSendByte(dat);
I2CWaitAck();
I2CStop();
}
void Write_AT24C02(unsigned char dat)
{
I2CStart();
I2CSendByte(0xa0);
I2CWaitAck();
I2CSendByte(0x00);
I2CWaitAck();
I2CSendByte(dat);
I2CWaitAck();
I2CStop();
}
unsigned char Read_v()
{
unsigned char temp;
I2CStart();
I2CSendByte(0x90);
I2CWaitAck();
I2CSendByte(0x43);
I2CWaitAck();
I2CStart();
I2CSendByte(0x91);
I2CWaitAck();
temp = I2CReceiveByte();
I2CSendAck(1);
I2CStop();
return temp;
}
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(unsigned char dat);
unsigned char Read_v();
#endif
sys.c
#include <STC15F2K60S2.H>
#include "sys.h"
#include "intrins.h"
sbit TX = P1^0;
sbit RX = P1^1;
unsigned int distance,dis;
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()
{
CMOD |= 0x08;
CCON = 0;
}
void Send()
{
char i;
CH = CL = 0;
for(i = 0;i < 8;i++)
{
TX = 1;
Delay12us();
TX = 0;
Delay12us();
}
CR = 1;
while(RX && !CF);
CR = 0;
if(CF == 0)
{
distance = (CH << 8 | CL) * 0.017 / 12;
}
else
{
distance = 99;
CF = 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 "sys.h"
#include "iic.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,~0x71,~0x76,~0x77,~0x73};
extern unsigned int distance,dis;//距离
unsigned char hum;//湿度
unsigned char count1;
unsigned int fre;//频率
bit mode_fre;//0-HZ 1-KHZ
bit mode_dis;//0-cm 1-m
unsigned int param_fre = 90;//频率参数
char param_hum = 40;//湿度参数
unsigned int param_dis = 6;//距离参数
bit flag_10ms,flag_500ms;
unsigned char key_val;//键值
unsigned char mode;//界面切换 0-频率 1-湿度 2-测距 3-参数
unsigned char mode_param;//参数界面切换 0-频率 1-湿度 2-距离
unsigned char relay_count;
bit L1_flag;
bit L2_flag;
bit L3_flag;
unsigned char count2,count3,count4;
unsigned char count5,count6;
char duty;
void Timer1Init(void) //10毫秒@12.000MHz
{
AUXR &= 0xBF; //定时器时钟12T模式
TMOD |= 0x05; //设置定时器模式
TL1 = 0xF0; //设置定时初值
TH1 = 0xD8; //设置定时初值
TH0 = TL0 = 0;
TF1 = 0; //清除TF1标志
TR0 = TR1 = 1; //定时器1开始计时
ET0 = ET1 = 1;
EA = 1;
}
void Timer1_isr() interrupt 3
{
if(count1 == 50) flag_500ms = 1;
if(++count1 > 100)
{
count1 = 0;
fre = TH0 << 8 | TL0;
TH0 = TL0 = 0;
}
flag_10ms = 1;
if(mode == 3 && mode_param == 0)
{
if(++count2 > 10)
L1_flag ^= 1;
}
else L1_flag = 0;
if(mode == 3 && mode_param == 1)
{
if(++count3 > 10)
L2_flag ^= 1;
}
else L2_flag = 0;
if(mode == 3 && mode_param == 2)
{
if(++count4 > 10)
L3_flag ^= 1;
}
else L3_flag = 0;
}
void Display_fre()//频率界面
{
Select_Bit(0,SMG[10]);
if(!mode_fre)//HZ
{
if(fre > 9999) Select_Bit(3,SMG[fre / 10000]);
if(fre > 999) Select_Bit(4,SMG[fre / 1000 % 10]);
if(fre > 99) Select_Bit(5,SMG[fre / 100 % 10]);
if(fre > 9) Select_Bit(6,SMG[fre / 10 % 10]);
Select_Bit(7,SMG[fre % 10]);
}
else//KHZ
{
if(fre / 10000 > 0) Select_Bit(5,SMG[fre / 10000]);
Select_Bit(6,SMG[fre / 1000 % 10] - 0x80);
Select_Bit(7,SMG[fre / 100 % 10]);
}
}
void Display_hum()//湿度界面
{
Select_Bit(0,SMG[11]);
Select_Bit(6,SMG[hum / 10]);
Select_Bit(7,SMG[hum % 10]);
}
void Display_dis()//测距界面
{
Select_Bit(0,SMG[12]);
if(!mode_dis)//cm
{
if(dis > 99) Select_Bit(5,SMG[dis / 100]);
if(dis > 9) Select_Bit(6,SMG[dis / 10 % 10]);
Select_Bit(7,SMG[dis % 10]);
}
else//m
{
Select_Bit(5,SMG[dis / 100] - 0x80);
Select_Bit(6,SMG[dis / 10 % 10]);
Select_Bit(7,SMG[dis % 10]);
}
}
void Display_param_fre()//频率参数
{
Select_Bit(0,SMG[13]);
Select_Bit(1,SMG[1]);
Select_Bit(5,SMG[param_fre / 100]);
Select_Bit(6,SMG[param_fre / 10 % 10] - 0x80);
Select_Bit(7,SMG[param_fre % 10]);
}
void Display_param_hum()//湿度参数
{
Select_Bit(0,SMG[13]);
Select_Bit(1,SMG[2]);
Select_Bit(6,SMG[param_hum / 10]);
Select_Bit(7,SMG[param_hum % 10]);
}
void Display_param_dis()//距离参数
{
Select_Bit(0,SMG[13]);
Select_Bit(1,SMG[3]);
Select_Bit(6,SMG[param_dis / 10] - 0x80);
Select_Bit(7,SMG[param_dis % 10]);
}
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 > 100) key_temp = 70;
else 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:
if(++mode > 3) mode = 0;
break;
case 5:
if(mode == 3)
{
if(++mode_param > 2)
mode_param = 0;
}
break;
case 6:
if(mode == 3)
{
switch(mode_param)
{
case 0:
param_fre += 5;
if(param_fre > 120) param_fre = 10;
break;
case 1:
param_hum += 10;
if(param_hum > 60) param_hum = 10;
break;
case 2:
param_dis += 1;
if(param_dis > 12) param_dis = 1;
break;
}
}
else if(mode == 2)
{
mode_dis ^= 1;
}
break;
case 7:
if(mode == 3)
{
switch(mode_param)
{
case 0:
param_fre -= 5;
if(param_fre < 10) param_fre = 120;
break;
case 1:
param_hum -= 10;
if(param_hum < 10) param_hum = 60;
break;
case 2:
param_dis -= 1;
if(param_dis < 1) param_dis = 12;
break;
}
}
else if(mode == 0)
{
mode_fre ^= 1;
}
break;
case 70:
if(mode == 1) relay_count = 0;
break;
}
if(mode == 2) mode_param = 0;
}
void Dac_Pro()
{
if(hum >= param_hum) Write_v(255);
else if(hum <= param_hum) Write_v(51);
else Write_v((4 / (80 - param_hum)) * (hum - param_hum) + 1);
}
void Relay(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(5);
temp_old = temp;
}
}
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(mode == 0) Led(0,1);
else if(L1_flag) Led(0,1);
else Led(0,0);
if(mode == 1) Led(1,1);
else if(L2_flag) Led(1,1);
else Led(1,0);
if(mode == 2) Led(2,1);
else if(L3_flag) Led(2,1);
else Led(2,0);
if(fre > param_fre * 100) Led(3,1);
else Led(3,0);
if(hum > param_hum) Led(4,1);
else Led(4,0);
if(dis > param_dis * 10) Led(5,1);
else Led(5,0);
}
//void Timer2Init(void) //20微秒@12.000MHz
//{
// AUXR &= 0xFB; //定时器时钟12T模式
// T2L = 0xEC; //设置定时初值
// T2H = 0xFF; //设置定时初值
// AUXR |= 0x10; //定时器2开始计时
// IE2 = 1 << 2;
//
//void Timer2_isr() interrupt 12
//{
// if(count5++ > 100) count5 = 0;
// if(fre > param_fre * 100) duty = 80;
// else duty = 20;
// Relay(5,count5 < duty);
if(count5 < 8) Relay(5,1);
else Relay(5,0);
if(count6++ > 10) count6 = 0;
if(count6 < 2) Relay(5,1);
else Relay(5,0);
//}
void main()
{
Sys_Init();
PCA_Init();
Timer1Init();
// Timer2Init();
while(1)
{
Send();
key_val = Key_Scan();
Key_Pro();
hum = Read_v() / 51.0 * 20;
if(hum > 99) hum = 99;
Dac_Pro();
if(flag_500ms)
{
flag_500ms = 0;
dis = distance;
}
if(dis > (param_dis * 10))
{
Relay(4,1);
relay_count++;
}
else Relay(4,0);
Write_AT24C02(relay_count);
Led_Pro();
switch(mode)
{
case 0:Display_fre();break;
case 1:Display_hum();break;
case 2:Display_dis();break;
case 3:
switch(mode_param)
{
case 0:Display_param_fre();break;
case 1:Display_param_hum();break;
case 2:Display_param_dis();break;
}
break;
}
}
}