#循循渐进学51单片机#实例练习与经验累积#not.9

1、掌握不同类型变量转换的规则与字节操作进行位修改的技巧。

unsigned char a;unsigned int b;unsigned int c;

1)自动类型转换

2)强制类型转换C = (unsigned long)a*bl; C = (unsigned long)(a*b);

3)不同类型变量的赋值b = a ;a = b;b = ? a = ?

4)bit型强制类型转换 bit a = 0;unsigned char b ; a = (bit)b; a =?\

定时器定时世界调整

单片机进出中断都需要响应时间,并且需要进行压栈。

方法一:使用debug观察时间,进行补偿

方法二:长时间运行累积误差进行调整

字节操作修改

对于一个字节的任何一位来说,不管原来这是一位是1还是0

它跟0进行与运算,结果是0,跟1进行与计算,保持原来的值

它跟1进行或运算,结果就是1,跟0进行或运算,保持原来的值

#循循渐进学51单片机#实例练习与经验累积#not.9_第1张图片

2.PWM的实质,尝试控制不同的小灯实现不同节奏的呼吸灯效果。

#include 

sbit led = P0^0;
sbit addr0 = P1^0;
sbit addr1 = P1^1;
sbit addr2 = P1^2;
sbit addr3 = P1^3;
sbit ENLED = P1^4;
unsigned char HighRH = 0;  //¸ßµçƽÖØÔØÖµµÄ¸ß×Ö½Ú
unsigned char HighRL = 0;  //¸ßµçƽÖØÔØÖµµÄµÍ×Ö½Ú
unsigned char LowRH  = 0;  //µÍµçƽÖØÔØÖµµÄ¸ß×Ö½Ú
unsigned char LowRL  = 0;  //µÍµçƽÖØÔØÖµµÄµÍ×Ö½Ú
unsigned char T1RH = 0;    //T1ÖØÔØÖµµÄ¸ß×Ö½Ú
unsigned char T1RL = 0;    //T1ÖØÔØÖµµÄµÍ×Ö½Ú
 unsigned long PeriodCnt; 
void ContPvm(unsigned int arr,unsigned char arr1);
void ConfigTimer1(unsigned int time);
	void ClosePWM();
void main()
{
	unsigned int i = 0;
	
	EA = 1;
	addr0 = 0;
	addr1 = 1;
	addr2 = 1;
	addr3 = 1;
	ENLED = 0;
	while(1)
	{
	ContPvm(100,30);
	ConfigTimer1(50);
		while(1);
	}
}
void ContPvm(unsigned int arr,unsigned char arr1)
{
	    unsigned int  high, low;
     
			PeriodCnt = (11059200 / 12) / arr;
			high = (PeriodCnt*arr1) / 100;
	    low  = PeriodCnt - high;
	   high = 65536 - high + 12;       //¼ÆËã¸ßµçƽµÄ¶¨Ê±Æ÷ÖØÔØÖµ²¢²¹³¥ÖжÏÑÓʱ
     low  = 65536 - low  + 12; 
	    HighRH = (unsigned char)(high >> 8);
			HighRL = (unsigned char)high;
			LowRH = (unsigned char)(low >> 8);
	    LowRL = (unsigned char)low;
			TMOD = 0x01;
	    TH0 = HighRH;
			TL0 = HighRL;
	    ET0 = 1;
	    TR0 = 1;
	    led = 1;
}
void ConfigTimer1(unsigned int time)
{
	unsigned int tmp;
	tmp = 11059200 / 12;
	tmp = (tmp*time) /1000;
	tmp = tmp + 12;
	T1RH = (unsigned char)(tmp>>8);
	T1RL = (unsigned char)tmp;
	TMOD &= 0x0f;
	TMOD |= 0x01;
	TH1 = T1RH;
	TL1 = T1RL;
	ET1 = 1;
	TR1 = 1;
}
void InterruptTimer0() interrupt 1
{
	if( led == 1)
	{
			 TH0 = LowRH;
		   TL0 = LowRL;
			led = 0;
	}
	else 
	{
	   TH0 = HighRH;
			TL0 = HighRL;
		led = 1;
	}
}
void AdjustDutyCycle(unsigned char dc)
{
	unsigned int high,low;
 	   high = (PeriodCnt*dc) / 100;
	    low  = PeriodCnt - high;
	   high = 65536 - high + 12;       //¼ÆËã¸ßµçƽµÄ¶¨Ê±Æ÷ÖØÔØÖµ²¢²¹³¥ÖжÏÑÓʱ
     low  = 65536 - low  + 12; 
	    HighRH = (unsigned char)(high >> 8);
			HighRL = (unsigned char)high;
			LowRH = (unsigned char)(low >> 8);
	    LowRL = (unsigned char)low;
}
void InterruptTimer1() interrupt 3
{
static bit dir = 0;
    static unsigned char index = 0;
    unsigned char code table[13] = {  //Õ¼¿Õ±Èµ÷Õû±í
        5, 18, 30, 41, 51, 60, 68, 75, 81, 86, 90, 93, 95
    };
  TH1 = T1RH;
	TL1 = T1RL;
		AdjustDutyCycle(table[index]); //µ÷ÕûPWMµÄÕ¼¿Õ±È
		if(dir == 1)
		{
			index++;
   if(index <= 12)
	 {
	 dir = 1;
	 }		 
			}
		else
		{
		index--;
			if(index == 0)
			{
			dir = 0;
			}
		
		}
}


3、实现数码管计时和流水灯同时显示的效果。

#include 

sbit ADDR0 = P1^0;
sbit ADDR1 = P1^1;
sbit ADDR2 = P1^2;
sbit ADDR3 = P1^3;
sbit ENLED = P1^4;
bit time1 = 0;
bit time2 = 0;
unsigned char code LedChar[] = {  //ÊýÂë¹ÜÏÔʾ×Ö·ûת»»±í
    0xC0, 0xF9, 0xA4, 0xB0, 0x99, 0x92, 0x82, 0xF8,
    0x80, 0x90, 0x88, 0x83, 0xC6, 0xA1, 0x86, 0x8E
};
unsigned char LedBuff[7] = {  //ÊýÂë¹Ü+¶ÀÁ¢LEDÏÔʾ»º³åÇø£¬³õÖµ0xFFÈ·±£Æô¶¯Ê±¶¼²»ÁÁ
    0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF
};
void 	FlowingLight();
void ShowCount();
void main()
{
	EA = 1;
	ENLED = 0;
	ADDR3 =1;
	TMOD = 0x01;
	TH0 = 0xfc;
	TL0 = 0x01;
	ET0 = 1;
	TR0 = 1;
	while(1)
{
	if(time1 == 1)
	{
	time1= 0;
	FlowingLight();
	  }
	if(time2 == 1)
	{
	time2 = 0;
  ShowCount();
	  }
}
}
void 	FlowingLight()
{
    unsigned char i = 0;
		static unsigned char shift = 0x01;  
		if(i == 1)
		{
		shift = shift << 1;
			P0 = ~shift;
			if(shift == 0x80)
			{
			i = 0;
}
			if(i == 0)
			{
			shift = shift >> 1;
			P0 = ~shift;
			if(shift == 0x00)
			{
			i = 1;
}
			}	
		}
}
 void ShowCount()
 {
     char i; 
	   unsigned char buf[6];
	   static unsigned long sec = 0;
	   sec++;
	  buf[0] = sec%10;  //½«sec°´Ê®½øÖÆλ´ÓµÍµ½¸ßÒÀ´ÎÌáÈ¡µ½bufÊý×éÖÐ
    buf[1] = sec/10%10;
    buf[2] = sec/100%10;
    buf[3] = sec/1000%10;
    buf[4] = sec/10000%10;
    buf[5] = sec/100000%10;
	 for(i=0;i<=5;i++)
	 {
	 if( buf[i] == 0)
		LedBuff[i] = 0xff;
	 else
		 break;
	 }
	 for( ;i>=0;i--)
	 {
	  LedBuff[i] = LedChar[buf[i]];
	 }
 }
 void InterruptTimer0() interrupt 1
 {
	 static unsigned char i = 0;   //¶¯Ì¬É¨ÃèµÄË÷Òý
    static unsigned char cnt200 = 0;  //200ms¶¨Ê±
    static unsigned int cnt1000 = 0;  //1000ms¼´1s¶¨Ê± 
	 TH0 = 0xfc;
	 TL0 = 0x01;
	 cnt200++;
	 if(cnt200 == 200)
	 {
	 time1 = 1;
		 	cnt200 = 0; 
	 }
 	 if(cnt1000 == 1000)
	 {
	 time2 = 1;
		cnt1000 = 0; 
	 }
     P0 = 0xFF;   //ÏÔʾÏûÒþ
    switch (i)
    {
        case 0: ADDR2=0; ADDR1=0; ADDR0=0; i++; P0=LedBuff[0]; break;
        case 1: ADDR2=0; ADDR1=0; ADDR0=1; i++; P0=LedBuff[1]; break;
        case 2: ADDR2=0; ADDR1=1; ADDR0=0; i++; P0=LedBuff[2]; break;
        case 3: ADDR2=0; ADDR1=1; ADDR0=1; i++; P0=LedBuff[3]; break;
        case 4: ADDR2=1; ADDR1=0; ADDR0=0; i++; P0=LedBuff[4]; break;
        case 5: ADDR2=1; ADDR1=0; ADDR0=1; i++; P0=LedBuff[5]; break;
        case 6: ADDR2=1; ADDR1=1; ADDR0=0; i=0; P0=LedBuff[6]; break;
        default: break;
    }
 }


4、学会长短按键的用法,独立把本节课的程序全部写出来。

#include 

sbit BUZZ  = P1^6;
sbit ADDR3 = P1^3;
sbit ENLED = P1^4;
sbit KEY_IN_1  = P2^4;
sbit KEY_IN_2  = P2^5;
sbit KEY_IN_3  = P2^6;
sbit KEY_IN_4  = P2^7;
sbit KEY_OUT_1 = P2^3;
sbit KEY_OUT_2 = P2^2;
sbit KEY_OUT_3 = P2^1;
sbit KEY_OUT_4 = P2^0;

unsigned char code LedChar[] = {  //ÊýÂë¹ÜÏÔʾ×Ö·ûת»»±í
    0xC0, 0xF9, 0xA4, 0xB0, 0x99, 0x92, 0x82, 0xF8,
    0x80, 0x90, 0x88, 0x83, 0xC6, 0xA1, 0x86, 0x8E
};
unsigned char LedBuff[7] = {  //ÊýÂë¹Ü+¶ÀÁ¢LEDÏÔʾ»º³åÇø
    0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF
};
unsigned char code KeyCodeMap[4][4] = { //¾ØÕó°´¼ü±àºÅµ½±ê×¼¼üÅ̼üÂëµÄÓ³Éä±í
    { 0x31, 0x32, 0x33, 0x26 }, //Êý×Ö¼ü1¡¢Êý×Ö¼ü2¡¢Êý×Ö¼ü3¡¢ÏòÉϼü
    { 0x34, 0x35, 0x36, 0x25 }, //Êý×Ö¼ü4¡¢Êý×Ö¼ü5¡¢Êý×Ö¼ü6¡¢Ïò×ó¼ü
    { 0x37, 0x38, 0x39, 0x28 }, //Êý×Ö¼ü7¡¢Êý×Ö¼ü8¡¢Êý×Ö¼ü9¡¢Ïòϼü
    { 0x30, 0x1B, 0x0D, 0x27 }  //Êý×Ö¼ü0¡¢ESC¼ü¡¢  »Ø³µ¼ü¡¢ ÏòÓÒ¼ü
};
unsigned char KeySta[4][4] = {  //È«²¿¾ØÕó°´¼üµÄµ±Ç°×´Ì¬
    {1, 1, 1, 1},  {1, 1, 1, 1},  {1, 1, 1, 1},  {1, 1, 1, 1}
};
unsigned long pdata KeyDownTime[4][4] = {  //ÿ¸ö°´¼ü°´ÏµijÖÐøʱ¼ä£¬µ¥Î»ms
    {0, 0, 0, 0},  {0, 0, 0, 0},  {0, 0, 0, 0},  {0, 0, 0, 0}
};
bit enBuzz = 0;    
bit flag1s = 0;     
bit flagStart = 0;  
unsigned char T0RH = 0;  
unsigned char T0RL = 0;  
unsigned int  CountDown = 0; 

void ConfigTimer0(unsigned int ms);
void ShowNumber(unsigned long num);
void KeyDriver();
void main()
{ 
	EA = 1;
	ADDR3 = 1;
	ENLED = 0;
	TMOD = 0x01;
	 ConfigTimer0(1); 
    ShowNumber(0);  
	while(1)
	{
	KeyDriver(); 
		if(flagStart && flag1s)
		{
		flag1s = 0;
			if(CountDown > 0)
			{
			CountDown--;
			ShowNumber(CountDown);	
				if(CountDown == 0)
				{
				enBuzz = 1;
				LedBuff[6] = 0x00;	
				}
			}
		}
	}	
}
void ConfigTimer0(unsigned int ms)  
{
  unsigned long tmp;
	tmp = 11059200 /12;
	tmp = (tmp*ms) / 1000;
	tmp = 65536 - tmp;
	tmp = tmp + 12;
	T0RH = (unsigned char)(tmp>>8);
	T0RL = (unsigned char)tmp;
	  TMOD &= 0xF0;   //ÇåÁãT0µÄ¿ØÖÆλ
    TMOD |= 0x01;   //ÅäÖÃT0Ϊģʽ1
    TH0 = T0RH;     //¼ÓÔØT0ÖØÔØÖµ
    TL0 = T0RL;
    ET0 = 1;        //ʹÄÜT0ÖжÏ
    TR0 = 1;        //Æô¶¯T0
}
void ShowNumber(unsigned long num)
{
 unsigned char i;
	unsigned char buf[6];
	unsigned char sec = 0;
	sec++;
	for(i = 0;i<=5;i++)
	{
	buf[i] = sec & 10;
		num = num / 10;
	}
	for(i = 0;i<=5;i++)
	{
	if(buf[i] == 0xff)
	{
	LedBuff[i] = 0xff;
	}
	else
		break;
	}
	for( ; i>= 0;i--)
	{
	  LedBuff[i] = LedChar[buf[i]];
	}
}
void KeyAction(unsigned char keycode)
{
	if(keycode == 0x26)
	{
	      if (CountDown < 9999)  //×î´ó¼Æʱ9999Ãë
        {
            CountDown++;
            ShowNumber(CountDown);
        }
	}
	else if(keycode == 0x28)
	{
	if(CountDown >1)
	{
          	CountDown--;
            ShowNumber(CountDown);
	}
}
	else if(keycode == 0x0d)
	{
	if(CountDown >1)
	{
          flagStart = 1;
	}
}
	else if(keycode == 0x1B)
	{
	if(CountDown >1)
	{
          flagStart = 0;
					enBuzz = 0;
		      LedBuff[8] = 0;
					CountDown = 0;
		  ShowNumber(CountDown);
	}
}
}
void KeyDriver()
{
   unsigned char i, j;
    static unsigned char pdata backup[4][4] = {  //°´¼üÖµ±¸·Ý£¬±£´æÇ°Ò»´ÎµÄÖµ
        {1, 1, 1, 1},  {1, 1, 1, 1},  {1, 1, 1, 1},  {1, 1, 1, 1}
    };
    static unsigned long pdata TimeThr[4][4] = {  //¿ìËÙÊäÈëÖ´ÐеÄʱ¼äãÐÖµ
        {1000, 1000, 1000, 1000},  {1000, 1000, 1000, 1000},
        {1000, 1000, 1000, 1000},  {1000, 1000, 1000, 1000}
    };
		for(i = 0;i<4;i++)
		{
		for(j = 0;j<4;j++)
			{
			if(backup[i][j] != KeySta[i][j])
			{
			  if(backup[i][j] != 0)
			{
			KeyAction(KeyCodeMap[i][j]);
			}
			backup[i][j] = KeySta[i][j];
			}
			  if(KeyDownTime[i][j] > 0)
			{
			if(KeyDownTime[i][j] >= KeySta[i][j])
			{
			KeyAction(KeyCodeMap[i][j]);
				TimeThr[i][j] += 200;
			}
				else
			{
				TimeThr[i][j] = 1000;
			}
		}
			
			}
		}
}
void KeyScan()
{    
	unsigned char i;
    static unsigned char keyout = 0;   
    static unsigned char keybuf[4][4] = {  
        {0xFF, 0xFF, 0xFF, 0xFF},  {0xFF, 0xFF, 0xFF, 0xFF},
        {0xFF, 0xFF, 0xFF, 0xFF},  {0xFF, 0xFF, 0xFF, 0xFF}
    };
    keybuf[keyout][0] = (keybuf[keyout][0]<<1) | KEY_IN_1;
		keybuf[keyout][1] = (keybuf[keyout][1]<<1) | KEY_IN_2;
		keybuf[keyout][2] = (keybuf[keyout][2]<<1) | KEY_IN_3;
		keybuf[keyout][3] = (keybuf[keyout][3]<<1) | KEY_IN_4;
		for(i = 0;i<4;i++)
		{
		if((keybuf[keyout][i] & 0x0f) == 0x00)
		{
		KeySta[keyout][i] = 0;
	  KeyDownTime[keyout][i] += 4;	
		}
		else if((keybuf[keyout][i] & 0x0f) == 0x0f)
		{
		  KeySta[keyout][i] = 1;
      KeyDownTime[keyout][i] = 0;
		}
		}
		keyout++;
		keyout &= 0x03;
		switch (keyout)  
    {
        case 0: KEY_OUT_4 = 1; KEY_OUT_1 = 0; break;
        case 1: KEY_OUT_1 = 1; KEY_OUT_2 = 0; break;
        case 2: KEY_OUT_2 = 1; KEY_OUT_3 = 0; break;
        case 3: KEY_OUT_3 = 1; KEY_OUT_4 = 0; break;
        default: break;
    }
}
void LedScan()
{
static unsigned char i = 0; 
	P0 = 0xff;
	P1 = (P1 & 0xf8)| i;
	P0 = LedBuff[i];
 if(i<6)
	i++;
else
	i = 0;
}
void InterruptTimer0() interrupt 1
{
  static unsigned int tmr1s = 0; 
   TH0 = T0RH;    
    TL0 = T0RL;
	if (enBuzz)   
        BUZZ = ~BUZZ;  
    else
        BUZZ = 1;      
    LedScan();   
    KeyScan();
		if(flagStart)
			{
			tmr1s++;
		if(tmr1s == 1000)
		{
		tmr1s = 0;
		flag1s = 1;
		}	
		}
			else
			{
			tmr1s = 0;
			}
}

你可能感兴趣的:(51学习记录,51单片机,嵌入式硬件,单片机,笔记,学习方法)