找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 4541|回复: 1
打印 上一主题 下一主题
收起左侧

PIC16F873 C程序(PICC9.8写)

[复制链接]
跳转到指定楼层
楼主
ID:21750 发表于 2012-7-10 13:58 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式

 


 #include <pic.h>
unsigned int i,max,X11=0,X12=0,X13=0,X14=0,X15=0,t=2;
unsigned long  AD_vasule[2],ADOUT,ADOUT_2;
unsigned long timer[5]; //11-15脚输出计数器
unsigned long delay11=0 ;    //模拟输入延时
unsigned long  TIMEALL  ;
unsigned long   DELAY;
#define  TIME   2        //设置时间1秒

void up_on(void) //I/O配置函数
 {
   INTCON=0x00; //关闭所有中断
   ADCON1=0X04; //A/D模块设置AN0 AN1 AN3为模拟输入;其他引脚为数字输出
   TRISC=0XE0;  //设置11-15脚为输出
   RBPU=0;      //RB设置弱上拉
 }
///////////////////
void AD(void)   //A/D配置函数
 {
   ADCS1=1;  //FOSC/32 A/D转换时钟
   ADCS0=0;
   CHS2=0;   //设置AN1为模拟输入
   CHS1=0;
   CHS0=1;
   ADIE=1; //A/D转换中断允许
   PEIE=1; //外围中断允许
   TRISA1=1; //设置RA1 为输入方式
   ADON=1;    //A/D模块开始工作
 }
////////////
void tmint0(void)  //timer0初始化函数
  {
    T0CS=0; //TMR0工作于定时器方式
    PSA=0;  //TMR0用分频
    PS2=0;  //TMR0输入16分频
    PS1=1;
    PS0=1;
    T0IF=0; //清除TMR0 的中断标志
    T0IE=1; //TMR0中断允许

  }


/////////

//////////

void interrupt AD_timer(void)   //A/D和timer中断函数
{
 
    if(T0IF)     //判断timer0是否发生中断
    {
       
         T0IF=0;        //清除中断标志

         if(!RB4&&RB5&&RB6) {max=0; timer[1]=TIMEALL;timer[2]=TIMEALL;timer[3]=TIMEALL; timer[4]=TIMEALL; t=4;}  //控制方式1
         else if(RB4&&!RB5&&RB6){ max=1; timer[2]=TIMEALL;timer[3]=TIMEALL; timer[4]=TIMEALL; 
                                  if(X12!=1){X11=0;X12=1;X13=0;X14=0;X15=0; i=0; timer[0]=0;timer[1]=TIMEALL; }
                                   t=2;
                                  }//控制方式2
         else if(!RB4&&!RB5&&RB6){ max=2; timer[3]=TIMEALL; timer[4]=TIMEALL;
                                   if(X13!=1){X11=0;X12=0;X13=1;X14=0;X15=0; i=0; timer[0]=0;timer[1]=TIMEALL;timer[2]=TIMEALL; }
                                  t=3; }  //控制方式3
         else if(RB4&&RB5&&!RB6) {max=3; timer[4]=TIMEALL;
                                   if(X14!=1){X11=0;X12=0;X13=0;X14=1;X15=0; i=0; timer[0]=0;timer[1]=TIMEALL;timer[2]=TIMEALL;timer[3]=TIMEALL; }
                                   t=4; }  //控制方式4
         else if(!RB4&&RB5&&!RB6) {max=4 ;    //控制方式5
                                    if(X15!=1){X11=0;X12=0;X13=0;X14=0;X15=1; i=0; timer[0]=0;timer[1]=TIMEALL;timer[2]=TIMEALL;timer[3]=TIMEALL;timer[4]=TIMEALL; }
                                   t=5; }                         
         else max=5;
           
       if(timer[0]<TIMEALL)timer[0]++;
         
       if (timer[1]<TIMEALL)timer[1]++;
               
       if (timer[2]<TIMEALL)timer[2]++;
               
       if (timer[3]<TIMEALL)timer[3]++; 
                
       if (timer[4]<TIMEALL)timer[4]++;
                
       if(delay11<DELAY) delay11++;
       else {
                 delay11=0;
                 if(max==0) {i=0;timer[0]=0;}
                 else {
                        if (i<max) i++;
                        else   i=0;
                        if(max<5) timer=0;
                      }
               
            
            }   
     }
  
    if(ADIF)    //判断AD是否发生中断
    {
   
     AD_vasule[0]=ADRESL; //保存AD转换结果低8位
     AD_vasule[1]=ADRESH; //保存AD转换结果高2位
     AD_vasule[1]=AD_vasule[1]<<2;  //A/D结果高2位左移2位
     AD_vasule[0]=AD_vasule[0]>>6;
     ADOUT=AD_vasule[1]+AD_vasule[0];  //转换成10位结果保存
     ADIF=0; //清除中断标志
     ADGO=1; //开始下次转换
    
   }
 
 
}

///////////////////////
//////////////////////
void OUT_DELAY(void)////计算延时
{
      ADOUT_2=ADOUT;
      if(ADOUT_2<10) ADOUT_2=10;
      if(ADOUT_2>=818) ADOUT_2=1024;
      if(max==0) DELAY=TIMEALL*1024*4/(5*ADOUT_2); //控制方式1延时
      else  DELAY= TIMEALL*1024/(t*ADOUT_2);
           
}


////////////////////
void key5(void)     //控制方式1-5


  
  
    
             OUT_DELAY();//计算延时
      
                 
             if((i==0)&&(timer[0]<TIMEALL))
               {
                 RC0=1;
               } 
             if(((i!=0)&&(timer[0]>=TIMEALL)&&(DELAY<=TIMEALL))||((DELAY>TIMEALL)&&(timer[0]>=TIMEALL)))

              {
                RC0=0;
              }
///////////////////////////////////
               if((i==1)&&(timer[1]<TIMEALL))
               {
                 RC1=1;
               } 
             if(((i!=1)&&(timer[1]>=TIMEALL)&&(DELAY<=TIMEALL))||((DELAY>TIMEALL)&&(timer[1]>=TIMEALL)))
               {
                 RC1=0;
               }
//////////////////////////////////////
///////////////////////////////////
               if((i==2)&&(timer[2]<TIMEALL))
               {
                 RC2=1;
               } 
             if(((i!=2)&&(timer[2]>=TIMEALL)&&(DELAY<=TIMEALL))||((DELAY>TIMEALL)&&(timer[2]>=TIMEALL)))
               {
                 RC2=0;
               }
//////////////////////////////////////
///////////////////////////////////
               if((i==3)&&(timer[3]<TIMEALL))
               {
                 RC3=1;
               } 
             if(((i!=3)&&(timer[3]>=TIMEALL)&&(DELAY<=TIMEALL))||((DELAY>TIMEALL)&&(timer[3]>=TIMEALL)))
               {
                 RC3=0;
               }
//////////////////////////////////////  
///////////////////////////////////
               if((i==4)&&(timer[4]<TIMEALL))
               {
                 RC4=1;
               } 
             if(((i!=4)&&(timer[4]>=TIMEALL)&&(DELAY<=TIMEALL))||((DELAY>TIMEALL)&&(timer[4]>=TIMEALL)))
               {
                 RC4=0;
               }

  
}

//////////////////
////////////////////

void main(void)
 
 {
    TIMEALL= 244*TIME;  //计算延时
    up_on(); //I/O配置函数
    AD() ;  //A/D配置函数
    tmint0();  //timer0初始化函数
    ei();     //打开所有中断
   ADGO=1; //设置这位为1开始下次转换
   for(i=1000;i>10;i--); //延时等待稳定
    i=0;
   while(1)
      {
        
         key5() ;    //控制方式1-5
   
      }

 }

 

 

 

 

兼职PIC全系列开发: QQ 122533653

 

16f873.rar (142.2 KB, 下载次数: 25)
[此贴子已经被作者于2012-7-10 13:59:26编辑过]
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享淘帖 顶 踩
回复

使用道具 举报

沙发
ID:59867 发表于 2014-3-21 12:02 | 只看该作者
參考看看
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表