第16屆藍橋杯單片機模擬試題Ⅱ

試題

代碼?

sys.h

#ifndef __SYS_H__
#define __SYS_H__#include <STC15F2K60S2.H>
//ds1302.c
extern unsigned char time[3];
void w_ds1302();
void r_ds1302();
//iic.c
float v_adc(unsigned char addr);
//sys.c
extern float light_v;
extern float rb2_v;
extern unsigned char UI;
extern unsigned char UI_keep;
extern unsigned char ui2_son;
extern unsigned char ui2_son_flag;
extern unsigned char flag_touch[3];
extern unsigned char time_touch[3][3];
extern bit flag_happen;
void init74hc138(unsigned char addr);
void init();
void led_ui();
void rb2_or_light();
//seg_key.c
extern unsigned char Seg_Buff[8];
void Seg_Loop();
void Key_Loop();
void seg_ui();
#endif

main.c

#include "sys.h"
bit flag_seg=0;
bit flag_key=0;
bit flag_ds=0;
bit flag_rb2=0;
bit flag_light=0;
void Timer0_Init(void)		//100微秒@12.000MHz
{AUXR |= 0x80;			//定時器時鐘1T模式TMOD &= 0xF0;			//設置定時器模式TL0 = 0x50;				//設置定時初始值TH0 = 0xFB;				//設置定時初始值TF0 = 0;				//清除TF0標志TR0 = 1;				//定時器0開始計時ET0 = 1;				//使能定時器0中斷EA = 1;
}
void main(){init();Timer0_Init();w_ds1302();while(1){led_ui();if(flag_seg){flag_seg=0;Seg_Loop();}if(flag_key){flag_key=0;Key_Loop();seg_ui();}if(flag_ds){flag_ds=0;r_ds1302();}if(flag_light){flag_light=0;light_v=v_adc(3);seg_ui();}if(flag_rb2){flag_rb2=0;rb2_v=v_adc(1);seg_ui();}rb2_or_light();}
}
void Timer0_Isr(void) interrupt 1
{static unsigned char count1=0,count2=0;static unsigned int count3=0,count4=0,count5=0;count1++;count2++;count3++;count5++;if(count1==2){count1=0;flag_seg=1;}if(count2==50){count2=0;flag_key=1;}if(count3==1000){flag_light=1;}else if(count3==2000){count3=0;flag_rb2=1;}if(flag_happen){count4++;if(count4==30000){count4=0;flag_happen=0;UI=UI_keep;}}if(count5==5000){count5=0;flag_ds=1;}
}

sys.c

#include "sys.h"
unsigned char UI=0;
unsigned char UI_keep;
unsigned char ui2_son=0;
unsigned char ui2_son_flag=0;
unsigned char flag_touch[3]={0,0,0};
unsigned char time_touch[3][3]={0,0,0,0,0,0,0,0,0};
bit flag_happen=0;
float light_v;
float rb2_v;
void init74hc138(unsigned char addr){P2=(P2&0x1f)|(addr<<5);P2&=0x1f;
}
void init(){P0=0x00;init74hc138(5);P0=0xff;init74hc138(4);
}
void led(unsigned char addr){P0=~(0x01<<(addr-1));init74hc138(4);
}
void led_ui(){switch(UI){case 0:led(1);break;case 1:led(2);break;case 2:led(3);break;case 4:led(8);break;}
}
void rb2_or_light(){EA = 0;if(light_v<rb2_v&&flag_happen==0){flag_happen=1;flag_touch[ui2_son_flag]=1;ui2_son_flag++;if(ui2_son_flag==3)ui2_son_flag=0;time_touch[2][0]=time_touch[1][0];time_touch[2][1]=time_touch[1][1];time_touch[2][2]=time_touch[1][2];time_touch[1][0]=time_touch[0][0];time_touch[1][1]=time_touch[0][1];time_touch[1][2]=time_touch[0][2];time_touch[0][0]=time[0];time_touch[0][1]=time[1];time_touch[0][2]=time[2];UI_keep=UI;UI=4;}EA =1;
}

seg_key.c

#include "sys.h"
sbit ROW3=P3^2;
sbit ROW4=P3^3;
sbit COL1=P4^4;
sbit COL2=P4^2;
code unsigned char Seg_Table[] = 
{
0xc0, //0
0xf9, //1
0xa4, //2
0xb0, //3
0x99, //4
0x92, //5
0x82, //6
0xf8, //7
0x80, //8
0x90, //9
0x88, //A 10
0xc6, //C 11
0x8c, //P 12
0xbf, //- 13
0xff, //熄滅 14
0xc1  //U 15
};
unsigned char Seg_Buff[8]={14,14,14,14,14,14,14,14};
unsigned char keyval,keyold,keyup,keydown;
void seg(unsigned char addr,unsigned char num){P0=0xff;init74hc138(7);P0=0x01<<addr;init74hc138(6);P0=Seg_Table[num];if(UI==1&&(addr==1||addr==5))P0&=0x7f;init74hc138(7);
}
void Seg_Loop(){static unsigned char i=0;seg(i,Seg_Buff[i]);i++;if(i==8) i=0;
}
unsigned char key_scan(){COL1=0;COL2=1;if(ROW3==0) return 5;if(ROW4==0) return 4;COL1=1;COL2=0;if(ROW4==0) return 8;return 0;
}
void Key_Loop(){keyval=key_scan();keydown=keyval&(keyold^keyval);keyup=~keyval&(keyold^keyval);if(keyval==4&&keyold!=4){UI++;if(UI==3) UI=0;ui2_son=0;}if(UI==2){if(keyval==5&&keyold!=5){ui2_son++;if(ui2_son==3) ui2_son=0;}if(keyval==8&&keyold!=8){ui2_son_flag=0;flag_touch[0]=0;flag_touch[1]=0;flag_touch[2]=0;}}keyold=keyval;keyval=0;
}
void ui0(){Seg_Buff[7]=time[0]%10;Seg_Buff[6]=time[0]/10;Seg_Buff[5]=13;Seg_Buff[4]=time[1]%10;Seg_Buff[3]=time[1]/10;Seg_Buff[2]=13;Seg_Buff[1]=time[2]%10;Seg_Buff[0]=time[2]/10;
}
void ui1(){Seg_Buff[7]=(unsigned char)(rb2_v*100+0.5)%10;Seg_Buff[6]=(unsigned char)(rb2_v*100+0.5)/10%10;Seg_Buff[5]=(unsigned char)(rb2_v*100+0.5)/100%10;Seg_Buff[4]=15; //USeg_Buff[3]=(unsigned char)(light_v*100+0.5)%10;Seg_Buff[2]=(unsigned char)(light_v*100+0.5)/10%10;Seg_Buff[1]=(unsigned char)(light_v*100+0.5)/100%10;Seg_Buff[0]=12; //p	
}
void ui2(){Seg_Buff[0]=10;Seg_Buff[1]=ui2_son+1;if(flag_touch[ui2_son]){Seg_Buff[7]=time_touch[ui2_son][0]%10;Seg_Buff[6]=time_touch[ui2_son][0]/10;Seg_Buff[5]=time_touch[ui2_son][1]%10;Seg_Buff[4]=time_touch[ui2_son][1]/10;Seg_Buff[3]=time_touch[ui2_son][2]%10;Seg_Buff[2]=time_touch[ui2_son][2]/10;}else{Seg_Buff[7]=13;Seg_Buff[6]=13;Seg_Buff[5]=13;Seg_Buff[4]=13;Seg_Buff[3]=13;Seg_Buff[2]=13;}
}
void ui4(){Seg_Buff[7]=time_touch[0][0]%10;Seg_Buff[6]=time_touch[0][0]/10;Seg_Buff[5]=time_touch[0][1]%10;Seg_Buff[4]=time_touch[0][1]/10;Seg_Buff[3]=time_touch[0][2]%10;Seg_Buff[2]=time_touch[0][2]/10;Seg_Buff[1]=11;Seg_Buff[0]=11;
}
void seg_ui(){switch(UI){case 0:ui0();break;case 1:ui1();break;case 2:ui2();break;case 4:ui4();break;}
}

iic.c

#include "sys.h"
#include "intrins.h"
#define DELAY_TIME	10
sbit sda=P2^1;
sbit scl=P2^0;
static void I2C_Delay(unsigned char n)
{do{_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();		}while(n--);      	
}
void I2CStart(void)
{sda = 1;scl = 1;I2C_Delay(DELAY_TIME);sda = 0;I2C_Delay(DELAY_TIME);scl = 0;    
}
void I2CStop(void)
{sda = 0;scl = 1;I2C_Delay(DELAY_TIME);sda = 1;I2C_Delay(DELAY_TIME);
}
void I2CSendByte(unsigned char byt)
{unsigned char i;for(i=0; i<8; i++){scl = 0;I2C_Delay(DELAY_TIME);if(byt & 0x80){sda = 1;}else{sda = 0;}I2C_Delay(DELAY_TIME);scl = 1;byt <<= 1;I2C_Delay(DELAY_TIME);}scl = 0;  
}
unsigned char I2CReceiveByte(void)
{unsigned char da;unsigned char i;for(i=0;i<8;i++){   scl = 1;I2C_Delay(DELAY_TIME);da <<= 1;if(sda) da |= 0x01;scl = 0;I2C_Delay(DELAY_TIME);}return da;    
}
unsigned char I2CWaitAck(void)
{unsigned char ackbit;scl = 1;I2C_Delay(DELAY_TIME);ackbit = sda; scl = 0;I2C_Delay(DELAY_TIME);return ackbit;
}
void I2CSendAck(unsigned char ackbit)
{scl = 0;sda = ackbit; I2C_Delay(DELAY_TIME);scl = 1;I2C_Delay(DELAY_TIME);scl = 0; sda = 1;I2C_Delay(DELAY_TIME);
}
unsigned char adc(unsigned char addr){unsigned char temp;EA = 0;I2CStart();I2CSendByte(0x90);I2CWaitAck();I2CSendByte(addr);I2CWaitAck();I2CStart();I2CSendByte(0x91);I2CWaitAck();temp=I2CReceiveByte();I2CSendAck(1);I2CStop();EA = 1;return temp;
}
float v_adc(unsigned char addr){return (float)(adc(addr)/256.0*5.0);
}

ds1302.c

#include "sys.h"
#include "intrins.h"
sbit SCK=P1^7;
sbit SDA=P2^3;
sbit RST=P1^3;
unsigned char time[3]={50,59,23};
void Write_Ds1302(unsigned  char temp) 
{unsigned char i;for (i=0;i<8;i++)     	{ SCK = 0;SDA = temp&0x01;temp>>=1; SCK=1;}
}
void Write_Ds1302_Byte( unsigned char address,unsigned char dat )     
{RST=0;	_nop_();SCK=0;	_nop_();RST=1; 	_nop_();  Write_Ds1302(address);	Write_Ds1302(dat/10*16|dat%10);		RST=0; 
}
unsigned char Read_Ds1302_Byte ( unsigned char address )
{unsigned char i,temp=0x00;unsigned char dat1,dat2;RST=0;	_nop_();SCK=0;	_nop_();RST=1;	_nop_();Write_Ds1302(address);for (i=0;i<8;i++) 	{		SCK=0;temp>>=1;	if(SDA)temp|=0x80;	SCK=1;} RST=0;	_nop_();SCK=0;	_nop_();SCK=1;	_nop_();SDA=0;	_nop_();SDA=1;	_nop_();dat1=temp/16;dat2=temp%16;temp=dat1*10+dat2;return (temp);			
}
void w_ds1302(){unsigned char i,addr=0x80;Write_Ds1302_Byte(0x8e,0x00);for(i=0;i<3;i++){Write_Ds1302_Byte(addr,time[i]);addr+=2;}Write_Ds1302_Byte(0x8e,0x80);
}
void r_ds1302(){unsigned char i,addr=0x81;for(i=0;i<3;i++){time[i]=Read_Ds1302_Byte(addr);addr+=2;}
}

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/web/74893.shtml
繁體地址,請注明出處:http://hk.pswp.cn/web/74893.shtml
英文地址,請注明出處:http://en.pswp.cn/web/74893.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

清華《數據挖掘算法與應用》FP-Growth算法

【例 8.7】實現FP 樹算法,并對模擬數據集 simpDat挖掘頻繁項集,最小支持度為2,繪制 FP樹并輸出頻繁項集。 運行結果&#xff1a; 聲明&#xff1a;著作權歸作者所有。商業轉載請聯系作者獲得授權&#xff0c;非商業轉載請注明出處。 # -*- coding: utf-8 -*- ""&q…

npm 項目命名規則

以下是 npm 項目命名規則的詳細說明&#xff1a; 一、核心命名規則 必須使用小寫字母 名稱中不能包含大寫字母。原因&#xff1a; 跨平臺兼容性&#xff08;如 Linux 區分大小寫&#xff0c;而 Windows 不區分&#xff09;。避免命令行和 URL 中的大小寫沖突&#xff08;例如包…

Ubertool 的詳細介紹、安裝指南及使用說明

Ubertool&#xff1a;多協議網絡分析與調試平臺 一、Ubertool 簡介 Ubertool 是一款開源的 多協議網絡分析工具&#xff0c;專為物聯網&#xff08;IoT&#xff09;、嵌入式系統和工業自動化領域設計。它支持藍牙、Wi-Fi、LoRa、CAN總線等多種通信協議的實時監控、數據包捕獲…

AI重構農業:從“面朝黃土“到“數字原野“的產業躍遷—讀中共中央 國務院印發《加快建設農業強國規劃(2024-2035年)》

在東北黑土地的萬畝良田上&#xff0c;無人機編隊正在執行精準施肥作業&#xff1b;在山東壽光的智慧大棚里&#xff0c;傳感器網絡實時調控著番茄生長的微環境&#xff1b;在云南的咖啡種植園中&#xff0c;區塊鏈溯源系統記錄著每粒咖啡豆的旅程。這場靜默的農業革命&#xf…

FogFL: Fog-Assisted Federated Learning for Resource-Constrained IoT Devices

摘要 提示&#xff1a;這里可以添加系列文章的所有文章的目錄&#xff0c;目錄需要自己手動添加 -在本文中&#xff0c;我們提出了一個支持霧的聯邦學習框架–FogFL–來促進資源受限的物聯網環境中延遲敏感應用的分布式學習。聯邦學習&#xff08;FL&#xff09;是一種流行的分…

linux下編譯Websocketpp,適用x86和armv8

編譯boost庫 下載源文件&#xff1a;Version 1.79.0 編譯&#xff1a; sudo ./bootstrap.sh sudo ./b2 install 安裝websocketpp git clone https://github.com/zaphoyd/websocketpp.git cd websocketpp #進入目錄 mkdir build cd build cmake .. make sudo make ins…

Linux學習筆記——零基礎詳解:什么是Bootloader?U-Boot啟動流程全解析!

零基礎詳解&#xff1a;什么是Bootloader&#xff1f;U-Boot啟動流程全解析&#xff01; 一、什么是Bootloader&#xff1f;&#x1f4cc; 舉個例子&#xff1a; 二、U-Boot 是什么&#xff1f;三、U-Boot啟動過程&#xff1a;分為兩個階段&#x1f539; 第一階段&#xff08;匯…

Word 頁眉設置(不同章節不同頁眉)

需求分析 要給文檔設置頁眉&#xff0c;但是要不同的頁眉不同的頁眉 問題點&#xff1a;一旦設置頁眉 每個頁眉都是一樣的 現在要設置不一樣的 設置了頁眉但是整個文章的頁眉都一樣 問題解決 取消鏈接 前一節&#xff08;不和前面的頁眉同步更新&#xff09; 小結 不同的…

Debezium日常分享系列之:Debezium3.1版本之增量快照

Debezium日常分享系列之&#xff1a;Debezium3.1版本之增量快照 按需快照觸發一次臨時增量快照觸發臨時阻塞快照增量快照增量快照過程如何 Debezium 解決具有相同主鍵的記錄之間的沖突快照窗口觸發增量快照使用附加條件運行臨時增量快照使用 Kafka 信號通道觸發增量快照臨時增量…

音視頻開發從入門到精通:編解碼、流媒體協議與FFmpeg實戰指南

音視頻開發從入門到精通&#xff1a;編解碼、流媒體協議與FFmpeg實戰指南 音視頻技術作為數字媒體領域的核心&#xff0c;正在成為互聯網和移動應用的重要組成部分。本文將全面介紹音視頻開發的學習路徑&#xff0c;從基礎概念到高級應用&#xff0c;從編解碼原理到實戰案例&a…

bookkeeper基本概念

Apache BookKeeper 架構與基本概念 Apache BookKeeper 的架構 Apache BookKeeper 是一個高性能的分布式日志存儲系統&#xff0c;主要用于存儲和管理順序寫入的數據。它被設計用來提供低延遲、高吞吐量和強一致性的服務&#xff0c;常用于分布式系統中的日志存儲需求&#xf…

Scala相關知識學習總結3

包 - 包聲明&#xff1a;和Java類似&#xff0c;作用是區分同名類、管理類命名空間。Scala包名只能含數字、字母等&#xff0c;不能數字開頭、不能用關鍵字。 - 包說明&#xff1a;有類似Java的包管理風格&#xff0c;也有獨特嵌套風格。嵌套風格有兩個特點&#xff0c;一是&…

在Spring Boot中實現圖片上傳和修改

1. 圖片上傳實現步驟 1.1 添加依賴 確保 spring-boot-starter-web 和 spring-boot-starter-validation 已存在&#xff1a; <dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId> <…

網絡原理 - HTTP/HTTPS

1. HTTP 1.1 HTTP是什么&#xff1f; HTTP (全稱為 “超文本傳輸協議”) 是?種應用非常廣泛的應用層協議. HTTP發展史&#xff1a; HTTP 誕生于1991年. 目前已經發展為最主流使用的?種應用層協議 最新的 HTTP 3 版本也正在完善中, 目前 Google / Facebook 等公司的產品已經…

第十屆MathorCup高校數學建模挑戰賽-A題:無車承運人平臺線路定價問題

目錄 摘 要 一、問題提出 1.1 背景 1.2 問題重述 二、基本假設 三、符號說明 四、問題分析 4.1 問題一的分析 4.2 問題二的分析 4.3 問題三的分析 4.4 問題四的分析 五、模型的建立與求解 5.1 問題一模型的建立與求解 5.1.1 數據預處理 5.1.2 問題一結果檢驗:因子分析模型 5.2…

C++假期練習

思維導圖 牛客練習

Go語言-初學者日記(四):包管理

眾所周知——“包”治百病。 理解包與模塊&#xff0c;是 Go 邁向工程化開發的關鍵一環&#xff01; &#x1f4c2; 一、包&#xff08;Package&#xff09;是 Go 的基本組織單位 在 Go 中&#xff0c;每個 .go 文件都屬于某個包&#xff08;package&#xff09;&#xff1a; …

Scala面向對象2

1. 抽象屬性和方法&#xff1a;用 abstract 關鍵字定義抽象類&#xff0c;其中抽象屬性無初始值&#xff0c;抽象方法無實現 。重寫抽象方法需用 override &#xff0c;重寫抽象屬性時&#xff0c;可變屬性用 var &#xff0c;不可變屬性用 val 。 匿名子類&#xff1a;和 Jav…

DiffAD:自動駕駛的統一擴散建模方法

25年3月來自新加坡公司 Carion 和北航的論文“DiffAD: A Unified Diffusion Modeling Approach for Autonomous Driving”。 端到端自動駕駛 (E2E-AD) 已迅速成為實現完全自動駕駛的一種有前途的方法。然而&#xff0c;現有的 E2E-AD 系統通常采用傳統的多任務框架&#xff0c…

Python四大核心數據結構深度解析:列表、元組、字典與集合

在Python編程語言中&#xff0c;數據結構是組織和存儲數據的基本方式。Python提供了四種內置的核心數據結構&#xff1a;列表&#xff08;List&#xff09;、元組&#xff08;Tuple&#xff09;、字典&#xff08;Dictionary&#xff09;和集合&#xff08;Set&#xff09;。這…