ap_F23_20090117072204382.jpg 

我做出的第一個51的東西

附上電路圖

七段顯示器 加 串列接收

MAX232 要加 22uF*4

閃爍控制

因為人眼有視覺暫留

所以delay時間只要少於 1 / 16 就是動畫了

所以就直接delay 一下下就完成了

用建表方式去寫一個decode 就馬上少掉7447了

 

#include <x51.h>
char ascii[4] = {0};
int pc_count = 0;
int int0 = 0;
int secclk = 0;
int time[4] = {0,0,0,0}; //time[3] = min, time[2] = Min,time[1] = hour, time[0] = Hour
unsigned char table[] = {0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};  // 0~9;
bit rx_flag;  
bit tx_flag;

void init_rs232(void);
unsigned char decoder(unsigned char time);
void delay(unsigned int time);

/*

串列中斷

ASCII碼要轉成整數就要 - 0X30 就行了

一次是傳四個ASCII碼所以加一個counter就可以完成了

*/

void int_s (void) interrupt 4

 if (RI)
 {
  ET0 = 0;
  
  ascii[pc_count] = SBUF;
  RI = 0;
  rx_flag = 1;
  (int)time[pc_count] = (int)ascii[pc_count] - 0x30;  //ascii to integer    
  pc_count++;
  if (pc_count == 4)
  {
   pc_count = 0;
  }
  ET0 = 1;
 }

}

/*

timer_counter 中斷的使用

0.05秒中斷一次 所以中斷20次就是一秒

*/

void time0_int(void) interrupt 1
{
 TH0 = (65536-50000)/256;
 TL0 = (65536-50000)%256;
 int0++;                //50ms; = 0.05sec;  
 if (int0 == 20)               //1sec;
 {
  int0 = 0;
  secclk++;
 }
 if (secclk == 60) //60sec = 1 min
 {
  secclk = 0;
  time[3]++;
  if (time[3] == 10) //min = 10, Min++
  {
   time[3] = 0;
   time[2]++;
   if (time[2] == 6 && time[3] == 0) //Min = 6, min = 10,hour++;
   {
    time[2] = 0;
    time[3] = 0;
    time[1]++;
    if (time[1] == 10) //hour = 10 , Hour ++
    {
     time[1] = 0;
     time[0]++;
     if (time[0] == 2 && time[1] == 4) //hour = 4,Hour = 2, 
     {
      time[0] = 0;
      time[1] = 0;
     }
    }
   }
  }
 }
}

main(void)
{
 init_rs232();
 while (1)
 {
  unsigned char i;
  unsigned char com;
  com = 0x01;
  if (rx_flag == 1)
  {
   rx_flag = 0;
  }
  else
  {
   for (i = 0;i < 4;i++)
   {
    P0 = ~com; //output to P0  (scan)  
    P1 = decoder(time[i]);
    com = com << 1;
    delay(2);
    P1 = 0xff;
   }
  }
 }
}

/*

設定RS232和time_counter

*/

void init_rs232(void)
{
 TMOD = 0x21; //time1  mod 2, time0 mod 1     
 SCON = 0x50; // SM1 = 1, mod 1, REN = 1
 ET0 = 1; //interrupt time start
 ES = 1; //serial interrupt start
 EA = 1; //inetrrupt start     
 PS = 1; //serial interrupt is first
 TI = 1;
 TI = 0;

 TH0 = (65536-50000)/256; //time0  0.05s
 TL0 = (65536-50000)%256;
 TR0 = 1; //time0 start   

 TH1 = 0xfd; //bps 9600 
 TR1 = 1; //time1 start   
 
}

/*

把抓到的值(整數)變成可以用七段顯示出來的碼(IC 7447的功能)

*/

unsigned char decoder(unsigned char time) //decode output to 7-seg
{
 switch (time)
  {
   case 0 : return table[0];
       break;
   case 1 : return table[1];
       break;
   case 2 : return table[2];
       break;
   case 3 : return table[3];
       break;
   case 4 : return table[4];
       break;
   case 5 : return table[5];
       break;
   case 6 : return table[6];
       break;
   case 7 : return table[7];
       break;
   case 8 : return table[8];
       break;
   case 9 : return table[9];
       break;
   default : return table[0];
       break;
  } 
}

/*

delay 1ms

*/

void delay(unsigned int time) //delay 1ms
{
 unsigned int n;
 while (time > 0)
 {
  n = 120;
  while (n>0) n--;
  time--;
 }
}

arrow
arrow
    全站熱搜
    創作者介紹
    創作者 ffyy99 的頭像
    ffyy99

    喜歡亂搞的世界

    ffyy99 發表在 痞客邦 留言(1) 人氣()