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.

im stupid cos i can do this in a 'C program but not in basic

Status
Not open for further replies.
ok b4 you go

it appears that this programme dosnt like being unzipped to a folder on the destop and it dosnt like path names greater than 63 charactors which seems a lot but its not in win2000 server so i put it all in the root of c and it only has one error that is as listed below
Code:
Warning[215] C:\NEW\MYFIRST.ASM 2 : Processor superceded by command line.  Verify processor symbol.
Error[105]   C:\NEW\MYFIRST.ASM 3 : Cannot open file (Include File "P16F628.inc" not found)
Error[113]   C:\NEW\MYFIRST.ASM 10 : Symbol not previously defined (CMCON)
Error[113]   C:\NEW\MYFIRST.ASM 12 : Symbol not previously defined (STATUS)
Error[113]   C:\NEW\MYFIRST.ASM 12 : Symbol not previously defined (RP0)
Error[113]   C:\NEW\MYFIRST.ASM 14 : Symbol not previously defined (TRISB)
Error[113]   C:\NEW\MYFIRST.ASM 15 : Symbol not previously defined (TRISA)
Error[113]   C:\NEW\MYFIRST.ASM 16 : Symbol not previously defined (STATUS)
Error[113]   C:\NEW\MYFIRST.ASM 16 : Symbol not previously defined (RP0)
Error[113]   C:\NEW\MYFIRST.ASM 20 : Symbol not previously defined (PORTA)
Error[113]   C:\NEW\MYFIRST.ASM 21 : Symbol not previously defined (PORTB)
Error[113]   C:\NEW\MYFIRST.ASM 25 : Symbol not previously defined (PORTA)
Error[113]   C:\NEW\MYFIRST.ASM 26 : Symbol not previously defined (PORTB)

i cant find that file either where can i get it from ???
 
My first thought is that you were told to, and presumably did?, download MPASM!. MPASM hasn't been available as a seperate download for a LONG! time - the only seperate version on the MicroChip is probably too old to support the 16F628.

What you should do, and what I do, is download and install the latest version of MPLAB - this includes the modern version of MPASM. I then just use MPASM and ignore MPLAB :)

As you have already found out, MPASM doesn't like long file names, stick to standard DOS 8.3 ones - you don't need to be in the root directory, but only use short directory names.
 
lol

as usual your advise was correct (give me an once of what you have ive not been right about anything for 20yrs in fact now that i mention it since the moment i said i do in church lol) thanks Nigel now i will have to wait to see what williB has planned for me lol and what this piece of code does unless you have an idea???
 
hmmm yes

ok well ive managed to do it well the biggest part of it and although i would like it to be smaller here it is in assembler which i believe is your forte im going to try to make it smaller


Code:
	NOLIST

	#include "C:\DOCUME~1\ADMINI~1\Desktop\3_BUTT~2.PBP" 

	LIST
BO Equ 41
BOH Equ 42
BOHH Equ 43
BOHHH Equ 44
F1_SOF equ $ ; 3_BUTT~2.BAS
F1_000004 equ $ ; in [3_BUTT~2.BAS] ALL_DIGITAL = TRUE
	Movlw 7
	Movwf CMCON
F1_000005 equ $ ; in [3_BUTT~2.BAS] bo = 0                  ' give variable initial value
	Clrf BOHHH
	Clrf BOHH
	Clrf BOH
	Clrf BO
F1_000006 equ $ ; in [3_BUTT~2.BAS] TRISB = 0         ' Set PORTB (LEDs) to all output
	Bsf STATUS,5
ram_bank = 1
	Clrf TRISB
F1_000007 equ $ ; in [3_BUTT~2.BAS] PORTB = 0         ' All LEDs off
	Bcf STATUS,5
ram_bank = 0
	Clrf PORTB
LOOP
F1_000009 equ $ ; in [3_BUTT~2.BAS] PORTA = 0         ' PORTA lines low to read buttons
	Clrf PORTA
F1_000010 equ $ ; in [3_BUTT~2.BAS] TRISA = $F0       ' Enable all buttons
	Movlw 240
	Bsf STATUS,5
ram_bank = 1
	Movwf TRISA
F1_000011 equ $ ; in [3_BUTT~2.BAS] If PORTA.0 = 1 Then     ' If 4th button pressed...
	Bcf STATUS,5
ram_bank = 0
	Btfss PORTA,0
	Goto bc@ll2
F1_000012 equ $ ; in [3_BUTT~2.BAS] bo = bo +1      ' add 1 to count variable
	Movlw 1
	Addwf BO,F
	Clrw
	Btfsc STATUS,0
	Movlw 1
	Addwf BOH,F
	Clrw
	Btfsc STATUS,0
	Movlw 1
	Addwf BOHH,F
	Clrw
	Btfsc STATUS,0
	Movlw 1
	Addwf BOHHH,F
F1_000013 equ $ ; in [3_BUTT~2.BAS] delayms 100
	Movlw 100
	Call dl@ms
F1_000014 equ $ ; in [3_BUTT~2.BAS] endif
bc@ll2
F1_000015 equ $ ; in [3_BUTT~2.BAS] if bo > 6 then
	Clrf PP2HHH
	Clrf PP2HH
	Clrf PP2H
	Movlw 6
	Movwf PP2
	Movfw PP2HHH
	Xorlw 128
	Movwf PP4
	Movfw BOHHH
	Xorlw 128
	Subwf PP4,W
	Btfss STATUS,2
	Goto cp@lb2
	Movfw BOHH
	Subwf PP2HH,W
	Btfss STATUS,2
	Goto cp@lb2
	Movfw BOH
	Subwf PP2H,W
	Btfss STATUS,2
	Goto cp@lb2
	Movfw BO
	Subwf PP2,W
cp@lb2
	Btfsc STATUS,0
	Goto bc@ll4
F1_000016 equ $ ; in [3_BUTT~2.BAS] bo = 6
	Clrf BOHHH
	Clrf BOHH
	Clrf BOH
	Movlw 6
	Movwf BO
F1_000017 equ $ ; in [3_BUTT~2.BAS] Endif
bc@ll4
F1_000018 equ $ ; in [3_BUTT~2.BAS] If PORTA.1 = 1 Then     ' If 3rd button pressed...
	Btfss PORTA,1
	Goto bc@ll6
F1_000019 equ $ ; in [3_BUTT~2.BAS] bo = bo -1
	Movlw 1
	Subwf BO,F
	Clrw
	Btfss STATUS,0
	Movlw 1
	Subwf BOH,F
	Clrw
	Btfss STATUS,0
	Movlw 1
	Subwf BOHH,F
	Clrw
	Btfss STATUS,0
	Movlw 1
	Subwf BOHHH,F
F1_000020 equ $ ; in [3_BUTT~2.BAS] if bo < 0 then
	Clrf PP2HHH
	Clrf PP2HH
	Clrf PP2H
	Clrf PP2
	Movfw BOHHH
	Xorlw 128
	Movwf PP4
	Movfw PP2HHH
	Xorlw 128
	Subwf PP4,W
	Btfss STATUS,2
	Goto cp@lb3
	Movfw PP2HH
	Subwf BOHH,W
	Btfss STATUS,2
	Goto cp@lb3
	Movfw PP2H
	Subwf BOH,W
	Btfss STATUS,2
	Goto cp@lb3
	Movfw PP2
	Subwf BO,W
cp@lb3
	Btfsc STATUS,0
	Goto bc@ll8
F1_000021 equ $ ; in [3_BUTT~2.BAS] bo = 0
	Clrf BOHHH
	Clrf BOHH
	Clrf BOH
	Clrf BO
F1_000022 equ $ ; in [3_BUTT~2.BAS] endif
bc@ll8
F1_000024 equ $ ; in [3_BUTT~2.BAS] endif
bc@ll6
F1_000025 equ $ ; in [3_BUTT~2.BAS] if bo = 0 then
	Movfw BOHHH
	Iorwf BOHH,W
	Iorwf BOH,W
	Iorwf BO,W
	Btfss STATUS,2
	Goto bc@ll10
F1_000026 equ $ ; in [3_BUTT~2.BAS] portb = 0
	Clrf PORTB
F1_000027 equ $ ; in [3_BUTT~2.BAS] endif
bc@ll10
F1_000028 equ $ ; in [3_BUTT~2.BAS] if bo = 1 then
	Movfw BO
	Xorlw 1
	Iorwf BOH,W
	Iorwf BOHH,W
	Iorwf BOHHH,W
	Btfss STATUS,2
	Goto bc@ll12
F1_000029 equ $ ; in [3_BUTT~2.BAS] portb.0 = 1
	Bsf PORTB,0
F1_000030 equ $ ; in [3_BUTT~2.BAS] portb.1 = 0
	Bcf PORTB,1
F1_000031 equ $ ; in [3_BUTT~2.BAS] portb.2 = 0
	Bcf PORTB,2
F1_000032 equ $ ; in [3_BUTT~2.BAS] portb.3 = 0
	Bcf PORTB,3
F1_000033 equ $ ; in [3_BUTT~2.BAS] portb.4 = 0
	Bcf PORTB,4
F1_000034 equ $ ; in [3_BUTT~2.BAS] portb.5 = 0
	Bcf PORTB,5
F1_000035 equ $ ; in [3_BUTT~2.BAS] end if
bc@ll12
F1_000036 equ $ ; in [3_BUTT~2.BAS] if bo = 2 then
	Movfw BO
	Xorlw 2
	Iorwf BOH,W
	Iorwf BOHH,W
	Iorwf BOHHH,W
	Btfss STATUS,2
	Goto bc@ll14
F1_000037 equ $ ; in [3_BUTT~2.BAS] portb.0 = 1
	Bsf PORTB,0
F1_000038 equ $ ; in [3_BUTT~2.BAS] portb.1 = 1
	Bsf PORTB,1
F1_000039 equ $ ; in [3_BUTT~2.BAS] portb.2 = 0
	Bcf PORTB,2
F1_000040 equ $ ; in [3_BUTT~2.BAS] portb.3 = 0
	Bcf PORTB,3
F1_000041 equ $ ; in [3_BUTT~2.BAS] portb.4 = 0
	Bcf PORTB,4
F1_000042 equ $ ; in [3_BUTT~2.BAS] portb.5 = 0
	Bcf PORTB,5
F1_000043 equ $ ; in [3_BUTT~2.BAS] end if
bc@ll14
F1_000044 equ $ ; in [3_BUTT~2.BAS] if bo = 3 then
	Movfw BO
	Xorlw 3
	Iorwf BOH,W
	Iorwf BOHH,W
	Iorwf BOHHH,W
	Btfss STATUS,2
	Goto bc@ll16
F1_000045 equ $ ; in [3_BUTT~2.BAS] portb.0 = 1
	Bsf PORTB,0
F1_000046 equ $ ; in [3_BUTT~2.BAS] portb.1 = 1
	Bsf PORTB,1
F1_000047 equ $ ; in [3_BUTT~2.BAS] portb.2 = 1
	Bsf PORTB,2
F1_000048 equ $ ; in [3_BUTT~2.BAS] portb.3 = 0
	Bcf PORTB,3
F1_000049 equ $ ; in [3_BUTT~2.BAS] portb.4 = 0
	Bcf PORTB,4
F1_000050 equ $ ; in [3_BUTT~2.BAS] portb.5 = 0
	Bcf PORTB,5
F1_000051 equ $ ; in [3_BUTT~2.BAS] end if
bc@ll16
F1_000052 equ $ ; in [3_BUTT~2.BAS] if bo = 4 then
	Movfw BO
	Xorlw 4
	Iorwf BOH,W
	Iorwf BOHH,W
	Iorwf BOHHH,W
	Btfss STATUS,2
	Goto bc@ll18
F1_000053 equ $ ; in [3_BUTT~2.BAS] portb.0 = 1
	Bsf PORTB,0
F1_000054 equ $ ; in [3_BUTT~2.BAS] portb.1 = 1
	Bsf PORTB,1
F1_000055 equ $ ; in [3_BUTT~2.BAS] portb.2 = 1
	Bsf PORTB,2
F1_000056 equ $ ; in [3_BUTT~2.BAS] portb.3 = 1
	Bsf PORTB,3
F1_000057 equ $ ; in [3_BUTT~2.BAS] portb.4 = 0
	Bcf PORTB,4
F1_000058 equ $ ; in [3_BUTT~2.BAS] portb.5 = 0
	Bcf PORTB,5
F1_000059 equ $ ; in [3_BUTT~2.BAS] endif
bc@ll18
F1_000060 equ $ ; in [3_BUTT~2.BAS] if bo = 5 then
	Movfw BO
	Xorlw 5
	Iorwf BOH,W
	Iorwf BOHH,W
	Iorwf BOHHH,W
	Btfss STATUS,2
	Goto bc@ll20
F1_000061 equ $ ; in [3_BUTT~2.BAS] portb.0 = 1
	Bsf PORTB,0
F1_000062 equ $ ; in [3_BUTT~2.BAS] portb.1 = 1
	Bsf PORTB,1
F1_000063 equ $ ; in [3_BUTT~2.BAS] portb.2 = 1
	Bsf PORTB,2
F1_000064 equ $ ; in [3_BUTT~2.BAS] portb.3 = 1
	Bsf PORTB,3
F1_000065 equ $ ; in [3_BUTT~2.BAS] portb.4 = 1
	Bsf PORTB,4
F1_000066 equ $ ; in [3_BUTT~2.BAS] portb.5 = 0
	Bcf PORTB,5
F1_000067 equ $ ; in [3_BUTT~2.BAS] endif
bc@ll20
F1_000068 equ $ ; in [3_BUTT~2.BAS] if bo = 6 then
	Movfw BO
	Xorlw 6
	Iorwf BOH,W
	Iorwf BOHH,W
	Iorwf BOHHH,W
	Btfss STATUS,2
	Goto bc@ll22
F1_000069 equ $ ; in [3_BUTT~2.BAS] portb.0 = 1
	Bsf PORTB,0
F1_000070 equ $ ; in [3_BUTT~2.BAS] portb.1 = 1
	Bsf PORTB,1
F1_000071 equ $ ; in [3_BUTT~2.BAS] portb.2 = 1
	Bsf PORTB,2
F1_000072 equ $ ; in [3_BUTT~2.BAS] portb.3 = 1
	Bsf PORTB,3
F1_000073 equ $ ; in [3_BUTT~2.BAS] portb.4 = 1
	Bsf PORTB,4
F1_000074 equ $ ; in [3_BUTT~2.BAS] portb.5 = 1
	Bsf PORTB,5
F1_000075 equ $ ; in [3_BUTT~2.BAS] end if
bc@ll22
F1_000076 equ $ ; in [3_BUTT~2.BAS] delayms 300
	Movlw 1
	Movwf PP1H
	Movlw 44
	Call dly@w
F1_000077 equ $ ; in [3_BUTT~2.BAS] Goto Loop         ' Do it forever
	Goto LOOP
	END



im sure there is a simpler way of doing it in assembler but im stupid.[/quote]
 
hey no problem i learned that it dosnt like paths longer than 63 charactors how many people know that i wonder lol :D
 
Ricardoco, if you are still interested try this code.


Code:
DEVICE = 16F84A			'ENTER WHAT DEVICE YOU ARE USING
XTAL = 4 				'SAME GOES FOR CRYSTAL

INPUT PORTA				'TELLING THE PIC TO SET PORTA TO INPUT
OUTPUT PORTB			'TELLING THE PIC TO SET PORTB TO OUTPUT
	  
SYMBOL BUTUP = PORTA.0	'DEFINING PORTA.0 TO EQUAL BUTUP 
SYMBOL BUTDN = PORTA.1 	'DEFINING PORTA.1 TO EQUAL BUTDN		  

BUTVAL VAR BYTE			'SETTING BUTVAL VARIABLE TO BYTE LENGTH 

MAIN: 

IF BUTUP = 1 THEN BUTVAL = BUTVAL +1 
IF BUTVAL > 6 THEN BUTVAL = 6 
IF BUTDN = 1 THEN BUTVAL = BUTVAL -1 
IF BUTVAL < 0 THEN BUTVAL = 0 

LEDSON:   	  	   		'SUBROUTINE FOR TURNING ON LEDs

IF BUTVAL > 0 THEN PORTB.0 = 1 
IF BUTVAL >= 1 THEN PORTB.1 = 1 
IF BUTVAL >= 2 THEN PORTB.2 = 1 
IF BUTVAL >= 3 THEN PORTB.3 = 1 
IF BUTVAL >= 4 THEN PORTB.4 = 1 
IF BUTVAL = 5 THEN PORTB.5 = 1 
IF BUTVAL = 0 THEN PORTB = 0 
GOTO MAIN

It compiles fine but I did not have a chance to wire it up and try it.
 
Not to steal anyone's thunder or anything, but here's a quick crash course on starting out an assembly project in MPLAB. I hope you all don't mind the large message. I've also taken the liberty of coding your project. The code is pretty efficient, and is thoroughly commented. Together with the data sheet, I hope it helps your understanding.

Download the latest version of MPLab from Microchip's website. https://www.microchip.com Go to Products / Development Tools then Software.

Run MPLab. Go to the Project menu, select Project Wizard. Select your device, which would be the 16F84 or 16F84A, depending on which exact one it is. When it asks for the Tool Suite, you might get an error for the MPLIB Librarian module. I got this back when I first installed it last summer. In order to "find" this program, use the same path as the other two files listed, and add the filename that is is looking for. Typically this would be "C:\Program Files\MPLAB IDE\MCHIP_Tools\mplib.exe". Give the project a name, such as Volume Buttons, and of course a directory to store it on the hard drive. On the next screen, don't worry about adding or removing any files to/from the project. Just click next. Then Finish.

Go to File / New. It will bring up an Untitled text window. Save this as volume.asm in the project directory you created a few steps ago. In the original project window, labeled Volume Buttons.mcw, right click on Source Files. Click Add. Then select your asm file you just saved.

The following picture is how I generally connect switches. This causes them to function as active low. When the button is not pressed, the PIC input pin sees the +5 volts through the resistor. When the button is pressed, the input pin sees 0v ground, as the 5 volts is dropped across the resistor. This makes sure the input pin sees a signal at all times, and is never floating. The capacitor debounces the switch.



Paste all of the following code into your blank volume.asm. I don't have access to my PICs and programmer tonight, so I had to run it in the simulator. Everything seems to work fine in it. Hopefully it will on the real thing too. :)

Code:
	LIST P=16F84			; list directive to define processor
	ERRORLEVEL -302			; Turn off bank select warnings
	include <p16F84.inc>	; processor specific variable definitions
	__CONFIG _CP_OFF & _WDT_OFF & _PWRTE_ON & _XT_OSC
	; Code protect off, watch dog timer off, power up timer enable, crystal oscillator

COUNTER	equ	0x20			; equate your file (variable) names to memory spaces.
							; See data sheet for valid locations.

	org		0x000			; Reset vector
	goto	Start
	org		0x004			; Interrupt vector
	nop						; Not using interrupts in this program.
	retfie

Start						; Initial startup.  See documentation on I/O ports.
	bcf		STATUS, RP0		; Ensure Bank 0
	clrf	PORTA			; clear data latches
	clrf	PORTB
	bsf		STATUS, RP0		; Select Bank 1
	movlw	0xFF			; All inputs
	movwf	TRISA			; set data direction for Port A.
	clrf	TRISB			; Port B will be all output.
	bcf		STATUS, RP0		; Bank 0.
	clrf	COUNTER			; Make sure counter starts at 0.

Main
	btfss	PORTA, 0		; Test the button on Port A.  If set (high), skip next instruction.
	call	ButtonUp		; This is executed only if A0 is low (button pressed).
	btfss	PORTA, 1
	call	ButtonDown
	goto	Main			; Loop back and test again.

ButtonUp
	movlw	0x06			; Set working register to 6, the highest valid value.
	xorwf	COUNTER, W		; XOR the counter variable with the working register, store result in W.
							; If all bits in COUNTER match 6 (00000110), result is zero. STATUS, Z is auto set.
	btfsc	STATUS, Z		; If the zero bit is clear, then skip the next instruction.
	return					; Otherwise, the counter is at 6 and shouldn't be incremented anymore.
	incf	COUNTER, F		; Increment COUNTER variable by 1, store back into file COUNTER.
	bsf		STATUS, C		; Turn on the carry bit, since the RLF rotates left through the carry bit.
	rlf		PORTB, F		; Shift the bits on PortB left once so the LED bar grows.
	return

ButtonDown
	clrw					; Set working register to 0, the lowest valid value.
	xorwf	COUNTER, W		; The rest is the same idea as above.
	btfsc	STATUS, Z
	return
	rrf		PORTB, F		; Shift the bits right so the LED bar shrinks.
	bcf		STATUS, C		; Clear the carry bit that the rrf just set.
	decf	COUNTER, F		; Decrement COUNTER by 1, store back into file COUNTER.
	return

	end		; Let MPLAB know we're done.
 
wow

i think that people like youself have another way of thinking when i was at college i noticed that a couple of the lads would just think in code (or so it appeared ) unlike me who needed lots of drawings and little bits of paper.
ok will try this out and see what we get hmmm looking at your diagram i think there will be at least one problem, the project board i am using has leds connected to rb0-rb5 and swiches that when pushed give a voltage to ra0-ra3 a 4 switch 6 led arangement ...ok ill have a go and thanks :D

HEY looking at your post times you dont sleep much do you .... nor do i :D
 
getting there

ok ive managed to make it work (both the pulse and the vu type led arrangment) in basic although it seems a bit clunky and there dosnt seem to be anyone who is programming pics in basic on here to help so while i ponder it im going to have a closer look at this assembler and see what is going on with that.. in the mean time if you are good at the basic side of it then let me know and ill pm the code to you as i dont think it is liked when big chunks of code are posted here... and by the way can i say that the people willing to help me on here is fantastic and i thank you all very much this is what forums should be like, most are full of people who well lets just say it is fine if you can speak several languages as long as you dont lose the ability to speak english politely thankfully i ahvnt met anyone on here who suffers with that problem again i cant thank you enough :D :D
 
I don't have a PIC Basic compiler or simulator, so I wouldn't be able to propery try to code it on the fly and be sure my method or code would work, without actually trying it myself. The assembler in MPLAB is free, so I just went with that. I'm quite a night owl on weekends too. :p

The issue of the buttons is very easy to fix. In the Main loop, there is a BTFSS command, which will skip the next instruction of the specified bit of the specified variable is a 1. But the following command will get executed if it a 0. hence "Bit Test F Skip if Set". For use with a switch that gives a high when pressed, simply change the two tests in Main loop to btfsc, "Bit Test F Skip if Clear".

As for drawings and paper, in any but the most trivial cases, they are pretty well necessary. For this little project I pretty well coded it in my head. "Test for pushed button, increase counter to reflect current state, change bit pattern, repeat." Most other things I need a drawing of the circuit, a flowchart or some kind of diagram of what is supposed to happen, maybe a little bit of psuedo code in English of what's supposed to occur for some complex ideas, and convert that to code. Although the majority of the time my coding is just plug and chug test and fix too. 8)
 
well i will get there

your code did work im glad to say but i was very bad and bashed away at the basic and it now works, effectively what i was trying to do was generate a pulse of variable duration between 1 and 5 seconds on port b5 led and show a visable representation of the speed on port b0-b3 led's controled as it turned out not by a pot but by 2 buttons connected to port a0 and port a1. im very proud of this little piece of code as it is my first and as simple as it may be it now has no glitches (i did have some looping issues but ive sorted them now) it is smooth. this is all not to say that im not going to do the assembler, because i am but i just couldnt let this beat me after all it is basic isnt it ,and i can program c to a certain extent in my sleep so this was a matter of pride... :D (I found a basic compiler that had constructs, structure and syntax close that of 'C' proton was its name. now i will be attempting to learn assembler..thanks for you code by the way i can see what is going on in it which will help a great deal cos all the other stuff i looked at just lost me.... :D
 
Status
Not open for further replies.

Latest threads

Back
Top