RenHao

MicroController_8051

Dec 4th, 2013
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.53 KB | None | 0 0
  1. //lab1
  2. #include <reg51.h>
  3.  
  4. void delay()
  5. {
  6. int i;
  7.  
  8. for(i=0;i<9999;i++)
  9. ;
  10.  
  11. }
  12.  
  13. void main()
  14. {
  15.  
  16. P0 = 0xA5 ;
  17. delay();
  18.  
  19.  
  20.  
  21. }
  22.  
  23. -----------------------------------------------------------------------------------------------------
  24. //lab2
  25. #include <reg51.h>
  26.  
  27. #define SW (~P3 & 0x1f )
  28.  
  29. unsigned char code seg7[] = {0xc0 , 0xf9 , 0xa4 , 0xb0 ,
  30.                              0x99 , 0x92 , 0x82 , 0xf8 ,   
  31.                              0x80 , 0x90 };
  32.  
  33. unsigned char code scan[4] = {0xf7 , 0xfb , 0xfd , 0xfe};
  34. int slow = 10 ;
  35. unsigned int count =0000 ;
  36.  
  37.  
  38. void delay1ms(int t)
  39. {
  40. int i,j;
  41.  
  42. for(j=0 ; j<t ; j++)
  43.     for(i=0 ; i<100 ; i++)
  44.         ;
  45. }
  46.  
  47. void S7(int c)
  48. {
  49. int i,a,b;
  50.  
  51. while(slow>0)
  52. {
  53. a = c ;
  54. for(i=0 ; i<4 ; i++)
  55. {
  56. P2 = 0xff ;
  57. b = a%10 ;
  58. a = a/10 ;
  59. P1 = seg7[b] ;
  60. P2 = ~(0x01<<i) ;
  61. delay1ms(1);
  62. }
  63. slow-- ;
  64. }  
  65. }
  66.  
  67.  
  68. main(void)
  69. {
  70.     S7(count);
  71.     while(1)
  72.     {
  73.                 if ((SW&0x10)==0x10) slow = 100;
  74.                                 else slow = 10;
  75.                 if((SW&0x04) == 0x04)
  76.                     {
  77.                         count = 0;
  78.                         S7(count);
  79.                     }
  80.                 else if((SW&0x08) == 0x08)
  81.                     {
  82.                         count = count ;
  83.                         S7(count);
  84.                     }
  85.                  
  86.                 else if((SW&0x01) == 0x01)
  87.                     {
  88.                         if(count == 9999)
  89.                             count = 0000;
  90.                         else
  91.                             count++;
  92.                         S7(count);     
  93.                     }
  94.                 else if((SW&0x02) == 0x02)
  95.                     {
  96.                         if(count == 0)
  97.                             count = 9999 ;
  98.                         else
  99.                             count--;
  100.                         S7(count);
  101.                     }
  102.                 else if((SW&0x04) == 0x04)
  103.                     {
  104.                         count = 0;
  105.                         S7(count);
  106.                     }
  107.                
  108.        
  109.     }
  110. }
  111.  
  112. -----------------------------------------------------------------------------------------------
  113. //lab3
  114. #include <reg51.h>
  115.  
  116. // seg7 decode 8bit = (dp)gfedcba
  117. char code TAB[] = { 0xc0 , 0xf9 , 0xa4 , 0xb0 , 0x99 ,  // num 0~4
  118.                     0x92 , 0x82 , 0xf8 , 0x80 , 0x98 ,  // num 5~9
  119.                     0x88 , 0x83 , 0xa7 , 0xa1 , 0x86 ,  // num AbcdE
  120.                     0x8e , 0xbf , 0x7f  };
  121.  
  122. unsigned char digit[4] ;
  123.  
  124. unsigned char scan[4] = { 0xef , 0xdf , 0xbf , 0x7f }; // seg7 scan line
  125.  
  126. void delay1ms(int t)
  127. {
  128.     int i,j;
  129.    
  130.     for(j=0 ; j<t ; j++)
  131.         for(i=0 ; i<114 ; i++)
  132.             ;
  133. }
  134.  
  135.  
  136. void debouncer()
  137. {
  138.     delay1ms(20);
  139. }
  140.  
  141. void display()
  142. {
  143.     int i;
  144.     for(i=0 ; i<4 ; i++)
  145.     {
  146.             P2 = 0xff ;     //P2 is scanline of seg7
  147.             P1 = digit[i] ; //P1 is data input
  148.             P2 = ~(0x01<<i) ;
  149.             delay1ms(2);
  150.     }
  151. }
  152.  
  153. int main()
  154. {
  155.     int i;
  156.     unsigned char col,row;
  157.     unsigned char rowkey,kcode ;
  158.     for(i=0; i<4; i++)
  159.         {
  160.             digit[i] = TAB[0] ;  //initialize
  161.         }
  162.     while(1)
  163.     {
  164.         for(col=0 ; col<4; col++)
  165.         {
  166.         P3 = scan[col];         //keyboard scan --> use P3 to output signal
  167.         display();
  168.         rowkey = ~P3 & 0x0f ;   //P3 is keyboard input at the same time
  169.         if(rowkey != 0)
  170.         {
  171.         debouncer();
  172.         if(rowkey == 0x01) row=0;
  173.         else if(rowkey == 0x02) row=1;
  174.         else if(rowkey == 0x04) row=2;
  175.         else if(rowkey == 0x08) row=3;
  176.         kcode = 4*col+row ;
  177.         digit[3] = digit[2] ;
  178.         digit[2] = digit[1] ;
  179.         digit[1] = digit[0] ;
  180.         digit[0] = TAB[kcode] ;
  181.         while(rowkey != 0)
  182.             rowkey = ~P3 & 0x0f ;
  183.         debouncer();
  184.         }
  185.     display();
  186.     }
  187.        
  188.     }
  189. }
  190. ----------------------------------------------------------------------------------------
  191. //8x8LED
  192. /*******************************************************************************
  193. ;*  標題:  8X8矩陣LED                                                           *
  194. ;*  檔案:  8X8 LED TEST.c                                                     *
  195. ;*  日期:  2007-12-10                                                           *
  196. ;*  Email: [email protected]                                                 *
  197. ;*  網站︰ http://oceansky-technology.com                                       *
  198. ;********************************************************************************
  199. ;                 74LS244 驅動                                                  *
  200. ; 測試方式:使用8051及AVR和PIC綜合實驗板(6th)                                    *  
  201. ; 使用方式:S4,S6,S3[7] ON                                                       *
  202. ; 使用方式1:P2接到A3,P1接到A4,並配合S3[7]ON                                     *
  203. ;********************************************************************************
  204. ;* 【版權】 Copyright(C)洋天科技有限公司                                        *
  205. ;* 【聲明】 此程式僅用於學習,參考請注明版權和作者                               *
  206. ;********************************************************************************/
  207. #include <REG51.H>         /*含8051所有暫存器宣告 */
  208. #define REPEAT 120
  209.  
  210. unsigned char ScanLine=0x80;/* 行掃描信號 */
  211.  
  212. char font[4][8] = { {0x22 , 0x24 , 0x28 , 0xF0 , 0x28 , 0x24 , 0x22 , 0x21},
  213.                     {0x3C , 0x24 , 0x24 , 0xFF , 0x24 , 0x24 , 0x3C , 0x00},
  214.                     {0x18 , 0x18 , 0x18 , 0xFF , 0xFF , 0x18 , 0x18 , 0x18},
  215.                     {0x81 , 0x8F , 0x81 , 0xFF , 0x91 , 0x91 , 0x91 , 0x81},
  216.                     }    ;
  217. char flash[2][8] =
  218.                     {
  219.                     {0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF},
  220.                     {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00}
  221.                     };
  222. void delay(int x)
  223. {
  224. int m,n;
  225. for(m=0 ; m<x; m++)
  226. for(n=0 ; n<120 ; n++)
  227. ;
  228. }
  229.                    
  230. void main()                /* 主程式 */
  231. {  
  232.   int i,j,k;
  233.     while(1)
  234.   {
  235.     for(i=0 ; i<4 ; i++)
  236.     {
  237.         for(j=0 ; j<REPEAT ; j++) //REPEAT 讓j迴圈持續
  238.         {      
  239.         P2=~font[i][k++];       //字
  240.         //P2=~flash[i][k++];    //閃爍
  241.         P1=~ScanLine;        /* 輸出行掃描信號 */
  242.         delay(2);
  243.         ScanLine >>= 1;       /* 掃描下一行 */
  244.         if(ScanLine==0)       /* 已掃描至最後一行? */
  245.           {
  246.             ScanLine=0x80;      /* 重設行掃描信號 */
  247.             k=0;            /* 重設字型資料指標k=0 */
  248.           }
  249.         }
  250.      }
  251.   };
  252. } /* main */
Advertisement
Add Comment
Please, Sign In to add comment