刚学C语言,写了一个追逐式流水灯;自我感觉太繁琐,请高手指点一下,有没有更简单的。
HI-TECH C Compiler for PIC10/12/16 MCUs (Lite Mode) V9.80编译通过
#include<pic.h>
#define uchar unsigned char
#define uint unsigned int
//函数申明
void delay1(uint x);
//=======================================//=============
void main() //主程序
{
uchar x,y,z; //定义变量
x=7;
TRISC=0X00; //C口定义为输出
PORTC=0Xfe; //点亮RCO口LED,为共阳
while(1)
{
PORTC&=0Xfe;
delay1(200);
if(x==0)
{
PORTC=0Xfe;
x=7;
}
if(x==1)
{
for(z=x;z>0;z--)
{
PORTC=PORTC<<1; //显示左移一位
PORTC&=0X03;
PORTC|=0X01;
delay1(200);
}
x--;
}
if(x==2)
{
for(z=x;z>0;z--)
{
PORTC=PORTC<<1; //显示左移一位
PORTC&=0X07;
PORTC|=0X01;
delay1(200);
}
x--;
}
if(x==3)
{
for(z=x;z>0;z--)
{
PORTC=PORTC<<1; //显示左移一位
PORTC&=0X0f;
PORTC|=0X01;
delay1(200);
}
x--;
}
if(x==4)
{
for(z=x;z>0;z--)
{
PORTC=PORTC<<1;
PORTC|=0X01;
PORTC&=0X1f;
delay1(200);
}
x--;
}
if(x==5)
{
for(z=x;z>0;z--)
{
PORTC=PORTC<<1; //显示左移一位
PORTC&=0X3f;
PORTC|=0X01;
delay1(200);
}
x--;
}
if(x==6)
{
for(z=x;z>0;z--)
{
PORTC=PORTC<<1; //显示左移一位
PORTC&=0X7f;
PORTC|=0X01;
delay1(200);
}
x--;
}
if(x==7)
{
for(z=x;z>0;z--)
{
PORTC=PORTC<<1; //显示左移一位
PORTC|=0X01;
delay1(200);
}
x--;
}
}
}
//=======================================//=============
//延时程序
void delay1(uint x) //延时时间为(X)ms
{
uint a,b;
for(a=x;a>0;a--)
for(b=75;b>0;b--); //延时数较准确固定多26us
}
//======================================================================
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
unsigned char OutBuf;
YiWei = 0x00;
OutBuf = 0x00;
while(1)
{
if(OutBuf==0xFF)//循环一周后清零
{
YiWei = 0x00;
OutBuf = 0x00;
}
YiWei <<= 1;
if((OutBuf|YiWei)==OutBuf)
{
OutBuf>>=1;
OutBuf += 0x80;
YiWei = 0x01;
}
PORTC = ~(OutBuf + YiWei>>1);
delay();
}
试试这个可以不
==========code=================
#include<regx51.h>
//hardware configuration
#define LED_PORT P2
#define LED_DDR P2
#define LEDs 0xff //all pins on LED_PORT for output
#define LED_DLY 9000 //cycles to waste
//end hardware configuration
#define LED_CLR(pins) LED_PORT &=~(pins) //clear pins
#define LED_SET(pins) LED_PORT |= (pins) //set pins
#define LED_OUT(pins) LED_DDR &=~(pins) //pins as output
#define LED_IN(pins) LED_DDR |= (pins) //pins as input
#define LED_ON(pins) LED_CLR(pins) //light up leds by clearing them
#define LED_OFF(pins) LED_SET(pins) //turn off leds by setting them
#define OUT(pins) LED_PORT = (pins) //turn on pins
//led sequencies, using shadow variable
#define SEQ_1L(sVal) sVal = (sVal << 1) | ((sVal & 0x80)? 0x01:0x00)
#define SEQ_1R(sVal) sVal = (sVal >> 1) | ((sVal & 0x01)? 0x80:0x00)
#define SEQ_2L(sVal) sVal = (sVal << 1) | ((sVal & 0x80)? 0x00:0x01)
#define SEQ_2R(sVal) sVal = (sVal >> 1) | ((sVal & 0x01)? 0x00:0x80)
void delay(unsigned int cnt)
{
while(cnt--) continue; //waste some time
}
int main(void)
{
unsigned char pattern=0x43; //initial pattern for the leds
//P1=0xfe;
LED_OUT(LEDs); //leds as output
//OUT(pattern);
while(1)
{
OUT(SEQ_2L(pattern)); //output the new pattern
delay(LED_DLY);
}
}
===================================
it was originally written for C51 but can work for pretty much any mcu with minimum changes.
一周热门 更多>