当前位置:网站首页>The first game of the 11th provincial single chip microcomputer competition of the Blue Bridge Cup
The first game of the 11th provincial single chip microcomputer competition of the Blue Bridge Cup
2022-07-02 03:38:00 【Super 561】




main.c
#include <STC15F2K60S2.H>
#include <IIC.H>
#define NO_KEY 0XFF
#define KEY_STATE0 0
#define KEY_STATE1 1
#define KEY_STATE2 2
void Device_ctrl(unsigned char p2date,unsigned char p0date)
{
P0=p0date;
P2=P2&0X1F|p2date;
P2&=0x1f;
}
unsigned int key_count;
unsigned char display_mode;
unsigned int set_dianya=0;
unsigned char count;
unsigned char key_press;
unsigned char key_scan()
{
static unsigned char key_state=KEY_STATE0;
unsigned char key_value=0,key_temp;
unsigned char key1,key2;
P30=0;P31=0;P32=0;P33=0;P34=1;P35=1;P42=1;P44=1;
if(P44==0){key1=0X70;}
if(P42==0){key1=0Xb0;}
if(P35==0){key1=0xd0;}
if(P34==0){key1=0xe0;}
if((P44==1)&&(P42==1)&&(P34==1)&&(P35==1)){key1=0xf0;}
P30=1;P31=1;P32=1;P33=1;P34=0;P35=0;P42=0;P44=0;
if(P30==0){key2=0x0e;}
if(P31==0){key2=0x0d;}
if(P32==0){key2=0x0b;}
if(P33==0){key2=0x07;}
if((P30==1)&&(P31==1)&&(P32==1)&&(P33==1)){key2=0x0f;}
key_temp=key1|key2;
switch(key_state)
{
case KEY_STATE0:
if(key_temp!=NO_KEY)
{
key_state=KEY_STATE1;
}
break;
case KEY_STATE1:
if(key_temp==NO_KEY)
{
key_state=KEY_STATE0;
}
else
{
switch(key_temp)
{
case 0x77:key_value=4;break;
case 0x7b:key_value=5;break;
case 0x7d:key_value=6;break;
case 0x7e:key_value=7;break;
case 0xb7:key_value=8;break;
case 0xbb:key_value=9;break;
case 0xbd:key_value=10;break;
case 0xbe:key_value=11;break;
case 0xd7:key_value=12;break;
case 0xdb:key_value=13;break;
case 0xdd:key_value=14;break;
case 0xde:key_value=15;break;
case 0xe7:key_value=16;break;
case 0xeb:key_value=17;break;
case 0xed:key_value=18;break;
case 0xee:key_value=19;break;
}
key_state=KEY_STATE2;
}
break;
case KEY_STATE2:
if(key_temp==NO_KEY)
{
key_state=KEY_STATE0;
}break;
}
return key_value;
}
void key_process()
{
unsigned char key_val;
if(key_count>=5)
{
key_count=0;
key_val=key_scan();
switch(key_val)
{
case 4:key_press++;break;
case 5:key_press++;break;
case 6:key_press++;break;
case 7:key_press++;break;
case 8:key_press++;break;
case 9:key_press++;break;
case 10:key_press++;break;
case 11:key_press++;break;
case 12:
display_mode++;
if(display_mode==3)
{display_mode=0;}
key_press=0;
break;
case 13:if(display_mode==2)
{count=0;}
key_press=0;
break;
case 14:key_press++;break;
case 15:key_press++;break;
case 16:
if(display_mode==1)
{
set_dianya=set_dianya+50;
if(set_dianya>500)
{set_dianya=0;}
}
key_press=0;break;
case 17:
if(display_mode==1)
{
if(set_dianya>0)
{
set_dianya=set_dianya-50;
}
else
{set_dianya=500;}
}
key_press=0;break;
case 18:key_press++;break;
case 19:key_press++;break;
}
}
}
unsigned char smg_display[8];
unsigned char smg_du[]={0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F};
unsigned int smg_count;
unsigned int dianya;
unsigned char ad;
unsigned int ad_count;
unsigned int ad_before;
unsigned int ad_now;
unsigned char read_ad(unsigned char add)
{
unsigned char date;
IIC_Start();
IIC_SendByte(0x90);
IIC_WaitAck();
IIC_SendByte(add);
IIC_WaitAck();
IIC_Start();
IIC_SendByte(0x91);
IIC_WaitAck();
date=IIC_RecByte();
IIC_SendAck(1);
IIC_Stop();
return date;
}
void ad_process()
{
if(ad_count>10)
{
ad_count=0;
ad=read_ad(0x03);
dianya=ad/51.0f*100;
ad_before=dianya;
ad_now=read_ad(0x03);
ad_now=ad_now/51.0f*100;
if((ad_before>set_dianya)&&(ad_before>ad_now)&&(ad_now<set_dianya))
{
count++;
}
}
}
void smg_show()
{
unsigned int i;
Device_ctrl(0xc0,0);
Device_ctrl(0xe0,~smg_display[i]);
Device_ctrl(0xc0,0x01<<i);
i=(i+1)%8;
}
void smg_process()
{
if(smg_count>3)
{
smg_count=0;
if(display_mode==0)
{
smg_display[0]=0x3E;
smg_display[1]=0x00;
smg_display[2]=0x00;
smg_display[3]=0x00;
smg_display[4]=0x00;
smg_display[5]=smg_du[dianya/100]|0x80;
smg_display[6]=smg_du[dianya/10%10];
smg_display[7]=smg_du[dianya%10];
}
if(display_mode==1)
{
smg_display[0]=0x73;
smg_display[1]=0x00;
smg_display[2]=0x00;
smg_display[3]=0x00;
smg_display[4]=0x00;
smg_display[5]=smg_du[(set_dianya)/100]|0x80;
smg_display[6]=smg_du[(set_dianya)/10%10];
smg_display[7]=smg_du[(set_dianya)%10];
}
if(display_mode==2)
{
smg_display[0]=0x73;
smg_display[1]=0x00;
smg_display[2]=0x00;
smg_display[3]=0x00;
smg_display[4]=0x00;
smg_display[5]=0x00;
smg_display[6]=smg_du[count/10];
smg_display[7]=smg_du[count%10];
}
}
}
void Timer2Init() //1 millisecond @12.000MHz
{
AUXR &= 0xFB; // Timer clock 12T Pattern
T2L = 0x18; // Set initial value of timing
T2H = 0xFC; // Set initial value of timing
AUXR |= 0x10; // Timer 2 Start timing
IE2|=0X04;
EA=1;
}
unsigned int led_count;
unsigned char led_ctrl=0xff;
unsigned int dianya_count;
bit led_flag=0;
void led_process()
{
if(led_count>10)
{
led_count=0;
if(dianya<set_dianya)
{
dianya_count++;
if(dianya_count>500)
{
led_flag=1;
dianya_count=0;
}
}
else
{
led_flag=0;
}
if(led_flag==1)
{
led_ctrl&=~0x01;
}
else
{
led_ctrl|=0x01;
}
if(count%2)
{
led_ctrl&=~0x02;
}
else
{
led_ctrl|=0x02;
}
if(key_press>=3)
{
led_ctrl&=~0x04;
}
else
{
led_ctrl|=0x04;
}
Device_ctrl(0x80,led_ctrl);
}
}
void main()
{
Timer2Init();
Device_ctrl(0xa0,0x00);
Device_ctrl(0x80,0xff);
while(1)
{
key_process();
smg_process();
ad_process();
led_process();
}
}
void timer2service() interrupt 12
{
key_count++;
smg_count++;
led_count++;
ad_count++;
smg_show();
}iic.c
#include "iic.h"
#define DELAY_TIME 5
//I2C Bus internal delay function
void IIC_Delay(unsigned char i)
{
do{_nop_();}
while(i--);
}
//I2C Bus start signal
void IIC_Start(void)
{
SDA = 1;
SCL = 1;
IIC_Delay(DELAY_TIME);
SDA = 0;
IIC_Delay(DELAY_TIME);
SCL = 0;
}
//I2C Bus stop signal
void IIC_Stop(void)
{
SDA = 0;
SCL = 1;
IIC_Delay(DELAY_TIME);
SDA = 1;
IIC_Delay(DELAY_TIME);
}
// Send a reply or non reply signal
void IIC_SendAck(bit ackbit)
{
SCL = 0;
SDA = ackbit;
IIC_Delay(DELAY_TIME);
SCL = 1;
IIC_Delay(DELAY_TIME);
SCL = 0;
SDA = 1;
IIC_Delay(DELAY_TIME);
}
// Waiting for an answer
bit IIC_WaitAck(void)
{
bit ackbit;
SCL = 1;
IIC_Delay(DELAY_TIME);
ackbit = SDA;
SCL = 0;
IIC_Delay(DELAY_TIME);
return ackbit;
}
//I2C The bus sends a byte of data
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 The bus receives a byte of data
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;
}
iic.h
#ifndef __IIC_H
#define __IIC_H
#include <STC15F2K60S2.H>
#include "intrins.h"
sbit SDA = P2^1;
sbit SCL = P2^0;
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);
#endif边栏推荐
- 蓝桥杯单片机省赛第六届
- Screenshot literacy tool download and use
- C # joint halcon out of halcon Environment and various Error Reporting and Resolution Experiences
- What is the logical structure of database file
- Comment élaborer une stratégie nuageuse à l'ère des nuages mixtes
- uniapp 使用canvas 生成海报并保存到本地
- Gradle foundation | customize the plug-in and upload it to jitpack
- 蓝桥杯单片机省赛第十一届
- < job search> process and signal
- How to establish its own NFT market platform in 2022
猜你喜欢

Discrimination between sap Hana, s/4hana and SAP BTP

This article describes the step-by-step process of starting the NFT platform project

Pointer array & array pointer
![[yolo3d]: real time detection of end-to-end 3D point cloud input](/img/5e/f17960d302f663db75ad82ae0fd70f.jpg)
[yolo3d]: real time detection of end-to-end 3D point cloud input

JIT deep analysis

高性能 低功耗Cortex-A53核心板 | i.MX8M Mini

Halcon image rectification

Failed to upgrade schema, error: “file does not exist

Which of PMP and software has the highest gold content?

In wechat applet, the externally introduced JS is used in xwml for judgment and calculation
随机推荐
Global and Chinese market of gynaecological health training manikin 2022-2028: Research Report on technology, participants, trends, market size and share
Kotlin 基础学习13
Comment élaborer une stratégie nuageuse à l'ère des nuages mixtes
Global and Chinese markets for ultrasonic probe disinfection systems 2022-2028: Research Report on technology, participants, trends, market size and share
Global and Chinese market of X-ray detectors 2022-2028: Research Report on technology, participants, trends, market size and share
Kubernetes cluster storageclass persistent storage resource core concept and use
PHP array processing
Sentry experience and architecture, a fledgling monitoring product with a market value of $100million
数据库文件逻辑结构形式指的是什么
Verilog parallel block implementation
Pycharm2021 delete the package warehouse list you added
This article describes the step-by-step process of starting the NFT platform project
In wechat applet, the externally introduced JS is used in xwml for judgment and calculation
aaaaaaaaaaaaa
Imageai installation
Interface debugging tool simulates post upload file - apipost
Discrimination between sap Hana, s/4hana and SAP BTP
Basic syntax of unity script (8) - collaborative program and destruction method
ImageAI安装
aaaaaaaaaaaaa