| 
 
查看: 31807|回复: 208
 
 | 
【练习】PIC 编程 - LED闪灯 (功力挑战)
  
[复制链接]
 | 
 
 
 | 
 | 
 
这是给有心学PIC MCU的练习功课, 有兴趣的网友可以挑战或试试看... (写成的有加分奖励。) 
 
 
条件: 
1. MCU: PIC 16F628A / 16F877A 
2. Clock: 4MHz~20Mhz  
3. LED1,LED2,LED3, 分别接在Port B.0, B.1, B.2 
4. 编译语言: ASM, C, Basic 都可以  
 
挑战: 
5. 记录你编程所用的时间  
6. 灵活性, 如要更改LED的闪烁Timing,不需要大改就能快速完成。  
 
 
题目 1:(加20)(初级-送分题) 
设计LED 闪灯,  
LED1, LED2,和 LED3 要闪1Hz (on 500ms, off 500ms) 
 
 
题目 2:(加50)(中级) 
设计LED 闪灯,  
LED1 要闪   1Hz , duty cycle 50% (on 500ms  , off 500ms ) 
LED2 要闪 0.5Hz , duty cycle 50% (on 1000ms , off 1000ms) 
LED3 要闪  10Hz , duty cycle 50% (on 50ms   , off 50ms  ) 
 
 
题目 3:(加80)(中高级) 
设计LED 闪灯,  
LED1 要闪   1Hz , duty cycle 5% (on 50ms   , off 950ms ) 
LED2 要闪 0.5Hz , duty cycle 15% (on 300ms  , off 1700ms) 
LED3 要闪  10Hz , duty cycle 10% (on 10ms   , off 90ms  ) 
 
 
写出后, 把源码贴出来, 把hex file链接放出来, 我会放在Simulator 里验收。 
 
注: 3个LED 的闪烁timing是在同一个时间发生, 不是个别的。 
如题目 2:LED1闪一次时, 其实LED3已经闪了10次。 
 
[ 本帖最后由 pic 于 10-11-2007 01:11 PM 编辑 ] |   
 
 
 
 |  
| 
 | 
 
 
 | 
 | 
 
| 
 | 
 | 
 
 
 
 | 
 | 
 
 
发表于 10-11-2007 11:51 AM
|
显示全部楼层
 
 
 
版主,可以换一点条件吗? 
用16f 877a 可以吗?  
 
Question_1(ASM) 
 
list               p=16f628a    ; list directive to define processor 
#include    <p16f628a.inc>    ; processor specific variable definitions 
 
COUNT_1        equ    0x20 
COUNT_2        equ    0x21 
COUNT_3        equ    0x22 
 
;PROGRAM 
 
    org        0x200 
    goto    start 
 
start:    bcf        STATUS,    RP1            ;clear bit 6 in the STATUS register 
            bsf        STATUS, RP0            ;select bank 1 
            movlw    b'00000111'            ;move b'00000001' to register W 
            movwf    TRISB                    ;set RB1,2,3 as output 
            bcf        STATUS,    RP0        ;select bank 0 
            goto    program 
 
;***************************************************************************** 
;program start here 
 
program:    bsf        PORTB,0    ;LED 1 turn on 
                call    delay_500m        ;delay 500ms 
                bcf        PORTB,0    ;LED 1 turn off 
                call    delay_500m 
                bsf        PORTB,1   ;LED 2 turn on 
                call    delay_500m 
                bcf        PORTB,1    ;LED 2 turn off 
                call    delay_500m 
                bsf        PORTB,2    ;LED 3 turn on 
                call    delay_500m 
                bcf        PORTB,2    ;LED 3 turn off 
                call    delay_500m 
 
            goto    program 
 
;***************************************************************************** 
;delay_section 
 
delay_500m:    movlw    d'254'     
                        movwf    COUNT_1 
xc:                    movlw    d'196'     
                        movwf    COUNT_2 
xb:                    movlw    d'2'     
                        movwf    COUNT_3 
xa:                    decfsz    COUNT_3,    1 
                        goto    xa     
                        decfsz    COUNT_2,    1     
                        goto    xb 
                        decfsz    COUNT_1,    1     
                        goto    xc 
    return 
 
        end 
 
QUESTION_2 
 
list        p=16f628a    ; list directive to define processor 
#include    <p16f628a.inc>    ; processor specific variable definitions 
 
COUNT_1        equ    0x20 
COUNT_2        equ    0x21 
COUNT_3        equ    0x22 
 
; PROGRAM 
 
    org        0x200 
    goto    start 
 
start:    bcf        STATUS,    RP1        ;clear bit 6 in the STATUS register 
        bsf        STATUS, RP0        ;select bank 1 
        movlw    b'00000111'        ;move b'00000001' to register W 
        movwf    TRISB            ;set RB1,2,3 as output 
        bcf        STATUS,    RP0        ;select bank 0 
        goto    program 
 
;***************************************************************************** 
;program start here 
 
program:    bsf        PORTB,0 
                call    delay_500m 
                bcf        PORTB,0 
                call    delay_500m 
                bsf        PORTB,1 
                call    delay_1000m 
                bcf        PORTB,1 
                call    delay_1000m 
                bsf        PORTB,2 
                call    delay_50m 
                bcf        PORTB,2 
                call    delay_50m 
 
            goto    program 
 
;***************************************************************************** 
;delay_section 
 
delay_500m:        movlw    d'254'    ;255     
                            movwf    COUNT_1 
xc:                        movlw    d'196'    ;255 
                            movwf    COUNT_2 
xb:                        movlw    d'2'    ;8 
                            movwf    COUNT_3 
xa:                        decfsz    COUNT_3,    1 
                            goto    xa     
                            decfsz    COUNT_2,    1     
                            goto    xb 
                            decfsz    COUNT_1,    1     
                            goto    xc 
                                    return 
 
delay_1000m:    movlw    d'252'         
                    movwf    COUNT_1 
xf:                movlw    d'180'     
                    movwf    COUNT_2 
xe:                movlw    d'6'     
                    movwf    COUNT_3 
xd:               decfsz    COUNT_3,    1 
                    goto    xd     
                    decfsz    COUNT_2,    1     
                    goto    xe 
                    decfsz    COUNT_1,    1     
                    goto    xf 
                          return 
 
delay_50m:       movlw    d'189'         
                        movwf    COUNT_1 
xi:                    movlw    d'26'     
                       movwf    COUNT_2 
xh:                   movlw    d'2'     
                       movwf    COUNT_3 
xg:                   decfsz    COUNT_3,    1 
                    goto    xg     
                    decfsz    COUNT_2,    1     
                    goto    xh 
                    decfsz    COUNT_1,    1     
                    goto    xi 
                    return 
 
        end 
 
QUESTION_3 
 
list        p=16f628a    ; list directive to define processor 
#include    <p16f628a.inc>    ; processor specific variable definitions 
 
COUNT_1        equ    0x20 
COUNT_2        equ    0x21 
COUNT_3        equ    0x22 
 
; PROGRAM 
 
    org        0x200 
    goto    start 
 
start:    bcf        STATUS,    RP1        ;clear bit 6 in the STATUS register 
        bsf        STATUS, RP0        ;select bank 1 
        movlw    b'00000111'        ;move b'00000001' to register W 
        movwf    TRISB            ;set RB1,2,3 as output 
        bcf        STATUS,    RP0        ;select bank 0 
        goto    program 
 
;***************************************************************************** 
;program start here 
 
program:    bsf        PORTB,0 
            call    delay_50m 
            bcf        PORTB,0 
            call    delay_950m 
            bsf        PORTB,1 
            call    delay_300m 
            bcf        PORTB,1 
            call    delay_1700m 
            bsf        PORTB,2 
            call    delay_10m 
            bcf        PORTB,2 
            call    delay_90m 
 
            goto    program 
 
;***************************************************************************** 
;delay_section 
 
delay_1700m:    movlw    d'255'    ;255     
                movwf    COUNT_1 
xc:                movlw    d'196'    ;255 
                movwf    COUNT_2 
xb:                movlw    d'10'    ;8 
                movwf    COUNT_3 
xa:                decfsz    COUNT_3,    1 
                goto    xa     
                decfsz    COUNT_2,    1     
                goto    xb 
                decfsz    COUNT_1,    1     
                goto    xc 
                return 
 
delay_950m:        movlw    d'252'         
                movwf    COUNT_1 
xf:                movlw    d'198'     
                movwf    COUNT_2 
xe:                movlw    d'5'     
                movwf    COUNT_3 
xd:                decfsz    COUNT_3,    1 
                goto    xd     
                decfsz    COUNT_2,    1     
                goto    xe 
                decfsz    COUNT_1,    1     
                goto    xf 
                return 
 
delay_50m:        movlw    d'189'         
                movwf    COUNT_1 
xi:                movlw    d'26'     
                movwf    COUNT_2 
xh:                movlw    d'2'     
                movwf    COUNT_3 
xg:                decfsz    COUNT_3,    1 
                goto    xg     
                decfsz    COUNT_2,    1     
                goto    xh 
                decfsz    COUNT_1,    1     
                goto    xi 
                return 
 
delay_90m:        movlw    d'182'         
                movwf    COUNT_1 
xl:                movlw    d'70'     
                movwf    COUNT_2 
xk:                movlw    d'1'     
                movwf    COUNT_3 
xj:                decfsz    COUNT_3,    1 
                goto    xj     
                decfsz    COUNT_2,    1     
                goto    xk 
                decfsz    COUNT_1,    1     
                goto    xl 
                return 
 
delay_10m:        movlw    d'189'         
                movwf    COUNT_1 
xo:                movlw    d'7'     
                movwf    COUNT_2 
xn:                movlw    d'1'     
                movwf    COUNT_3 
xm:                decfsz    COUNT_3,    1 
                goto    xm     
                decfsz    COUNT_2,    1     
                goto    xn 
                decfsz    COUNT_1,    1     
                goto    xo 
                return 
 
delay_300m:        movlw    d'233'         
                movwf    COUNT_1 
xr:                movlw    d'6'     
                movwf    COUNT_2 
xq:                movlw    d'70'     
                movwf    COUNT_3 
xp:                decfsz    COUNT_3,    1 
                goto    xp 
                decfsz    COUNT_2,    1     
                goto    xq 
                decfsz    COUNT_1,    1     
                goto    xr 
                return 
 
        end 
 
pic版主请看看可以吗? 
还有,要怎样寄hex file 给你? 
我不会   
 
[ 本帖最后由 jason86 于 10-11-2007 12:56 PM 编辑 ] |   
 
 
 
 |  
| 
 | 
 
 
 | 
 | 
 
| 
 | 
 | 
 
 
 
 | 
 | 
 
 
 楼主 |
发表于 10-11-2007 12:54 PM
|
显示全部楼层
 
 
 
回复 #2 jason86 的帖子
不对哦, 3个LED 要同时进行, 并重复, 不是一个一个处理。 
 
在试试看。。。 |   
 
 
 
 |  
| 
 | 
 
 
 | 
 | 
 
| 
 | 
 | 
 
 
 
 | 
 | 
 
 
发表于 10-11-2007 12:58 PM
|
显示全部楼层
 
 
 
回复 #3 pic 的帖子
原来是酱。。。 
等下再试。。。    |   
 
 
 
 |  
| 
 | 
 
 
 | 
 | 
 
| 
 | 
 | 
 
 
 
 | 
 | 
 
 
发表于 10-11-2007 01:12 PM
|
显示全部楼层
 
 
 
list        p=16f628a    ; list directive to define processor 
#include    <p16f628a.inc>    ; processor specific variable definitions 
 
COUNT_1        equ    0x20 
COUNT_2        equ    0x21 
COUNT_3        equ    0x22 
 
; PROGRAM 
 
    org        0x200 
    goto    start 
 
start:    bcf        STATUS,    RP1        ;clear bit 6 in the STATUS register 
            bsf        STATUS, RP0        ;select bank 1 
            movlw    b'00000000'        ;move b'00000001' to register W 
            movwf    TRISB            ;set RB1,2,3 as output 
            bcf        STATUS,    RP0        ;select bank 0 
            goto    program 
 
;***************************************************************************** 
; program start here 
 
program:    movlw    b'00000111' 
                movwf    PORTB 
                call    delay_500m 
                movlw    b'00000000' 
                movwf    PORTB 
                call    delay_500m 
 
            goto    program 
 
;***************************************************************************** 
;delay_section (4Mhz ) 
 
delay_500m:    movlw    d'254'    ;255     
                movwf    COUNT_1 
xc:                movlw    d'196'    ;255 
                movwf    COUNT_2 
xb:                movlw    d'2'    ;8 
                movwf    COUNT_3 
xa:                decfsz    COUNT_3,    1 
                goto    xa     
                decfsz    COUNT_2,    1     
                goto    xb 
                decfsz    COUNT_1,    1     
                goto    xc 
                return 
    end 
 
第一题酱对吗?  
第二题可以给一点tips吗?  
暂时想不到叻  
 
[ 本帖最后由 jason86 于 12-11-2007 10:34 PM 编辑 ] |   
 
评分
- 
查看全部评分
 
 
 
 
 
 |  
| 
 | 
 
 
 | 
 | 
 
| 
 | 
 | 
 
 
 
 | 
 | 
 
 
发表于 10-11-2007 02:16 PM
|
显示全部楼层
 
 
 
原帖由 jason86 于 10-11-2007 01:12 PM 发表   
list        p=16f628a    ; list directive to define processor 
#include        ; processor specific variable definitions 
 
COUNT_1        equ    0x20 
COUNT_2        equ    0x21 
COUNT_3         ...   
 
 
用timer interrupt. 
2 and 3 就可以很容易解决 |   
 
 
 
 |  
| 
 | 
 
 
 | 
 | 
 
| 
 | 
 | 
 
 
 
		
 | 
 | 
 
 
 楼主 |
发表于 10-11-2007 03:44 PM
|
显示全部楼层
 
 
 
 |  
| 
 | 
 
 
 | 
 | 
 
| 
 | 
 | 
 
 
 
 | 
 | 
 
 
发表于 10-11-2007 04:44 PM
|
显示全部楼层
 
 
 
原帖由 pic 于 10-11-2007 10:07 AM 发表   
这是给有心学PIC MCU的练习功课, 有兴趣的网友可以挑战或试试看... (写成的有加分奖励。) 
 
 
条件: 
1. MCU: PIC 16F628A / 16F877A 
2. Clock: 4MHz~20Mhz  
3. LED1,LED2,LED3, 分别接在Port B.0, B ...   
 
 
pic 版主,我也来支持下,请验收。 
 
因为程序有错误,在此删除正确的在第 10 楼。 
 
[ 本帖最后由 rothmans 于 10-11-2007 09:52 PM 编辑 ] |   
 
 
 
 |  
| 
 | 
 
 
 | 
 | 
 
| 
 | 
 | 
 
 
 
 | 
 | 
 
 
 楼主 |
发表于 10-11-2007 06:04 PM
|
显示全部楼层
 
 
 
原帖由 rothmans 于 10-11-2007 04:44 PM 发表   
pic 版主,我也来支持下,请验收。  
是第一题吗? 只有LED1 & LED2 是Flash 1Hz, LED3是off。 
请放源码。(你的Timer 好像是用Timer 0 interupt) 
 
 
目前只有人做第一题,还有第二第三题,应该难不到你们。快! 150积分等等着你们哦!  |   
 
 
 
 |  
| 
 | 
 
 
 | 
 | 
 
| 
 | 
 | 
 
 
 
 | 
 | 
 
 
发表于 10-11-2007 06:38 PM
|
显示全部楼层
 
 
 
原帖由 pic 于 10-11-2007 06:04 PM 发表   
 
是第一题吗? 只有LED1 & LED2 是Flash 1Hz, LED3是off。 
请放源码。(你的Timer 好像是用Timer 0 interupt) 
 
 
目前只有人做第一题,还有第二第三题,应该难不到你们。快! 150积分等等着你们哦!    
 
是的,只有 LED1 & LED2 而已。 
 
这第一题,有三个 LED 一起闪烁,请验收.请问版主是用什么来 simulate 的呢?请指教。 
 
 
 
http://rapidshare.com/files/68708095/ledFlash.hex.html 
 
[ 本帖最后由 rothmans 于 10-11-2007 08:25 PM 编辑 ] |   
 
评分
- 
查看全部评分
 
 
 
 
 
 |  
| 
 | 
 
 
 | 
 | 
 
| 
 | 
 | 
 
 
 
 | 
 | 
 
 
发表于 10-11-2007 06:55 PM
|
显示全部楼层
 
 
 
 |  
| 
 | 
 
 
 | 
 | 
 
| 
 | 
 | 
 
 
 
 | 
 | 
 
 
 楼主 |
发表于 10-11-2007 08:32 PM
|
显示全部楼层
 
 
 
 |  
| 
 | 
 
 
 | 
 | 
 
| 
 | 
 | 
 
 
 
 | 
 | 
 
 
发表于 10-11-2007 08:35 PM
|
显示全部楼层
 
 
 
原帖由 pic 于 10-11-2007 08:32 PM 发表   
 
为什么? 希望不是故意的。  
 
哦!不好意思,一时大意给搞错了。 |   
 
 
 
 |  
| 
 | 
 
 
 | 
 | 
 
| 
 | 
 | 
 
 
 
 | 
 | 
 
 
发表于 10-11-2007 08:36 PM
|
显示全部楼层
 
 
 
 |  
| 
 | 
 
 
 | 
 | 
 
| 
 | 
 | 
 
 
 
 | 
 | 
 
 
 楼主 |
发表于 10-11-2007 08:37 PM
|
显示全部楼层
 
 
 
 |  
| 
 | 
 
 
 | 
 | 
 
| 
 | 
 | 
 
 
 
 | 
 | 
 
 
发表于 10-11-2007 09:24 PM
|
显示全部楼层
 
 
 
 |  
| 
 | 
 
 
 | 
 | 
 
| 
 | 
 | 
 
 
 
 | 
 | 
 
 
发表于 10-11-2007 10:24 PM
|
显示全部楼层
 
 
 
原帖由 pic 于 10-11-2007 08:37 PM 发表   
 
慢慢来, 二和三是有一点难度, 可是一点就破的。   
 
pic 版主,哪个能成功验收的? 
 
[ 本帖最后由 rothmans 于 28-7-2008 06:51 PM 编辑 ] |   
 
 
 
 |  
| 
 | 
 
 
 | 
 | 
 
| 
 | 
 | 
 
 
 
 | 
 | 
 
 
 楼主 |
发表于 10-11-2007 11:35 PM
|
显示全部楼层
 
 
 
 |  
| 
 | 
 
 
 | 
 | 
 
| 
 | 
 | 
 
 
 
 | 
 | 
 
 
发表于 10-11-2007 11:46 PM
|
显示全部楼层
 
 
 
 |  
| 
 | 
 
 
 | 
 | 
 
| 
 | 
 | 
 
 
 
 | 
 | 
 
 
发表于 10-11-2007 11:54 PM
|
显示全部楼层
 
 
 
 |  
| 
 | 
 
 
 | 
 | 
 
| 
 | 
 | 
 
 
 
 
 |   | 
            本周最热论坛帖子
 
 
 
 |