Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //lab1
- #include <reg51.h>
- void delay()
- {
- int i;
- for(i=0;i<9999;i++)
- ;
- }
- void main()
- {
- P0 = 0xA5 ;
- delay();
- }
- -----------------------------------------------------------------------------------------------------
- //lab2
- #include <reg51.h>
- #define SW (~P3 & 0x1f )
- unsigned char code seg7[] = {0xc0 , 0xf9 , 0xa4 , 0xb0 ,
- 0x99 , 0x92 , 0x82 , 0xf8 ,
- 0x80 , 0x90 };
- unsigned char code scan[4] = {0xf7 , 0xfb , 0xfd , 0xfe};
- int slow = 10 ;
- unsigned int count =0000 ;
- void delay1ms(int t)
- {
- int i,j;
- for(j=0 ; j<t ; j++)
- for(i=0 ; i<100 ; i++)
- ;
- }
- void S7(int c)
- {
- int i,a,b;
- while(slow>0)
- {
- a = c ;
- for(i=0 ; i<4 ; i++)
- {
- P2 = 0xff ;
- b = a%10 ;
- a = a/10 ;
- P1 = seg7[b] ;
- P2 = ~(0x01<<i) ;
- delay1ms(1);
- }
- slow-- ;
- }
- }
- main(void)
- {
- S7(count);
- while(1)
- {
- if ((SW&0x10)==0x10) slow = 100;
- else slow = 10;
- if((SW&0x04) == 0x04)
- {
- count = 0;
- S7(count);
- }
- else if((SW&0x08) == 0x08)
- {
- count = count ;
- S7(count);
- }
- else if((SW&0x01) == 0x01)
- {
- if(count == 9999)
- count = 0000;
- else
- count++;
- S7(count);
- }
- else if((SW&0x02) == 0x02)
- {
- if(count == 0)
- count = 9999 ;
- else
- count--;
- S7(count);
- }
- else if((SW&0x04) == 0x04)
- {
- count = 0;
- S7(count);
- }
- }
- }
- -----------------------------------------------------------------------------------------------
- //lab3
- #include <reg51.h>
- // seg7 decode 8bit = (dp)gfedcba
- char code TAB[] = { 0xc0 , 0xf9 , 0xa4 , 0xb0 , 0x99 , // num 0~4
- 0x92 , 0x82 , 0xf8 , 0x80 , 0x98 , // num 5~9
- 0x88 , 0x83 , 0xa7 , 0xa1 , 0x86 , // num AbcdE
- 0x8e , 0xbf , 0x7f };
- unsigned char digit[4] ;
- unsigned char scan[4] = { 0xef , 0xdf , 0xbf , 0x7f }; // seg7 scan line
- void delay1ms(int t)
- {
- int i,j;
- for(j=0 ; j<t ; j++)
- for(i=0 ; i<114 ; i++)
- ;
- }
- void debouncer()
- {
- delay1ms(20);
- }
- void display()
- {
- int i;
- for(i=0 ; i<4 ; i++)
- {
- P2 = 0xff ; //P2 is scanline of seg7
- P1 = digit[i] ; //P1 is data input
- P2 = ~(0x01<<i) ;
- delay1ms(2);
- }
- }
- int main()
- {
- int i;
- unsigned char col,row;
- unsigned char rowkey,kcode ;
- for(i=0; i<4; i++)
- {
- digit[i] = TAB[0] ; //initialize
- }
- while(1)
- {
- for(col=0 ; col<4; col++)
- {
- P3 = scan[col]; //keyboard scan --> use P3 to output signal
- display();
- rowkey = ~P3 & 0x0f ; //P3 is keyboard input at the same time
- if(rowkey != 0)
- {
- debouncer();
- if(rowkey == 0x01) row=0;
- else if(rowkey == 0x02) row=1;
- else if(rowkey == 0x04) row=2;
- else if(rowkey == 0x08) row=3;
- kcode = 4*col+row ;
- digit[3] = digit[2] ;
- digit[2] = digit[1] ;
- digit[1] = digit[0] ;
- digit[0] = TAB[kcode] ;
- while(rowkey != 0)
- rowkey = ~P3 & 0x0f ;
- debouncer();
- }
- display();
- }
- }
- }
- ----------------------------------------------------------------------------------------
- //8x8LED
- /*******************************************************************************
- ;* 標題: 8X8矩陣LED *
- ;* 檔案: 8X8 LED TEST.c *
- ;* 日期: 2007-12-10 *
- ;* Email: [email protected] *
- ;* 網站︰ http://oceansky-technology.com *
- ;********************************************************************************
- ; 74LS244 驅動 *
- ; 測試方式:使用8051及AVR和PIC綜合實驗板(6th) *
- ; 使用方式:S4,S6,S3[7] ON *
- ; 使用方式1:P2接到A3,P1接到A4,並配合S3[7]ON *
- ;********************************************************************************
- ;* 【版權】 Copyright(C)洋天科技有限公司 *
- ;* 【聲明】 此程式僅用於學習,參考請注明版權和作者 *
- ;********************************************************************************/
- #include <REG51.H> /*含8051所有暫存器宣告 */
- #define REPEAT 120
- unsigned char ScanLine=0x80;/* 行掃描信號 */
- char font[4][8] = { {0x22 , 0x24 , 0x28 , 0xF0 , 0x28 , 0x24 , 0x22 , 0x21},
- {0x3C , 0x24 , 0x24 , 0xFF , 0x24 , 0x24 , 0x3C , 0x00},
- {0x18 , 0x18 , 0x18 , 0xFF , 0xFF , 0x18 , 0x18 , 0x18},
- {0x81 , 0x8F , 0x81 , 0xFF , 0x91 , 0x91 , 0x91 , 0x81},
- } ;
- char flash[2][8] =
- {
- {0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF},
- {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00}
- };
- void delay(int x)
- {
- int m,n;
- for(m=0 ; m<x; m++)
- for(n=0 ; n<120 ; n++)
- ;
- }
- void main() /* 主程式 */
- {
- int i,j,k;
- while(1)
- {
- for(i=0 ; i<4 ; i++)
- {
- for(j=0 ; j<REPEAT ; j++) //REPEAT 讓j迴圈持續
- {
- P2=~font[i][k++]; //字
- //P2=~flash[i][k++]; //閃爍
- P1=~ScanLine; /* 輸出行掃描信號 */
- delay(2);
- ScanLine >>= 1; /* 掃描下一行 */
- if(ScanLine==0) /* 已掃描至最後一行? */
- {
- ScanLine=0x80; /* 重設行掃描信號 */
- k=0; /* 重設字型資料指標k=0 */
- }
- }
- }
- };
- } /* main */
Advertisement
Add Comment
Please, Sign In to add comment