当前位置:网站首页>蓝桥杯单片机省赛第九届
蓝桥杯单片机省赛第九届
2022-07-02 03:29:00 【超561】
main.c
#include <STC15F2K60S2.H>
#include <IIC.H>
void Device_ctrl(unsigned char p2date,unsigned char p0date )
{
P0=p0date;
P2=P2&0X1F|p2date;
P2&=0X1F;
}
unsigned int ad_count;
unsigned char ad;
unsigned int pwm;
bit smg_flag;
unsigned char set_mode=1;
unsigned int interval_time[5]={0,1200,1000,800,400};
unsigned char key_press;
unsigned char display_mode;
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>100)
{
ad_count=0;
ad=read_ad(0x03);
ad=ad/85+1;
pwm=ad*2;
}
}
unsigned char smg_display[8];
unsigned char smg_du[]={0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F};
unsigned int smg_count;
unsigned char mode=1;
unsigned char index;
unsigned int interval=1000;
unsigned int time_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==1)
{
if(key_press==1)
{
if(smg_flag)
{
smg_display[0]=smg_du[0];
smg_display[1]=smg_du[set_mode];
smg_display[2]=smg_du[0];
}
else
{
smg_display[0]=0x00;
smg_display[1]=0x00;
smg_display[2]=0x00;
}
smg_display[3]=0x00;
smg_display[4]=smg_du[interval_time[set_mode]/1000];
smg_display[5]=smg_du[interval_time[set_mode]/100%10];
smg_display[6]=smg_du[interval_time[set_mode]/10%10];
smg_display[7]=smg_du[interval_time[set_mode]%10];
}
if(key_press==2)
{
smg_display[0]=smg_du[0];
smg_display[1]=smg_du[set_mode];
smg_display[2]=smg_du[0];
smg_display[3]=0x00;
if(smg_flag)
{
smg_display[4]=smg_du[interval_time[set_mode]/1000];
smg_display[5]=smg_du[interval_time[set_mode]/100%10];
smg_display[6]=smg_du[interval_time[set_mode]/10%10];
smg_display[7]=smg_du[interval_time[set_mode]%10];
}
else
{
smg_display[4]=0x00;
smg_display[5]=0x00;
smg_display[6]=0x00;
smg_display[7]=0x00;
}
}
}
if(display_mode==0)
{
smg_display[0]=0x00;
smg_display[1]=0x00;
smg_display[2]=0x00;
smg_display[3]=0x00;
smg_display[4]=0x00;
smg_display[5]=0x00;
smg_display[6]=0x00;
smg_display[7]=0x00;
}
if(display_mode==2)
{
smg_display[0]=0x00;
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[ad/10];
smg_display[7]=smg_du[ad%10];
}
}
}
unsigned char Trig_btn;
unsigned char Cont_btn;
unsigned int key_count;
bit led_flag;
void key_btn()
{
unsigned char readdate=P3^0xff;
Trig_btn=readdate&(Cont_btn^readdate);
Cont_btn=readdate;
}
void key_process()
{
if(key_count>=10)
{
key_count=0;
key_btn();
if(Trig_btn==0x08)//s4
{
if(display_mode==1)
{
if(key_press==1)
{
if(set_mode>1)
{
set_mode--;
}
}
if(key_press==2)
{
if(interval>400)
{
interval=interval-100;
interval_time[set_mode]=interval;
}
}
}
if(display_mode==0)
{
display_mode=2;
}
}
if(Trig_btn==0x04)//s5
{
if(display_mode==1)
{
if(key_press==1)
{
if(set_mode<4)
{
set_mode++;
}
}
if(key_press==2)
{
if(interval<1200)
{
interval=interval+100;
interval_time[set_mode]=interval;
}
}
}
}
if(Trig_btn==0x02)//s6
{
display_mode=1;
key_press++;
if(key_press==3)
{
key_press=0;
display_mode=0;
interval_time[set_mode]=interval;
}
}
if(Trig_btn==0x01)//s7
{
led_flag=~led_flag;
index=0;
}
if(Trig_btn==0&Cont_btn==0)
{
if(display_mode==2)
{
display_mode=0;
}
}
}
}
void Timer2Init() //1毫秒@12.000MHz
{
AUXR &= 0xFB; //定时器时钟12T模式
T2L = 0x18; //设置定时初值
T2H = 0xFC; //设置定时初值
AUXR |= 0x10; //定时器2开始计时
IE2|=0X04;
EA=1;
}
void main()
{
Timer2Init();
Device_ctrl(0x80,0xff);
Device_ctrl(0xa0,0x00);
while(1)
{
smg_process();
key_process();
ad_process();
}
}
unsigned int led_count;
unsigned int pwm_count;
void timer2service() interrupt 12
{
smg_count++;
key_count++;
smg_show();
led_count++;
ad_count++;
pwm_count++;
time_count++;
if(time_count>800)
{
time_count=0;
smg_flag=~smg_flag;
}
if(led_flag)
{
if(pwm_count<pwm)
{
if(mode==1)
{
P0=~(0x01<<index);P2=0X80;P2=0;
if(led_count>interval_time[1])
{
led_count=0;
index++;
}
if(index==8)
{
index=0;
mode=2;
}
}
if(mode==2)
{
P0=~(0x80>>index);P2=0X80;P2=0;
if(led_count>interval_time[2])
{
led_count=0;
index++;
}
if(index==8)
{
index=0;
mode=3;
}
}
if(mode==3)
{
P0=~((0x01<<index)|(0X80>>index));P2=0X80;P2=0;
if(led_count>interval_time[3])
{
led_count=0;
index++;
}
if(index==4)
{
mode=4;
}
}
if(mode==4)
{
P0=~((0x01<<index)|(0X80>>index));P2=0X80;P2=0;
if(led_count>interval_time[4])
{
led_count=0;
index++;
}
if(index==8)
{
index=0;
mode=1;
}
}
}
else if(pwm_count<10)
{
P0=0XFF;P2=0X80;P2=0;
}
else
{
pwm_count=0;
}
}
}
iic.c
#include "iic.h"
#define DELAY_TIME 5
//I2C总线内部延时函数
void IIC_Delay(unsigned char i)
{
do{_nop_();}
while(i--);
}
//I2C总线启动信号
void IIC_Start(void)
{
SDA = 1;
SCL = 1;
IIC_Delay(DELAY_TIME);
SDA = 0;
IIC_Delay(DELAY_TIME);
SCL = 0;
}
//I2C总线停止信号
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;
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;
}
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
边栏推荐
- [golang] leetcode intermediate bracket generation & Full Permutation
- High performance and low power cortex-a53 core board | i.mx8m Mini
- Yan Rong looks at how to formulate a multi cloud strategy in the era of hybrid cloud
- 竞争与冒险 毛刺
- Generate random numbers that obey normal distribution
- /silicosis/geo/GSE184854_ scRNA-seq_ mouse_ lung_ ccr2/GSE184854_ RAW/GSM5598265_ matrix_ inflection_ demult
- Global and Chinese markets for welding equipment and consumables 2022-2028: Research Report on technology, participants, trends, market size and share
- In depth interpretation of pytest official documents (26) customized pytest assertion error information
- Retrofit's callback hell is really vulnerable in kotlin synergy mode
- Kotlin basic learning 14
猜你喜欢
[yolo3d]: real time detection of end-to-end 3D point cloud input
Pointer array & array pointer
《MATLAB 神经网络43个案例分析》:第42章 并行运算与神经网络——基于CPU/GPU的并行神经网络运算
verilog 并行块实现
Introduction to Robotics II. Forward kinematics, MDH method
What do you know about stock selling skills and principles
Sentry experience and architecture, a fledgling monitoring product with a market value of $100million
Oracle的md5
MySQL advanced (Advanced) SQL statement (II)
NLog使用
随机推荐
Gradle 笔记
High performance and low power cortex-a53 core board | i.mx8m Mini
Comment élaborer une stratégie nuageuse à l'ère des nuages mixtes
Verilog 避免 Latch
This article describes the step-by-step process of starting the NFT platform project
MySQL index, transaction and storage engine
Kotlin basic learning 16
【DesignMode】原型模式(prototype pattern)
Gradle notes
Verilog avoid latch
Aaaaaaaaaaaa
[designmode] builder model
Global and Chinese market of bone adhesives 2022-2028: Research Report on technology, participants, trends, market size and share
[punch in] flip the string (simple)
Download and use of the super perfect screenshot tool snipaste
/silicosis/geo/GSE184854_scRNA-seq_mouse_lung_ccr2/GSE184854_RAW/GSM5598265_matrix_inflection_demult
Kotlin基础学习 15
SAML2.0 笔记(一)
Oracle 查看被锁的表和解锁
Grpc quick practice