| 
 | 
【练习】PIC 编程 - LED闪灯 (功力挑战)
  
[复制链接]
 | 
 
 
 | 
 | 
 
 
发表于 3-7-2010 11:18 PM
|
显示全部楼层
 
 
 
Q1: 
program LED_Blink_Q1 
 
dim LED_1, LED_2, LED_3 as byte 
 
sub procedure interrupt 
    LED_1 = LED_1 + 1 
    LED_2 = LED_2 + 1 
    LED_3 = LED_3 + 1 
    TMR0 = 96 
    INTCON = 0x20 
end sub 
 
main: 
     OPTION_REG = 0x84 
     ADCON1 = 0x07 
     CMCON = 0x07 
     PORTB = 0x00 
     TRISB = 0x00 
     TMR0 = 96 
     INTCON = 0xa0 
     LED_1 = 0 
     LED_2 = 0 
     LED_3 = 0 
      
while TRUE 
      if (LED_1 > 50) then 
         PORTB = not PORTB 
         LED_1 = 0 
      end if 
wend 
end. |   
 
评分
- 
查看全部评分
 
 
 
 
 
 |  
| 
 | 
 
 
 | 
 | 
 
| 
 | 
 | 
 
 
 
 | 
 | 
 
 
发表于 3-7-2010 11:19 PM
|
显示全部楼层
 
 
 
Q2: 
 
program LED_Blink_Q2 
 
dim LED_1, LED_2, LED_3 as byte 
 
sub procedure interrupt 
    LED_1 = LED_1 + 1 
    LED_2 = LED_2 + 1 
    LED_3 = LED_3 + 1 
    TMR0 = 96 
    INTCON = 0x20 
end sub 
 
main: 
     OPTION_REG = 0x84 
     ADCON1 = 0x07 
     CMCON = 0x07 
     PORTB = 0x00 
     TRISB = 0x00 
     TMR0 = 96 
     INTCON = 0xa0 
     LED_1 = 0 
     LED_2 = 0 
     LED_3 = 0 
      
while TRUE 
      if (LED_1 > 50) then 
         PORTB.0 = not PORTB.0 
         LED_1 = 0 
      end if 
      if (LED_2 > 100) then 
         PORTB.1 = not PORTB.1 
         LED_2 = 0 
      end if 
      if (LED_3 > 5) then 
         PORTB.2 = not PORTB.2 
         LED_3 = 0 
      end if 
wend 
end. |   
 
评分
- 
查看全部评分
 
 
 
 
 
 |  
| 
 | 
 
 
 | 
 | 
 
| 
 | 
 | 
 
 
 
 | 
 | 
 
 
发表于 3-7-2010 11:21 PM
|
显示全部楼层
 
 
 
Q3: 
 
program LED_Blink_Q3 
 
dim LED_1, LED_2, LED_3 as byte 
 
sub procedure interrupt 
    LED_1 = LED_1 + 1 
    LED_2 = LED_2 + 1 
    LED_3 = LED_3 + 1 
    TMR0 = 96 
    INTCON = 0x20 
end sub 
 
main: 
     OPTION_REG = 0x84 
     ADCON1 = 0x07 
     CMCON = 0x07 
     PORTB = 0x00 
     TRISB = 0x00 
     TMR0 = 96 
     INTCON = 0xa0 
     LED_1 = 0 
     LED_2 = 0 
     LED_3 = 0 
      
while TRUE 
       
      if (LED_1 < 5) then 
         PORTB.0 = 1 
         else PORTB.0 = 0 
         if LED_1 >95 then 
            LED_1 = 0 
         end if 
      end if 
       
      if (LED_2 < 30) then 
         PORTB.1 = 1 
         else PORTB.1 = 0 
         if LED_2 >170 then 
            LED_2 = 0 
         end if 
      end if 
       
      if (LED_3 < 1) then 
         PORTB.2 = 1 
         else PORTB.2 = 0 
         if LED_3 >9 then 
            LED_3 = 0 
         end if 
      end if 
wend 
end. |   
 
评分
- 
查看全部评分
 
 
 
 
 
 |  
| 
 | 
 
 
 | 
 | 
 
| 
 | 
 | 
 
 
 
 | 
 | 
 
 
发表于 3-7-2010 11:24 PM
|
显示全部楼层
 
 
 
 |  
| 
 | 
 
 
 | 
 | 
 
| 
 | 
 | 
 
 
 
 | 
 | 
 
 
发表于 15-8-2010 08:17 PM
|
显示全部楼层
 
 
 
 本帖最后由 电子达人 于 16-8-2010 03:14 PM 编辑  
 
哇,这是什么程序语言? sub 和 function 名后面没有()做parameter passing ? 
看起来有点像BASIC... |   
 
 
 
 |  
| 
 | 
 
 
 | 
 | 
 
| 
 | 
 | 
 
 
 
 | 
 | 
 
 
 楼主 |
发表于 17-8-2010 07:44 AM
|
显示全部楼层
 
 
 
PIC大大,刚刚学习PIC,帮我看看可以过关吗? 
chieh2020 发表于 3-7-2010 11:24 PM    
我没有PICBasic, 你可以提供Hex 吗? 或你自己用Proteus 来模拟再贴上来看看? |   
 
 
 
 |  
| 
 | 
 
 
 | 
 | 
 
| 
 | 
 | 
 
 
 
		
 | 
 | 
 
 
发表于 20-8-2010 03:43 PM
|
显示全部楼层
 
 
 
 |  
| 
 | 
 
 
 | 
 | 
 
| 
 | 
 | 
 
 
 
 | 
 | 
 
 
 楼主 |
发表于 21-8-2010 12:34 AM
|
显示全部楼层
 
 
 
我是用Mikrobasic写的。 
以下是我用Proteus7.4 模拟 
 
 
Q3   
chieh2020 发表于 20-8-2010 03:43 PM    
10Hz 的不完美~ |   
 
 
 
 |  
| 
 | 
 
 
 | 
 | 
 
| 
 | 
 | 
 
 
 
 | 
 | 
 
 
发表于 21-8-2010 03:29 AM
|
显示全部楼层
 
 
 
我改了TMR0的prescale。现在是10Hz的了。 
  |   
 
 
 
 |  
| 
 | 
 
 
 | 
 | 
 
| 
 | 
 | 
 
 
 
 | 
 | 
 
 
 楼主 |
发表于 24-8-2010 10:51 AM
|
显示全部楼层
 
 
 
我改了TMR0的prescale。现在是10Hz的了。 
chieh2020 发表于 21-8-2010 03:29 AM    
不错, 很好啊~  |   
 
 
 
 |  
| 
 | 
 
 
 | 
 | 
 
| 
 | 
 | 
 
 
 
 | 
 | 
 
 
发表于 4-9-2010 01:51 PM
|
显示全部楼层
 
 
 
 本帖最后由 电子达人 于 4-9-2010 02:05 PM 编辑  
 
我来试试看。。。。 
第一题:- #include <htc.h>   //hi-tech C header
 
  
- __CONFIG(0x2130);
 
 - #define bitset(var,bitno)((var)|=1UL<<(bitno))
 
 - #define bitclr(var,bitno)((var)&=~(1UL<<(bitno)))
 
 - #define testbit_on(data,bitno) ((data>>bitno)&0x01)
 
 - #define _XTAL_FREQ 4000000 //operating frequency in hz
 
  
- char x=0;
 
  
- void init()
 
 - {
 
 -         x=0;
 
 -         TRISB=0b11111000;
 
 -         PORTB=0;
 
 -         PCON=0B00001000;
 
 - }
 
  
- void main()
 
 - {        
 
 -         
 
 -         init();
 
 -         while(1)
 
 -         {        
 
 -                 __delay_ms(100); //500ms delay is too large
 
 -                 x++;
 
 -                 if(x==5)
 
 -                 {        
 
 -                         x=0;
 
 -                         PORTB=~PORTB;
 
 -                 }
 
 -         }
 
 - }
 
  复制代码 第二题:- #include <htc.h>   //hi-tech C header
 
  
- __CONFIG(0x2130);
 
 - #define bitset(var,bitno)((var)|=1UL<<(bitno))
 
 - #define bitclr(var,bitno)((var)&=~(1UL<<(bitno)))
 
 - #define testbit_on(data,bitno) ((data>>bitno)&0x01)
 
 - #define _XTAL_FREQ 4000000 //operating frequency in hz
 
  
- char x=0,y=0,z=0;
 
  
- void init()
 
 - {
 
 -         x=0;
 
 -         y=0;
 
 -         z=0;
 
 -         TRISB=0b11111000;
 
 -         PORTB=0;
 
 -         PCON=0B00001000;
 
 - }
 
  
- void main()
 
 - {        
 
 -         
 
 -         init();
 
 -         while(1)
 
 -         {        
 
 -                 __delay_ms(50); 
 
 -                 x++,y++,z++;
 
 -                 if(x==10)                //LED1
 
 -                 {        
 
 -                         x=0;
 
 -                         if(testbit_on(PORTB,0)) bitclr(PORTB,0);
 
 -                         else bitset(PORTB,0);
 
 -                 }
 
 -                 if(y==20)                //LED2
 
 -                 {        
 
 -                         y=0;
 
 -                         if(testbit_on(PORTB,1)) bitclr(PORTB,1);
 
 -                         else bitset(PORTB,1);
 
 -                 }
 
 -                 if(z)   //"if" added just for clarity/scalability   //LED3
 
 -                 {        
 
 -                         z=0;
 
 -                         if(testbit_on(PORTB,2)) bitclr(PORTB,2);
 
 -                         else bitset(PORTB,2);
 
 -                 }
 
 -                 
 
 -         }
 
 - }
 
  复制代码 hex file :  
1. 9w2hfr.freedomwhisper.com/files/3 LED flasher(1).hex 
2. 9w2hfr.freedomwhisper.com/files/3 LED flasher (2).hex |   
 
 
 
 |  
| 
 | 
 
 
 | 
 | 
 
| 
 | 
 | 
 
 
 
 | 
 | 
 
 
发表于 4-9-2010 02:10 PM
|
显示全部楼层
 
 
 
 本帖最后由 电子达人 于 4-9-2010 03:34 PM 编辑  
 
第三题的,单用delay,timing也是差不多吧。。。 
看了其他的,我发现到我的我和suyy10的很接近,不一样的是我只用delay。。。懒惰翻查F628Ainterrupt 的 registers 的用法。。。 |   
 
 
 
 |  
| 
 | 
 
 
 | 
 | 
 
| 
 | 
 | 
 
 
 
 | 
 | 
 
 
发表于 4-9-2010 04:17 PM
|
显示全部楼层
 
 
 
第三题:- #include <htc.h>   //hi-tech C header
 
  
- __CONFIG(0x2130);
 
 - #define bitset(var,bitno)((var)|=1UL<<(bitno))
 
 - #define bitclr(var,bitno)((var)&=~(1UL<<(bitno)))
 
 - #define testbit_on(data,bitno) ((data>>bitno)&0x01)
 
 - #define _XTAL_FREQ 4000000 //operating frequency in hz
 
 - #define ledaon  5           //on time and off time for LED A,B,C  respectively
 
 - #define ledaoff   95           
 
 - #define ledbon        30
 
 - #define ledboff 170
 
 - #define ledcon 1
 
 - #define ledcoff 9
 
 - #define delayconst 10         //time multiplication constant for above LED settings
 
 - //delayconst*ledaon= on time of led A in milisecs
 
  
- char x=0,y=0,z=0;
 
  
- void init()
 
 - {
 
 -         x=0;
 
 -         y=0;
 
 -         z=0;
 
 -         TRISB=0b11111000;
 
 -         PORTB=0xff;
 
 -         PCON=0B00001000;
 
 - }
 
  
- void main()
 
 - {        
 
 -         
 
 -         init();
 
 -         while(1)
 
 -         {        
 
 -                 __delay_ms(delayconst); 
 
 -                 x++,y++,z++;
 
 -                 if (x==ledaoff)                 //LED1
 
 -                 {        
 
 -                         x=0;
 
 -                         bitset(PORTB,0);
 
 -                 }
 
 -                 else if((x==ledaon)&& testbit_on(PORTB,0))
 
 -                 {
 
 -                         x=0;
 
 -                         bitclr(PORTB,0);
 
 -                 }
 
 -                 if (y==ledboff)                 //LED2
 
 -                 {        
 
 -                         y=0;
 
 -                         bitset(PORTB,1);
 
 -                 }
 
 -                 else if((y==ledbon) && testbit_on(PORTB,1))
 
 -                 {
 
 -                         y=0;
 
 -                         bitclr(PORTB,1);
 
 -                 }
 
 -                 if (z==ledcoff)     //LED3
 
 -                 {        
 
 -                         z=0;
 
 -                         bitset(PORTB,2);
 
 -                 }
 
 -                 else if((z==ledcon) && testbit_on(PORTB,2))
 
 -                 {
 
 -                         z=0;
 
 -                         bitclr(PORTB,2);
 
 -                 }
 
 -         }
 
 - }
 
  复制代码 hex file:  
3.   9w2hfr.freedomwhisper.com/files/3 LED flasher(3).hex |   
 
 
 
 |  
| 
 | 
 
 
 | 
 | 
 
| 
 | 
 | 
 
 
 
 | 
 | 
 
 
发表于 4-9-2010 04:29 PM
|
显示全部楼层
 
 
 
 本帖最后由 电子达人 于 4-9-2010 04:30 PM 编辑  
 
我的编码是以flexibility和fast code 为重...所以有很多redundant 的部分 ...又没有用到interrupt....请各位见谅...  |   
 
 
 
 |  
| 
 | 
 
 
 | 
 | 
 
| 
 | 
 | 
 
 
 
 | 
 | 
 
 
发表于 4-9-2010 04:46 PM
|
显示全部楼层
 
 
 
 |  
| 
 | 
 
 
 | 
 | 
 
| 
 | 
 | 
 
 
 
 | 
 | 
 
 
发表于 5-9-2010 09:42 AM
|
显示全部楼层
 
 
 
回复 173# 电子达人  
 
 
     
不错。 你的idea很好。 
如果加上interrupt就很perfect的了。。。 |   
 
 
 
 |  
| 
 | 
 
 
 | 
 | 
 
| 
 | 
 | 
 
 
 
 | 
 | 
 
 
发表于 7-9-2010 08:24 AM
|
显示全部楼层
 
 
 
 |  
| 
 | 
 
 
 | 
 | 
 
| 
 | 
 | 
 
 
 
 | 
 | 
 
 
发表于 7-9-2010 09:33 AM
|
显示全部楼层
 
 
 
*我是用 PIC16F877A来测试。 
刚看到各位大大用的Proteus Pro 7.4 ,好像很好玩  
现在下载着Proteus Pro 7.4 ,等下就可以试试Proteus的功力了。  |   
 
 
 
 |  
| 
 | 
 
 
 | 
 | 
 
| 
 | 
 | 
 
 
 
 | 
 | 
 
 
发表于 7-9-2010 11:09 PM
|
显示全部楼层
 
 
 
第一次用这Proteus Pro 7.4 
摸了一整晚,终于摸到了一点点头绪,这个 Proteus Pro 7.4,一个字,赞! 
我第2第3题的output 如下: 
  |   
 
 
 
 |  
| 
 | 
 
 
 | 
 | 
 
| 
 | 
 | 
 
 
 
 | 
 | 
 
 
发表于 7-9-2010 11:18 PM
|
显示全部楼层
 
 
 
 本帖最后由 qweeer 于 7-9-2010 11:20 PM 编辑  
 
我第2题的方法: 
                                                                                 
                Device 16F877A 
                XTAL = 4 
                ALL_DIGITAL = TRUE                                 
                TRISB = %00000000                                  
 
Start:   Gosub LEDB1 
           Goto Start         
 
LEDB2:      PortB.2 = 1              ' 100ms (Total)                                 
                Delayms 50                '  50ms 
                PortB.2 = 0 
                Delayms 50                '  50ms 
                Return 
                 
LEDB2x5:   Gosub LEDB2               ' 500ms (Total) 
                Gosub LEDB2 
                Gosub LEDB2 
                Gosub LEDB2 
                Gosub LEDB2 
                Return 
 
LEDB0:      PortB.0 = 1                '1000ms (Total)                 
                Gosub LEDB2x5          ' 500ms 
                PortB.0 = 0 
                Gosub LEDB2x5          ' 500ms 
                Return                 
 
LEDB1:  PortB.1 = 1               '2000ms (Total) 
           Gosub LEDB0              '1000ms 
           PortB.1 = 0 
           Gosub LEDB0              '1000ms 
           Return |   
 
 
 
 |  
| 
 | 
 
 
 | 
 | 
 
| 
 | 
 | 
 
 
 
 
 |   | 
            本周最热论坛帖子
 
 
 
 |