蓝桥杯笔记

目录

1.实现独立按键控制数码管加减

2.矩阵键盘控制数码管 

3.独立按键S5开秒表和暂停秒表 S4清零 

4.PWM  S7控制LED亮度

5.串口

6.数码管一边显示一遍S4 S5控制LED

7.实时时钟ds1302

8.温度传感器ds18b20

9.IIC-PCF8951-A/D

10.iic【IIC-AT24C02】

在线编辑器C++(方便查看汇编)

Compiler Explorer

INT0 -----interrupt 0;

TIMER0-----interrupt 1;

INT1-----interrupt 2;

TIMER1-----interrupt 3;

串口中断------ interrupt 4;

1.实现独立按键控制数码管加减


#include <STC15F2K60S2.H>	
#include <intrins.h>
void Delay1ms()		//@11.0592MHz
{
	unsigned char i, j;

	_nop_();
	_nop_();
	_nop_();
	i = 11;
	j = 190;
	do
	{
		while (--j);
	} while (--i);
}


void y4567(unsigned  char data1,unsigned char data2);

sbit S7 = P3^0;
sbit S6 = P3^1;
unsigned char code SMG[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0x88,0x83,0xc6,0xa1,0x86,0x8e,0xBf,0x7F};
unsigned char n = 0;
unsigned char cnt = 0;
unsigned char m = 0;
void Delay10ms()		//@11.0592MHz
{
	unsigned char i, j;

	i = 108;
	j = 145;
	do
	{
		while (--j);
	} while (--i);
}


void _80A0init(){
y4567(0x80,0xFF);
y4567(0xA0,0x00);

}
void SMGinit(){
y4567(0xC0,0xE0);
y4567(0xE0,SMG[0]);

}
void SMG_show(){
  y4567(0xC0,0x80);
	y4567(0xE0,SMG[cnt%10]);
	Delay1ms();
	y4567(0xC0,0x40);
	y4567(0xE0,SMG[cnt%100/10]);
	Delay1ms();
	y4567(0xC0,0x20);
	y4567(0xE0,SMG[cnt/100]);
	Delay1ms();
}
void keys_SMG(){
if(S7 == 0){
	Delay10ms();
	if(S7 == 0) {
			cnt++;
	if (cnt == 255)  cnt = 0;
		while(S7 == 0) {SMG_show();}
		while((S7 == 1) && (S6 == 1)) {SMG_show();}
	  }
  }
else if(S6 == 0){
	Delay10ms();
		if(S6 == 0) {
			if(cnt > 0) {
				cnt--;
			while(S6 == 0) {SMG_show();}
			while((S6 == 1)&&(S7 == 1)) {SMG_show();}
			}
		}
	}
}
void main(void){	
	_80A0init();
	SMGinit();
	while(1){
	keys_SMG();
		}
}

2.矩阵键盘控制数码管 


#include <STC15F2K60S2.H>	
#include <intrins.h>
void Delay1ms()		//@11.0592MHz
{
	unsigned char i, j;

	_nop_();
	_nop_();
	_nop_();
	i = 11;
	j = 190;
	do
	{
		while (--j);
	} while (--i);
}


void y4567(unsigned  char data1,unsigned char data2);
unsigned char code SMG[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0x88,0x83,0xc6,0xa1,0x86,0x8e,0xBf,0x7F};

sbit R1 = P3^0;
sbit R2 = P3^1;
sbit R3 = P3^2;
sbit R4 = P3^3;

sbit C1 = P4^4;
sbit C2 = P4^2;
sbit C3 = P3^5;
sbit C4 = P3^4;
void SMG_show(unsigned char n){
	y4567(0xC0,0x01);
	y4567(0xE0,SMG[n]);
}
void Delay10ms()		//@11.0592MHz
{
	unsigned char i, j;

	i = 108;
	j = 145;
	do
	{
		while (--j);
	} while (--i);
}


void _80A0init(){
y4567(0x80,0xFF);
y4567(0xA0,0x00);

}
void SMGinit(){
y4567(0xC0,0x01);
y4567(0xE0,SMG[0]);
}



void JZ_keys(){
R1 = 0;
R2 = R3 = R4 = 1;
C1 = C2 = C3 = C4 = 1;
	if(C1 == 0){
		Delay10ms();
		if(C1 == 0){
			SMG_show(1);
			while(C1 == 0);
			SMG_show(0);
		}
	
	}
	else if(C2 == 0){
		Delay10ms();
		if(C2 == 0){
			SMG_show(2);
			while(C2 == 0);
			SMG_show(0);
		}
	}
}

void main(void){	
	_80A0init();
	SMGinit();
	while(1){
	JZ_keys();
		}
}

3.独立按键S5开秒表和暂停秒表 S4清零 


#include <STC15F2K60S2.H>	
#include <intrins.h>
void Delay10ms()		//@11.0592MHz
{
	unsigned char i, j;

	i = 108;
	j = 145;
	do
	{
		while (--j);
	} while (--i);
}


void Delay1ms()		//@11.0592MHz
{
	unsigned char i, j;

	_nop_();
	_nop_();
	_nop_();
	i = 11;
	j = 190;
	do
	{
		while (--j);
	} while (--i);
}


void y4567(unsigned  char data1,unsigned char data2);
unsigned char code SMG[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0x88,0x83,0xc6,0xa1,0x86,0x8e,0xBf,0x7F};
sbit S5 = P3^2;
sbit S4 = P3^3;
unsigned char t_m,t_s,t_ms = 0;
void SMG_show(unsigned char value,unsigned char pos){
	y4567(0xC0,0x01<<pos);
	y4567(0xE0,SMG[value]);
}
void Delay(unsigned char t){

while(t--);
}

void SMG_show2(){
	SMG_show(t_ms%10,7);
	Delay(500);
	SMG_show(t_ms/10,6);
	Delay(500);
	SMG_show(16,5);
	Delay(500);
	
	SMG_show(t_s%10,4);
	Delay(500);
	SMG_show(t_s/10,3);
	Delay(500);
	SMG_show(16,2);
	Delay(500);
	
	SMG_show(t_m%10,1);
	Delay(500);
	SMG_show(t_m/10,0);
	Delay(500);
	
}
void _80A0init(){
y4567(0x80,0xFF);
y4567(0xA0,0x00);

}
void SMGinit(){
y4567(0xC0,0xFF);
y4567(0xE0,SMG[0]);
}
void Timer0Init(void)		//50??@12.000MHz
{
	AUXR &= 0x7F;		//?????12T??
	TMOD &= 0xF0;		//???????
	TL0 = 0xB0;		//??????
	TH0 = 0x3C;		//??????
	TF0 = 0;		//??TF0??
	TR0 = 1;	
ET0=1;
EA = 1;	
}

void Timer0server() interrupt 1{
	t_ms++;
	if(t_ms == 20){
	t_s++;
		t_ms = 0;
		if(t_s == 60){
				t_m++;
			    t_s = 0;
			if(t_m == 99){
			
			t_m = 0;
			}
		}
	
	}

}
void key_alone(){
if(S5 == 0){
	Delay(100);
	if(S5 == 0){
		TR0 = ~ TR0;
	while(S5 == 0){
		SMG_show2();
		}
	}
}
if(S4 == 0){
	Delay(100);
	if(S4 == 0){
			t_ms = 0;
			t_s = 0;
			t_m = 0;
		while(S4 == 0){
		SMG_show2();
		}
		}
	}
}
void main(void){	
	_80A0init();
	SMGinit();
	 Timer0Init();
	
	while(1){
		SMG_show2();

		key_alone();
	
		}
}

4.PWM  S7控制LED亮度

#include <STC15F2K60S2.H>
sbit S7 = P3^0;
sbit L1 = P0^0;
unsigned char value,cnt = 0;
unsigned char stat = 0;
void y4567(unsigned char data1,unsigned char data2);
void Delay(unsigned int t){
while(t--);
}
void Timer0Init(void)		//100??@12.000MHz
{
	AUXR &= 0x7F;		//?????12T??
	TMOD &= 0xF0;		//???????
	TL0 = 0x9C;		//??????
	TH0 = 0xFF;		//??????
	TF0 = 0;
ET0 = 1;
EA = 1;	//??TF0??
			//???0????
}
void Timer0()interrupt 1{

cnt++;
	if(cnt == value){
		L1 = 1;
	}
	if(cnt == 100){
		L1 = 0;
		cnt = 0;
	}

}
void Keyalone_Scan(){
if(S7 == 0){
Delay(100);
	if(S7 == 0){
		switch(stat){
			case 0:
				L1 = 0;
				TR0 = 1;
				stat = 1;
			value = 10;
			break;
			case 1:
				stat = 2;
			value = 50;
			break;
			case 2:
				stat = 3;
			value = 90;
			break;
			case 3:
				stat = 0;
			L1 = 1;
			TR0 = 0;
			break;
	}
		while(S7 == 0);

}
}
}
void main(){
y4567(0xA0,0);
	P2 = 0x80;
	P0 = 0xFF;
Timer0Init();
	while(1){
	Keyalone_Scan();
	}

}

5.串口

 SBUF又接又发,看综合程序。

6.数码管一边显示一遍S4 S5控制LED

#include <STC15F2K60S2.H>
#include <intrins.h>
unsigned char i, j,m,n;
unsigned char t_h,t_m,t_s,cnt,stat1,stat2=0;
unsigned char stat_led = 0xff;
sbit S5 = P3^2;
sbit S4 = P3^3;
sbit L7 = P0^6;
sbit L8 = P0^7;
unsigned char code SMG[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0x88,0x83,0xc6,0xa1,0x86,0x8e,0xBf,0x7F};
void Delay100ms()		//@11.0592MHz
{
	unsigned char i, j, k;

	_nop_();
	_nop_();
	i = 5;
	j = 52;
	k = 195;
	do
	{
		do
		{
			while (--k);
		} while (--j);
	} while (--i);
}


void Delay(unsigned int t){
while(t--);
}
void Y_select(unsigned char n){
switch(n){
	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 LED_check(){
Y_select(4);
	for(i = 1; i < 9; i++){
		P0 = 0xff << i;
		Delay100ms();
	}
	for(j = 1; j < 9; j++){
		P0 = ~(0xff << j);
		Delay100ms();
	}
}
void SMG_check(){
for(m = 1; m < 9; m++){
	Y_select(6);
P0 = ~(0xff << m);
	Y_select(7);
	P0 = 0x00;
	Delay100ms();
}
for(n = 1; n < 9; n++){
		Y_select(6);
P0 = 0xff << n;
	Y_select(7);
	P0 = 0x00;
	Delay100ms();
}

}
void SMG_ctl(unsigned char x,unsigned char value){
	P0 = 0xff; // xiao ying
	Y_select(6);
	P0 = 0x01 << x;
	Y_select(7);
	P0 = SMG[value];

	


}
void Init80a0(){
Y_select(4);
	P0 = 0xff;
Y_select(5);
P0 = 0x00;	


}
void Timer0Init(void)		//1000??@11.0592MHz
{
AUXR &= 0x7F;		//?????12T??
	TMOD &= 0xF0;		//???????
	TL0 = 0x00;		//??????
	TH0 = 0x4C;		//??????
	TF0 = 0;		//??TF0??
	TR0 = 1;
ET0 = 1;
EA = 1;	//???0????
}

void SMG_showtime(){
	
	SMG_ctl(7,t_s%10);
	Delay(500);
		SMG_ctl(6,t_s/10);
	Delay(500);
		SMG_ctl(5,16);
	Delay(500);
	SMG_ctl(4,t_m%10);
	Delay(500);
		SMG_ctl(3,t_m/10);
	Delay(500);
		SMG_ctl(2,16);
	Delay(500);
		SMG_ctl(1,t_h%10);
	Delay(500);
		SMG_ctl(0,t_h/10);
	Delay(500);
	
}
void Timer0()interrupt 1{
	cnt++;
	if(cnt == 20){
		cnt = 0;
	t_s++;
	if(t_s == 60){
	t_m++;
		t_s = 0;
		if(t_m == 60){
		t_h++;
			t_m = 0;
			if(t_h == 255) t_h = 0;
		}
	} 
}
}
void Key_scan(){
if(S5 == 0){
Delay(100);
	if(S5 == 0){
		while(S5 == 0){	SMG_showtime();}
		Y_select(4);
		stat_led = (stat_led | 0x80) & (~stat_led |  (~0x80)); //背
		P0 = stat_led;
		P2 = 0;main中在一直showtime,一直改变P2,所以这里想显示,要固定P0输出,关P2
}
}
if(S4 == 0){
Delay(100);
	if(S4 == 0){
		while(S4 == 0){	SMG_showtime();}
				Y_select(4);
		stat_led = (stat_led | 0x40) & (~stat_led |  (~0x40)); 
		P0 = stat_led;
		P2 = 0;
}
}
}

void main(){
	Init80a0();
	LED_check();
	Delay100ms();      
	SMG_check();
	
	Timer0Init();
	while(1){
	SMG_showtime();
		Key_scan();
	}
	
}

7.实时时钟ds1302

#include <STC15F2K60S2.H>
#include "ds1302.h"
unsigned char code SMG[]={0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F,0x77,0x7C,0x39,0x5E,0x79,0x71};
unsigned char Write_DS1302_adrr[7] = {0x80,0x82,0x84,0x86,0x88,0x8a,0x8c};
unsigned char Read_DS1302_adrr[7] = {0x81,0x83,0x85,0x87,0x89,0x8b,0x8d};
unsigned char  Timer[7] = {0x50,0x59,0x23,0x05,0x09,0x07,0x21};	
void Y_select(unsigned char n){
  switch(n){
		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 InitDS1302()//DS1302?????
{
	unsigned char i;
	Write_Ds1302_Byte(0x8e,0x00);//0x8e???????
	for(i=0; i<7; i++)
		Write_Ds1302_Byte(Write_DS1302_adrr[i],Timer[i]);
	Write_Ds1302_Byte(0x8e,0x80);
}
void Delay(unsigned int t){
while(t--);
}
void ReadDS1302()//??DS1302??
{
	unsigned char i;
	for(i=0; i<7; i++)
		  Timer[i] = Read_Ds1302_Byte(Read_DS1302_adrr[i]);
}
void SMG_show(unsigned char pos,unsigned char x){
Y_select(6);
	P0 = 0x01<<pos;
	
Y_select(7);
	P0 = ~SMG[x];
}
void show(){

	
SMG_show(0,Timer[2]/16);Delay(500);
	SMG_show(1,Timer[2]%16);Delay(500);
	
Y_select(6);
P0 = 0x01<<2;
Y_select(7);
P0 = 0xbf;;Delay(500);
	
	SMG_show(3,Timer[1]/16);Delay(500);
	SMG_show(4,Timer[1]%16);Delay(500);
	
	
Y_select(6);
P0 = 0x01<<5;
Y_select(7);
P0 = 0xbf;
	Delay(500);
		SMG_show(6,Timer[0]/16);Delay(500);
	SMG_show(7,Timer[0]%16);Delay(500);
	Y_select(6);
P0 = 0xff;
Y_select(7);
P0 = 0xff;
Delay(500);
	
}
void main(){
Y_select(4);
	P0 = 0xff;
	Y_select(5);
	P0 = 0x00;
InitDS1302();
while(1){
	ReadDS1302();
show();

}

}

8.温度传感器ds18b20

1.导入onewire.h .c,主文件运行一下,进入.h  返回值改成int

2.把.h中的头文件拿到.c中定义

3.t=t*12,注意移位运算,结果*0.0625为正常温度,如23.5 但是要显示的话 先*10 再每个位显示就行。

onewire.c


#include <STC15F2K60S2.H>
#include "onewire.h"
sbit DQ = P1^4;  
void Delay_OneWire(unsigned int t)  
{;
	t=t*12;
	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;
}
unsigned int rd_temperature(void){
	unsigned int T_dat;
	unsigned char tml ,tmh;
init_ds18b20();
	Write_DS18B20(0xCC);			
	Write_DS18B20(0x44);
init_ds18b20();	
	Write_DS18B20(0xCC);		
	Write_DS18B20(0xBE);		
	tml = Read_DS18B20();		
	tmh = Read_DS18B20();	
	init_ds18b20();
	T_dat = tmh;
	T_dat <<= 8;
	T_dat = T_dat | tml;
	T_dat = T_dat * 0.625;
		//T_dat >>= 4;
		//T_dat = T_dat*10;
	//	T_dat = T_dat + (tml&0x0f)*0.625;
	
	return T_dat;
}







main

#include <STC15F2K60S2.H>
#include <intrins.h>
#include "onewire.h"
unsigned char code SMG[]={0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F,0x77,0x7C,0x39,0x5E,0x79,0x71};
unsigned int  te ;
void Delay(unsigned int t){
while(t--);
}
void Y_select(unsigned char n){
switch(n){
	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 Init_80a0(){
Y_select(4);
	P0 = 0xff;
	Y_select(5);
	P0 = 0x00;

}
void SMG_show(unsigned char pos,unsigned char x){
Y_select(6);
	P0 = 0x01<<pos;
Y_select(7);
	P0 = ~SMG[x];
}

void wendu_show(){

SMG_show(7,te%10);
	Delay(500);
Y_select(6);
	P0 = 0x01<<6;
Y_select(7);
P0 =( ~SMG[(te/10)%10]) - 0x80;
	Delay(500);
	SMG_show(5,te/100);
	Delay(500);
	
	Y_select(6);
	P0 = 0xff;
	Y_select(7);
	P0 = 0xff;

}
void main(){
 Init_80a0();
while(1)	{	
te = rd_temperature();
 wendu_show();
}

}


h

#ifndef __ONEWIRE_H
#define __ONEWIRE_H

unsigned int rd_temperature(void);  

#endif

9.IIC-PCF8951-A/D

在官方提供的iic驱动中添加这个,.h .c。

unsigned char PCF8591_adc(unsigned char addr);  //新增adc函数
void PCF8591_dac(unsigned char dat);			//新增dac函数
unsigned char PCF8591_adc(unsigned char addr) 
{
	unsigned char tmp;
	
	IIC_Start();				//IIC总线起始信号	
	
	IIC_SendByte(0x90);			//PCF8591的写设备地址
	IIC_WaitAck();				//等待从机应答
	
	IIC_SendByte(addr);			//写入PCF8591的控制字节
	IIC_WaitAck();				//等待从机应答
	
	IIC_Stop(); 				//IIC总线停止信号
	
	IIC_Start();				//IIC总线起始信号
	
	IIC_SendByte(0x91);			//PCF8591的读设备地址
	IIC_WaitAck();				//等待从机应答
	
	tmp = IIC_RecByte();		//读取PCF8591的数据 
	IIC_SendAck(1);				//产生非应答信号
	
	IIC_Stop();					//IIC总线停止信号
	return tmp;
}

void PCF8591_dac(unsigned char dat)
{
	IIC_Start();				//IIC总线起始信号	
	
	IIC_SendByte(0x90);			//PCF8591的写设备地址
	IIC_WaitAck();				//等待从机应答
	
	IIC_SendByte(0x43);			//写入PCF8591的控制字节
	IIC_WaitAck();				//等待从机应答

	IIC_SendByte(dat);			//dat-输出dac数据
	IIC_WaitAck();				//等待从机应答
	
	IIC_Stop();					//IIC总线停止信号
}
#include <STC15F2K60S2.H>

#include "iic.h"
#include "smg.h"
unsigned char SMG_NoDot[10]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};
unsigned char SMG_Dot[10]={0x40,0x79,0x24,0x30,0x19,0x12,0x02,0x78,0x00,0x10};

void Select_HC573(unsigned char channel)
{
	switch(channel)
	{
		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 SMG_Delay(unsigned char t)
{
	while(t--);
}

void SMG_Bit(unsigned char pos,unsigned char value)
{
	Select_HC573(7);
	P0 = 0xff;
	Select_HC573(6);
	P0 = 0x01 << pos;
	Select_HC573(7);
	P0 = value;
}

void SMG_All(unsigned char dat)
{
	Select_HC573(6);
	P0 = 0xff;
	Select_HC573(7);
	P0 = dat;
}
	

unsigned char dat = 0;
unsigned char channel = 0;
unsigned char stat = 0;
sbit S4 = P3^3;

void SMG_Display()
{
	SMG_Bit(0,0xbf);
	SMG_Delay(500);
	SMG_Bit(1,SMG_NoDot[channel]);
	SMG_Delay(500);
	SMG_Bit(2,0xbf);
	SMG_Delay(500);
	SMG_Bit(3,0xff);
	SMG_Delay(500);
	SMG_Bit(4,0xff);
	SMG_Delay(500);
	
	SMG_Bit(5,SMG_NoDot[dat / 100]);
	SMG_Delay(500);
	SMG_Bit(6,SMG_NoDot[dat % 100 / 10]);
	SMG_Delay(500);
	SMG_Bit(7,SMG_NoDot[dat % 10]);
	SMG_Delay(500);
	
	SMG_All(0xff);
}

void Init_System()
{
	Select_HC573(5);
	P0 = 0x00;
	Select_HC573(4);
	P0 = 0xff;
}

void Key_Scan()
{
	if(S4 == 0)
	{
		SMG_Delay(100);
		if(S4 == 0)
		{
			if(stat == 0)
			{
				stat = 1;
				while(S4 == 0)
				{
					channel = 3;  
					dat = PCF8591_adc(0x03);
					SMG_Display();
				}
			}
			else if(stat == 1)
			{
				stat = 0;
				while(S4 == 0)
				{
					channel = 1;  
					dat = PCF8591_adc(0x01);
					SMG_Display();
				}
			}
		}
	}
}

void main()
{
	Init_System();
	while(1)
	{
		Key_Scan();
		if(stat == 0)  				//???stat=0,??????????
		{
			channel = 1;  
			dat = PCF8591_adc(0x01);
			SMG_Display();
			
		}
		else if(stat == 1)			//????,?????????
		{
			channel = 3;  
			dat = PCF8591_adc(0x03);
			SMG_Display();
		}
	}
}


10.IIC-AT24C02

iic.h 加

unsigned char AT24C02_read(unsigned char addr);
void AT24C02_write(unsigned char addr,unsigned char date);

iic.c定义

unsigned char AT24C02_read(unsigned char addr)
{
	unsigned char tmp;
	IIC_Start();
	IIC_SendByte(0xa0);
	IIC_WaitAck();
	IIC_SendByte(addr);
	IIC_WaitAck();
	IIC_Stop();
	
	IIC_Start();
	IIC_SendByte(0xa1);
	IIC_WaitAck();
	tmp = IIC_RecByte();
	IIC_SendAck(1);
	IIC_Stop();
	return tmp;
}	
void AT24C02_write(unsigned char addr,unsigned char date)
{
	IIC_Start();	
	IIC_SendByte(0xa0);
	IIC_WaitAck();
	IIC_SendByte(addr);
	IIC_WaitAck();
	IIC_SendByte(date);
	IIC_WaitAck();
	IIC_Stop();
}
#include <STC15F2K60S2.H>
#include "iic.h"
unsigned char SMG_NoDot[10]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};
void Select_HC573(unsigned char channel)
{
	switch(channel)
	{
		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 Init_System()
{
	Select_HC573(5);
	P0 = 0x00;
	Select_HC573(4);
	P0 = 0xff;	
}


void SMG_Delay(unsigned char t)
{
	while(t--);
}

void SMG_Bit(unsigned char pos,unsigned char value)
{
	//Select_HC573(7);
	//P0 = 0xff;
	Select_HC573(6);
	P0 = 0x01 << pos;
	Select_HC573(7);
	P0 = value;
}

void SMG_All(unsigned char dat)
{
	Select_HC573(6);
	P0 = 0xff;
	Select_HC573(7);
	P0 = dat;
}

unsigned char dat1 = 0,dat2 = 0,dat3 = 0;

void Delay(unsigned int t)
{
	while(t--);
}

void read_write()
{
	dat1 = AT24C02_read(0x01);
	dat2 = AT24C02_read(0x03);
	dat3 = AT24C02_read(0x05);
	
	dat1 = dat1 + 1;
	dat2 = dat2 + 2;
	dat3 = dat3 + 3;
	
	if(dat1 > 10)dat1 = 0;
	if(dat2 > 20)dat2 = 0;
	if(dat3 > 30)dat3 = 0;
	
	AT24C02_write(0x01, dat1);
	Delay(3000);
	AT24C02_write(0x03, dat2);
	Delay(3000);
	AT24C02_write(0x05, dat3);
	Delay(3000);
}

void SMG_Display()
{
	SMG_Bit(0,SMG_NoDot[dat1 / 10]);
	SMG_Delay(500);
	SMG_Bit(1,SMG_NoDot[dat1 % 10]);
	SMG_Delay(500);
	
	SMG_Bit(2,0xbf);
	SMG_Delay(500);
	
	SMG_Bit(3,SMG_NoDot[dat2 / 10]);
	SMG_Delay(500);
	SMG_Bit(4,SMG_NoDot[dat2 % 10]);
	SMG_Delay(500);
	
	SMG_Bit(5,0xbf);
	SMG_Delay(500);
	
	SMG_Bit(6,SMG_NoDot[dat3 / 10]);
	SMG_Delay(500);
	SMG_Bit(7,SMG_NoDot[dat3 % 10]);
	SMG_Delay(500);
	
	SMG_All(0xff);
}


void main()
{
	Init_System();
	read_write();
	while(1)
	{
		SMG_Display();
		
	}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值