|
EDA365欢迎您登录!
您需要 登录 才可以下载或查看,没有帐号?注册
x
这是我单片机学习项目
, l) q3 D$ d4 Z9 f! R4 V- z) {可以当作学习单片机的小练习
5 f: k! M4 j, [2 G半个小时完成的,附上代码
$ D: I/ B/ |( v* m. X欢迎大家一起来交流学习/ B4 T" x2 N- L. U3 {+ S& x
+ ]! K- G m8 r n
单片机源程序如下:: X9 O: q7 u- P; L2 q* ]$ N
- #include <REGX52.H>
- #define N 50
- #define Fosc 11059200L
- #define INT_PERIOD 5
- #define OveRFreq (1000/INT_PERIOD)
- #define ClockPeriod (0x10000-Fosc/12/OverFreq)
- unsigned char code DispSeg[] = {
- 0x00,
- 0x06,0x5b,0x4f,0x66,0x6d,
- 0x7d,0x27,0x7f,0x6f,0x3f,
- 0x08
- };
- void Delay(unsigned int xms)
- {
- unsigned char i, j;
- while(xms--){
- i = 2;
- j = 199;
- do
- {
- while (--j);
- } while (--i);
- }
- }
- unsigned char Buffer[]={0,0,0,0,0,1,7,2,11,3,5};
- unsigned char show[6];
- unsigned char j=0;
- unsigned char timecnt;
- sbit K1 = P2^0;
- void timer0_init(void)
- {
- TMOD &= 0xF0;
- TMOD |= 0x01;
- TH0 = ClockPeriod >> 8;
- TL0 = ClockPeriod & 0xFF;
- ET0 = 1;
- EA = 1;
- TR0 = 1;
- }
- void change()
- {
- unsigned char i;
- for(i=0;i<6;i++)
- {
- show=Buffer[i+j];
- }
- }
- void Display()
- {
- unsigned char i=0x80;
- unsigned char *ptr=show;
- while(i!=0x02)
- {
- P0=DispSeg[*ptr++];
- P1=i;
- Delay(1);
- i=i>>1;
- }
- }
- void state1(void)
- {
- unsigned char tim,i;
- for(i=0;i<6;i++)
- {
- tim=N;
- change();
- while(tim--)
- {
- Display();
- }
- j++;
- }
- }
- void state3()
- {
- unsigned char i;
- P0=0x7f;
- i = 0x80;
- while(i!=0x02)
- {
- P1=i;
- Delay(500);
- i=i>>1;
- }
- }
- void state4()
- {
- unsigned char temp,i;
- temp = 0x01;
- P1 = 0x00;
- P1 = 0x02;
- for(i=0;i<7;i++)
- {
- P0 = temp;
- if(temp == 0x80)
- temp = 0x01;
- else
- temp <<= 1;
- Delay(500);
- }
- }
- void keypre()
- {
- if( K1 == 0 )
- {
- Display();
- if( K1 == 0 )
- {
- timer0_init();
- P0 = 0x00;
- Delay(1000);
- TR0 = 0;
- while( K1 == 0 );
- while(1)
- {
- state3();
- state4();
- }
- }
- }
- }
- void main()
- {
- state1();
- while(1)
- {
- j=5;
- change();
- Display();
- keypre();
- }
- }
- void Time0Interrupt( void ) interrupt 1
- {
- timecnt++;
- TH0 = ClockPeriod >> 8;
- TL0 = ClockPeriod & 0xFF;
- P1 ^= 1;
- }
- F, s/ I$ R# I( S. P5 L
0 j( f6 f$ P5 Q/ Y' Y
5 v4 w! p& R' \* Q- s |
|