一、按键
#include "stc15f2k60s2.h"
#define InitHC138(n) {P2 = P2 & 0x1f | (n << 5);}
sbit led1 = P0^0;
sbit S7 = P3^0;
sbit S6 = P3^1;
void Delay(unsigned int t)
{
unsigned char i, j;
while(t--)
{
i = 12;
j = 169;
do
{
while (--j);
} while (--i);
}
}
void Init_Key()
{
if(!S7)
{
Delay(15);
if(!S7)
{
InitHC138(4);
led1 = ~led1;
}
while(!S7);
}
if(!S6)
{
Delay(15);
if(!S6)
{
InitHC138(4);
led1 = ~led1;
}
while(!S6);
}
}
void main (void)
{
InitHC138(5);
P0 = 0x00; P2 = 0x00;
InitHC138(4);
P0 = 0xff; P2 = 0x00;
while (1)
{
Init_Key();
}
}
1、 按键感悟
有次意外看到有个例程不用消抖,而且还是很灵敏
#include "stc15f2k60s2.h"
#define InitHC138(n) {P2 = P2 & 0x1f | (n << 5);}
sbit S7 = P3^0;
void main()
{
InitHC138(4);
P0 = 0xff; P2 = 0x00;
InitHC138(5);
P0 = 0x00; P2 = 0x00;
while(1)
{
if(S7 == 0)
{
InitHC138(4);
P0 = 0xfe;
P2 = 0x00;
while(!S7);
}
else if(S7 == 1)
{
InitHC138(4);
P0 = 0xff;
P2 = 0x00;
}
}
}
二、外部中断
#include "stc15f2k60s2.h"
#define InitHC138(n) {P2 = P2 & 0x1f | (n << 5);}
sbit led1 = P0^0;
sbit S7 = P3^0;
sbit S6 = P3^1;
sbit S4 = P3^3;
void Delay_ms(unsigned int t)
{
unsigned char i, j;
while(t--)
{
i = 12;
j = 169;
do
{
while (--j);
} while (--i);
}
}
void Init_key()
{
if(!S7)
{
Delay_ms(15);
if(!S7)
{
S4 = 0;
}
while(!S7);
}
if(!S6)
{
Delay_ms(15);
if(!S6)
{
S4 = 1;
}
while(!S6);
}
}
void Init_inter0()
{
IT1 = 0;
EX1 = 1;
EA = 1;
}
void Servive_0() interrupt 2
{
InitHC138(4);
led1 = ~led1;
}
void main (void)
{
InitHC138(5);
P0 = 0x00; P2 = 0x00;
InitHC138(4);
P0 = 0xff; P2 = 0x00;
Init_inter0();
while (1)
{
Init_key();
}
}
定时器
#include "stc15f2k60s2.h"
#define InitHC138(n) {P2 = P2 & 0x1f | (n << 5);}
sbit led1 = P0^0;
sbit S7 = P3^0;
sbit S6 = P3^1;
sbit S4 = P3^3;
void Init_Timer_0()
{
TMOD |= 0x01;
TH0 = (65536 - 1000) / 256;
TL0 = (65536 - 1000) % 256;
ET0 = 1;
EA = 1;
TR0 = 1;
}
unsigned int count = 0;
void Timer0() interrupt 1
{
count++;
if(count == 500)
{
count = 0;
InitHC138(4);
led1 = ~led1;
P2 = 0x00;
}
TH0 = (65536 - 1000) / 256;
TL0 = (65536 - 1000) % 256;
}
void main (void)
{
InitHC138(5);
P0 = 0x00; P2 = 0x00;
InitHC138(4);
P0 = 0xff; P2 = 0x00;
Init_Timer_0();
while (1)
{
}
}
四、串口通讯
#include "stc15f2k60s2.h"
#define InitHC138(n) {P2 = P2 & 0x1f | (n << 5);}
sbit led1 = P0^0;
void UartInit(void)
{
SCON = 0x50;
AUXR |= 0x40;
AUXR &= 0xFE;
TMOD &= 0x0F;
TL1 = 0xE8;
TH1 = 0xFF;
ET1 = 0;
TR1 = 1;
}
void uart() interrupt 4
{
if(RI)
{
RI=0;
}
if(TI)
{
TI=0;
}
}
void uart_sendchar(unsigned char ch)
{
SBUF=ch;
while(!TI);
TI=0;
}
unsigned char uart_recechar()
{
unsigned char dat;
while(!RI);
RI = 0;
dat = SBUF;
return dat;
}
void main (void)
{
char i;
InitHC138(5);
P0 = 0x00; P2 = 0x00;
InitHC138(4);
P0 = 0xff; P2 = 0x00;
UartInit();
while (1)
{
i = uart_recechar();
if(i == 'a')
{
InitHC138(4); led1 = ~led1; P2 = 0x00;
uart_sendchar(i);
}
}
}
在串口通讯情况下重映射调用 printf( ) 函数
#include "STC15F2K60S2.H"
#include "stdio.h"
void Delay_ms(unsigned int t)
{
unsigned char i, j;
while(t--)
{
i = 12;
j = 169;
do
{
while (--j);
} while (--i);
}
}
void UartInit(void)
{
SCON = 0x50;
AUXR |= 0x40;
AUXR &= 0xFE;
TMOD &= 0x0F;
TL1 = 0xE0;
TH1 = 0xFE;
ET1 = 0;
TR1 = 1;
TI=1;
}
void main()
{
float i =3.14159;
UartInit();
while(1)
{
printf("hello world\r\n");
printf("%.5f\n", i);
Delay_ms(1000);
}
}
在串口通信情况下用 sancf() 函数
#include "STC15F2K60S2.H"
#include "stdio.h"
#define InitHC138(n) {P2 = P2 & 0x1f | (n << 5);}
void UartInit(void)
{
SCON = 0x50;
AUXR |= 0x40;
AUXR &= 0xFE;
TMOD &= 0x0F;
TL1 = 0xE0;
TH1 = 0xFE;
ET1 = 0;
TR1 = 1;
TI = 1;
RI = 1;
}
void main()
{
char i =0;
InitHC138(5);
P0 = 0x00; P2 = 0x00;
InitHC138(4);
P0 = 0xff; P2 = 0x00;
UartInit();
while(1)
{
scanf("%c", &i);
printf("%c\n", i);
if(i == 'a')
{
InitHC138(4);
P00 = ~P00;
P2 = 0x00;
}
}
}
五、DS18B20 温度测量
#include <stc15f2k60s2.h>
#include "onewire.h"
unsigned char tab[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0xff,0xbf,0xc1,0x8e,0xc6,0x89,0x8c};
unsigned char buff[]={0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff};
long wendu;
long read18b20()
{
unsigned char low,high;
long 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&0x0f;
temp=temp<<8;
temp=temp|low;
temp=temp*6.25;
return temp;
}
void close()
{
P2=(P2&0x1f)|0xa0;
P0=0;
P2&=0x1f;
P2=(P2&0x1f)|0x80;
P0=0xfb;
P2&=0x1f;
}
void delay(unsigned int ms)
{
unsigned int i,j;
for(i=ms;i>0;i--)
for(j=845;j>0;j--);
}
void display()
{
unsigned char index;
for(index=0;index<8;index++)
{
P2=(P2&0x1f)|0xe0;
P0=0xff;
P2&=0x1f;
P2=(P2&0x1f)|0xc0;
P0=1<<index;
P2&=0x1f;
P2=(P2&0x1f)|0xe0;
P0=buff[index];
P2&=0x1f;
delay(1);
}
P2=(P2&0x1f)|0xe0;
P0=0xff;
P2&=0x1f;
}
void main()
{
close();
while(1)
{
wendu=read18b20();
buff[0]=tab[10];
buff[1]=tab[10];
buff[2]=tab[10];
buff[3]=tab[10];
buff[4]=tab[wendu/1000];
buff[5]=tab[wendu%1000/100]&0x7f;
buff[6]=tab[wendu%100/10];
buff[7]=tab[wendu%10];
display();
}
}
六、电子钟
#include "stc15f2k60s2.h"
#include "ds1302.h"
#define InitHC138(n) {P2 = P2 & 0x1f | (n << 5);}
code unsigned char SMGNoDot_CA[10] = {0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};
code unsigned char SMGDot_CA[10] = {0x40,0x79,0x24,0x30,0x19,0x12,0x02,0x78,0x00,0x10};
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] = {0x24,0x59,0x23,0x03,0x03,0x03,0x20};
void DelaySMG(unsigned char t)
{
while(t--);
}
void DisplaySMG_Bit(unsigned char pos, unsigned char dat)
{
InitHC138(7);
P0 = 0xff; P2 = 0x00;
InitHC138(6);
P0 = 0x01 << pos; P2 = 0x00;
InitHC138(7);
P0 = dat; P2 = 0x00;
}
void DS1302_Config()
{
char i;
Write_Ds1302_Byte(0x8e,0x00);
for( i = 0; i < 7; i++)
{
Write_Ds1302_Byte(Write_DS1302_adrr[i],Timer[i] );
}
Write_Ds1302_Byte(0x8e,0x80);
}
void Read_DS1302_Timer()
{
char i;
for(i = 0; i < 7; i++)
{
Timer[i] = Read_Ds1302_Byte( Read_DS1302_adrr[i] );
}
}
void Display_DS1302()
{
DisplaySMG_Bit(0, SMGNoDot_CA[Timer[2] / 16]);
DelaySMG(100);
DisplaySMG_Bit(1, SMGNoDot_CA[Timer[2] % 16]);
DelaySMG(100);
DisplaySMG_Bit(2,0xbf);
DelaySMG(100);
DisplaySMG_Bit(3, SMGNoDot_CA[Timer[1] / 16]);
DelaySMG(100);
DisplaySMG_Bit(4, SMGNoDot_CA[Timer[1] % 16]);
DelaySMG(100);
DisplaySMG_Bit(5,0xbf);
DelaySMG(100);
DisplaySMG_Bit(6, SMGNoDot_CA[Timer[0] / 16]);
DelaySMG(100);
DisplaySMG_Bit(7, SMGNoDot_CA[Timer[0] % 16]);
DelaySMG(100);
InitHC138(6);
P0 = 0xff; P2 = 0x00;
InitHC138(7);
P0 = 0xff; P2 = 0x00;
}
void main()
{
InitHC138(4);
P0 = 0xff; P2 = 0x00;
InitHC138(5);
P0 = 0x00; P2 = 0x00;
DS1302_Config();
while(1)
{
Read_DS1302_Timer();
Display_DS1302();
}
}
七、超声波
#include "stc15f2k60s2.h"
#include "intrins.h"
#define InitHC138(n) {P2 = P2 & 0x1f | (n << 5);}
#define uint unsigned int
#define uchar unsigned char
#define somenop {_nop_();_nop_();_nop_();_nop_();_nop_();\
_nop_();_nop_();_nop_();_nop_();_nop_();}
sbit TX = P1^0;
sbit RX = P1^1;
code uchar tab[] = {0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0xff};
uchar dspbuf[8] = {10,10,10,10,10,10,10,10};
uchar dspcom = 0;
uint count = 0;
bit flag;
uint t = 0;
void Send_ult()
{
uchar 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--);
}
void Display()
{
InitHC138(7);
P0 = 0xff; P2 = 0x00;
InitHC138(6);
P0 = (1 << dspcom); P2 = 0x00;
InitHC138(7);
P0 = tab[dspbuf[dspcom]]; P2 = 0x00;
if(++dspcom == 8)
{
dspcom = 0;
}
}
uint distance = 0;
void Init_time()
{
TMOD |= 0x11;
TH0 = (65536 - 2000) / 256;
TL0 = (65536 - 2000) % 256;
TH1 = 0;
TL1 = 0;
EA = 1;
ET0 = 1;
TR0 = 1;
}
void Servie_time() interrupt 1
{
TH0 = (65536 - 2000) / 256;
TL0 = (65536 - 2000) % 256;
Display();
if(++count == 200)
{
flag = 1;
count = 0;
}
}
void Config_ult()
{
if(flag)
{
flag = 0;
Send_ult();
TR1 = 1;
while((RX == 1) && (TF1 == 0));
TR1 = 0;
if(TF1 == 1)
{
TF1 = 0;
distance = 9999;
}
else
{
t = TH1;
t <<= 8;
t |= TL1;
distance = (uint)(t*0.017);
}
TH1 = 0;
TL1 = 0;
}
dspbuf[5] = distance / 100;
dspbuf[6] = distance % 100 / 10;
dspbuf[7] = distance % 10;
}
void main()
{
InitHC138(4);
P0 = 0xff; P2 = 0x00;
InitHC138(5);
P0 = 0x00; P2 = 0x00;
Init_time();
while(1)
{
Config_ult();
}
}
八、EEPROM 存储器
#include "stc15f2k60s2.h"
#include "iic.h"
#include "intrins.h"
#define InitHC138(n) {P2 = P2 & 0x1f | (n << 5);}
#define uchar unsigned char
#define uint unsigned int
code uchar tab[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};
uchar boot_time = 0;
void Delay_ms(uint t)
{
unsigned char i, j;
while(t--)
{
_nop_();
_nop_();
_nop_();
i = 11;
j = 190;
do
{
while (--j);
} while (--i);
}
}
void write_eeprom(unsigned char add,unsigned char date)
{
IIC_Start();
IIC_SendByte(0xa0);
IIC_WaitAck();
IIC_SendByte(add);
IIC_WaitAck();
IIC_SendByte(date);
IIC_WaitAck();
IIC_Stop();
}
unsigned char read_eeprom(unsigned char add)
{
unsigned char temp;
EA = 0;
IIC_Start();
IIC_SendByte(0xa0);
IIC_WaitAck();
IIC_SendByte(add);
IIC_WaitAck();
IIC_Start();
IIC_SendByte(0xa1);
IIC_WaitAck();
temp = IIC_RecByte();
IIC_WaitAck();
IIC_Stop();
EA = 1;
return temp;
}
void Display_SMG(uint pos, uint dat)
{
InitHC138(7);
P0 = 0xff; P2 = 0x00;
InitHC138(6);
P0 = 0x01 << pos; P2 = 0x00;
InitHC138(7);
P0 = dat; P2 = 0x00;
}
void Show_SMG()
{
Display_SMG(6, tab[boot_time / 10]);
Delay_ms(10);
Display_SMG(7, tab[boot_time % 10]);
Delay_ms(10);
}
void main()
{
InitHC138(4);
P2 = 0xff; P2 = 0x00;
InitHC138(5);
P2 = 0x00; P2 = 0x00;
boot_time = read_eeprom(0x01);
write_eeprom(0x01,++boot_time);
while(1)
{
Show_SMG();
}
}
|