佳礼资讯网

 找回密码
 注册

ADVERTISEMENT

楼主: pic

【练习】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.

评分

参与人数 1积分 +20 收起 理由
pic + 20 Q1 挑战奖励

查看全部评分

回复

使用道具 举报


ADVERTISEMENT

发表于 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.

评分

参与人数 1积分 +50 收起 理由
pic + 50 Q2 挑战奖励

查看全部评分

回复

使用道具 举报

发表于 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.

评分

参与人数 1积分 +80 收起 理由
pic + 80 Q3 挑战奖励

查看全部评分

回复

使用道具 举报

发表于 3-7-2010 11:24 PM | 显示全部楼层
PIC大大,刚刚学习PIC,帮我看看可以过关吗?
回复

使用道具 举报

发表于 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 来模拟再贴上来看看?
回复

使用道具 举报

Follow Us
发表于 20-8-2010 03:43 PM | 显示全部楼层
我是用Mikrobasic写的。
以下是我用Proteus7.4 模拟
Q1

Q2

Q3
回复

使用道具 举报

 楼主| 发表于 21-8-2010 12:34 AM | 显示全部楼层
我是用Mikrobasic写的。
以下是我用Proteus7.4 模拟


Q3
chieh2020 发表于 20-8-2010 03:43 PM

10Hz 的不完美~
回复

使用道具 举报


ADVERTISEMENT

发表于 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 编辑

我来试试看。。。。
第一题:
  1. #include <htc.h>   //hi-tech C header

  2. __CONFIG(0x2130);
  3. #define bitset(var,bitno)((var)|=1UL<<(bitno))
  4. #define bitclr(var,bitno)((var)&=~(1UL<<(bitno)))
  5. #define testbit_on(data,bitno) ((data>>bitno)&0x01)
  6. #define _XTAL_FREQ 4000000 //operating frequency in hz

  7. char x=0;

  8. void init()
  9. {
  10.         x=0;
  11.         TRISB=0b11111000;
  12.         PORTB=0;
  13.         PCON=0B00001000;
  14. }

  15. void main()
  16. {       
  17.        
  18.         init();
  19.         while(1)
  20.         {       
  21.                 __delay_ms(100); //500ms delay is too large
  22.                 x++;
  23.                 if(x==5)
  24.                 {       
  25.                         x=0;
  26.                         PORTB=~PORTB;
  27.                 }
  28.         }
  29. }
复制代码
第二题:
  1. #include <htc.h>   //hi-tech C header

  2. __CONFIG(0x2130);
  3. #define bitset(var,bitno)((var)|=1UL<<(bitno))
  4. #define bitclr(var,bitno)((var)&=~(1UL<<(bitno)))
  5. #define testbit_on(data,bitno) ((data>>bitno)&0x01)
  6. #define _XTAL_FREQ 4000000 //operating frequency in hz

  7. char x=0,y=0,z=0;

  8. void init()
  9. {
  10.         x=0;
  11.         y=0;
  12.         z=0;
  13.         TRISB=0b11111000;
  14.         PORTB=0;
  15.         PCON=0B00001000;
  16. }

  17. void main()
  18. {       
  19.        
  20.         init();
  21.         while(1)
  22.         {       
  23.                 __delay_ms(50);
  24.                 x++,y++,z++;
  25.                 if(x==10)                //LED1
  26.                 {       
  27.                         x=0;
  28.                         if(testbit_on(PORTB,0)) bitclr(PORTB,0);
  29.                         else bitset(PORTB,0);
  30.                 }
  31.                 if(y==20)                //LED2
  32.                 {       
  33.                         y=0;
  34.                         if(testbit_on(PORTB,1)) bitclr(PORTB,1);
  35.                         else bitset(PORTB,1);
  36.                 }
  37.                 if(z)   //"if" added just for clarity/scalability   //LED3
  38.                 {       
  39.                         z=0;
  40.                         if(testbit_on(PORTB,2)) bitclr(PORTB,2);
  41.                         else bitset(PORTB,2);
  42.                 }
  43.                
  44.         }
  45. }
复制代码
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 | 显示全部楼层
第三题:
  1. #include <htc.h>   //hi-tech C header

  2. __CONFIG(0x2130);
  3. #define bitset(var,bitno)((var)|=1UL<<(bitno))
  4. #define bitclr(var,bitno)((var)&=~(1UL<<(bitno)))
  5. #define testbit_on(data,bitno) ((data>>bitno)&0x01)
  6. #define _XTAL_FREQ 4000000 //operating frequency in hz
  7. #define ledaon  5           //on time and off time for LED A,B,C  respectively
  8. #define ledaoff   95           
  9. #define ledbon        30
  10. #define ledboff 170
  11. #define ledcon 1
  12. #define ledcoff 9
  13. #define delayconst 10         //time multiplication constant for above LED settings
  14. //delayconst*ledaon= on time of led A in milisecs

  15. char x=0,y=0,z=0;

  16. void init()
  17. {
  18.         x=0;
  19.         y=0;
  20.         z=0;
  21.         TRISB=0b11111000;
  22.         PORTB=0xff;
  23.         PCON=0B00001000;
  24. }

  25. void main()
  26. {       
  27.        
  28.         init();
  29.         while(1)
  30.         {       
  31.                 __delay_ms(delayconst);
  32.                 x++,y++,z++;
  33.                 if (x==ledaoff)                 //LED1
  34.                 {       
  35.                         x=0;
  36.                         bitset(PORTB,0);
  37.                 }
  38.                 else if((x==ledaon)&& testbit_on(PORTB,0))
  39.                 {
  40.                         x=0;
  41.                         bitclr(PORTB,0);
  42.                 }
  43.                 if (y==ledboff)                 //LED2
  44.                 {       
  45.                         y=0;
  46.                         bitset(PORTB,1);
  47.                 }
  48.                 else if((y==ledbon) && testbit_on(PORTB,1))
  49.                 {
  50.                         y=0;
  51.                         bitclr(PORTB,1);
  52.                 }
  53.                 if (z==ledcoff)     //LED3
  54.                 {       
  55.                         z=0;
  56.                         bitset(PORTB,2);
  57.                 }
  58.                 else if((z==ledcon) && testbit_on(PORTB,2))
  59.                 {
  60.                         z=0;
  61.                         bitclr(PORTB,2);
  62.                 }
  63.         }
  64. }
复制代码
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 | 显示全部楼层

评分

参与人数 1积分 +100 收起 理由
fritlizt + 100 加分奖励

查看全部评分

回复

使用道具 举报

发表于 5-9-2010 09:42 AM | 显示全部楼层
回复 173# 电子达人


   
不错。 你的idea很好。
如果加上interrupt就很perfect的了。。。
回复

使用道具 举报


ADVERTISEMENT

发表于 7-9-2010 08:24 AM | 显示全部楼层
各位大大与版主,不好意思,刚刚才看到这里有这样的练习,感觉很有兴趣,就进来看看了。看了题目,就开始第2和第三题,大概用了我1个小时左右吧!

我是用 PIC Basic 的,总觉得Basic 比较容易。
我是用hardware来测试的,用眼睛看 led 闪乐,应该没有问题吧。
我是用很容易明白但是很愚蠢的方法来做的,希望可以过关
迟来的作品:
http://rapidshare.com/files/417524505/LED2.hex
http://rapidshare.com/files/417525435/LED3.hex
请多多指教
回复

使用道具 举报

发表于 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
回复

使用道具 举报

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

本版积分规则

 

ADVERTISEMENT



ADVERTISEMENT



ADVERTISEMENT

ADVERTISEMENT


版权所有 © 1996-2023 Cari Internet Sdn Bhd (483575-W)|IPSERVERONE 提供云主机|广告刊登|关于我们|私隐权|免控|投诉|联络|脸书|佳礼资讯网

GMT+8, 19-4-2024 10:01 PM , Processed in 0.066624 second(s), 22 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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