#include <STC15F2K60S2.H>
#include "iic.h"
unsigned char code smg_code[]={0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F,0x76,0x79,0x38}; //共阴数码管码表
unsigned char smg[8];
bit led_open=1;//LED指示灯开关
typedef struct
{
unsigned char b1:1;
unsigned char b2:1;
unsigned char b3:1;
unsigned char b4:1;
unsigned char b5:1;
unsigned char b6:1;
unsigned char b7:1;
unsigned char b8:1;
}bits;
typedef union
{
unsigned char Hex;
bits B;
}HexTobits;
HexTobits led_ctrl;
void vDevice_Process(unsigned char p2dat,unsigned char p0dat)
{
P0 = p0dat;
P2 = (P2&0x1f)|p2dat;
P2 = (P2&0x1f)|0x00;
}
//定时器0 计数器
void Timer0Init(void) //计数器@12.000MHz
{
AUXR |= 0x80; //定时器时钟1T模式
TMOD |= 0x05; //设置定时器模式 计数器模式 16位不可重装
TL0 = 0x00; //设置定时初值
TH0 = 0x00; //设置定时初值
TF0 = 0; //清除TF0标志
TR0=1;
}
//测量ne555
unsigned int cnt_ne555;
unsigned int cnt_1s; //计时1s
unsigned int frequence; //频率
void Get_freq()
{
if(cnt_1s>=1000)//800ms刷新一次
{
cnt_1s=0;
frequence = (TH0<<8)|TL0;
TL0 = 0x00; //设置定时初值
TH0 = 0x00; //设置定时初值
}
}
//定时器2
void Timer2Init(void) //1毫秒@12.000MHz
{
AUXR |= 0x04; //定时器时钟1T模式
T2L = 0x20; //设置定时初值
T2H = 0xD1; //设置定时初值
AUXR |= 0x10; //定时器2开始计时
EA=1;
IE2|=0x04;
}
//采集rb2电压
unsigned long Rb2;
unsigned int cnt_rb2; //刷新时间
void Read_rb2()
{
if(cnt_rb2>=400)//400ms采集一次
{
cnt_rb2=0;
Rb2 = (unsigned long)rb2_Read();
}
}
//数码管操作函数
unsigned char mode_smg=0;//显示界面模式
bit SMG_open=1;
void vSMG_Process()
{
if(SMG_open==1)
{
if(mode_smg==0)
{
smg[0]=0x3e;
smg[1]=0x00;
smg[2]=0x00;
smg[3]=0x00;
smg[4]=0x00;
smg[5]=smg_code[Rb2/100]|0x80; //|0x80 让dp点亮 小数点
smg[6]=smg_code[Rb2/10%10];
smg[7]=smg_code[Rb2%10];
}
if(mode_smg==1)
{
smg[0]=0x71;
smg[1]=0x00;
if(frequence<=99999)
{
smg[2]=0x00;
}
else
{
smg[2]=smg_code[frequence/100000];
}
smg[3]=smg_code[frequence/10000%10];
smg[4]=smg_code[frequence/1000%10];
smg[5]=smg_code[frequence/100%10]; //|0x80 让dp点亮 小数点
smg[6]=smg_code[frequence/10%10];
smg[7]=smg_code[frequence%10];
}
}
else
{
smg[0]=0x00;
smg[1]=0x00;
smg[2]=0x00;
smg[3]=0x00;
smg[4]=0x00;
smg[5]=0x00;
smg[6]=0x00;
smg[7]=0x00;
}
}
//数码管显示
void SMG_Show()
{
unsigned char i;
vDevice_Process(0xc0, 0x00);
vDevice_Process(0xe0, ~smg[i]);
vDevice_Process(0xc0, 0x01<<i);
i = (i+1)%8;
}
//DAC输出模式
unsigned char mode_dac=0;
void DAC_out()
{
if(mode_dac==0)
{
DAC_Process(102); //51*2=102 2V
}
else if(mode_dac==1)
{
DAC_Process(Rb2*51/100); //转为0~255区间
}
}
//三行按键
unsigned char Trg;
unsigned char Cont;
void Three_Key()
{
unsigned char ReadData = P3^0xff;
Trg = ReadData&(ReadData^Cont);
Cont = ReadData;
}
//获取键值
unsigned char cnt_key;
void Read_Key()
{
if(cnt_key>=10)//10ms获取一次 消抖
{
cnt_key=0;
Three_Key();
if(Trg & 0x01)//S7
{
SMG_open =~SMG_open;
}
if(Trg & 0x02)//S6
{
led_open=~led_open;
}
if(Trg & 0x04)//S5
{
mode_dac++;
if(mode_dac==2)mode_dac=0;
}
if(Trg & 0x08)//S4
{
mode_smg++;
if(mode_smg==2)mode_smg=0;
}
}
}
void System_init()
{
led_ctrl.Hex = 0xff;
vDevice_Process(0x80, led_ctrl.Hex);
vDevice_Process(0xa0, 0x00);
}
void main()
{
System_init();
Timer2Init();
Timer0Init();
while(1)
{
Get_freq();
Read_rb2();
vSMG_Process();
Read_Key();
DAC_out();
}
}
void Timer2() interrupt 12
{
cnt_rb2++;
cnt_ne555++;
cnt_1s++;
cnt_key++;
SMG_Show();
if(led_open)
{
//L1 L2
switch(mode_smg)
{
case 0:
led_ctrl.B.b1=0;
led_ctrl.B.b2=1;
break;
case 1:
led_ctrl.B.b1=1;
led_ctrl.B.b2=0;
break;
}
//L3
if((Rb2<150)||(Rb2>=250&&Rb2<350))
{
led_ctrl.B.b3=1;
}
else
{
led_ctrl.B.b3=0;
}
//L4
if((frequence<1000)||(frequence>=5000&&frequence<10000))
{
led_ctrl.B.b4=1;
}
else
{
led_ctrl.B.b4=0;
}
//L5
switch(mode_dac)
{
case 0:
led_ctrl.B.b5=1;
break;
case 1:
led_ctrl.B.b5=0;
break;
}
vDevice_Process(0x80, led_ctrl.Hex);
}
else
{
vDevice_Process(0x80, 0xff);
}
}
蓝桥杯单片机第10届
于 2023-03-16 23:21:28 首次发布