答案仅供参考,有错误欢迎指正,非常感谢!!!
文件较多,代码较长,感谢查看!!!
目录
对于次序循环往复运行,具体是一个什么效果,我还不是很清楚。
最终仍然存在一个小问题,当RB2输入的电压越大,按键灵敏度下降,数码管刷新频率下降。
main.h
#ifndef __MAIN_H_
#define __MAIN_H_
#define uchar unsigned char
#define uint unsigned int
#include <STC15F2K60S2.H>
#include "Init.h"
#include "SMG.h"
#include "DSQ.h"
#include "YanShi.h"
#include "DLKey.h"
#include "iic.h"
#include "PWM.h"
unsigned char DLKey_GetKeynum();
#endif
main.c
#include "main.h"
uchar pwm;
uchar pwm_time=0;
uchar QiDong_TingZhi=0;//启动停止。0:停止;1:启动。
uchar key_num;
uchar SheZhi=0;//设置。0:数码管全熄灭;1:模式编号;2流转间隔
uchar MoShi_BianHao=1;//模式编号。1-4
uchar MoShi_BianHao_temp=1;//模式编号临时值。1-4
uchar LiuZhuan_JianGe[4];//流转间隔
uchar LiuZhuan_JianGe_temp;//流转间隔临时值
uchar LiangDu_DengJi_flag=0;//亮度等级标志位。0:不显示亮度等级;1:显示亮度等级
uint XianShi_DanYuan_time=0;//显示单元的时间。
uchar LiangDu_DengJi;//亮度等级
uchar LiangDu;//亮度
uchar EEPROM_Data_flag;//EEPROM数据标志位
uchar code MoShi1_2[]={0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f};
uchar code MoShi3_4[]={0xe7,0xdb,0xbd,0x7e};
uint time=0;
char num=0;
uchar ADC_time=0;
unsigned char DLKey_GetKeynum(){
unsigned char Key_num=0;
if(P30==0){
Delay10ms();
if(P30==0){
Key_num=7;
}
while(P30==0);
}
else if(P31==0){
Delay10ms();
if(P31==0){
Key_num=6;
}
while(P31==0);
}
else if(P32==0){
Delay10ms();
if(P32==0){
Key_num=5;
}
while(P32==0);
}
else if(P33==0){
Delay10ms();
if(P33==0){
Key_num=4;
}
if(SheZhi>0){
while(P33==0);
}
}
DLKey_Init();
return Key_num;
}
void main(){
uchar i;
Init_BZ();
SMG_Init();
DSQ_2_Init();
PWM_DSQ_1_Init();
EEPROM_Data_flag=IIC_EEPROM_GetData(10);
Delay10ms();
if(EEPROM_Data_flag!=10){
IIC_EEPROM_XieData(10,10);
Delay10ms();
for(i=11;i<=14;i++){
IIC_EEPROM_XieData(i,4);
Delay10ms();
}
}
Delay10ms();
for(i=0;i<=3;i++){
LiuZhuan_JianGe[i]=IIC_EEPROM_GetData(i+11);
Delay10ms();
}
while(1){
key_num=DLKey_GetKeynum();
if(key_num==7){
QiDong_TingZhi++;
QiDong_TingZhi=QiDong_TingZhi%2;
if(QiDong_TingZhi){
ET1=1;
}
else{
ET1=0;
}
}
else if(key_num==6){
SheZhi++;
SheZhi=SheZhi%3;
if(SheZhi==0){
LiuZhuan_JianGe[MoShi_BianHao_temp-1]=LiuZhuan_JianGe_temp;
MoShi_BianHao=MoShi_BianHao_temp;
IIC_EEPROM_XieData(MoShi_BianHao+10,LiuZhuan_JianGe_temp);
num=0;
}
else if(SheZhi==1){
MoShi_BianHao_temp=MoShi_BianHao;
LiuZhuan_JianGe_temp=LiuZhuan_JianGe[MoShi_BianHao-1];
}
}
else if(key_num==5){
if(SheZhi==1){
MoShi_BianHao_temp++;
if(MoShi_BianHao_temp>=4){
MoShi_BianHao_temp=4;
}
LiuZhuan_JianGe_temp=LiuZhuan_JianGe[MoShi_BianHao_temp-1];
}
else if(SheZhi==2){
LiuZhuan_JianGe_temp++;
if(LiuZhuan_JianGe_temp>=12){
LiuZhuan_JianGe_temp=12;
}
}
}
else if(key_num==4){
if(SheZhi==0){
LiangDu_DengJi_flag=1;
}
else if(SheZhi==1){
MoShi_BianHao_temp--;
if(MoShi_BianHao_temp<=1){
MoShi_BianHao_temp=1;
}
LiuZhuan_JianGe_temp=LiuZhuan_JianGe[MoShi_BianHao_temp-1];
}
else if(SheZhi==2){
LiuZhuan_JianGe_temp--;
if(LiuZhuan_JianGe_temp<=4){
LiuZhuan_JianGe_temp=4;
}
}
}
else{
LiangDu_DengJi_flag=0;
}
if(ADC_time>=3){
ADC_time=0;
LiangDu=IIC_ADC_GetData(3);
if(0<=LiangDu&&LiangDu<63){
LiangDu_DengJi=1;
pwm=10;
}
else if(63<=LiangDu&&LiangDu<126){
LiangDu_DengJi=2;
pwm=40;
}
else if(126<=LiangDu&&LiangDu<=189){
LiangDu_DengJi=3;
pwm=70;
}
else{
LiangDu_DengJi=4;
pwm=100;
}
}
}
}
void DSQ_2_ZD() interrupt 12{
ADC_time++;
if(QiDong_TingZhi){
time++;
if(time>=LiuZhuan_JianGe[MoShi_BianHao-1]*100){
time=0;
if(MoShi_BianHao==1){
num++;
num=num%8;
}
else if(MoShi_BianHao==2){
num--;
if(num<0){
num=7;
}
}
else if(MoShi_BianHao==3){
num++;
num=num%4;
}
else if(MoShi_BianHao==4){
num--;
if(num<0){
num=3;
}
}
}
}
XianShi_DanYuan_time++;
if(XianShi_DanYuan_time>=1600){
XianShi_DanYuan_time=0;
}
switch(SheZhi){
case 0:{
if(LiangDu_DengJi_flag){
SMG_Show(16,16,16,16,16,16,17,LiangDu_DengJi);
}
else{
SMG_Show(16,16,16,16,16,16,16,16);
}
break;
}
case 1:{
if(XianShi_DanYuan_time<800){
if(LiuZhuan_JianGe_temp>=10){
SMG_Show(17,MoShi_BianHao_temp,17,16,LiuZhuan_JianGe_temp/10,LiuZhuan_JianGe_temp%10,0,0);
}
else if(LiuZhuan_JianGe_temp>=0){
SMG_Show(17,MoShi_BianHao_temp,17,16,16,LiuZhuan_JianGe_temp,0,0);
}
}
else{
if(LiuZhuan_JianGe_temp>=10){
SMG_Show(16,16,16,16,LiuZhuan_JianGe_temp/10,LiuZhuan_JianGe_temp%10,0,0);
}
else if(LiuZhuan_JianGe_temp>=0){
SMG_Show(16,16,16,16,16,LiuZhuan_JianGe_temp,0,0);
}
}
break;
}
case 2:{
if(XianShi_DanYuan_time<800){
if(LiuZhuan_JianGe_temp>=10){
SMG_Show(17,MoShi_BianHao_temp,17,16,LiuZhuan_JianGe_temp/10,LiuZhuan_JianGe_temp%10,0,0);
}
else if(LiuZhuan_JianGe_temp>=0){
SMG_Show(17,MoShi_BianHao_temp,17,16,16,LiuZhuan_JianGe_temp,0,0);
}
}
else{
SMG_Show(17,MoShi_BianHao_temp,17,16,16,16,16,16);
}
break;
}
}
}
void DSQ_1_ZD() interrupt 3{
if(QiDong_TingZhi){
pwm_time++;
if(pwm_time<pwm){
if(MoShi_BianHao==1||MoShi_BianHao==2){
P0=0xff&MoShi1_2[num];
}
else if(MoShi_BianHao==3||MoShi_BianHao==4){
P0=0xff&MoShi3_4[num];
}
}
else if(pwm_time>=pwm&&pwm_time<=100){
P0=0xff;
}
else if(pwm_time>=101){
pwm_time=0;
P0=0xff;
}
P2=(P2&0x1f)|0x80;
P2=(P2&0x1f)|0x00;
}
}
Init.h
#ifndef __INIT_H_
#define __INIT_H_
#include <STC15F2K60S2.H>
void Init_BZ();
#endif
Init.c
#include "Init.h"
void Init_BZ(){
P2=(P2&0x1f)|0xa0;
P0=0x00;
P2=(P2&0x1f)|0x80;
P0=0xff;
}
DSQ.h
#ifndef __DSQ_H_
#define __DSQ_H_
#include <STC15F2K60S2.H>
void DSQ_2_Init();
#endif
DSQ.c
#include "DSQ.h"
void DSQ_2_Init(){
AUXR |= 0x04; //定时器时钟1T模式
T2L = 0x20; //设置定时初值
T2H = 0xD1; //设置定时初值
AUXR |= 0x10; //定时器2开始计时
AUXR=AUXR&0xf7;
IE2=IE2|0x04;
EA=1;
}
DLKey.h
#ifndef __DLKEY_H_
#define __DLKEY_H_
#include "YanShi.h"
void DLKey_Init();
#endif
DLKey.c
#include "DLKey.h"
void DLKey_Init(){
P30=1;
P31=1;
P32=1;
P33=1;
}
YanShi.h
#ifndef __YANSHI_H_
#define __YANSHI_H_
#include <STC15F2K60S2.H>
#include "intrins.h"
void Delay10ms();
#endif
YanShi.c
#include "YanShi.h"
void Delay10ms() //@12.000MHz
{
unsigned char i, j;
i = 117;
j = 184;
do
{
while (--j);
} while (--i);
}
iic.h
#ifndef _IIC_H
#define _IIC_H
#include <STC15F2K60S2.H>
#include "intrins.h"
void IIC_Start(void);
void IIC_Stop(void);
bit IIC_WaitAck(void);
void IIC_SendAck(bit ackbit);
void IIC_SendByte(unsigned char byt);
unsigned char IIC_RecByte(void);
void IIC_DAC_XieData(unsigned char date);
unsigned char IIC_ADC_GetData(unsigned char di_zhi);
void IIC_EEPROM_XieData(unsigned char di_zhi,date);
unsigned char IIC_EEPROM_GetData(unsigned char di_zhi);
#endif
iic.c
/*
程序说明: IIC总线驱动程序
软件环境: Keil uVision 4.10
硬件环境: CT107单片机综合实训平台 8051,12MHz
日 期: 2011-8-9
*/
#include "iic.h"
#define DELAY_TIME 5
#define SlaveAddrW 0xA0
#define SlaveAddrR 0xA1
//总线引脚定义
sbit SDA = P2^1; /* 数据线 */
sbit SCL = P2^0; /* 时钟线 */
void IIC_Delay(unsigned char i)
{
do{_nop_();}
while(i--);
}
//总线启动条件
void IIC_Start(void)
{
SDA = 1;
SCL = 1;
IIC_Delay(DELAY_TIME);
SDA = 0;
IIC_Delay(DELAY_TIME);
SCL = 0;
}
//总线停止条件
void IIC_Stop(void)
{
SDA = 0;
SCL = 1;
IIC_Delay(DELAY_TIME);
SDA = 1;
IIC_Delay(DELAY_TIME);
}
//发送应答
void IIC_SendAck(bit ackbit)
{
SCL = 0;
SDA = ackbit; // 0:应答,1:非应答
IIC_Delay(DELAY_TIME);
SCL = 1;
IIC_Delay(DELAY_TIME);
SCL = 0;
SDA = 1;
IIC_Delay(DELAY_TIME);
}
//等待应答
bit IIC_WaitAck(void)
{
bit ackbit;
SCL = 1;
IIC_Delay(DELAY_TIME);
ackbit = SDA;
SCL = 0;
IIC_Delay(DELAY_TIME);
return ackbit;
}
//通过I2C总线发送数据
void IIC_SendByte(unsigned char byt)
{
unsigned char i;
for(i=0; i<8; i++)
{
SCL = 0;
IIC_Delay(DELAY_TIME);
if(byt & 0x80) SDA = 1;
else SDA = 0;
IIC_Delay(DELAY_TIME);
SCL = 1;
byt <<= 1;
IIC_Delay(DELAY_TIME);
}
SCL = 0;
}
//从I2C总线上接收数据
unsigned char IIC_RecByte(void)
{
unsigned char i, da;
for(i=0; i<8; i++)
{
SCL = 1;
IIC_Delay(DELAY_TIME);
da <<= 1;
if(SDA) da |= 1;
SCL = 0;
IIC_Delay(DELAY_TIME);
}
return da;
}
void IIC_DAC_XieData(unsigned char date){
IIC_Start();
IIC_SendByte(0x90);
IIC_WaitAck();
IIC_SendByte(0x40);
IIC_WaitAck();
IIC_SendByte(date);
IIC_WaitAck();
IIC_Stop();
}
unsigned char IIC_ADC_GetData(unsigned char di_zhi){
unsigned char date;
IIC_Start();
IIC_SendByte(0x90);
IIC_WaitAck();
IIC_SendByte(di_zhi);
IIC_WaitAck();
IIC_Stop();
IIC_Start();
ET1=0;
IIC_SendByte(0x91);
IIC_WaitAck();
date=IIC_RecByte();
ET1=1;
IIC_SendAck(1);
IIC_WaitAck();
IIC_Stop();
return date;
}
void IIC_EEPROM_XieData(unsigned char di_zhi,date){
IIC_Start();
IIC_SendByte(0xa0);
IIC_WaitAck();
IIC_SendByte(di_zhi);
IIC_WaitAck();
IIC_SendByte(date);
IIC_WaitAck();
IIC_Stop();
}
unsigned char IIC_EEPROM_GetData(unsigned char di_zhi){
unsigned char date;
IIC_Start();
IIC_SendByte(0xa0);
IIC_WaitAck();
IIC_SendByte(di_zhi);
IIC_WaitAck();
IIC_Stop();
IIC_Start();
IIC_SendByte(0xa1);
IIC_WaitAck();
date=IIC_RecByte();
IIC_SendAck(1);
IIC_WaitAck();
IIC_Stop();
return date;
}
SMG.h
#ifndef __SMG_H_
#define __SMG_H_
#include <STC15F2K60S2.H>
void SMG_Init();
void SMG_Show(unsigned char n1,n2,n3,n4,n5,n6,n7,n8);
#endif
SMG.c
#include "SMG.h"
unsigned char code t_display[]={ //????
// 0 1 2 3 4 5 6 7 8 9 A B C D E F
0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F,0x77,0x7C,0x39,0x5E,0x79,0x71,
//black - H J K L N o P U t G Q r M y
0x00,0x40,0x76,0x1E,0x70,0x38,0x37,0x5C,0x73,0x3E,0x78,0x3d,0x67,0x50,0x37,0x6e,
0xBF,0x86,0xDB,0xCF,0xE6,0xED,0xFD,0x87,0xFF,0xEF,0x46}; //0. 1. 2. 3. 4. 5. 6. 7. 8. 9. -1
void SMG_Init(){
P2=(P2&0x1f)|0xc0;
P0=0xff;
P2=(P2&0x1f)|0xe0;
P0=0xff;
}
void SMG_Show(unsigned char n1,n2,n3,n4,n5,n6,n7,n8){
static unsigned char i=0;
i++;
i=i%8;
P2=(P2&0x1f)|0xc0;
switch(i){
case 1:{
P0=0x01;
P2=(P2&0x1f)|0xe0;
P0=~t_display[n1];
break;
}
case 2:{
P0=0x02;
P2=(P2&0x1f)|0xe0;
P0=~t_display[n2];
break;
}
case 3:{
P0=0x04;
P2=(P2&0x1f)|0xe0;
P0=~t_display[n3];
break;
}
case 4:{
P0=0x08;
P2=(P2&0x1f)|0xe0;
P0=~t_display[n4];
break;
}
case 5:{
P0=0x10;
P2=(P2&0x1f)|0xe0;
P0=~t_display[n5];
break;
}
case 6:{
P0=0x20;
P2=(P2&0x1f)|0xe0;
P0=~t_display[n6];
break;
}
case 7:{
P0=0x40;
P2=(P2&0x1f)|0xe0;
P0=~t_display[n7];
break;
}
case 0:{
P0=0x80;
P2=(P2&0x1f)|0xe0;
P0=~t_display[n8];
break;
}
}
P2=(P2&0x1f)|0x00;
}
PWM.h
#ifndef __PWM_H_
#define __PWM_H_
#include <STC15F2K60S2.H>
void PWM_DSQ_1_Init();
#endif
PWM.c
#include "PWM.h"
void PWM_DSQ_1_Init(){
AUXR |= 0x40; //定时器时钟1T模式
TMOD &= 0x0F; //设置定时器模式
TL1 = 0x88; //设置定时初值
TH1 = 0xFF; //设置定时初值
TF1 = 0; //清除TF1标志
TR1 = 1; //定时器1开始计时
EA=1;
ET1=0;
}