蓝桥杯笔记
1. 基础模块调用模板
#include <stc15f2k60s2.h>
#define uchar unsigned char
#define uint unsigned int
uchar tab[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,
0x40,0x79,0x24,0x30,0x19,0x12,0x02,0x78,0x00,0x10,0xbf,0xff};
uchar yi,er,san,si,wu,liu,qi,ba;
uchar num;
void delayms(int ms)
{
int i,j;
for(i=0;i<ms;i++)
for(j=845;j>0;j--);
}
void Allinit()
{
P2=0xA0;
P0=0x00;
P2=0x80;
P0=0xFF;
P2=0xC0;
P0=0xFF;
P2=0xFF;
P0=0xFF;
}
void display1(uchar yi,uchar er,uchar san,uchar si)
{
P2=0xC0;
P0=0x01;
P2=0xFF;
P0=tab[yi];
delayms(1);
P2=0xC0;
P0=0x02;
P2=0xFF;
P0=tab[er];
delayms(1);
P2=0xC0;
P0=0x04;
P2=0xFF;
P0=tab[san];
delayms(1);
P2=0xC0;
P0=0x08;
P2=0xFF;
P0=tab[si];
delayms(1);
}
void display2(uchar wu,uchar liu,uchar qi,uchar ba)
{
P2=0xC0;
P0=0x10;
P2=0xFF;
P0=tab[wu];
delayms(1);
P2=0xC0;
P0=0x20;
P2=0xFF;
P0=tab[liu];
delayms(1);
P2=0xC0;
P0=0x40;
P2=0xFF;
P0=tab[qi];
delayms(1);
P2=0xC0;
P0=0x80;
P2=0xFF;
P0=tab[ba];
delayms(1);
}
void keyscan16(void)
{
uchar temp;
P44=0;P42=1;P3=0X7F;
temp=P3;
temp=temp&0x0f;
if(temp!=0x0f)
{
delayms(5);
temp=P3;
temp=temp&0x0f;
if(temp!=0x0f)
{
temp=P3;
switch(temp)
{
case 0x7e: num=0; break;
case 0x7d: num=1; break;
case 0x7b: num=2; break;
case 0x77: num=3; break;
}
while(temp!=0x0f)
{
temp=P3;
temp=temp&0x0f;
}
}
}
P44=1;P42=0;P3=0XBF;
temp=P3;
temp=temp&0x0f;
if(temp!=0x0f)
{
delayms(5);
temp=P3;
temp=temp&0x0f;
if(temp!=0x0f)
{
temp=P3;
switch(temp)
{
case 0xbe: num=4; break;
case 0xbd: num=5; break;
case 0xbb: num=6; break;
case 0xb7: num=7; break;
}
while(temp!=0x0f)
{
temp=P3;
temp=temp&0x0f;
}
}
}
P44=1;P42=1;P3=0XDF;
temp=P3;
temp=temp&0x0f;
if(temp!=0x0f)
{
delayms(5);
temp=P3;
temp=temp&0x0f;
if(temp!=0x0f)
{
temp=P3;
switch(temp)
{
case 0xde: num=8; break;
case 0xdd: num=9; break;
case 0xdb: num=10; break;
case 0xd7: num=11; break;
}
while(temp!=0x0f)
{
temp=P3;
temp=temp&0x0f;
}
}
}
P44=1;P42=1;P3=0XEF;
temp=P3;
temp=temp&0x0f;
if(temp!=0x0f)
{
delayms(5);
temp=P3;
temp=temp&0x0f;
if(temp!=0x0f)
{
temp=P3;
switch(temp)
{
case 0xee: num=12; break;
case 0xed: num=13; break;
case 0xeb: num=14; break;
case 0xe7: num=15; break;
}
while(temp!=0x0f)
{
temp=P3;
temp=temp&0x0f;
}
}
}
}
void main(void)
{
Allinit();
while(1)
{
keyscan16();
display1(yi,er,san,si);
display2(wu,liu,qi,ba);
}
}
2. 官方驱动模块改写
2.1 onewire (DS18b20)
官方onewire.c
#include "reg52.h"
sbit DQ = P1^4;
void Delay_OneWire(unsigned int t)
{
while(t--);
}
void Write_DS18B20(unsigned char dat)
{
unsigned char i;
for(i=0;i<8;i++)
{
DQ = 0;
DQ = dat&0x01;
Delay_OneWire(5);
DQ = 1;
dat >>= 1;
}
Delay_OneWire(5);
}
unsigned char Read_DS18B20(void)
{
unsigned char i;
unsigned char dat;
for(i=0;i<8;i++)
{
DQ = 0;
dat >>= 1;
DQ = 1;
if(DQ)
{
dat |= 0x80;
}
Delay_OneWire(5);
}
return dat;
}
bit init_ds18b20(void)
{
bit initflag = 0;
DQ = 1;
Delay_OneWire(12);
DQ = 0;
Delay_OneWire(80);
DQ = 1;
Delay_OneWire(10);
initflag = DQ;
Delay_OneWire(5);
return initflag;
}
改写onewire(ds18b20)
unsigned char Tempget(void)
{
unsigned char low,high,temp;
init_ds18b20();
Write_DS18B20(0XCC);
Write_DS18B20(0X44);
Delay_OneWire(200);
init_ds18b20();
Write_DS18B20(0XCC);
Write_DS18B20(0XBE);
low=Read_DS18B20();
high=Read_DS18B20();
temp=high<<4;
temp|=(low>>4);
return temp;
}
long Tempget(void)
{
long high,low,temp;
init_ds18b20();
Write_DS18B20(0xCC);
Write_DS18B20(0x44);
Delay_OneWire(200);
init_ds18b20();
Write_DS18B20(0xCC);
Write_DS18B20(0xBE);
Delay_OneWire(200);
low=Read_DS18B20();
high=Read_DS18B20();
temp=((high&0x0F)<<8)|low;
temp=temp*625;
return temp;
}
2.2 ds1302
官方ds1302.c
#include <reg52.h>
#include <intrins.h>
sbit SCK=P1^7;
sbit SDA=P2^3;
sbit RST = P1^3;
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);
RST=0;
}
unsigned char Read_Ds1302_Byte ( unsigned char address )
{
unsigned char i,temp=0x00;
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_();
return (temp);
}
改写ds1302
void Write_Ds1302_Byte( unsigned char address,unsigned char dat )
{
RST=0; _nop_(),_nop_(),_nop_(),_nop_(),_nop_(),_nop_(),_nop_(),_nop_();
SCK=0; _nop_(),_nop_(),_nop_(),_nop_(),_nop_(),_nop_(),_nop_(),_nop_();
RST=1; _nop_(),_nop_(),_nop_(),_nop_(),_nop_(),_nop_(),_nop_(),_nop_();
Write_Ds1302(address);
Write_Ds1302((dat/10<<4)|(dat%10));
RST=0;
}
unsigned char Read_Ds1302_Byte ( unsigned char address )
{
unsigned char i,dat1,dat2,temp=0x00;
RST=0; _nop_(),_nop_(),_nop_(),_nop_(),_nop_(),_nop_(),_nop_(),_nop_();
SCK=0; _nop_(),_nop_(),_nop_(),_nop_(),_nop_(),_nop_(),_nop_(),_nop_();
RST=1; _nop_(),_nop_(),_nop_(),_nop_(),_nop_(),_nop_(),_nop_(),_nop_();
Write_Ds1302(address);
for (i=0;i<8;i++)
{
SCK=0;
temp>>=1;
if(SDA)
temp|=0x80;
SCK=1;
}
RST=0; _nop_(),_nop_(),_nop_(),_nop_(),_nop_(),_nop_(),_nop_(),_nop_();
SCK=0; _nop_(),_nop_(),_nop_(),_nop_(),_nop_(),_nop_(),_nop_(),_nop_();
SCK=1; _nop_(),_nop_(),_nop_(),_nop_(),_nop_(),_nop_(),_nop_(),_nop_();
SDA=0; _nop_(),_nop_(),_nop_(),_nop_(),_nop_(),_nop_(),_nop_(),_nop_();
SDA=1; _nop_(),_nop_(),_nop_(),_nop_(),_nop_(),_nop_(),_nop_(),_nop_();
dat1=temp/16;
dat2=temp%16;
temp=dat1*10+dat2;
return (temp);
}
void DS_init(void)
{
uchar i,add;
add=0x80;
Write_Ds1302_Byte(0x8E,0x00);
for(i=0;i<7;i++)
{
Write_Ds1302_Byte(add,shijian[i]);
add=add+2;
}
Write_Ds1302_Byte(0x8E,0x80);
}
void DS_get(void)
{
uchar i,add;
add=0x81;
Write_Ds1302_Byte(0x8E,0x00);
for(i=0;i<7;i++)
{
shijian[i]=Read_Ds1302_Byte(add);
add=add+2;
}
Write_Ds1302_Byte(0x8E,0x80);
}
2.3 iic (IIC和EEPROM)
官方iic.c
#include "reg52.h"
#include "intrins.h"
#define DELAY_TIME 5
#define SlaveAddrW 0xA0
#define SlaveAddrR 0xA1
sbit SDA = P2^1;
sbit SCL = P2^0;
void IIC_Delay(unsigned char i)
{
do{_nop_();}
while(i--);
}
void IIC_Start(void)
{
SDA = 1;
SCL = 1;
IIC_Delay(DELAY_TIME);
SDA = 0;
IIC_Delay(DELAY_TIME);
SCL = 0;
}
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;
}
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;
}
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
void DA_write(unsigned char dat)
{
IIC_Start();
IIC_SendByte(0X90);
IIC_WaitAck();
IIC_SendByte(0x40);
IIC_WaitAck();
IIC_SendByte(dat);
IIC_WaitAck();
IIC_Stop();
}
unsigned char AD_read(unsigned char add)
{
unsigned char temp;
IIC_Start();
IIC_SendByte(0x90);
IIC_WaitAck();
IIC_SendByte(add);
IIC_WaitAck();
IIC_Stop();
IIC_Start();
IIC_SendByte(0x91);
IIC_WaitAck();
temp=IIC_RecByte();
IIC_Stop();
temp=temp*0.39;
return temp;
}
unsigned char EEPROM_read(unsigned char add)
{
unsigned char temp;
IIC_Start();
IIC_SendByte(0xA0);
IIC_WaitAck();
IIC_SendByte(add);
IIC_WaitAck();
IIC_Stop();
IIC_Start();
IIC_SendByte(0xA1);
IIC_WaitAck();
temp=IIC_RecByte();
IIC_Stop();
return temp;
}
void EEPROM_write(unsigned char add,unsigned char dat)
{
IIC_Start();
IIC_SendByte(0xA0);
IIC_WaitAck();
IIC_SendByte(add);
IIC_WaitAck();
IIC_SendByte(dat);
IIC_WaitAck();
IIC_Stop();
}
3. 国赛重要模块
3.1 超声波
#define somenop {_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_(); _nop_();}
sbit TX = P1^0;
sbit RX = P1^1;
unsigned int intr = 0;
bit s_flag;
unsigned int t = 0;
unsigned int distance;
void dist_get(void)
{
if(s_flag)
{
s_flag = 0;
send_wave();
TR1 = 1;
while((RX == 1) && (TF1 == 0));
TR1 = 0;
if(TF1 == 1)
{
TF1 = 0;
distance = 9999;
}
else
{
t = TH1;
t <<= 8;
t |= TL1;
distance = (unsigned int)(t*0.017);
distance = distance/12;
}
TH1 = 0;
TL1 = 0;
}
liu = distance/100;
qi = distance%100/10;
ba = distance%10;
}
void Timer0Init(void)
{
AUXR |= 0x80;
TMOD &= 0xF0;
TL0 = 0x9A;
TH0 = 0xA9;
TF0 = 0;
TR0 = 1;
}
void Timer1Init(void)
{
AUXR |= 0x40;
TMOD &= 0x0F;
}
void isr_timer_0(void) interrupt 1
{
TL0 = 0x9A;
TH0 = 0xA9;
if(++intr == 100)
{
s_flag = 1;
intr = 0;
}
}
void send_wave(void)
{
unsigned char i = 8;
do
{
TX = 1;
somenop;somenop;somenop;somenop;somenop;somenop;somenop;somenop;somenop;somenop;
TX = 0;
somenop;somenop;somenop;somenop;somenop;somenop;somenop;somenop;somenop;somenop;
}
while(i--);
}
3.2 串口通信
uchar flag,num;
unsigned char txbuffer1[] = "IAP15F2 USART TEST PROGRAM\r\n";
unsigned char enter[] = {"\r\n"};
void ser() interrupt 4
{
RI=0;
num=SBUF;
if(num=='$')
{
P2=0X80; P0=0X55;
}
}
void uart_tx(unsigned char *p,unsigned char length)
{
unsigned char i;
for(i=0;i<length;i++)
{
SBUF = *(p+i);
while(TI == 0);
TI = 0;
}
}
void send(unsigned char *p)
{
unsigned char index=0;
do
{
SBUF=p[index++];
while(TI == 0);
TI = 0;
}
while(p[index]!=0);
}
void UartInit(void)
{
SCON = 0x50;
AUXR |= 0x40;
AUXR &= 0xFE;
TMOD &= 0x0F;
TL1 = 0xE0;
TH1 = 0xFE;
ET1 = 0;
TR1 = 1;
}
4. 常用功能
4.1 LED灯控制
控制单个LED灯
P2=0x80;
P0=0xFE;
P2=0x80;
P0=0xFF;
多控不冲突
unsigned char LED_Bit=0XFF;
#define LED1_ON P2|=0X80;P2&=0X9F; LED_Bit&=0xFE; P0=LED_Bit;
#define LED1_OFF P2|=0X80;P2&=0X9F; LED_Bit|=0X01; P0=LED_Bit;
#define LED2_ON P2|=0X80;P2&=0X9F; LED_Bit&=0xFD; P0=LED_Bit;
#define LED2_OFF P2|=0X80;P2&=0X9F; LED_Bit|=0X02; P0=LED_Bit;
#define LED3_ON P2|=0X80;P2&=0X9F; LED_Bit&=0xFB; P0=LED_Bit;
#define LED3_OFF P2|=0X80;P2&=0X9F; LED_Bit|=0X04; P0=LED_Bit;
#define LED4_ON P2|=0X80;P2&=0X9F; LED_Bit&=0xF7; P0=LED_Bit;
#define LED4_OFF P2|=0X80;P2&=0X9F; LED_Bit|=0X08; P0=LED_Bit;
#define LED5_ON P2|=0X80;P2&=0X9F; LED_Bit&=0xEF; P0=LED_Bit;
#define LED5_OFF P2|=0X80;P2&=0X9F; LED_Bit|=0X10; P0=LED_Bit;
#define LED6_ON P2|=0X80;P2&=0X9F; LED_Bit&=0xDF; P0=LED_Bit;
#define LED6_OFF P2|=0X80;P2&=0X9F; LED_Bit|=0X20; P0=LED_Bit;
#define LED7_ON P2|=0X80;P2&=0X9F; LED_Bit&=0xBF; P0=LED_Bit;
#define LED7_OFF P2|=0X80;P2&=0X9F; LED_Bit|=0X40; P0=LED_Bit;
#define LED8_ON P2|=0X80;P2&=0X9F; LED_Bit&=0x7F; P0=LED_Bit;
#define LED8_OFF P2|=0X80;P2&=0X9F; LED_Bit|=0X80; P0=LED_Bit;
4.2 蜂鸣器控制
P2=0xA0;
P0=0x40;
4.3 中断优先级
![在这里插入图片描述](https://img-blog.csdnimg.cn/755d3f3c71f043218aa19e11142c3394.png)
4.4 频率采集*
long intr=0,freq;
void Timer2Init(void)
{
AUXR |= 0x04;
T2L = 0x20;
T2H = 0xD1;
AUXR |= 0x10;
IE2 |= 0x04;
EA=1;
}
void Timer0Init(void)
{
AUXR |= 0x80;
TMOD = 0x05;
TL0=0;
TH0=0;
TR0=1;
}
void timer2int() interrupt 12
{
intr++;
if(intr==999)
{
intr=0;
freq=(TH0<<8)|TL0;
TL0=0;TH0=0;
}
}
void Timer0Init(void)
{
AUXR |= 0x80;
TMOD &= 0xF0;
TL0 = 0xC4;
TH0 = 0xFF;
TF0 = 0;
}
void Timer0_Rountine(void) interrupt 1
{
if(P34==0)
{
zheng++;
if(t_flag==1)
{
t_flag=0;
fan_now=fan;
fan=0;
}
}
else if(P34==1)
{
fan++;
if(t_flag==0)
{
t_flag=1;
zheng_now=zheng;
zheng=0;
}
}
}
void Timer1Init(void)
{
AUXR |= 0x40;
TMOD &= 0x0F;
TL1 = 0x40;
TH1 = 0xA2;
TF1 = 0;
TR1 = 1;
}
void Timer1_Rountine(void) interrupt 3
{
if(pinlv==1)
{
intr++;
if(intr==495)
{
TR0=1;ET0 = 1;
}
else if(intr== 500)
{
intr = 0;
TR0=0;ET0 = 0;
}
}
}
4.5 PWM波
uchar mode=1;
uchar Time=0;
uchar tt=0;
uint miao=0;
void Timer0Init(void)
{
AUXR |= 0x80;
TMOD &= 0xF0;
TL0 = 0xAE;
TH0 = 0xFB;
TF0 = 0;
TR0 = 1;
ET0=1;
EA=1;
}
void timer0() interrupt 1
{
tt++;miao++;
if(tt==11)tt=1;
if(Time>0)
{
if(mode==1)
{
if(tt<3)P34=1;
else P34=0;
}
else if(mode==2)
{
if(tt<4)P34=1;
else P34=0;
}
else if(mode==3)
{
if(tt<8)P34=1;
else P34=0;
}
}
if(miao==10000)
{
miao=0;
if(Time>0)Time--;
}
}
4.6 彩灯控制
- 思想参考PWM波控制
- 可在定时器中控制以达到更好的效果