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.

HOW to

Status
Not open for further replies.

zhaniko93

New Member
Hello and happy new year to all
I have faced such a problem: (pic16f84a assembly)
I want to save 3 digit (in binary, in decimal 0-7) numbers in registers and I have lot of them, so each register for each of them will not be enough. I decided to do so:
in one register (REG1), save first 3 digits for first one, (0-2), second (3-5) to second digit and last two digits of REG1 and first digit of REG2 to the third one. in conclusion, I need 3 registers for 8 numbers which is enought but I want this to be done automatically. I mean, I will call something, which will save number which is in W in appropriate place, (so that I will need 3 registers for 8 numbers) and than when I call another thing, it must return the n'th number I saved. I'm stuck. can anyone help me?
 
Hi,

Well cannot quiet follow your question / description, so can I ask you about what you are trying to do.

First the actual numbers your are trying to save - they start at Decimal 0 and go up to what Decimal number ? - 999 ?

Second, where are these numbers coming from in the first place.

There are various ways data can be addesssed and stored in memory , some by using register names or by direct memory addressing etc.
If we can understand your numbers a bit better we can perhaps suggest the best way to handle them.
 
Well, I want my microcontroller to generate random number from 0 to 4 (0, 1, 2, 3 or 4), then light the appropriate LED and save the number. then, after doing so n times, (generating and turning led on for about 0.5 sec), the "user" has to click appropriate button and microcontroller must compare if the correct button was clicked and than wait for the next button to be clicked. (I have 5 LED and 5 Button connected to 16f84a). for example:
light LED 2
turn LED 2 off
light LED 0
turn LED 0 off
light LED 4
turn LED 4 off
light LED 0
turn LED 0 off
light LED 1
turn LED 1 off
the sequence (2 0 4 0 1) must be saved and then user must click Button 2, then button 0, then button 4, then button 0 and 1. if the user clicks correctly, the number of flashes increaces (ie. becomes 6) and then 6 times is generated, flashed and then clicked and so on. if user fails t click the correct button, it starts form generating 3 numbers again. the problems are:
1) how to generate number from 0 to 4
2) how to save them in registers so that 1 register is used for 2 numgers (I guessed it, it is done by indirect addressing and I want to know if there exists any other way). thats all
 
Hi,

Well there are many software ways to generate a random number - just google for them. think there are some in this forum.

The random number result is simply saved in a user register called - numbers - or whatever you want, this is simply copied over to the Output port to turn on your leds.
Movf numbers,W
Movwf PORT,B

You can then use the file numbers as the reference to check against as the keys are pressed.
 
Here you go, untested and straight of the top of my head so may need debugging.

Code:
		cblock	0x20
ListStart:8
ListCount
Temp
		endc

ResetList	clrf	ListCount
		return

StoreNumber	movwf	Temp
		rrf	ListCount,W
		addlw	ListStart
		movwf	FSR
		incf	ListCount,F
		btfss	ListCount,0
		goto	IsUpper
		movfw	Temp
		movwf	INDF
		return
IsUpper		swapf	Temp,W
		iorwf	INDF
		movwf	INDF
		return	

RetrieveNumber	movwf	Temp
		rrf	Temp,W
		addlw	ListStart
		movwf	FSR
		movfw	INDF
		btfss	Temp,0
		swapf	INDF,W
		andlw	0x0f
		return

Call with the random number in W to store and the Offset in W to retrieve.

Edit, you can have the task of commenting it.

Mike.
 
Last edited:
I wrote everything and in ISIS works perfect but in reality doesn't.
Code:
     #define L1 PORTB, 2
     #define L2 PORTB, 1
     #define L3 PORTB, 0
     #define L4 PORTA, 1
     #define L5 PORTA, 3

    #define S1 PORTB, 5
    #define S2 PORTB, 6
    #define S3 PORTB, 7
    #define S4 PORTA, 0
    #define S5 PORTA, 4


   
     LIST P=16F84A, R=DEC    ; Use the PIC16F628 and decimal system
     #include "P16F84A.INC"  ; Include header file

        __config  _CP_OFF & _PWRTE_ON & _WDT_OFF & _HS_OSC 
	errorlevel -302
        CBLOCK 0x0C 
           DelayTMP1  ;Delay-shi sachiro 
           DelayTMP2 
           DelayTMP  ;Delay-shi sachiro           
           Level  ;Leveli
           TMP1 ;Action-shi sachiroa!
           SPEC ;Sachiroa: 0-statusia 5-ze meti iyo tu ara.
           LAST_TMP
        ENDC

      ;AQ FRTXILAD!
      movlw 0x13
      movwf FSR

        ;TRIS Register Configdeba Sesabamisad!
         BANKSEL TRISA
         movlw b'10001'
         movwf TRISA
         movlw b'11111000'
         movwf TRISB
         clrf INTCON         

         movlw b'00010000'
         movwf OPTION_REG ;1KK Instruqcia wamshi

        BANKSEL PORTA
        clrf PORTA
        clrf PORTB
        movlw 4
        movwf Level
         movwf LAST_TMP
         movlw 0xFF
         movwf DelayTMP1
         movwf DelayTMP2


Starter bsf L1
   bsf L2
   bsf L3
   bsf L4
   bsf L5
   btfsc S1
     goto MMMM
   btfsc S2
   goto MMMM
   btfsc S3
   goto MMMM
   btfsc S4
   goto MMMM
   btfsc S5
   goto MMMM
   goto Starter


MMMM 
   bcf L1
   bcf L2
   bcf L3
   bcf L4
   bcf L5
   movlw 4
call Delay
   movfw Level
   movwf LAST_TMP
      movlw 0x13
      movwf FSR
      clrf SPEC
   
Main decfsz LAST_TMP, f
   goto Action ;Antebs naturas da inaxavs M-shi
ZUZUZUZUZ   movf LAST_TMP, f
   btfsc STATUS, Z
   goto UserTime
   goto Main

;//////////////////////////////////////////////////////////////////////////////////////////         
;//////////////////////////////////////////////////////////////////////////////////////////         
;//////////////////////////////////////////////////////////////////////////////////////////         




Action  movfw TMR0
   movwf TMP1

Act_   movfw TMP1
   sublw 4
   btfsc STATUS, Z
   call LED_5
   


   movfw TMP1
   sublw 3
   btfsc STATUS, Z
   call LED_4


   movfw TMP1
   sublw 2
   btfsc STATUS, Z
   call LED_3


   movfw TMP1
   sublw 1
   btfsc STATUS, Z
   call LED_2
   

   movfw TMP1
   sublw 0
   btfsc STATUS, Z
   call LED_1


   btfsc  SPEC, 0
   incf FSR, f
   btfss  SPEC, 0
   call TMRMinus4
   btfss  SPEC, 0
   goto Act_
   
   ;AQ GVAQ ROMELIME LED-i
   movlw 0x1
   call Delay
   bcf L1
   bcf L2
   bcf L3
   bcf L4
   bcf L5
   movlw 0x1
   call Delay
   
   bcf SPEC, 0
   

   goto ZUZUZUZUZ
   
;//////////////////////////////////////////////////////////////////////////////////////////         
;//////////////////////////////////////////////////////////////////////////////////////////   ;/////////////////////////////////////////////////////////////////////////////////////////         

Delay movwf DelayTMP
JMI decfsz DelayTMP1, f
   goto JMI
   movlw 0xFF
   movwf DelayTMP1
   decfsz DelayTMP2, f
   goto JMI
   movlw 0xFF
   movwf DelayTMP2
   decfsz DelayTMP, f
   goto JMI
   
   return



UserTime movfw Level
   movwf LAST_TMP
   decf LAST_TMP, f
   movlw 0x13;Reset
   movwf FSR

KLL   btfsc S1
   call Switch_1
   


   btfsc S2
   call Switch_2


   btfsc S3
   call Switch_3


   btfsc S4
   call Switch_4


   btfsc S5
   call Switch_5

;CHECK IF ANY WAS CHECKED
   btfss SPEC, 1
   goto KLL

   bcf SPEC, 1
;romeligacas daechira


   decf LAST_TMP, f
   btfsc STATUS, Z
   call NextLevel

   btfsc SPEC, 2
   goto MMMM ;TU BOLOA MIVDIVART MAINSHI!
   goto KLL

Check btfss STATUS, Z
    goto WAAGE
    return






NextLevel  incf Level, f
   bsf SPEC, 2
   movlw 3
   call Delay
   return



LED_5  bsf L5
   bsf SPEC, 0
   movlw 5
   movwf INDF
   return

LED_4  bsf L4
   bsf SPEC, 0
   movlw 4
   movwf INDF
   return

LED_3  bsf L3
   bsf SPEC, 0
   movlw 3
   movwf INDF
   return

LED_2  bsf L2
   bsf SPEC, 0
   movlw 2
   movwf INDF
   return

LED_1  bsf L1
   bsf SPEC, 0
   movlw 1
   movwf INDF
   return

TMRMinus4 
   movfw TMP1
   sublw 5
   movwf TMP1
   comf TMP1, f
   incf TMP1, f
   return



Switch_1 
   movlw 1
   subwf INDF, f
   call Check ;Sworea
   bsf SPEC, 1
   incf FSR, f

   bsf L1
   movlw 0x3
   call Delay
   bcf L1

   return

Switch_2 
   movlw 2
   subwf INDF, f
   call Check ;Sworea
   bsf SPEC, 1
   incf FSR, f

   bsf L2
   movlw 0x3
   call Delay
   bcf L2

   return

Switch_3
   movlw 3
   subwf INDF, f
   call Check ;Sworea
   bsf SPEC, 1
   incf FSR, f

   bsf L3
   movlw 0x3
   call Delay
   bcf L3

   return

Switch_4
   movlw 4
   subwf INDF, f
   call Check ;Sworea
   bsf SPEC, 1
   incf FSR, f

   bsf L4
   movlw 0x3
   call Delay
   bcf L4

   return

Switch_5 
   movlw 5
   subwf INDF, f
   call Check ;Sworea
   bsf SPEC, 1
   incf FSR, f

   bsf L5
   movlw 0x3
   call Delay
   bcf L5

   return


WAAGE 
   movlw 4
   movwf Level
   bsf L1
   bsf L2
   bsf L3
   bsf L4
   bsf L5
   movlw 3
   call Delay
   goto Starter


   END


CODE doesn't matter too much because when I plug in 5v, it starts blinking random LED's immediately, I haven't pressed any button. the circuit is absolutely the same and the result is the same(I mean, the voltage 1.67 on pin) I haven't a clue why is it so and probably thats the reason why in reality PIC thinks that I have pressed that button. see the pic:
 

Attachments

  • untitled.JPG
    untitled.JPG
    241.5 KB · Views: 165
P.S. I have set TRIS register correctly and don't know whats wrong
 
from what i see there are no pull down resistors. The switches are floating and are random right now..

set the OPTION register pin 7 bit to high

Code:
         movlw 0x80
         movwf OPTION
 
Last edited:
there ARE pull-down resistors on the lower side (10k each). follow the line from the pins and you'll see
 
ah... didnt notice that

Um... if it works in ISIS i dont see another reason for it not to work in reality... it has to be some missed or wrong wiring then
 
Last edited:
AtomSoft
why? I don't want pull-ups.
 
yes, but why is 1.67v in ISIS? I have pull-down and the pin is input...
 
we cant see the actual value of that DC signal you should use the POWER and GND symbols
Can you post the isis file?
 
Last edited:
there are two power sources, one 5v and second 0v and 0v is considered to be ground, so there must be 0v between them as the pin is set to input and the switch is off
 
here they are:
 

Attachments

  • JaKo 16F84A.zip
    14.4 KB · Views: 114
  • jako.HEX
    1.3 KB · Views: 152
yes it is, I'm confused
 
Status
Not open for further replies.

Latest threads

Back
Top