![]() | ![]() | ![]() |
| |||||||
| Micro Controllers Discuss all aspects of micro controllers - building them, coding them, etc. All controllers are welcome - PIC, BASIC, Z8 Encore!, etc. |
![]() |
| | Tools |
| | #1 |
|
Hi, I'm starting like most people doing a blinky program. I am running some code through the mplab sim to see if my program runs properly and to get a feel for the commands. When the program gets to my delay loop it seems to hit a snag. The sim says running.... and doesn't ever make any progress. Here is my code I imagine I am using the wrong registers for my count1 and count 2, or I have overlooked something else. Thanks for taking a look at my code. list p=16f628a #include <p16f628a.inc> __config _CP_OFF & _WDT_OFF & _PWRTE_ON & _BODEN_OFF &_INTRC_OSC_NOCLKOUT & _MCLRE_ON & _LVP_OFF ERRORLEVEL -302 ;*****Set up the Constants**** STATUS equ 03h ;Address of the STATUS register TRISA equ 85h ;Address of the tristate register for port A PORTA equ 05h ;Address of Port A COUNT1 equ 9Ah ;First counter for our delay loops COUNT2 equ 9Bh ;Second counter for our delay loops ;****Set up the port**** bsf STATUS,5 ;Switch to Bank 1 movlw 00h ;Set the Port A pins movwf TRISA ;to output. movlw 85h ;set value of counter movwf 9Ah ;move to counter folder movlw 85h ;set value of counter2 movwf 9Bh ;move value to counter folder bcf STATUS,5 ;Switch back to Bank 0 ;****Turn the LED on**** Start movlw 02h ;Turn the LED on by first putting it movwf PORTA ;into the w register and then on the port call Delay movlw 00h ;Turn the LED off by first putting it movwf PORTA ;into the w register and then on the port call Delay goto Start ;go back to Start and turn LED on again Delay Loop1 decfsz COUNT1,1 ;This second loop keeps the LED goto Loop1 ;turned off long enough for us to decfsz COUNT2,1 ;see it turned off goto Loop1 return ;****End of the program**** end
__________________ jeremy | |
| |
| | #2 |
|
You've used the include file so you don't have to redefine the I/O Try using bsf STATUS,RP0 ;bank1 I've got to run for lunch, but will look at your code when I get back. Also try looking at the Hello World poster on my site (download section) | |
| |
| | #3 |
|
hi jeremy, Dont forget the 'sloooow' execution speed of the program when running in the simulator. Your delays will seem like forever!. Use a Variable and some conditional statements in the program delays. That is when running in sim mode, it skips [dosn't compile] the delays. When you post your code 'select it' and add the CODE quotes from the menu bar, the source code will keep its format. EDIT: another quick look,, you are using register addresses that are in Bank1 [for the counters] also these are defined registers, its not the ideal way. Use General Registers 0x20 up, see Figure 4.2 on the datasheet.
__________________ Eric " Good enough is Perfect " I will NOT answer PM's requesting technical help, please use the Forum PIC tutorials: Nigel's www.winpicprog.co.uk/ Bill's: www.blueroomelectronics.com/ Last edited by ericgibbs; 28th September 2007 at 05:34 PM. | |
| |
| | #4 |
|
wow that was a rush. I didn't think I would be that happy to see the program finally work. I used the suggestions that guys submitted and it works perfectly. I'm almost ready to program this chip. Thanks again and here is the code revised. Code: list p=16f628a
#include <p16f628a.inc>
__config _CP_OFF & _WDT_OFF & _PWRTE_ON & _BODEN_OFF &_INTRC_OSC_NOCLKOUT & _MCLRE_ON & _LVP_OFF
ERRORLEVEL -302
COUNT1 equ 20h ;First counter for our delay loops
COUNT2 equ 21h ;Second counter for our delay loops
bsf STATUS,RP0 ;Switch to Bank 1
movlw 00h ;Set the Port A pins
movwf TRISA ;to output.
bcf STATUS,RP0 ;Switch back to Bank 0
movlw 07h ;turn off comparators
movwf CMCON
Start
movlw 02h ;Turn the LED on by first putting it
movwf PORTA ;into the w register and then on the port
call Delay
movlw 00h ;Turn the LED off by first putting it
movwf PORTA ;into the w register and then on the port
call Delay
goto Start ;go back to Start and turn LED on again
Delay
movlw 85h ;set value of counter
movwf 20h ;move to counter folder
movlw 03h ;set value of counter2
movwf 21h ;move value to counter folder
Loop1
decfsz COUNT1,1 ;This second loop keeps the LED
goto Loop1 ;turned off long enough for us to
decfsz COUNT2,1 ;see it turned off
goto Loop1
return
end
__________________ jeremy Last edited by jeremygaughan; 28th September 2007 at 06:56 PM. | |
| |
| | #5 |
|
Nicely done. Since you've defined your variables COUNT1, COUNT2 you can call them by name. so Code: movwf 21h Code: movwf COUNT2 Code: cblock 0x20 ; start of variable space same as CBLOCK 21h COUNT1 COUNT2 x y z endc Code: COUNT:4 ; block of four COUNT Last edited by blueroomelectronics; 28th September 2007 at 11:50 PM. | |
| |
| | #6 |
|
I've made a few modifications. I like your advice to use variables. I found that turning on a few lights is be about the same a turning on one light just repeating more of the same program. So I made a little stoplight program and a small stoplight with some leds and a capacitor / resistor for the clock. If you have anymore advice I really appreciate it. Thanks again for the guidance. ![]() ![]() Code: list p=16f628a
#include <p16f628a.inc>
__config _CP_OFF & _WDT_OFF & _PWRTE_ON & _BODEN_OFF &_INTRC_OSC_NOCLKOUT & _MCLRE_ON & _LVP_OFF
ERRORLEVEL -302
cblock 20h ;establish variables
x,y,z
endc
bsf STATUS,RP0 ;Switch to Bank 1
movlw 00h ;Set the Port A pins
movwf TRISA ;to output.
bcf STATUS,RP0 ;Switch back to Bank 0
movlw 07h ;turn off comparators
movwf CMCON
Start
movlw 01h ;Turn the red LED on by first putting it
movwf PORTA ;into the w register and then on the port
call Delay
call Delay
movlw 00h ;Turn all LEDs off by first putting it
movwf PORTA ;into the w register and then on the port
movlw 02h ;Turn on green led
movwf PORTA ;
call Delay
call Delay
movlw 00h ;turn off all leds
movwf PORTA
movlw 04h ;turn on yellow led
movwf PORTA
call Delay
movlw 00h ;turn off all leds
movwf PORTA
goto Start ;go back to Start and turn LED on again
Delay
movlw 03h ;set value of counter2
movwf y ;assign to varible y
Loop2
movlw 10h ;set value of counter
movwf x ;assign to varible x
Loop1
decfsz x,1 ;This second loop keeps the LED
goto Loop1 ;on for a moment
decfsz y,1
goto Loop2
return
end
__________________ jeremy | |
| |
| | #7 |
|
hi jeremy, If you use the MPLAB simulator to step thru your program, you may find 'conditional assembly' useful on many occasions when writing programs. Have edited your delay subr and added two lines near the top. It will run in the simulator as it is now. By changing the Sim1 to '0' or '1' you will see the effect, after you assemble/build, as you step thru the program. I would suggest that you give the CBlock variables more meaningful names, x,y,z single character will be difficult to keep track of in your head. eg: del_cnt1, del_cntr2, asc_bfr3 etc... Code: list p=16f628a
#include <p16f628a.inc>
__config _CP_OFF & _WDT_OFF & _PWRTE_ON & _BODEN_OFF &_INTRC_OSC_NOCLKOUT & _MCLRE_ON & _LVP_OFF
ERRORLEVEL -302
;add these two lines of text
Variable Sim1
Sim1 SET .1 ;if '1' its running in the Simulator, else '0'
;
cblock 20h ;establish variables
x,y,z
endc
;Your Source here...........
;test only
lp1: call Delay
goto lp1
;Running a program with long delays in the Simulator can be 'slow'
;by adding a Variable say, Sim1 and setting it to either '0' or '1' and then
;testing that Variable during assembly will direct the assembler to
;generate code that gives a shortened delay for Simulation
;or the required delay for a PIC.
;
;
Delay:
if Sim1 == 0x01 ;assembler conditional assembly
movlw .1 ;this is for the Sim, just 1 loop
else
movlw 03h ;set value of counter2
endif
movwf y ;assign to varible y
Loop2:
if Sim1 == 0x01
movlw .1 ;this is for the Sim, just 1 loop
else
movlw 10h ;set value of counter
endif
movwf x ;assign to varible x
Loop1
decfsz x,1 ;This second loop keeps the LED
goto Loop1 ;on for a moment
decfsz y,1
goto Loop2
return
end
__________________ Eric " Good enough is Perfect " I will NOT answer PM's requesting technical help, please use the Forum PIC tutorials: Nigel's www.winpicprog.co.uk/ Bill's: www.blueroomelectronics.com/ | |
| |
| | #8 |
|
To have an understanding of how we know these tips like CBLOCK, first get familiar with Chapter 4 of the MPASM data sheet http://ww1.microchip.com/downloads/e...Doc/33014J.pdf Last edited by donniedj; 29th September 2007 at 05:38 PM. | |
| |
|
| Tags |
| mplab, running, sim |
| Thread Tools | |
| Display Modes | |
| |
Similar | ||||
| Title | Starter | Forum | Replies | Latest |
| reprog-16f84a - failed | lemonyx | Micro Controllers | 6 | 25th August 2007 04:32 AM |
| Debugging 877A with MPLAB | williB | Micro Controllers | 10 | 7th May 2007 04:53 PM |
| Need help badly on Inchworm and MPLAB | thushy | Micro Controllers | 14 | 11th March 2007 07:05 PM |
| Some Question About Inchworm | Ayne | Micro Controllers | 29 | 7th March 2007 04:08 AM |
| Inchworm project started | williB | Micro Controllers | 69 | 5th March 2007 07:56 PM |