第一個單片機程序-----流水燈

#include <reg52.h>

#define SIZE 8

void delay(void);	 				/*function prototype of delay*/
void light_on_led(int num);		    /*function prototype of light_led*/
void flow_water_led(void);	  		/*function prototype of flow_water_led*/

int main (void)
{
	flow_water_led();

  	return 0;
}


/*the function of delay*/
void delay(void)
{
	unsigned int  num = 3000;
	while (num--);
}

/*the function of light_led*/
void light_on_led(int num)
{
	unsigned char led  = 0x00;
	unsigned char temp = 0x01;
	
	temp <<= num-1;		  		/*move the 1*/

	led ^= temp;


	P1 = ~led;		  
}	


/*the function of flow_water_led()*/
void flow_water_led(void)
{
	int i = 0;
	
	while (1){

		while (i < SIZE) {

			light_on_led(i+1);
			delay();
			P1 = 0xff;
			delay();
			i++;
		}

		i--;

		while (i > -1){
			light_on_led(i+1);
			delay();
			P1 = 0xff;
			delay();
			i--;
		}

	}


}
	

說明:

1.程序分爲三個主要部分,點亮任意一個num的燈,延遲,生成流水線。

2.點亮用的移位位取反,總線操作

3.延遲用的while()循環

4.流水線設計爲順序下去,倒序上來。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章