一、按键
#include "stc15f2k60s2.h"
#define InitHC138(n) {P2 = P2 & 0x1f | (n << 5);}
#define u8 unsigned char
#define u32 unsigned int
bit key_flag;
u8 key;
u8 Key4_Read(void)
{
u32 Key_temp;
u8 Key_Value;
P3 |= 0x0f;
Key_temp = P3&0x0f;
switch(Key_temp)
{
case 0x0e : Key_Value = 7; break;
case 0x0d : Key_Value = 6; break;
case 0x0b : Key_Value = 5; break;
case 0x07 : Key_Value = 4; break;
default: Key_Value = 0;
}
return Key_Value;
}
u8 Key16_Read(void)
{
u32 Key_temp;
u8 Key_Value;
P44=0; P42=1; P35=1; P34=1; P3|=0X0F;
Key_temp = P3;
P44=1; P42=0; P35=1; P34=1; P3|=0X0F;
Key_temp = (Key_temp<<4) | (P3&0X0F);
P44=1; P42=1; P35=0; P34=1; P3|=0X0F;
Key_temp = (Key_temp<<4) | (P3&0X0F);
P44=1; P42=1; P35=1; P34=0; P3|=0X0F;
Key_temp = (Key_temp<<4) | (P3&0X0F);
switch(~Key_temp)
{
case 0X8000: Key_Value = 4; break;
case 0X4000: Key_Value = 5; break;
case 0X2000: Key_Value = 6; break;
case 0X1000: Key_Value = 7; break;
case 0X0800: Key_Value = 8; break;
case 0X0400: Key_Value = 9; break;
case 0X0200: Key_Value = 10; break;
case 0X0100: Key_Value = 11; break;
case 0X0080: Key_Value = 12; break;
case 0X0040: Key_Value = 13; break;
case 0X0020: Key_Value = 14; break;
case 0X0010: Key_Value = 15; break;
case 0X0008: Key_Value = 16; break;
case 0X0004: Key_Value = 17; break;
case 0X0002: Key_Value = 18; break;
case 0X0001: Key_Value = 19; break;
default: Key_Value = 0;
}
return Key_Value;
}
void Key_config()
{
while(key_flag)
{
key_flag = 0;
key = Key4_Read();
switch(key)
{
case 7:
InitHC138(4);
P0 = 0xfe; P2 = 0x00;
break;
case 6:
InitHC138(4);
P0 = 0xff; P2 = 0x00;
break;
}
}
}
void Timer1Init(void)
{
AUXR &= 0xBF;
TMOD &= 0x0F;
TH1 = (65535 - 1000) / 256;
TL1 = (65535 - 1000) % 256;
TF1 = 0;
TR1 = 1;
ET1 = 1;
EA = 1;
}
void main()
{
InitHC138(4);
P0 = 0xff; P2 = 0x00;
InitHC138(5);
P0 = 0x00; P2 = 0x00;
Timer1Init();
while(1)
{
Key_config();
}
}
void Serv_T1() interrupt 3
{
u32 i;
TH1 = (65535 - 1000) / 256;
TL1 = (65535 - 1000) % 256;
i++;
if(i == 10)
{
i = 0;
key_flag = 1;
}
}
二、外部中断
#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)
{
}
}
定时器2实现数码刷新
#include "STC15F2K60S2.h"
#include "ds1302.h"
#define InitHC138(n) {P2 = P2 & 0x1f | (n << 5);}
#define u8 unsigned char
#define u32 unsigned int
code u8 tab[]={0xc0, 0xf9, 0xa4, 0xb0, 0x99, 0x92, 0x82, 0xf8, 0x80, 0x90, 0xff, 0xbf};
u8 Buf[8];
u8 Timer[7] = {0x24,0x59,0x23,0x03,0x03,0x03,0x20};
void DS1302_config()
{
u8 i;
u8 Write = 0x80;
Write_Ds1302_Byte(0x8e, 0x00);
for(i = 0; i < 7; i++)
{
Write_Ds1302_Byte(Write, Timer[i]);
Write += 2;
}
Write_Ds1302_Byte(0x8e, 0x80);
}
void Read_ds1320_time()
{
u8 i;
u8 Read = 0x81;
for(i = 0; i < 7; i++)
{
Timer[i] = Read_Ds1302_Byte(Read);
Read += 2;
}
}
void Display_SMG(u8 pos, u8 dat)
{
InitHC138(7);
P0 = 0xff; P2 = 0x00;
InitHC138(6);
P0 = 0x01 << pos; P2 = 0x00;
InitHC138(7);
P0 = dat; P2 = 0x00;
}
void Timer2Init(void)
{
AUXR &= 0xFB;
T2L = 0x18;
T2H = 0xFC;
AUXR |= 0x10;
IE2 |= 0x04;
EA = 1;
}
void T0_Servive() interrupt 12
{
u8 pos;
T2L = 0x18;
T2H = 0xFC;
Display_SMG(pos, Buf[pos]);
if(++pos == 8) pos = 0;
}
void smgdatset(u8 d0,u8 d1,u8 d2,u8 d3,
u8 d4,u8 d5,u8 d6,u8 d7)
{
Buf[0]=d0;
Buf[1]=d1;
Buf[2]=d2;
Buf[3]=d3;
Buf[4]=d4;
Buf[5]=d5;
Buf[6]=d6;
Buf[7]=d7;
}
void Show_time()
{
Read_ds1320_time();
smgdatset(tab[Timer[2]/16], tab[Timer[2]%16], tab[11],
tab[Timer[1]/16], tab[Timer[1]%16], tab[11],
tab[Timer[0]/16], tab[Timer[0]%16]);
}
int main(void)
{
InitHC138(4);
P0 = 0xff; P2 = 0x00;
InitHC138(5);
P0 = 0x00; P2 = 0x00;
Timer2Init();
DS1302_config();
while(1)
{
Show_time();
}
}
四、串口通讯
#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"
#define InitHC138(n) {P2 = P2 & 0x1f | (n << 5);}
#define uint unsigned int
#define uchar unsigned char
unsigned char tab[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};
long wendu;
void Delay_ms(uint t)
{
unsigned char i, j;
while(t--)
{
i = 12;
j = 169;
do
{
while (--j);
} while (--i);
}
}
void Display_SMG(uchar pos, uchar dat)
{
InitHC138(7);
P0 = 0xff; P2 = 0x00;
InitHC138(6);
P0 = 0x01 << pos; P2 = 0x00;
InitHC138(7);
P0 = dat;
}
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)<<8)|(low);
temp *= 6.25;
return temp;
}
void Show_SMG()
{
wendu = read18b20();
Display_SMG(4, tab[wendu / 1000]);
Delay_ms(2);
Display_SMG(5, tab[wendu / 100 % 10]&0x7f);
Delay_ms(2);
Display_SMG(6, tab[wendu / 10 % 10]);
Delay_ms(2);
Display_SMG(7, tab[wendu % 10]);
Delay_ms(2);
}
void main()
{
InitHC138(4);
P0 = 0xff; P2 = 0x00;
InitHC138(5);
P0 = 0x00; P2 = 0x00;
while(1)
{
Show_SMG();
}
}
六、电子钟
#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 nop() {_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 = 16;
TX = 0;
for (i = 0; i < 16; i++)
{
TX = ~TX;
nop();nop();nop();nop();nop();
nop();nop();nop();nop();nop();
}
}
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
{
distance = (uint) (TH1 << 8) | TL1;
distance *= 0.017 * 1.085;
}
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();
}
}
九、PCF8591_ADC实验
#include "stc15f2k60s2.h"
#include "iic.h"
#include "intrins.h"
#define InitHC138(n) {P2 = P2 & 0x1f | (n << 5);}
#define uint unsigned int
#define uchar unsigned char
code unsigned char tab[] = { 0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};
unsigned char count_t;
unsigned int ADC = 0;
unsigned char Bit = 0;
void Delay_ms(unsigned int t)
{
unsigned char i, j;
while(t--)
{
_nop_();
_nop_();
_nop_();
i = 11;
j = 190;
do
{
while (--j);
} while (--i);
}
}
void Display_SMG(uchar pos, uchar dat)
{
InitHC138(7);
P0 = 0xff; P2 = 0x00;
InitHC138(6);
P0 = 0x01 << pos; P2 = 0x00;
InitHC138(7);
P0 = dat; P2 = 0x00;
}
unsigned int ADC_PCF8591(unsigned char ch)
{
unsigned int dat;
IIC_Start();
IIC_SendByte(0x90);
IIC_WaitAck();
IIC_SendByte(0x40+ch);
IIC_WaitAck();
IIC_Start();
IIC_SendByte(0x91);
IIC_WaitAck();
dat = IIC_RecByte();
IIC_SendAck(1);
IIC_Stop();
return dat;
}
void Init_T0()
{
AUXR |= 0x80;
TMOD &= 0xF0;
TL0 = 0xCD;
TH0 = 0xD4;
TF0 = 0;
TR0 = 1;
ET0 = 1;
EA = 1;
}
void Servive_T0(void) interrupt 1
{
if(++count_t == 50)
{
count_t = 0;
Bit = 1;
}
}
void Show_SMG()
{
if(Bit == 1)
{
Bit = 0;
ADC = ADC_PCF8591(0x01);
}
Display_SMG(5, tab[ADC / 100]);
Delay_ms(2);
Display_SMG(6, tab[ADC / 10 % 10]);
Delay_ms(2);
Display_SMG(7, tab[ADC % 10]);
Delay_ms(2);
}
void main(void)
{
InitHC138(4);
P0 = 0xff; P2 = 0x00;
InitHC138(5);
P0 = 0x00; P2 = 0x00;
Init_T0();
while(1)
{
Show_SMG();
}
}
十、PCF8591_DAC实验
#include "stc15f2k60s2.h"
#include "iic.h"
#include "intrins.h"
#define uint unsigned int
#define uchar unsigned char
#define InitHC138(n) {P2 = P2 & 0x1f | (n << 5);}
#define VREF 5200
sbit S7 = P3^0;
sbit S6 = P3^1;
unsigned char dac_value;
unsigned int Vaout;
code unsigned char tab[] = { 0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};
void Delay_ms(unsigned int t)
{
unsigned char i, j;
while(t--)
{
_nop_();
_nop_();
_nop_();
i = 11;
j = 190;
do
{
while (--j);
} while (--i);
}
}
void Display_SMG(uchar pos, uchar dat)
{
InitHC138(7);
P0 = 0xff; P2 = 0x00;
InitHC138(6);
P0 = 0x01 << pos; P2 = 0x00;
InitHC138(7);
P0 = dat; P2 = 0x00;
}
void PCF8591_DAC(unsigned char dat)
{
IIC_Start();
IIC_SendByte(0x90);
IIC_WaitAck();
IIC_SendByte(0x40);
IIC_WaitAck();
IIC_SendByte(dat);
IIC_WaitAck();
IIC_Stop();
}
void Scanf_key()
{
if(S7 == 0)
{
dac_value+=50;
PCF8591_DAC(dac_value);
while(!S7);
}
if(S6 == 0)
{
dac_value-=50;
PCF8591_DAC(dac_value);
while(!S6);
}
}
void Show_SMG()
{
Vaout = VREF/255*dac_value;
Display_SMG(4, tab[Vaout / 1000]);
Delay_ms(2);
Display_SMG(5, tab[Vaout / 100 % 10]);
Delay_ms(2);
Display_SMG(6, tab[Vaout / 10 % 10]);
Delay_ms(2);
Display_SMG(7, tab[Vaout % 10]);
Delay_ms(2);
}
void main(void)
{
InitHC138(4);
P0 = 0xff; P2 = 0x00;
InitHC138(5);
P0 = 0x00; P2 = 0x00;
while(1)
{
Scanf_key();
Show_SMG();
}
}
十一、NE555频率
#include "STC15F2K60S2.h"
#define InitHC138(n, dat) {P0 = dat; P2 = P2 & 0x1f | (n << 5); P2 &= 0x1f;}
#define u8 unsigned char
#define u32 unsigned int
code u8 tab[] = {0xc0, 0xf9, 0xa4, 0xb0, 0x99, 0x92, 0x82, 0xf8, 0x80, 0x90};
u8 buf[8];
u32 freq;
void Display_SMG(u8 pos, u8 dat)
{
InitHC138(7, 0xff);
InitHC138(6, 0x01 << pos);
InitHC138(7, dat);
}
void SMG_set(u8 d0, u8 d1, u8 d2, u8 d3,
u8 d4, u8 d5, u8 d6, u8 d7)
{
buf[0] = d0;
buf[1] = d1;
buf[2] = d2;
buf[3] = d3;
buf[4] = d4;
buf[5] = d5;
buf[6] = d6;
buf[7] = d7;
}
void Init_Fre()
{
TMOD = 0x15;
TH0 = TL0 = 0;
TL1 = 0x18;
TH1 = 0xFC;
TR0 = TR1 = 1;
ET1 = 1;
EA = 1;
}
void timer() interrupt 3
{
u32 count;
TL1 = 0x18;
TH1 = 0xFC;
if (++count == 1000)
{
count = 0;
TR0 = 0;
freq = (u32) (TH0 << 8) | TL0;
TL0 = TH0 = 0;
TR0 = 1;
}
}
void Timer2Init(void)
{
AUXR |= 0x04;
T2L = 0xCD;
T2H = 0xD4;
AUXR |= 0x10;
IE2 |= 0x04;
EA = 1;
}
void Ser_T2(void) interrupt 12
{
u8 pos;
T2L = 0xCD;
T2H = 0xD4;
Display_SMG(pos, buf[pos]);
if(++pos == 8)
pos = 0;
}
void Show_fre(void)
{
SMG_set(0xff, 0xff, 0xff,
tab[freq/10000%10], tab[freq/1000%10], tab[freq/100%10], tab[freq/10%10], tab[freq%10]);
}
void main()
{
InitHC138(4, 0xff);
InitHC138(5, 0x00);
Init_Fre();
Timer2Init();
while(1)
{
Show_fre();
}
}
十二、PWM输出
#include "stc15f2k60s2.h"
#define InitHC138(n) {P2 = P2 & 0x1f | (n << 5);}
typedef unsigned int u16;
typedef unsigned char u8;
sbit PWM = P0^0;
u16 count;
u16 i;
void Timer1Init()
{
TMOD|=0X10;
TH1 = 0xFF;
TL1 = 0xff;
ET1=1;
EA=1;
TR1=1;
}
void Time1(void) interrupt 3
{
TH1 = 0xFF;
TL1 = 0xff;
count++;
}
void Out_Pwm(u16 value)
{
if(count>1000)
{
count=0;
}
if(count <value)
{
InitHC138(4);
PWM=1; P2 = 0x00;
}
else
{
InitHC138(4);
PWM=0; P2 = 0x00;
}
}
void main()
{
Timer1Init();
while(1)
{
Out_Pwm(900);
}
}
呼吸灯
#include <stc15f2k60s2.h>
bit flag_pwn;
unsigned char led_dat;
void Timer0Init()
{
AUXR |= 0x80;
TMOD &= 0xF0;
TL0 = 0x88;
TH0 = 0xFF;
TF0 = 0;
TR0 = 1;
ET0 = 1;
EA = 1;
}
void ShowLed(unsigned char led_dat)
{
P2 = P2&0x1f|0x80;
P0 = led_dat;
P2 = P2&0x1f;
}
void T0_time() interrupt 1
{
static unsigned int count = 0,n = 0,pwn = 0;
TL0 = 0x88;
TH0 = 0xFF;
if(++count >= 1000)
{
count = 0;
}
if(++n >= 100)
{
n = 0;
if(flag_pwn == 0)
{
pwn++;
if(pwn == 1000)
{
flag_pwn = 1;
}
}
else if(flag_pwn == 1)
{
pwn--;
if(pwn == 0)
{
flag_pwn = 0;
}
}
}
if(count > pwn)
{
ShowLed(0xfe);
}
else
{
ShowLed(0xff);
}
}
void CloseFucker()
{
P2 = P2&0x1f|0xa0;
P0 = 0xaf;
P2 = P2&0x1f;
}
void main()
{
CloseFucker();
Timer0Init();
while(1)
{
;
}
}