蓝桥杯单片机组第九届省赛

main.c

#include"stc15f2k60s2.h"
#include"iic.h"
#include"intrins.h"

unsigned char code smgduan[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,~0x40,0xff};
unsigned char smgbuffer[8];
unsigned char smgcom,keytt;
unsigned char mode=0,Lmode=1,liangmode=0;
unsigned int gap1=400,gap2=400,gap3=400,gap4=400;
unsigned char Rb2,ADtt;
unsigned int ttt;
bit xieyiciflag;
bit startflag,yiflag;
unsigned char PWM,PWMtt,PWMmode;

float aaa;


void display();
void keyscan();
void xieyici();

void Delay2ms()		//@12.000MHz
{
	unsigned char i, j;

	i = 24;
	j = 85;
	do
	{
		while (--j);
	} while (--i);
}


void Timer1Init(void)		//1毫秒@12.000MHz
{
	AUXR |= 0x40;		//定时器时钟1T模式
	TMOD &= 0x0F;		//设置定时器模式
	TL1 = 0x20;		//设置定时初值
	TH1 = 0xD1;		//设置定时初值
	TF1 = 0;		//清除TF1标志
	TR1 = 1;		//定时器1开始计时
	ET1=1;
	EA=1;
}

void Timer1() interrupt 3    
{
	unsigned char P0_kepper=0,P2_kepper=0;
	P0_kepper=P0; P2_kepper=P2; P2=P2&0x1f;

	display();
	keytt++; if(keytt==10) {keytt=0; keyscan();}
  ADtt++;

	if(Lmode==1 && startflag==1)     {ttt++; if(ttt==gap1) {yiflag=1;ttt=0;}}
	else if(Lmode==2 && startflag==1){ttt++; if(ttt==gap2) {yiflag=1;ttt=0;}}

	P0=P0_kepper; P2=P2_kepper;
}

void Timer0Init(void)		//100微秒@12.000MHz
{
	AUXR |= 0x80;		//定时器时钟1T模式
	TMOD &= 0xF0;		//设置定时器模式
	TL0 = 0x50;		//设置定时初值
	TH0 = 0xFB;		//设置定时初值
	TF0 = 0;		//清除TF0标志
	TR0 = 1;		//定时器0开始计时
	ET0=1;
}

void Timer0() interrupt 1
{
	if(startflag==1) 
	{
		PWMtt++;
		if(PWMtt<=PWM)                PWMmode=1;
		else if(PWMtt>PWM&&PWMtt<100) PWMmode=2;
		else if(PWMtt==100) {PWMtt=0; PWMmode=3;}
	}
}

void display()
{
	P0=0xff; P2=P2&0x1f|0xc0; P2=P2&0x1f;
	P0=0xff; P2=P2&0x1f|0xe0; P2=P2&0x1f;
	P0=1<<smgcom; P2=P2&0x1f|0xc0; P2=P2&0x1f;
	P0=smgduan[smgbuffer[smgcom++]]; P2=P2&0x1f|0xe0; P2=P2&0x1f;
	if(smgcom==8) smgcom=0;
}

void keyscan()    
{
	unsigned char dat,trg,cnt;
	dat=P3^0xff;
	trg=dat&(dat^cnt);
	cnt=dat;
	
	if(trg&0x01) startflag=~startflag;	
	else if(trg&0x02)
	{
		mode++;
		if(mode==3)
		{
			mode=0;
			xieyiciflag=1;
		}
	}
	else if(trg&0x04)
	{
		if(mode==1){ttt=0; yiflag=0; Lmode++; if(Lmode==5) Lmode=4;}
		else if(mode==2)
		{
		  if(Lmode==1)     {gap1=gap1+100; if(gap1==1300) gap1=1200;}
			else if(Lmode==2){gap2=gap2+100; if(gap2==1300) gap2=1200;}
			else if(Lmode==3){gap3=gap3+100; if(gap3==1300) gap3=1200;}
			else if(Lmode==4){gap4=gap4+100; if(gap4==1300) gap4=1200;}
		}	
	}
	else if(trg&0x08)
	{
		if(mode==1){ttt=0; yiflag=0; Lmode--; if(Lmode==0) Lmode=1;}
		else if(mode==2)
		{
		  if(Lmode==1)     {gap1=gap1-100; if(gap1==300) gap1=400;}
			else if(Lmode==2){gap2=gap2-100; if(gap2==300) gap2=400;}
			else if(Lmode==3){gap3=gap3-100; if(gap3==300) gap3=400;}
			else if(Lmode==4){gap4=gap4-100; if(gap4==300) gap4=400;}
		}		
	}

	if(mode==0 && dat&0x08)	   mode=6;
	else if(mode==6 && dat==0) mode=0;	
}

void duyici()
{
	gap1=10*EEPROM_read(0x01);  Delay2ms();
	gap2=10*EEPROM_read(0x02);	Delay2ms();
	gap3=10*EEPROM_read(0x03);	Delay2ms();
	gap4=10*EEPROM_read(0x04);	Delay2ms();					
}

void xieyici()
{
	if(xieyiciflag==1)
	{
		EEPROM_write(0x01,gap1/10); Delay2ms();
		EEPROM_write(0x02,gap2/10); Delay2ms();
		EEPROM_write(0x03,gap3/10); Delay2ms();
		EEPROM_write(0x04,gap4/10); Delay2ms();
		xieyiciflag=0;
	}
}

void ADC()
{
	if(ADtt==200)
	{
	  ADtt=0;
	  Rb2=AD_read(0x03);
	}

	if(Rb2<=64)                  {liangmode=1; PWM=25;}
	else if(Rb2>64 && Rb2<=128)  {liangmode=2; PWM=50;}
	else if(Rb2>128 && Rb2<=192) {liangmode=3; PWM=75;}
	else if(Rb2>192 && Rb2<=255) {liangmode=4; PWM=100;}
}

void xianshi()
{
	if(mode==0){smgbuffer[0]=smgbuffer[1]=smgbuffer[2]=smgbuffer[3]=smgbuffer[4]=smgbuffer[5]=smgbuffer[6]=smgbuffer[7]=11;}
	else if(mode==1){smgbuffer[0]=smgbuffer[2]=10;smgbuffer[1]=Lmode;smgbuffer[3]=11;}

	if(mode==1 || mode==2)
	{
		if(Lmode==1)
		{
			if(gap1>=1000){smgbuffer[4]=gap1/1000;smgbuffer[5]=gap1%1000/100;smgbuffer[6]=gap1%1000%100/10;smgbuffer[7]=gap1%10;}
			else if(gap1<1000){smgbuffer[4]=11;smgbuffer[5]=gap1/100;smgbuffer[6]=gap1%100/10;smgbuffer[7]=gap1%10;}
	  }
		else if(Lmode==2)
		{
			if(gap2>=1000){smgbuffer[4]=gap2/1000;smgbuffer[5]=gap2%1000/100;smgbuffer[6]=gap2%1000%100/10;smgbuffer[7]=gap2%10;}
			else if(gap2<1000){smgbuffer[4]=11;smgbuffer[5]=gap2/100;smgbuffer[6]=gap2%100/10;smgbuffer[7]=gap2%10;}		
		}
		else if(Lmode==3)
		{
			if(gap3>=1000){smgbuffer[4]=gap3/1000;smgbuffer[5]=gap3%1000/100;smgbuffer[6]=gap3%1000%100/10;smgbuffer[7]=gap3%10;}
			else if(gap3<1000){smgbuffer[4]=11;smgbuffer[5]=gap3/100;smgbuffer[6]=gap3%100/10;smgbuffer[7]=gap3%10;}		
		}
		else if(Lmode==4)
		{
			if(gap4>=1000){smgbuffer[4]=gap4/1000;smgbuffer[5]=gap4%1000/100;smgbuffer[6]=gap4%1000%100/10;smgbuffer[7]=gap4%10;}
			else if(gap4<1000){smgbuffer[4]=11;smgbuffer[5]=gap4/100;smgbuffer[6]=gap4%100/10;smgbuffer[7]=gap4%10;}				
		}
	}
	
	else if(mode==6)
	{
		 smgbuffer[0]=smgbuffer[1]=smgbuffer[2]=smgbuffer[3]=smgbuffer[4]=smgbuffer[5]=11;
		 smgbuffer[6]=10;
		 smgbuffer[7]=liangmode;
	}	
}

void LED()
{
	if(startflag==1)
	{
		 //aaa不需要加if(aaa==8) aaa=0;
		/**************************************************************************************************/
		if(Lmode==1)     {if(yiflag==1){P0=_crol_(0xfe,aaa++); P2=P2&0x1f|0x80; P2=P2&0x1f;  yiflag=0;}} 
		else if(Lmode==2){if(yiflag==1){P0=_cror_(0x7f,aaa++); P2=P2&0x1f|0x80; P2=P2&0x1f;  yiflag=0;}}
	}
	else if(startflag==0)
	{
		P0=0xff; P2=P2&0x1f|0x80; P2=P2&0x1f;
	}
}

void yidong()
{
	if(PWMmode==1||PWMmode==3)
	{
		if(Lmode==1)     {P0=_crol_(0xfe,aaa);P2=P2&0x1f|0x80;P2=P2&0x1f;}
		else if(Lmode==2){P0=_cror_(0x7f,aaa);P2=P2&0x1f|0x80;P2=P2&0x1f;}  //这里只需要显示不能移动
	}
	else if(PWMmode==2){P0=0xff;P2=P2&0x1f|0x80;P2=P2&0x1f;}
}

void main()
{
	P0=0x00; P2=P2&0x1f|0xa0; P2=P2&0x1f;
	P0=0xff; P2=P2&0x1f|0x80; P2=P2&0x1f;
	duyici();
	Timer0Init();
  Timer1Init();
	while(1)
	{
	 xieyici();
	 ADC();
	 yidong();
	 LED();
	 xianshi();
	 
	}
}

iic.c

/*
  程序说明: IIC总线驱动程序
  软件环境: Keil uVision 4.10 
  硬件环境: CT107单片机综合实训平台 8051,12MHz
  日    期: 2011-8-9
*/

#include "reg52.h"
#include "intrins.h"

#define DELAY_TIME 5     //扩大8倍

#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;  					// 0:应答,1:非应答
//    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;
}

//通过I2C总线发送数据
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;  
}

//从I2C总线上接收数据
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;    
}

unsigned char AD_read(unsigned char add)
{
	unsigned char AD_temp;
	
	IIC_Start();
	IIC_SendByte(0x90);
	IIC_WaitAck();
	IIC_SendByte(add);
	IIC_WaitAck();

	
	IIC_Start();
	IIC_SendByte(0x91);
	IIC_WaitAck();
  AD_temp=IIC_RecByte();
	IIC_Stop();
	
	return AD_temp;
}

//void AD_write(unsigned char add,unsigned char dat)  //注意没有返回值 是void!!!!!
//{
//	IIC_Start();
//	IIC_SendByte(0x90);
//	IIC_WaitAck();
//	IIC_SendByte(add);
//	IIC_WaitAck();
//	IIC_SendByte(dat);
//	IIC_WaitAck();
//                    
//	IIC_Stop();
//}

unsigned char EEPROM_read(unsigned char add)
{
	unsigned char EEPROM_temp;
	
	IIC_Start();
	IIC_SendByte(0xA0);
	IIC_WaitAck();
	IIC_SendByte(add);
	IIC_WaitAck();
	IIC_Stop();
	
	IIC_Start();
	IIC_SendByte(0xA1);
	IIC_WaitAck();
  EEPROM_temp=IIC_RecByte();
	IIC_Stop();
	
	return EEPROM_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();
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值