赞
踩
我参加了第十二届蓝桥杯单片机比赛,马马虎虎,拿了省一进了国赛。现在考研结束了,有时间就想把备赛的练习分享一下。我学习的是DK的编程风格和思路,所有代码都是自己码的(大家不要依赖复制粘贴,要不然考场上会出问题的),所以有少许bug(当时没影响功能就没改),分享的目的是记录一下此次竞赛。同时也希望能够帮助到正在备赛的同学们,更欢迎大家批评指正。
(相信你们都有题目吧,这里怕侵权就不放了)
本次使用的是iic
代码如下(iic.c)
/* 程序说明: IIC总线驱动程序 软件环境: Keil uVision 4.10 硬件环境: CT107单片机综合实训平台 8051,12MHz 日 期: 2011-8-9 */ #include "stc15.h" #include "intrins.h" #define DELAY_TIME 40 #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 dat=0; IIC_Start(); IIC_SendByte(0x90); IIC_WaitAck(); IIC_SendByte(add); IIC_WaitAck(); IIC_Stop(); IIC_Start(); IIC_SendByte(0x91); IIC_WaitAck(); dat = IIC_RecByte(); IIC_Stop(); return dat; }
iic.h
#ifndef _IIC_H
#define _IIC_H
#define uint unsigned int
#define uchar unsigned char
void IIC_Start(void);
void IIC_Stop(void);
bit IIC_WaitAck(void);
void IIC_SendAck(bit ackbit);
void IIC_SendByte(unsigned char byt);
unsigned char IIC_RecByte(void);
uchar AD_Read(uchar add);
#endif
代码如下:
#include <stc15.h> #include "iic.h" #define uint unsigned int #define uchar unsigned char uchar yi,er,san,si,wu, liu,qi,ba; uchar code com[]={0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80}; uchar code tab[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0xbf,0xff, 0x40,0x79,0x24,0x30,0x19,0x12,0x02,0x78,0x00,0x10}; uchar t = 0,L=0; uint ml=0,money=0; uchar s7=0,s6=0; void allinit(); void delay(uint ms); void smg_display(uchar yi,uchar er,uchar san,uchar si,uchar wu,uchar liu, uchar qi,uchar ba); void Timer0Init(void); //5毫秒@11.0592MHz void keyscan(); void led(); void main() { allinit(); Timer0Init(); yi=11,er=11,san=11,si=11,wu=11,liu=11,qi=11,ba=11; while(1) { L=AD_Read(0x01); led(); if(s7==1) { s7=0; EA=1;ET0=1; P2=0XA0;P0=0X10; yi= 11,er=12,san=5,si=0; } else if(s6==1) { s6=0; ET0=EA=0,P2=0xa0;P0=0x00; money = ml*0.5; yi=11,er=12,san=5,si=0,wu=money/1000,liu=money%1000/100+12,qi=money%100/10,ba=money%10; ml=0; } keyscan(); smg_display( yi,er,san,si,wu,liu,qi,ba); } } void allinit() { P2=0XA0; P0=0X00; P2=0X80; P0=0XFF; P2=0XC0; P0=0XFF; P0=0XFF; P0=0XFF; } void delay(uint ms) { uint a,b; for(a=ms;a>0;a--) for(b=810;b>0;b--); } void smg_display(uchar yi,uchar er,uchar san,uchar si,uchar wu,uchar liu,uchar qi,uchar ba) { uchar i; for(i = 0; i<8; i++) { P2 = 0xc0; P0 = com[i]; P2 = 0xff; switch(i) { case 0:P0 = tab[yi];break; case 1:P0 = tab[er];break; case 2:P0 = tab[san];break; case 3:P0 = tab[si];break; case 4:P0 = tab[wu];break; case 5:P0 = tab[liu];break; case 6:P0 = tab[qi];break; case 7:P0 = tab[ba];break; } delay(1); } if(i == 8) i = 0; } void Timer0Init(void) //5毫秒@11.0592MHz { AUXR |= 0x80; //定时器时钟1T模式 TMOD &= 0xF0; //设置定时器模式 TL0 = 0x00; //设置定时初值 TH0 = 0x28; //设置定时初值 TF0 = 0; //清除TF0标志 TR0 = 1; //定时器0开始计时 } void Timer0() interrupt 1 { t++; if(t == 20) { t=0; ml=ml+1; wu=ml/1000; liu=ml%1000/100 + 12; qi=ml%100/10; ba=ml%10; if(ml==9999) { ET0=EA=0,s7=0;P2=0xa0;P0=0x00; money = ml*0.5; yi= 11,er=12,san=5,si=0,wu=money/1000,liu=money%1000/100+12,qi=money%100/10,ba=money%10; ml=0; } } } void keyscan() { if(P30==0) { delay(5); if(P30==0) { s7 = 1; } while(!P30); } if(P31==0) { delay(5); if(P31==0) { s6 = 1; } while(!P31); } } void led() { if(L<=64) { P2=0X80;P0=0XFE; } else { P2=0X80;P0=0XFf; } }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。