答案仅供参考,有错误欢迎指正,非常感谢!!!
文件较多,代码较长,感谢查看!!!
目录
main.h
main.c
Init.h
Init.c
SMG.h
SMG.c
DSQ.h
DSQ.c
JZKey.h
JZKey.c
YanShi.h
YanShi.c
iic.h
iic.c
#ifndef __MAIN_H_
#define __MAIN_H_
#define uchar unsigned char
#define uint unsigned int
#include
#include "Init.h"
#include "SMG.h"
#include "DSQ.h"
#include "JZKey.h"
#include "YanShi.h"
#include "iic.h"
void JieMian_Show();
void LED_Show();
#endif
#include "main.h"
uint key_num;
uchar jie_mian=0;
long int ji_shu=0;
int DianYa_CanShu=250;
uchar flag0=0;
uint dian_ya=0;
uchar flag=0;
uint dian_ya_old;
long int WuXiao_AnJian=0;
uint time=0;
uchar gao=0;
uchar di=0;
uchar flag1=0;
void main(){
Init_BanZi();
SMG_Init();
DSQ_0_Init();
JZKey_Init();
DianYa_CanShu=IIC_EEPROM_GetDate(0x00)*10;
while(1){
key_num=JZKey_GetKeynum();
if(key_num>0){
if(key_num==12){
WuXiao_AnJian=0;
jie_mian++;
jie_mian=jie_mian%3;
if(jie_mian==2){//保存电压参数
IIC_EEPROM_XieDate(0,DianYa_CanShu/10);
}
}
else if(key_num==13&&jie_mian==2){
WuXiao_AnJian=0;
ji_shu=0;
}
else if(key_num==16&&jie_mian==1){
WuXiao_AnJian=0;
DianYa_CanShu=DianYa_CanShu+50;
DianYa_CanShu=DianYa_CanShu%550;
}
else if(key_num==17&&jie_mian==1){
WuXiao_AnJian=0;
DianYa_CanShu=DianYa_CanShu-50;
if(DianYa_CanShu<0){
DianYa_CanShu=500;
}
}
else {
WuXiao_AnJian++;
}
}
if(flag==2){
dian_ya=(IIC_ADC_GetDate(3)*1.0)/51.0*100;
flag=0;
}
}
}
void DSQ_0_ZD() interrupt 1{
flag0++;
flag++;
flag1=1;
if(dian_ya=6000){
time=6000;
}
}
else{
time=0;
}
if(dian_ya>DianYa_CanShu){
gao=1;
}
if(gao&&(dian_ya=2){
flag0=0;
JieMian_Show();
LED_Show();
}
}
void JieMian_Show(){//界面显示
switch(jie_mian){
case 0:{
SMG_Show(25,16,16,16,16,(dian_ya/100)+32,dian_ya/10%10,dian_ya%10);
break;
}
case 1:{
SMG_Show(24,16,16,16,16,(DianYa_CanShu/100)+32,DianYa_CanShu/10%10,DianYa_CanShu%10);
break;
}
case 2:{
if(ji_shu>=1000000){
SMG_Show(22,ji_shu/1000000,ji_shu/100000%10,ji_shu/10000%10,ji_shu/1000%10,ji_shu/100%10,ji_shu/10%10,ji_shu%10);
}
else if(ji_shu>=100000){
SMG_Show(22,16,ji_shu/100000,ji_shu/10000%10,ji_shu/1000%10,ji_shu/100%10,ji_shu/10%10,ji_shu%10);
}
else if(ji_shu>=10000){
SMG_Show(22,16,16,ji_shu/10000,ji_shu/1000%10,ji_shu/100%10,ji_shu/10%10,ji_shu%10);
}
else if(ji_shu>=1000){
SMG_Show(22,16,16,16,ji_shu/1000,ji_shu/100%10,ji_shu/10%10,ji_shu%10);
}
else if(ji_shu>=100){
SMG_Show(22,16,16,16,16,ji_shu/100,ji_shu/10%10,ji_shu%10);
}
else if(ji_shu>=10){
SMG_Show(22,16,16,16,16,16,ji_shu/10,ji_shu%10);
}
else if(ji_shu>=0){
SMG_Show(22,16,16,16,16,16,16,ji_shu%10);
}
break;
}
}
}
void LED_Show(){
uchar L1=0xff;
uchar L2=0xff;
uchar L3=0xff;
if(time>5000){//超过5秒,L1亮
L1=0xfe;
}
else{
L1=0xff;
}
if((ji_shu%2)==1){//计数为奇数,L2亮
L2=0xfd;
}
else{
L2=0xff;
}
if(WuXiao_AnJian>=3){//无效按键3次以上,L3亮
L3=0xfb;
}
else{
L3=0xff;
}
P0=L1&L2&L3;
P2=(P2&0x1f)|0x80;
P2=(P2&0x1f)|0x00;
}
#ifndef __INIT_H_
#define __INIT_H_
#include
void Init_BanZi();
#endif
#include "Init.h"
void Init_BanZi(){
P2=(P2&0x1f)|0xa0;
P0=0x00;
P2=(P2&0x1f)|0x80;
P0=0xff;
}
#ifndef __SMG_H_
#define __SMG_H_
#include
void SMG_Init();
void SMG_Show(unsigned char n1,n2,n3,n4,n5,n6,n7,n8);
#endif
#include "SMG.h"
unsigned char code SMG_ZK[]={ //标准字库
// 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=~SMG_ZK[n1];
break;
}
case 2:{
P0=0x02;
P2=(P2&0x1f)|0xe0;
P0=~SMG_ZK[n2];
break;
}
case 3:{
P0=0x04;
P2=(P2&0x1f)|0xe0;
P0=~SMG_ZK[n3];
break;
}
case 4:{
P0=0x08;
P2=(P2&0x1f)|0xe0;
P0=~SMG_ZK[n4];
break;
}
case 5:{
P0=0x10;
P2=(P2&0x1f)|0xe0;
P0=~SMG_ZK[n5];
break;
}
case 6:{
P0=0x20;
P2=(P2&0x1f)|0xe0;
P0=~SMG_ZK[n6];
break;
}
case 7:{
P0=0x40;
P2=(P2&0x1f)|0xe0;
P0=~SMG_ZK[n7];
break;
}
case 0:{
P0=0x80;
P2=(P2&0x1f)|0xe0;
P0=~SMG_ZK[n8];
break;
}
}
P2=(P2&0x1f)|0x00;
}
#ifndef __DSQ_H_
#define __DSQ_H_
#include
void DSQ_0_Init();
#endif
#include "DSQ.h"
void DSQ_0_Init(){
AUXR |= 0x80; //定时器时钟1T模式
TMOD &= 0xF0; //设置定时器模式
TL0 = 0x20; //设置定时初值
TH0 = 0xD1; //设置定时初值
TF0 = 0; //清除TF0标志
TR0 = 1; //定时器0开始计时
EA=1;
ET0=1;
}
#ifndef __JZKEY_H_
#define __JZKEY_H_
#include "YanShi.h"
void JZKey_Init();
unsigned char JZKey_GetKeynum();
#endif
#include "JZKey.h"
void JZKey_Init(){
P30=1;
P31=1;
P32=1;
P33=1;
P34=0;
P35=0;
P42=0;
P44=0;
}
unsigned char JZKey_GetKeynum(){
unsigned char key_num=0;
if(P30==0){
P30=0;
P34=1;
P35=1;
P42=1;
P44=1;
if(P34==0){
Delay10ms();
if(P34==0){
key_num=19;
}
while(P34==0);
}
else if(P35==0){
Delay10ms();
if(P35==0){
key_num=15;
}
while(P35==0);
}
else if(P42==0){
Delay10ms();
if(P42==0){
key_num=11;
}
while(P42==0);
}
else if(P44==0){
Delay10ms();
if(P44==0){
key_num=7;
}
while(P44==0);
}
}
else if(P31==0){
P31=0;
P34=1;
P35=1;
P42=1;
P44=1;
if(P34==0){
Delay10ms();
if(P34==0){
key_num=18;
}
while(P34==0);
}
else if(P35==0){
Delay10ms();
if(P35==0){
key_num=14;
}
while(P35==0);
}
else if(P42==0){
Delay10ms();
if(P42==0){
key_num=10;
}
while(P42==0);
}
else if(P44==0){
Delay10ms();
if(P44==0){
key_num=6;
}
while(P44==0);
}
}
else if(P32==0){
P32=0;
P34=1;
P35=1;
P42=1;
P44=1;
if(P34==0){
Delay10ms();
if(P34==0){
key_num=17;
}
while(P34==0);
}
else if(P35==0){
Delay10ms();
if(P35==0){
key_num=13;
}
while(P35==0);
}
else if(P42==0){
Delay10ms();
if(P42==0){
key_num=9;
}
while(P42==0);
}
else if(P44==0){
Delay10ms();
if(P44==0){
key_num=5;
}
while(P44==0);
}
}
else if(P33==0){
P33=0;
P34=1;
P35=1;
P42=1;
P44=1;
if(P34==0){
Delay10ms();
if(P34==0){
key_num=16;
}
while(P34==0);
}
else if(P35==0){
Delay10ms();
if(P35==0){
key_num=12;
}
while(P35==0);
}
else if(P42==0){
Delay10ms();
if(P42==0){
key_num=8;
}
while(P42==0);
}
else if(P44==0){
Delay10ms();
if(P44==0){
key_num=4;
}
while(P44==0);
}
}
JZKey_Init();
return key_num;
}
#ifndef __YANSHI_H_
#define __YANSHI_H_
#include
#include "intrins.h"
void Delay5ms();
void Delay10ms();
#endif
#include "YanShi.h"
void Delay5ms() //@12.000MHz
{
unsigned char i, j;
i = 59;
j = 90;
do
{
while (--j);
} while (--i);
}
void Delay10ms() //@12.000MHz
{
unsigned char i, j;
i = 117;
j = 184;
do
{
while (--j);
} while (--i);
}
#ifndef _IIC_H
#define _IIC_H
#include
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);
unsigned char IIC_ADC_GetDate(unsigned char di_zhi);
void IIC_DAC_XieDate(unsigned char date);
void IIC_EEPROM_XieDate(unsigned char di_zhi,unsigned char date);
unsigned char IIC_EEPROM_GetDate(unsigned char di_zhi);
#endif
/*
程序说明: IIC总线驱动程序
软件环境: Keil uVision 4.10
硬件环境: CT107单片机综合实训平台 8051,12MHz
日 期: 2011-8-9
*/
#include "iic.h"
#include "intrins.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;
}
unsigned char IIC_ADC_GetDate(unsigned char di_zhi){
unsigned char date;
IIC_Start();
IIC_SendByte(0x90);
IIC_WaitAck();
IIC_SendByte(di_zhi);
IIC_WaitAck();
IIC_Stop();
IIC_Start();
IIC_SendByte(0x91);
IIC_WaitAck();
date=IIC_RecByte();
IIC_SendAck(1);
IIC_WaitAck();
IIC_Stop();
return date;
}
void IIC_DAC_XieDate(unsigned char date){
IIC_Start();
IIC_SendByte(0x90);
IIC_WaitAck();
IIC_SendByte(0x40);
IIC_WaitAck();
IIC_SendByte(date);
IIC_WaitAck();
IIC_Stop();
}
void IIC_EEPROM_XieDate(unsigned char di_zhi,unsigned char 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_GetDate(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;
}