Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

convert binary on pic port to progress bar

Status
Not open for further replies.

kgavionics

Full Member level 3
Full Member level 3
Joined
Jun 12, 2012
Messages
167
Helped
7
Reputation
14
Reaction score
11
Trophy points
1,298
Location
Alberta.Canada
Visit site
Activity points
2,482
hi
i made a little project based on pic 16f8777a to convert a analog pot movement (from zero to 5v) to binary value on 8 led attached to portd. Everything works pretty fine and i want to do a modification that when i rotate the pot the led lights up linearly like a progress bar.
i use assembler to write my program and i use proteus to simulate before implementing the project on breadboard. i have a little problem here because obviously my code doesn't work.Here's the routine that displays the led on portd:

Sort
movlw 0x1f
movwf reg
bsf STATUS,C
movf ADRESH,W
subwf reg,1
btfsc STATUS,C
goto display0
movlw 0x20
movwf reg
bsf STATUS,C
movf ADRESH,W
subwf reg,1
btfsc STATUS,C
goto display1
movlw 0x40
movwf reg
bsf STATUS,C
movf ADRESH,W
subwf reg,1
btfsc STATUS,C
movf ADRESH,W
goto display2
movlw 0x60
movwf reg
bsf STATUS,C
movf ADRESH,W
subwf reg,1
btfsc STATUS,C
goto display3
movlw 0x80
movwf reg
bsf STATUS,C
movf ADRESH,W
subwf reg,1
btfsc STATUS,C
goto display4
movlw 0xa0
movwf reg
bsf STATUS,C
movf ADRESH,W
subwf reg,1
btfsc STATUS,C
goto display4
movlw 0xc0
movwf reg
bsf STATUS,C
movf ADRESH,W
subwf reg,1
btfsc STATUS,C
goto display4
movlw 0xe0
movwf reg
bsf STATUS,C
movf ADRESH,W
subwf reg,1
btfsc STATUS,C
goto display5
movlw 0xff
movwf reg
bsf STATUS,C
movf ADRESH,W
subwf reg,1
btfsc STATUS,C
goto display6
return
the idea of routine is to check if the converted binary number is belonging to wish intervals (we have 8 intervals, because we have 256/8).Depending on the interval , the corresponding leds are lighted.
for instance, between 0 and 31----led1 is on
between 32 and 64---led1 and led2 are on.
and so on.

can you help me please and want to know what's wrong with my code ????

thank you in advance and i'm sorry because i'm a newbie to micro controllers programming.
 

If you align the AD result to the LEFT, and use only ADRESH, you will have one byte with 256 possible values. You can split this '256' range into 8 intervals, one for each LED.

Now check this out:

Interval 1: 0 to 31 (00000000 to 00011111)
Interval 2: 32 to 63 (00100000 to 00111111)
Interval 3: 64 to 95 (01000000 to 01011111)
Interval 4: 96 to 127 (01100000 to 01111111)
Interval 5: 128 to 159 (10000000 to 10011111)
Interval 6: 160 to 191 (10100000 to 10101111)
Interval 7: 192 to 223 (11000000 to 11011111)
Interval 8: 224 to 255 (11100000 to 11111111)

If you take a look at the bolded numbers, they make a sequence from 'zero' to 'seven'. This is what you need to know in which rage the AD value is. To get this value, you can just eliminate the 5 least significant bits.

I suggest you to do the following code:

Code:
; declare 8 register at the begining of a bank (0x20, for example)
; initialize them at the begining of the code with the 8 values that you want in portd

movlw	0b00000001
movwf	value1
movlw	0b00000011
movwf	value2
movlw	0b00000111
movwf	value3
movlw	0b00001111
movwf	value4
movlw	0b00011111
movwf	value5
movlw	0b00111111
movwf	value6
movlw	0b01111111
movwf	value7
movlw	0b11111111
movwf	value8

;now the following code should be your function

rrf		ADRESH,f		; rotate right to alin the 3 MSB at the bit 4 
swapf	ADRESH,W	; swap it and put it into W
andlw	0x07				; elimiate all the undesired bits
movwf	temp     		; save the interval value in 'temp'
movlw 	0x20				; this is the first adress of the buffer containing the values
movwf	FSR				; load the pointer
movf		temp,W			; takes the value of the interval
addwf	FSR,f			; offsets the pointer to the right position
movf		INDF,W			; loads the content
movwf	PORTD			; put the desired value into the port


There's an important detail: Maybe this is not the actual code... In PIC16 programming, every time ou write to a simple register or a SFR, you need to adjust the memory bank selection. The 'code' I posted is just a simple idea how to solve your problem. Hope it helps.
 
Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top