Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

first PIC assembly program

Status
Not open for further replies.

Hank Fletcher

New Member
Hi all! I'm still on my quest to write simultaneously (or either) my first PIC assembly program, and a PIC program that produces an NTSC video signal. I found this post elsewhere on the internet: http://www.rohitab.com/discuss/lofiversion/index.php/t15740.html

It seems to me that it's a little more straight forward than the Rickard's website explanation (which I can't seem to find online anymore, anyway), at least in terms of being able to understand the code. I think I'm almost to the point of being able to understand it, but I still have a few questions.

1. How would I go about modifying the header to use a 16F88 and internal oscillator? I think I can get 4MHz as an internal oscillator option on a 16F88, can't I?

2. Generally, what does "org" do? Specifically, what do these lines in the program do:
Code:
org 0x00
goto Main
?

3. Is the "Main" section of the code outdated/redundant/unnecessarily wordy? It seems to me I was called out for something along this line before, because some part of this is now articulated in the include file? At the end of the day, I believe this part of the code is just switching banks, setting PORTA as an input, and PORTB as an output, and then switching back banks again, correct? What's the best way to do that if this isn't it?

4. I don't really understand what's happening with the goto statements. What's the syntax of "goto" as it relates to this code? I think I remember reading up on what happens, but I'm either confused or have forgotten, and I'm unclear on the use of the $ symbol in this code.

5. I also don't understand what's happening in the "Delay" subroutine, and the use of the _D variable.

I just wanted to add that I think I understand what needs to happen electronically to produce an NTSC signal at this point. I'm just having trouble following the code, and getting to the point where I understand enough of it to comfortably code in assembly. I probably should clarify that the poster of this code did so with the intention of it being used to create a PAL signal, but as it doesn't use any vertical sync (just a continuous stream of horizontal patterns), and with the leeway that, as far as I know, modern NTSC televisions will permit, this shouldn't be a problem. I hope.

Code:
list p=16f84A
#include "P16f84.inc"
__config _CP_OFF & _WDT_OFF & _PWRTE_ON & _XT_OSC

w equ 0
f equ 1
SYNC equ h'00'
BLACK equ b'00010000'
GREY equ b'00100000'
WHITE equ b'00110000'
_D equ h'12'

org 0x00
goto Main

org 0x04
goto Main

_2NOP macro
    nop
    nop
    endm

_6NOP    macro
    nop
    nop
    nop
    nop
    nop
    nop
    endm

Main
    bsf STATUS,5
    movlw h'00'
    movwf TRISB
    movlw h'ff'
    movwf TRISA
    bcf STATUS,5

Line
    movlw SYNC                                      /G16    
    movwf PORTB    ;2µs unused                      /G17
    _2NOP        ;Start 4µs SYNC                  S2
    movlw BLACK                                     S3
    movwf PORTB                                     S4
    _6NOP        ;Start 8µs BlackLevel                B6
    movlw WHITE                                     B7
    movwf PORTB                                     B8
    btfsc PORTA,0     ;Is the button pushed?            W1/2
    goto $+3        ;yes jump 3 lines forward    W3
    movlw h'07'        ;no                        W3
    call Delay        ;25µs white line           W28
    movlw BLACK                                     W4/29
    movwf PORTB                                     W5/30
    btfss PORTA,0    ;is the button pushed?              B1
    goto $+3        ;no jump 3 lines forward       B3
    movlw h'07'        ;yes                       B3
    call Delay                                       B28
    movlw GREY                                   B4/29
    movwf PORTB                                     B5/30
    _6NOP                                               G6
    _6NOP                                              G12
    Nop                                            G13
    goto Line                                      G15
    
Delay
    movwf _D
    decfsz _D
    goto $-1
    return
    
end
 
Last edited:
1. How would I go about modifying the header to use a 16F88 and internal oscillator? I think I can get 4MHz as an internal oscillator option on a 16F88, can't I?
Yes 4 and 8MHz internal are available.

2. Generally, what does "org" do? Specifically, what do these lines in the program do:
Code:
org 0x00
goto Main
?
It instructs the assembler to start the coding, from program counter address 0000

3. Is the "Main" section of the code outdated/redundant/unnecessarily wordy? It seems to me I was called out for something along this line before, because some part of this is now articulated in the include file? At the end of the day, I believe this part of the code is just switching banks, setting PORTA as an input, and PORTB as an output, and then switching back banks again, correct? What's the best way to do that if this isn't it?
The main can be any label/name you choose.

4. I don't really understand what's happening with the goto statements. What's the syntax of "goto" as it relates to this code? I think I remember reading up on what happens, but I'm either confused or have forgotten, and I'm unclear on the use of the $ symbol in this code.
The goto tells the program to load the goto destination address into the program counter and then go there.

5. I also don't understand what's happening in the "Delay" subroutine, and the use of the _D variable.
The -D is just a register label, in this context its used as counter register for the delay routine.

I just wanted to add that I think I understand what needs to happen electronically to produce an NTSC signal at this point. I'm just having trouble following the code, and getting to the point where I understand enough of it to comfortably code in assembly. I probably should clarify that the poster of this code did so with the intention of it being used to create a PAL signal, but as it doesn't use any vertical sync (just a continuous stream of horizontal patterns), and with the leeway that, as far as I know, modern NTSC televisions will permit, this shouldn't be a problem. I hope.

Code:
list p=16f84A
#include "P16f84.inc"
__config _CP_OFF & _WDT_OFF & _PWRTE_ON & _XT_OSC

w equ 0
f equ 1
SYNC equ h'00'
BLACK equ b'00010000'
GREY equ b'00100000'
WHITE equ b'00110000'
_D equ h'12'

org 0x00
goto Main

org 0x04
goto Main

_2NOP macro
    nop
    nop
    endm

_6NOP    macro
    nop
    nop
    nop
    nop
    nop
    nop
    endm

Main
    bsf STATUS,5
    movlw h'00'
    movwf TRISB
    movlw h'ff'
    movwf TRISA
    bcf STATUS,5

Line
    movlw SYNC                                      /G16    
    movwf PORTB    ;2µs unused                      /G17
    _2NOP        ;Start 4µs SYNC                  S2
    movlw BLACK                                     S3
    movwf PORTB                                     S4
    _6NOP        ;Start 8µs BlackLevel                B6
    movlw WHITE                                     B7
    movwf PORTB                                     B8
    btfsc PORTA,0     ;Is the button pushed?            W1/2
    goto $+3        ;yes jump 3 lines forward    W3
    movlw h'07'        ;no                        W3
    call Delay        ;25µs white line           W28
    movlw BLACK                                     W4/29
    movwf PORTB                                     W5/30
    btfss PORTA,0    ;is the button pushed?              B1
    goto $+3        ;no jump 3 lines forward       B3
    movlw h'07'        ;yes                       B3
    call Delay                                       B28
    movlw GREY                                   B4/29
    movwf PORTB                                     B5/30
    _6NOP                                               G6
    _6NOP                                              G12
    Nop                                            G13
    goto Line                                      G15
    
Delay
    movwf _D
    decfsz _D
    goto $-1
    return
    
end

hi,
The $ is the current value of the Program counter.

Does this help.?:)

EDIT: image added for freq select
 

Attachments

  • esp01 Jul. 30.gif
    esp01 Jul. 30.gif
    38 KB · Views: 157
Last edited:
hi Hank,

If you are porting this program to the 16F88, remember the 'free registers' start address at 0x20.

BTW: I would check the stabilty of the internal RC osc, when using for video timing applications.
 
Last edited:
Have you figured out if the internal Osc will work for video. I was told the ceramic osc's were not.

A ceramic resonator is FAR better than the internal RC oscillator, a ceramic one would probably work fine. But even with the internal oscillator it should work anyway, the TV syncs to the incoming signal, so slight variations should make no difference.
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top