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);
}unsigned char Read_v()
{unsigned int temp;I2CStart();I2CSendByte(0x90);I2CWaitAck();I2CSendByte(0x41);I2CWaitAck();I2CStart();I2CSendByte(0x91);I2CWaitAck();temp = I2CReceiveByte();I2CSendAck(1);I2CStop();return temp;
}void Write_v(unsigned char dat)
{I2CStart();I2CSendByte(0x90);I2CWaitAck();I2CSendByte(0x40);I2CWaitAck();I2CSendByte(dat);I2CWaitAck();I2CStop();
}
iic.h
#ifndef __iic_h
#define __iic_hstatic 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);
unsigned char Read_v();
void Write_v(unsigned char dat);#endif
ds1302.c
/* # DS1302代码片段说明1. 本文件夹中提供的驱动代码供参赛选手完成程序设计参考。2. 参赛选手可以自行编写相关代码或以该代码为基础,根据所选单片机类型、运行速度和试题中对单片机时钟频率的要求,进行代码调试和修改。
*/ //
#include <STC15F2K60S2.H>
#include "ds1302.h"
#include "intrins.h"sbit SCK = P1^7;
sbit SDA = P2^3;
sbit RST = P1^3;code unsigned char Write_addr[] = {0x80,0x82,0x84,0x86,0x88,0x8a,0x8c};
code unsigned char Read_addr[] = {0x81,0x83,0x85,0x87,0x89,0x8b,0x8d};
unsigned char time[] = {0x01,0x20,0x20};void Write_Ds1302(unsigned char temp)
{unsigned char i;for (i=0;i<8;i++) { SCK = 0;SDA = temp&0x01;temp>>=1; SCK=1;}
} //
void Write_Ds1302_Byte( unsigned char address,unsigned char dat )
{RST=0; _nop_();SCK=0; _nop_();RST=1; _nop_(); Write_Ds1302(address); Write_Ds1302(dat); RST=0;
}//
unsigned char Read_Ds1302_Byte ( unsigned char address )
{unsigned char i,temp=0x00;RST=0; _nop_();SCK=0; _nop_();RST=1; _nop_();Write_Ds1302(address);for (i=0;i<8;i++) { SCK=0;temp>>=1; if(SDA)temp|=0x80; SCK=1;} RST=0; _nop_();SCK=0; _nop_();SCK=1; _nop_();SDA=0; _nop_();SDA=1; _nop_();return (temp);
}void Write_time()
{char i;Write_Ds1302_Byte(0x8e,0x00);for(i = 0;i < 3;i++)Write_Ds1302_Byte(Write_addr[i],time[i]);Write_Ds1302_Byte(0x8e,0x80);
}void Read_time()
{char i;for(i = 0;i < 3;i++)time[i] = Read_Ds1302_Byte(Read_addr[i]);
}
ds1302.h
#ifndef __ds1302_h
#define __ds1302_hvoid Write_Ds1302(unsigned char temp);
void Write_Ds1302_Byte( unsigned char address,unsigned char dat );
unsigned char Read_Ds1302_Byte ( unsigned char address );
void Write_time();
void Read_time();#endif
sys.c
#include <STC15F2K60S2.H>
#include "sys.h"
#include "intrins.h"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);
}
sys.h
#ifndef __sys_h
#define __sys_hvoid Delay12us();
void Delay_ms(unsigned int t) ;
void Select_Hc573(char n);
void Sys_Init();
void Select_Bit(unsigned char pos,dat);#endif
main.c
#include <STC15F2K60S2.H>
#include "ds1302.h"
#include "iic.h"
#include "sys.h"
#include "stdio.h"sbit TX = P1^0;
sbit RX = P1^1;sbit R1 = P3^0;
sbit R2 = P3^1;
sbit R3 = P3^2;
sbit R4 = P3^3;
sbit C1 = P4^4;
sbit C2 = P4^2;
sbit C3 = P3^5;
sbit C4 = P3^4;code unsigned char SMG[] = { ~0x3F,~0x06,~0x5B,~0x4F,~0x66,~0x6D,~0x7D,~0x07,~0x7F,~0x6F,~0x40,~0x38,~0x39,~0x71,~0x76,0xfe,0xbf,0xf7,~0x73};unsigned int dis,distance,a;//距离
bit flag,flag_10ms;
unsigned char count;
unsigned int count1,count2;
extern unsigned char time[];
bit mode_dis;//0-触发 1-定时
unsigned char mode_record;// 0-最大值 1-最小值 2-均值
unsigned char dis_max,dis_min = 9;
float dis_sum;
float dis_ave;
char param_time[] = {2,3,5,7,9};
char param_time_P[] = {2,3,5,7,9};
char index;//时间参数索引
char index_P;//时间参数索引
unsigned int dis_index;//距离索引,用于计算平均值
char param_dis = 20;
char param_dis_P = 20;
unsigned char key_val;
bit mode;//0-数据界面 1-参数界面
unsigned char mode_dat;//0-时间 1-距离 2-数据记录
bit mode_param;// 0-时间 1-距离
bit L5_flag;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 = 99;CF = 0;}CCAPM0 &= (~0x01);CR = 0;
}void Display_time()//时间界面
{Select_Bit(0,SMG[time[2] / 16]);Select_Bit(1,SMG[time[2] % 16]);Select_Bit(2,SMG[10]);Select_Bit(3,SMG[time[1] / 16]);Select_Bit(4,SMG[time[1] % 16]);Select_Bit(5,SMG[10]);Select_Bit(6,SMG[time[0] / 16]);Select_Bit(7,SMG[time[0] % 16]);
}void Display_dis()//距离界面
{Select_Bit(0,SMG[11]);if(!mode_dis) Select_Bit(1,SMG[12]);else Select_Bit(1,SMG[13]);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]);}void Display_record()//数据记录界面
{Select_Bit(0,SMG[14]);switch(mode_record){case 0:Select_Bit(1,SMG[15]);if(dis_max > 99) Select_Bit(5,SMG[dis_max / 100]); if(dis_max > 9) Select_Bit(6,SMG[dis_max / 10 % 10]);Select_Bit(7,SMG[dis_max % 10]);break;case 1:Select_Bit(1,SMG[17]);if(dis_min > 99) Select_Bit(5,SMG[dis_min / 100]); if(dis_min > 9) Select_Bit(6,SMG[dis_min / 10 % 10]);Select_Bit(7,SMG[dis_min % 10]);break;case 2:Select_Bit(1,SMG[16]);if(dis_ave > 99) Select_Bit(4,SMG[((unsigned char)(dis_ave * 10)) / 1000]); if(dis_ave > 9) Select_Bit(5,SMG[((unsigned char)(dis_ave * 10)) / 100 % 10]); Select_Bit(6,SMG[((unsigned char)(dis_ave * 10)) / 10 % 10] - 0x80);Select_Bit(7,SMG[((unsigned char)(dis_ave * 10)) % 10]);break;}
}void Display_param_time()//时间参数界面
{Select_Bit(0,SMG[18]);Select_Bit(1,SMG[1]);Select_Bit(6,SMG[param_time[index] / 10]);Select_Bit(7,SMG[param_time[index] % 10]);
}void Display_param_dis()//距离参数界面
{Select_Bit(0,SMG[18]);Select_Bit(1,SMG[2]);Select_Bit(6,SMG[param_dis / 10]);Select_Bit(7,SMG[param_dis % 10]);
}void Timer0Init(void) //10毫秒@12.000MHz
{AUXR &= 0x7F; //定时器时钟12T模式TMOD &= 0xF0; //设置定时器模式TL0 = 0xF0; //设置定时初值TH0 = 0xD8; //设置定时初值TF0 = 0; //清除TF0标志TR0 = 1; //定时器0开始计时ET0 = 1;EA = 1;
}void Timer0_isr() interrupt 1
{flag_10ms = 1;if(mode_dis)//定时模式下{count2 = 0;if(++count1 > param_time_P[index_P] * 100)//距离的刷新时间等于时间参数才刷新一次{count1 = 0;flag = 1;}}else//触发模式下,距离的刷新时间为1秒{count1 = 0;if(++count2 > 100){count2 = 0;flag = 1;}}
}unsigned char Key_Scan()
{unsigned char temp = 0;static unsigned char cnt4 = 0;static unsigned char cnt5 = 0;static unsigned char cnt8 = 0;static unsigned char cnt9 = 0;if(flag_10ms){R3 = 0;R1 = R2 = R4 = C1 = C2 = C3 = C4 = 1;if(C1 == 0) cnt5++;if(C1 == 1) {if(cnt5 > 2) temp = 5;cnt5 = 0;}if(C2 == 0) cnt9++;if(C2 == 1) {if(cnt9 > 2) temp = 9;cnt9 = 0;}R4 = 0;R1 = R2 = R3 = C1 = C2 = C3 = C4 = 1;if(C1 == 0) cnt4++;if(C1 == 1) {if(cnt4 > 2) temp = 4;cnt4 = 0;}if(C2 == 0) cnt8++;if(C2 == 1) {if(cnt8 > 2) temp = 8;cnt8 = 0;}flag_10ms = 0;}return temp;
}void Key_Pro()
{switch(key_val){case 4:mode ^= 1;if(!mode) mode_param = 0;else mode_dat = 0;break;case 5:if(!mode){if(++mode_dat > 2)mode_dat = 0;}else{mode_param ^= 1;}break;case 8:if(!mode) {if(mode_dat == 1) mode_dis ^= 1;if(mode_dat == 2){if(++mode_record > 2)mode_record = 0;}}break;case 9:if(mode){if(!mode_param){if(++index > 4)index = 0;}else{param_dis += 10;if(param_dis > 80)param_dis = 10;}}break;}if(mode_dat == 1)mode_record = 0;if(!mode){param_time_P[index_P] = param_time[index];index_P = index;param_dis_P = param_dis;}
}void Out_dac()//DAC输出
{if(dis <= 10) Write_v(1 * 51);else if(dis >= 80) Write_v(5 * 51);else Write_v((((dis - 10) * 4 / 70.0 + 1)) * 51);
}void Dis_Pro()//超声波测距
{if(flag){flag = 0;a = distance;if((dis >= (param_dis_P - 5)) && (dis <= (param_dis_P + 5))) count++;else {count = 0;L5_flag = 0;}if(count == 3) {L5_flag = 1;count = 0;}}if(mode_dis){if((time[0] / 16 * 10 + time[0] % 16) % param_time_P[index_P] == 0){dis = a;dis_index++;if(dis > dis_max) dis_max = dis;if(dis < dis_min) dis_min = dis;dis_sum += dis;dis_ave = (dis_sum / dis_index);}}else{if(Read_v() > 150)if(Read_v() < 150){dis = distance;dis_index++;if(dis > dis_max) dis_max = dis;if(dis < dis_min) dis_min = dis;dis_sum += dis;dis_ave = (dis_sum / dis_index);}}
}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 && mode_dat == 0) Led(0,1);else Led(0,0);if(!mode && mode_dat == 1) Led(1,1);else Led(1,0);if(!mode && mode_dat == 2) Led(2,1);else Led(2,0);if(!mode_dis) Led(3,1);else Led(3,0);if(L5_flag) Led(4,1);else Led(4,0);if(Read_v() > 150) Led(5,1);else Led(5,0);
}void UartInit(void) //9600bps@12.000MHz
{SCON = 0x50; //8位数据,可变波特率AUXR &= 0xBF; //定时器1时钟为Fosc/12,即12TAUXR &= 0xFE; //串口1选择定时器1为波特率发生器TMOD &= 0x0F; //设定定时器1为16位自动重装方式TL1 = 0xE6; //设定定时初值TH1 = 0xFF; //设定定时初值ET1 = 0; //禁止定时器1中断TR1 = 1; //启动定时器1
}extern char putchar(char dat)
{SBUF = dat;while(!TI);TI = 0;return dat;
}void main()
{Sys_Init();PCA_Init();Write_time();Timer0Init();UartInit();while(1){Send();Read_time();key_val = Key_Scan();Key_Pro();Out_dac();Dis_Pro();Led_Pro();if(!mode){switch(mode_dat){case 0:Display_time();break;case 1:Display_dis();break;case 2:Display_record();break;}}else{if(!mode_param) Display_param_time();else Display_param_dis();} }
}