蓝桥杯省赛学习笔记

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
{
TH0=0xFF;
TL0=0xA4;
cnt++;
if(cnt>100)
cnt=0;
}
串行通信
#include "reg52.h"
sfr AUXR=0x8e;由于reg52.h头文件里不含AUXR所以要申明
unsigned char urdat;
void Uart_Init();
void SendData(unsigned char dat);
综合运用工厂灯光控制系统
AT24C02写入速度比较慢,在连续写入数据时,要增长延时,
E2Write_Byte(0x04,dat);
Delay(800);//延时
E2Write_Byte(0x01,temp);
Delay(800);//
void Delay(u16 t)
{
while(t--);
}
51单片机精确延时
PWM控制灯光的亮度
100Hz即周期0.01秒,10ms=10000us=100*100us,可以分成100份,每份100us,根据此,可以设置定时器的高八位和低八位。
void LED_Mode(unsigned char t)
{
if(t==1)
{
L1=1;
}
if(t==2)
{
if(cnt<=90)
{
switch(a)
{
case 4:P2=(P2&0x1F)|0x80;break;
case 5:P2=(P2&0x1F)|0xA0;break;
case 6:P2=(P2&0x1F)|0xC0;break;
case 7:P2=(P2&0x1F)|0xE0;break;
}
}
void FMQ_Init()
P0=0xFF;
while(1)
{
Key_Scan();
}wk.baidu.com
}
void Key_Scan()
{
if(S7==0&&d2)
{
while(!S7);
LED1=~LED1;
}
if(LED1==0)
{
c1=1;
c2=0;
}
if(LED1==1)
{
c1=0;
c2=1;
}
if(c1)
{
if(S5==0)
{
Delay(100);
sbit S4=P3^3;
sbit LED1=P0^0;
sbit LED2=P0^1;
sbit LED3=P0^2;
sbit LED4=P0^3;
sbit LED5=P0^4;
sbit LED6=P0^5;
sbit LED7=P0^6;
sbit LED8=P0^7;
unsigned char c1,c2=1;
{
SBUF=dat;
while(TI==0);
TI=0;
}
void Uart() interrupt 4
{
if(RI==1)
{
RI=0;
urdat=SBUF;
SendData(urdat+1);
}
}
TB8,RB8用于奇偶校验,如果不用,置0即可
串口进阶
IO扩展技术与存储器映射扩展
若使用矩阵键盘,则P3^6引脚会冲突,当使用矩阵键盘时,用IO扩展方式
LED3=0;
while(!S5);
LED3=1;
}
if(S4==0)
{
Delay(100);
LED4=0;
while(!S4);
LED4=1;
}
}
if(S6==0&&c2)
{
while(!S6);
LED2=~LED2;
}
if(LED2==0)要放到if(S6==0&&c2)外面去检测,因为单片机检测到按键松开时这个地方的程序就执行不到
unsigned char d1,d2=1;
void Key_Scan();
void HC138_Select(unsigned char a);
void FMQ_Init();
void Delay(unsigned int t);
void main()
{
FMQ_Init();
HC138_Select(4);
数码管
另外要写蜂鸣器初始化函数,关闭蜂鸣器
void Init()
{
HC138_Selection(5);
P0=0xBF;
HC138_Selection(6);
P0=0;
}
数码管动态显示作业
按键作业
#include "reg52.h"
sbit S7=P3^0;
sbit S6=P3^1;
sbit S5=P3^2;
L1=1;
if(cnt>90&&cnt<=100)
L1=0;
}
if(t==3)
{
if(cnt<=50)
L1=1;
if(cnt>50&&cnt<=100)
L1=0;
}
if(t==4)
{
if(cnt<=10)
L1=1;
if(cnt>10&&cnt<=100)
L1=0;
}
}
void Timer0() interrupt 1
Keil中数据类型占用的字节数
void main()
{
Uart_Init();
}
void Uart_Init()
{
TMOD&=0x0F;
TMOD|=0x20;
TH1=0xfd;
TL1=0xfd;
TR1=1;
ES=1;
EA=1;
AUXR=0x00;AUXR在IAP15F2K61S2中要置零
SCON=0x50;
}
void SendData(unsigned char dat)
if(!c1) cnt=0;
if(!c2) cnt=1;
if(!c3) cnt=2;
if(!c4) cnt=3;
r2=0;
r1=r3=r4=c1=c2=c3=c4=1;
if(!c1) cnt=4;
if(!c2) cnt=5;
if(!c3) cnt=6;
if(!c4) cnt=7;
r3=0;
r1=r2=r4=c1=c2=c3=c4=1;
{
d1=1;
d2=0;
}
if(LED2==1)
{
d1=0;
d2=1;
}
if(d1)
{
if(S5==0)
{
Delay(100);
LED5=0;
while(!S5);
LED5=1;
}
if(S4==0)
{
Delay(100);
LED6=0;
while(!S4);
LED6=1;
}
}
}
void HC138_Select(unsigned char a)
{
HC138_Select(5);
P0=0xBF;
}
void Delay(unsigned int t)
{
while(t--);
while(t--);
}
蓝桥杯单片机矩阵按键的扫描P3^6----P4^2 P3^7-----P4^4特殊之处
void Key_scan()
{
r1=0;
r2=r3=r4=c1=c2=c3=c4=1;
if(!c1) cnt=8;
if(!c2) cnt=9;
if(!c3) cnt=10;
if(!c4) cnt=11;
r4=0;
r2=r3=r1=c1=c2=c3=c4=1;
if(!c1) cnt=12;
if(!c2) cnt=13;
if(!c3) cnt=14;
if(!c4) cnt=15;
}
外部中断
相关文档
最新文档