当前位置:网站首页>The 11th Blue Bridge Cup single chip microcomputer provincial competition
The 11th Blue Bridge Cup single chip microcomputer provincial competition
2022-07-02 03:38:00 【Super 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 int dianya1;
unsigned int dianya3;
unsigned char add=0x01;
bit dianya_flag;
unsigned int set_dianya1=250;
unsigned int set_dianya3=300;
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;
dianya1=read_ad(0x01);
dianya1=dianya1/51.0f*100;
dianya3=read_ad(0x03);
dianya3=dianya3/51.0f*100;
}
}
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 display_mode;
unsigned char mode=1;
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)
{
smg_display[0]=0x73;
smg_display[1]=smg_du[mode];
smg_display[2]=0x00;
smg_display[3]=0x00;
smg_display[4]=0x00;
if(dianya_flag)
{
smg_display[5]=smg_du[set_dianya3/100]|0x80;
smg_display[6]=smg_du[set_dianya3/10%10];
smg_display[7]=smg_du[set_dianya3%10];
}
else
{
smg_display[5]=smg_du[set_dianya1/100]|0x80;
smg_display[6]=smg_du[set_dianya1/10%10];
smg_display[7]=smg_du[set_dianya1%10];
}
}
else
{
smg_display[0]=0x3E;
smg_display[1]=smg_du[mode];
smg_display[2]=0x00;
smg_display[3]=0x00;
smg_display[4]=0x00;
if(dianya_flag==0)
{
smg_display[5]=smg_du[dianya3/100]|0x80;
smg_display[6]=smg_du[dianya3/10%10];
smg_display[7]=smg_du[dianya3%10];
}
else
{
smg_display[5]=smg_du[dianya1/100]|0x80;
smg_display[6]=smg_du[dianya1/10%10];
smg_display[7]=smg_du[dianya1%10];
}
}
}
}
unsigned char Trig_btn;
unsigned char Cont_btn;
unsigned int key_count;
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)
{
Device_ctrl(0x80,~0X01);
dianya_flag=~dianya_flag;
if(display_mode)
{
if(dianya_flag)
{
mode=3;
add=0x03;
}
else
{
mode=1;
add=0x01;
}
}
else
{
if(dianya_flag)
{
mode=3;
add=0x03;
}
else
{
mode=1;
add=0x01;
}
}
}
if(Trig_btn==0x04)
{
display_mode++;
if(display_mode==2)
{
display_mode=0;
}
Device_ctrl(0x80,~0X02);
}
if(Trig_btn==0x02)
{
if(display_mode==1)
{
if(mode==1)
{
if(set_dianya1<500)
{
set_dianya1=set_dianya1+20;
}
}
if(mode==3)
{
if(set_dianya3<500)
{
set_dianya3=set_dianya3+20;
}
}
}
Device_ctrl(0x80,~0X04);
}
if(Trig_btn==0x01)
{
if(display_mode==1)
{
if(mode==1)
{
if(set_dianya1>=20)
{
set_dianya1=set_dianya1-20;
}
}
if(mode==3)
{
if(set_dianya3>=20)
{
set_dianya3=set_dianya3-20;
}
}
}
Device_ctrl(0x80,~0X08);
}
}
}
void Timer2Init() //1 millisecond @12.000MHz
{
AUXR |= 0x04; // Timer clock 1T Pattern
T2L = 0x20; // Set initial value of timing
T2H = 0xD1; // Set initial value of timing
AUXR |= 0x10; // Timer 2 Start timing
IE2|=0X04;
EA=1;
}
unsigned int led_count;
unsigned char led_ctrl=0xff;
void led_process()
{
if(led_count>10)
{
led_count=0;
if(mode==1)
{
if(dianya1>set_dianya1)
{
led_ctrl&=~0x01;
}
else
{
led_ctrl|=0x01;
}
}
if(mode==3)
{
if(dianya3>set_dianya3)
{
led_ctrl&=~0x02;
}
else
{
led_ctrl|=0x02;
}
}
if(mode==1)
{
led_ctrl&=~0x04;
}
else
{
led_ctrl|=0x04;
}
if(display_mode==0)
{
led_ctrl&=~0x08;
}
else
{
led_ctrl|=0x08;
}
if(dianya1>dianya3)
{
led_ctrl&=~0x10;
}
else
{
led_ctrl|=0x10;
}
Device_ctrl(0x80,led_ctrl);
}
}
void main()
{
Timer2Init();
Device_ctrl(0xa0,0x00);
Device_ctrl(0x80,0xff);
while(1)
{
smg_process();
key_process();
ad_process();
led_process();
}
}
void timer2service() interrupt 12
{
key_count++;
smg_count++;
ad_count++;
led_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
边栏推荐
猜你喜欢
Eight steps of agile development process
SAML2.0 notes (I)
Getting started with MQ
Pycharm2021 delete the package warehouse list you added
焱融看 | 混合云时代下,如何制定多云策略
[C Advanced] brother Peng takes you to play with strings and memory functions
Basic syntax of unity script (6) - specific folder
焱融看 | 混合雲時代下,如何制定多雲策略
[mv-3d] - multi view 3D target detection network
近段时间天气暴热,所以采集北上广深去年天气数据,制作可视化图看下
随机推荐
Get started with Aurora 8b/10b IP core in one day (5) -- learn from the official routine of framing interface
h5中的页面显示隐藏执行事件
One of the future trends of SAP ui5: embrace typescript
蓝桥杯单片机省赛第八届
How to do medium and long-term stocks, and what are the medium and long-term stock trading skills?
Review materials of project management PMP high frequency examination sites (8-1)
Global and Chinese markets for electronic laryngoscope systems 2022-2028: Research Report on technology, participants, trends, market size and share
In depth analysis of C language - variable error prone knowledge points # dry goods inventory #
How about Ping An lifetime cancer insurance?
ThreadLocal详解
Kubernetes cluster storageclass persistent storage resource core concept and use
Large screen visualization from bronze to the advanced king, you only need a "component reuse"!
This article describes the step-by-step process of starting the NFT platform project
Uniapp uses canvas to generate posters and save them locally
Getting started with MQ
Oracle 常用SQL
Kotlin basic learning 14
[designmode] Prototype Pattern
Failed to upgrade schema, error: “file does not exist
一天上手Aurora 8B/10B IP核(5)----从Framing接口的官方例程学起