led 灯渐亮程序

//实验目的:通过LED渐亮渐暗实验,熟悉dsPIC30F6014A的简单编程
//软件思路:
//          1、PORTD口的低8位的LED渐亮渐暗流水灯实验
//          2、用TMR1的定时做延时时间
 
//硬件规划:拨码开关S1全置ON,其他拨码开关OFF

#include <p30f6014a.h>             //dsPIC30F6014标准头文件

  _FOSC(CSW_FSCM_OFF & XT_PLL4);  //4倍频晶振,Failsafe 时钟关闭
  _FWDT(WDT_OFF);                 //关闭看门狗定时器
  _FBORPOR(PBOR_OFF & MCLR_EN);   //掉电复位禁止,MCLR复位使能。
  _FGS(CODE_PROT_OFF);            //代码保护禁止


#define LED LATD
#define LED0 _LATD0
#define LED1 _LATD1
#define LED2 _LATD2
#define LED3 _LATD3
#define LED4 _LATD4
#define LED5 _LATD5
#define LED6 _LATD6
#define LED7 _LATD7

#define Hdata 255
#define Ldata  0

union {
      struct {
         unsigned b0:1;
         unsigned b1:1;
         unsigned b2:1;
         unsigned b3:1;
         unsigned b4:1;
         unsigned b5:1;
         unsigned b6:1;
         unsigned b7:1;
         unsigned b8:1;
         unsigned b9:1;
         unsigned b10:1;
         unsigned b11:1;
         unsigned b12:1;
         unsigned b13:1;
         unsigned b14:1;
         unsigned b15:1;
         }LEDBit;
        unsigned int LEDBits;
       } _LED;
#define LED0Bit _LED.LEDBit.b0
#define LED1Bit _LED.LEDBit.b1
#define LED2Bit _LED.LEDBit.b2
#define LED3Bit _LED.LEDBit.b3
#define LED4Bit _LED.LEDBit.b4
#define LED5Bit _LED.LEDBit.b5
#define LED6Bit _LED.LEDBit.b6
#define LED7Bit _LED.LEDBit.b7

#define LED0_pcBit _LED.LEDBit.b8
#define LED1_pcBit _LED.LEDBit.b9
#define LED2_pcBit _LED.LEDBit.b10
#define LED3_pcBit _LED.LEDBit.b11
#define LED4_pcBit _LED.LEDBit.b12
#define LED5_pcBit _LED.LEDBit.b13
#define LED6_pcBit _LED.LEDBit.b14
#define LED7_pcBit _LED.LEDBit.b15

union {
      struct {
         unsigned b0:1;
         unsigned b1:1;
         unsigned b2:1;
         unsigned b3:1;
         unsigned b4:1;
         unsigned b5:1;
         unsigned b6:1;
         unsigned b7:1;
         
         }nextBit;
       unsigned char nextBits;
       } _next;

#define LEDnext1Bit  _next.nextBit.b0
#define LEDnext2Bit  _next.nextBit.b1
#define LEDnext3Bit  _next.nextBit.b2
#define LEDnext4Bit  _next.nextBit.b3
#define LEDnext5Bit  _next.nextBit.b4
#define LEDnext6Bit  _next.nextBit.b5
#define LEDnext7Bit  _next.nextBit.b6
 

 

unsigned  char   TH0,TL0,LEDH0,LEDL0;
unsigned  char   TH1,TL1,LEDH1,LEDL1;
unsigned  char   TH2,TL2,LEDH2,LEDL2;
unsigned  char   TH3,TL3,LEDH3,LEDL3;
unsigned  char   TH4,TL4,LEDH4,LEDL4;
unsigned  char   TH5,TL5,LEDH5,LEDL5;
unsigned  char   TH6,TL6,LEDH6,LEDL6;
unsigned  char   TH7,TL7,LEDH7,LEDL7;

//unsigned  int T1pc;
unsigned  char T1pc;


int main(void)                    //主函数
{unsigned char i;                 //定义一循环变量  
/*设置LED控制寄存器和定时器*/
//初始化LED端口和按键开关端口
LATD = 0x0000;                   //关闭所有LED
TRISD = 0xFF00;                  //设定LED引脚为输出

T1CON = 0;                       //关闭定时器1

PR1 = 200;               //设定TMR1时间 

T1CON = 0x8000;                  //配置定时器1 
//------设定定时器 的 中断优先级并使能中断
_T1IP=2;
_T1IF= 0;
_T1IE=1;
 
LEDL7=LEDL6=LEDL5=LEDL4=LEDL3=LEDL2=LEDL1=LEDL0=Hdata;
LEDH7=LEDH6=LEDH5=LEDH4=LEDH3=LEDH2=LEDH1=LEDH0=Ldata;  
TH7= TH6= TH5= TH4= TH3= TH2= TH1= TH0=Ldata;
TL7= TL6= TL5= TL4= TL3= TL2= TL1= TL0=Hdata; 

TMR1 = 0;                        //启动定时器1的计数寄存器为0
  
  while(1)
  {  
      if(LED0_pcBit)
      {  LED0_pcBit=0; 
         if(LEDH0==32) LEDnext1Bit=1;
         if(LEDH0==64) LEDnext2Bit=1;
         if(LEDH0==96) LEDnext3Bit=1;
         if(LEDH0==128)LEDnext4Bit=1;
         if(LEDH0==160)LEDnext5Bit=1;
         if(LEDH0==192)LEDnext6Bit=1; 
         if(LEDH0==224)LEDnext7Bit=1;
         if(LED0Bit==0){LEDH0++;LEDL0--;if(LEDH0==Hdata)LED0Bit=1;}
         if(LED0Bit==1){LEDH0--;LEDL0++;if(LEDH0==Ldata)LED0Bit=0;} 
      }
 
     if(LEDnext1Bit&&LED1_pcBit)
      {  LED1_pcBit=0; 
         
         if(LED1Bit==0){LEDH1++;LEDL1--;if(LEDH1==Hdata)LED1Bit=1;}
         if(LED1Bit==1){LEDH1--;LEDL1++;if(LEDH1==Ldata) LED1Bit=0;} 
      }

      if(LEDnext2Bit&&LED2_pcBit)
      {  LED2_pcBit=0;
        
         if(LED2Bit==0){LEDH2++;LEDL2--;if(LEDH2==Hdata)LED2Bit=1;}
         if(LED2Bit==1){LEDH2--;LEDL2++;if(LEDH2==Ldata)LED2Bit=0;} 
      }

     if(LEDnext3Bit&&LED3_pcBit)
      {  LED3_pcBit=0;
        
         if(LED3Bit==0){LEDH3++;LEDL3--;if(LEDH3==Hdata)LED3Bit=1;}
         if(LED3Bit==1){LEDH3--;LEDL3++;if(LEDH3==Ldata)LED3Bit=0;} 
      }
     if(LEDnext4Bit&&LED4_pcBit)
      {  LED4_pcBit=0; 
          
         if(LED4Bit==0){LEDH4++;LEDL4--;if(LEDH4==Hdata)LED4Bit=1;}
         if(LED4Bit==1){LEDH4--;LEDL4++;if(LEDH4==Ldata)LED4Bit=0;} 
      }

     if(LEDnext5Bit&&LED5_pcBit)
      {  LED5_pcBit=0;
           
         if(LED5Bit==0){LEDH5++;LEDL5--;if(LEDH5==Hdata)LED5Bit=1;}
         if(LED5Bit==1){LEDH5--;LEDL5++;if(LEDH5==Ldata)LED5Bit=0;} 
      }

      if(LEDnext6Bit&&LED6_pcBit)
      {   LED6_pcBit=0;
        
         if(LED6Bit==0){LEDH6++;LEDL6--;if(LEDH6==Hdata)LED6Bit=1;}
         if(LED6Bit==1){LEDH6--;LEDL6++;if(LEDH6==Ldata)LED6Bit=0;} 
      }

     if(LEDnext7Bit&&LED7_pcBit)
      {  LED7_pcBit=0;
         if(LED7Bit==0){LEDH7++;LEDL7--;if(LEDH7==Hdata)LED7Bit=1;}
         if(LED7Bit==1){LEDH7--;LEDL7++;if(LEDH7==Ldata)LED7Bit=0;} 
    
      } 
   }
   
}

void __attribute__((__interrupt__)) _T1Interrupt(void)
{   _T1IF =0;   //清定时器1溢出中断标志位
    T1pc--;

    if(T1pc==0){LED0_pcBit=LED1_pcBit=LED2_pcBit=LED3_pcBit=LED4_pcBit=LED5_pcBit=LED6_pcBit= LED7_pcBit=1;}
    if(TH0>0){ TH0--; LED0=1;}if(TH0==0&&TL0==0)TL0=LEDL0;
    if(TL0>0){ TL0--; LED0=0;}if(TL0==0&&TH0==0)TH0=LEDH0;

   
    if(TH1>0){ TH1--; LED1=1;}if(TH1==0&&TL1==0)TL1=LEDL1;
    if(TL1>0){ TL1--; LED1=0;}if(TL1==0&&TH1==0)TH1=LEDH1;
    
   
    if(TH2>0){ TH2--; LED2=1;}if(TH2==0&&TL2==0)TL2=LEDL2;
    if(TL2>0){ TL2--; LED2=0;}if(TL2==0&&TH2==0)TH2=LEDH2;

   
    if(TH3>0){ TH3--; LED3=1;}if(TH3==0&&TL3==0)TL3=LEDL3;
    if(TL3>0){ TL3--; LED3=0;}if(TL3==0&&TH3==0)TH3=LEDH3;
    
    
    if(TH4>0){ TH4--; LED4=1;}if(TH4==0&&TL4==0)TL4=LEDL4;
    if(TL4>0){ TL4--; LED4=0;}if(TL4==0&&TH4==0)TH4=LEDH4;

    
    if(TH5>0){ TH5--; LED5=1;}if(TH5==0&&TL5==0)TL5=LEDL5;
    if(TL5>0){ TL5--; LED5=0;}if(TL5==0&&TH5==0)TH5=LEDH5;

    
    if(TH6>0){ TH6--; LED6=1;}if(TH6==0&&TL6==0)TL6=LEDL6;
    if(TL6>0){ TL6--; LED6=0;}if(TL6==0&&TH6==0)TH6=LEDH6;
     
   
    if(TH7>0){ TH7--; LED7=1;}if(TH7==0&&TL7==0)TL7=LEDL7;
    if(TL7>0){ TL7--; LED7=0;}if(TL7==0&&TH7==0)TH7=LEDH7;
   
}
posted on 2012-11-23 14:17  WithYouTh  阅读(939)  评论(0编辑  收藏  举报