# PIC16F877A senior design project NEED HELP

Status
Not open for further replies.

#### ericgibbs

##### Well-Known Member
hi Elizabeth,
The reason for asking your location is because it seems when I am close to closing down you are starting up..

These are asm tutorials from Nigel Goodwins site.

Specific to your project are the 7seg LED mux and Analog tutorials.

This one for switches.

You could easily adapt these to suit the project.

IIRC 'Bill' of Blueroom electronics has done some programming for the HS1101 sensor.?

Why dont you write up a program for the LED's and post it, then we can work thru it with you. Likewise the analog module.

I hope you are finding these posts helpful, if not ask specific questions.

#### blueroomelectronics

##### Well-Known Member
Hardware yes with the HS1101, software very basic but I cheated Insteaded of a frequency counter it was simple to connect a spare I/O pin to the 7555 reset pin and the output to one of the CCP inputs. Easy.

I agree, as a first PIC project you've got quite an effort ahead of you to complete it by May 9.

#### engineergirl27

##### New Member
Thank you, those will help. I am just getting frustrated with myself, because this all seems so new to me. I have had the semester to work on it, but we have also had to do tons of business work for marketing the product, and everything else. We don't get enough training with the programming, at school, we need to teach ourselves.

So thanks, I will be back with more specific questions if I have any.

Elizabeth

#### engineergirl27

##### New Member
Hey guys,

So I have more of a program written, It still isn't what it needs to be but i have a specific question, here is the code that i have

include <p16F877a.inc>
LIST p=16F877a
errorlevel 1,-302 ;to disable MPLAB bank warnings.
__config _XT_OSC & _PWRTE_ON & _WDT_OFF &_LVP_OFF
;
;Some versions of MPLAB use _LVP_OFF
;This sets all portB pins to output.
;Variables
cblock 0x20 ;start of general purpose registers
count
endc
;/*---------Init Ports
bsf STATUS,RP0
MOVLW B'00000001'
MOVWF TRISA ;All outputs except RA0.
MOVLW B'00000011'
movwf TRISD
CLRF TRISB ;RA0 is wiper of 50k pot/Vin
movlw b'11111111'
movwf TRISC ;PortB is 8 LED output to show

;/*-----Set up A2D parameters
MOVLW B'00001110' ;Hex value of Vin from pot.
MOVWF ADCON1 ;left justified, RA0 = input
MOVLW B'11010111' ;prescaler 1:256 tmr0, internal clock
MOVWF OPTION_REG
BCF STATUS,RP0

;/*---------Set up chip parameters
Start CALL A2D
MOVWF PORTB ;output Hex value to LEDs

PC clrf count
BTFSS PORTD,0
bsf count,0

BTFSS PORTD,1
bsf count,0

BTFSS count,0
goto Start

movf PORTC,w
movwf PORTB

goto PC

A2D
MOVLW B'01000001' ;a2d = on, ch0, fosc/8
;/*-----Delay loop to settle A2D, similar to de-bounce???
mnloop btfss INTCON,T0IF ;50us loop delay @ 4Mhz
goto mnloop
;/*-----Stop timer0 interrupt
BCF INTCON,T0IF
WAITA2D NOP ;wait 4 finish
GOTO WAITA2D
;/*-----Put A2D/PWM value in W and send to ports.
MOVF ADRESH,W ;upper 8 bits-ignor lower 3

RETURN ;DUTY CYCLE IS 25% OF PWM PERIOD

END

Now I plug in the temperature sensor that is also tied to an amplifier to amplify the voltage, when it goes through this program and displays on the eight LEDs on port B it counts very fast. I tried to change the
to see if it was counting the wrong 8 bits, but it did the same thing.
Any Suggestions?
Also any quick suggestions for help with entering an equation like this into assembly
windchill = 35.74+0.6215T-35.75(V^0.16)+0.4275T(V^0.16)

Elizabeth

#### Gayan Soyza

##### Active Member
Code:
	include <p16F877a.inc>
LIST p=16F877a
errorlevel 1,-302 ;to disable MPLAB bank warnings.
__config _XT_OSC & _PWRTE_ON & _WDT_OFF &_LVP_OFF
;
;Some versions of MPLAB use _LVP_OFF
;This sets all portB pins to output.
;Variables
cblock 0x20 		;start of general purpose registers
count
endc

;/*---------Init Ports

bsf 	STATUS,RP0
MOVLW 	B'00000001'
MOVWF 	TRISA 		;All outputs except RA0.
MOVLW 	B'00000011'
movwf 	TRISD
CLRF 	TRISB 		;RA0 is wiper of 50k pot/Vin
movlw 	b'11111111'
movwf 	TRISC 		;PortB is 8 LED output to show

;/*-----Set up A2D parameters

MOVLW 	B'00001110' 	;Hex value of Vin from pot.
MOVWF 	ADCON1 		;left justified, RA0 = input
MOVLW 	B'11010111' 	;prescaler 1:256 tmr0, internal clock
MOVWF 	OPTION_REG
BCF 	STATUS,RP0

;/*---------Set up chip parameters

Start 	CALL 	A2D
MOVWF 	PORTB 		;output Hex value to LEDs

PC 	clrf count
BTFSS 	PORTD,0
bsf 	count,0

BTFSS 	PORTD,1
bsf 	count,0

BTFSS 	count,0
goto 	Start

movf 	PORTC,w
movwf 	PORTB

goto 	PC

A2D
MOVLW 	B'01000001' 	;a2d = on, ch0, fosc/8

;/*-----Delay 	loop to settle A2D, similar to de-bounce???

mnloop 	btfss 	INTCON,T0IF 	;50us loop delay @ 4Mhz
goto 	mnloop

;/*-----Stop timer0 interrupt

BCF 	INTCON,T0IF
WAITA2D NOP 			;wait 4 finish
GOTO 	WAITA2D
;/*-----Put 	A2D/PWM value in W and send to ports.
MOVF 	ADRESH,W 	;upper 8 bits-ignor lower 3
RETURN 			;DUTY CYCLE IS 25% OF PWM PERIOD

END

#### engineergirl27

##### New Member
Blue could you explain a little more what you did with the humidity sensor? And did you used a completely different port than the other inputs. And with the CCP pins, I am currently using the Port C as the input from the windchill. Should I move those?

#### engineergirl27

##### New Member
Hey Guys,

I have been programming, and I have quite a bit done, I was wondering if any of you could help with the math. The equations are quite extensive and I am not that familiar with it enough to do these extensive of equations.

Here is what I have got. AND IT WORKS

Code:
        #include <p16F877a.inc>
LIST p=16F877a
errorlevel 1,-302	;to disable MPLAB bank warnings.
__config _XT_OSC & _PWRTE_ON & _WDT_OFF &_LVP_OFF
;
;Some versions of MPLAB use _LVP_OFF
;This sets all portB pins to output.
;Variables
cblock	0x20			;start of general purpose registers
COUNT
TEMP
CYC
endc
;/*---------Init Ports
bsf     STATUS,RP0
MOVLW   B'00000001'
MOVWF   TRISA        ;All outputs except RA0.

MOVLW   B'00000011'
movwf	TRISE
CLRF    TRISB			;PortB is 8 LED output to show
movlw	b'11111111'
movwf   TRISD

;/*-----Set up A2D parameters
MOVLW   B'00001110'
MOVLW   B'11010111'
MOVWF   OPTION_REG
BCF     STATUS,RP0

;/*---------Set up chip parameters
Start	CALL	A2D
MOVWF   TEMP
MOVWF   PORTB		;output Hex value to LEDs

PC
BTFSC	PORTE,0
goto    Start

;TEMP <= 80
movf TEMP,w
skpnc
Call HeatIndex
;TEMP <= 40
movf TEMP,w
skpc
Call WindChill

GOTO  Start

A2D
MOVLW   B'01000001'     ;a2d = on, ch0, fosc/8
;/*-----Delay loop to settle A2D, similar to de-bounce???
mnloop  btfss   INTCON,T0IF     ;50us loop delay @ 4Mhz
goto    mnloop
;/*-----Stop timer0 interrupt
BCF     INTCON,T0IF
WAITA2D NOP                     ;wait 4 finish
GOTO    WAITA2D
;/*-----Put A2D W and send to port B.
MOVF    ADRESH,W        ;upper 8 bits-ignor lower 3
RETURN

HeatIndex

START  MOVLW d'10'
MOVWF CYC

CLRF  COUNT
L1     BTFSS PORTC,2
GOTO  L1

L2     INCF  COUNT
BTFSC PORTC,2
GOTO  L2

DECFSZ CYC
GOTO  L1

MOVF  COUNT,w
MOVWF PORTB

BTFSS PORTE,0

GOTO  HeatIndex

RETURN

WindChill
movf	PORTD,w
movwf	PORTB

BTFSS PORTE,0
GOTO  WindChill
RETURN

END

Last edited:

#### ericgibbs

##### Well-Known Member
engineergirl27 said:
Hey Guys,

I have been programming, and I have quite a bit done, I was wondering if any of you could help with the math. The equations are quite extensive and I am not that familiar with it enough to do these extensive of equations.

Hi,
Nice job.

Post the 'equations' in their mathematic context so that we can suggest suitable software algorithm's for the program.

#### mvs sarma

##### Well-Known Member
blueroomelectronics said:
R1 & R2 only have one side connected. What sort of humidity sensor are you using?
What language are you going to program it with?

As the switches are found to ground, perhaps R1 to R3 might be pull-up resistors.

@engineergirl27,
please put your combined code for both functions as an ASM file instead of Text. It will be useful to analyze

#### Pommie

##### Well-Known Member
Engineergirl,

When you post code put
Code:
 before your code and [/co[U][/U]de] after it so that it keeps it's format.

Also, what is the range of T and V.

Mike.

Last edited:

#### engineergirl27

##### New Member
Thanks for the responses guys

Thanks for responding, I wish I would have checked before I went to bed at 3:30am but that is ok. I hope that this is readable to you guys.

Heat Index = -42.379 + 2.04901523T + 10.14333127R - 0.22475541TR - (6.83783x10^-3) T^2 - (5.481717x10^-2) R^2 + (1.22874x10^-3) T^2R + (8.5282x10^-4) TR^2 - (1.99x10^-6) T^2 R^2

Where T= Temp in Farenheit, R= Relative Humidity

Wind Chill = 35.74+0.6215T-35.75(V^0.16)+0.4275T(V^0.16)
where T=Air Temperature(˚F) V=Wind Speed(mph)

I know you guys work in celcius I hope it is ok that it is in Farenheit.

I appreciate the help you have given me already, I have to update the schematic too, because that has changed.
Elizabeth

Last edited:

#### blueroomelectronics

##### Well-Known Member
I used a CCP (capture) input for the output of the 555. You simply count the number of pulses in a set time to get the humidity.

#### skyhawk

##### New Member
I would suggest table lookups for the heat index and wind chill. Do the computations on your PC using your favorite programming language then load them into tables in the PIC. For instance, tables of the heat index for each 10% of relative humidity indexed by the temperature. Then do linear interpolation. Similar tables for wind chill using wind speed and temperature.

#### Pommie

##### Well-Known Member
Those are rather complex formulas to do in assembler. You can either use floating point or fixed point. I suggest fixed point with lookup tables.

Here is a table lookup that will do the V^0.16 for V=0-100
Code:
;GetV16 returns (W^0.16)*256 in word variable Acc
;Acc must be in common area (70-7f)

bsf	STATUS,RP1		;bank 2
movlw	high V16Tab
btfsc	STATUS,C
call	ReadFlash		;will return in bank 0
return

bsf	STATUS,RP0		;#3
nop				;required delay
nop
bcf	STATUS,RP0		;#2
movfw	EEDAT			;move into Acc
movwf	Acc
movfw	EEDATH
movwf	Acc+1
bcf	STATUS,RP1		;#0
return

V16Tab		dw	.0,.256,.286,.305,.319,.331,.340,.349
dw	.357,.363,.370,.375,.380,.385,.390,.394
dw	.398,.402,.406,.410,.413,.416,.419,.422
dw	.425,.428,.431,.433,.436,.438,.441,.443
dw	.445,.447,.450,.452,.454,.456,.458,.460
dw	.461,.463,.465,.467,.469,.470,.472,.473
dw	.475,.477,.478,.480,.481,.483,.484,.486
dw	.487,.488,.490,.491,.492,.494,.495,.496
dw	.497,.499,.500,.501,.502,.504,.505,.506
dw	.507,.508,.509,.510,.511,.512,.514,.515
dw	.516,.517,.518,.519,.520,.521,.522,.523
dw	.524,.524,.525,.526,.527,.528,.529,.530
dw	.531,.532,.533,.534,.534

Note that the routine can't return a value like 1.614971 (20^0.16) and so it returns this value multiplied by 256 (413). You can multiply this value by 35.75 to get 14764 and then divide by 256 to get 57. This is within 2% of the full accuracy calculation. If you keep all your terms multiplied by 256 and divide after they are summed you will maintain greater accuracy.

You can produce similar tables for your other terms. Just repeat the GetV16 routine with a different table address. Note that the largest value that can go in a table is 16383 as the flash memory is only 14 bits wide.

For 16 and 32 bit multiplication and addition routines try .

BTW, Skyhawks suggestion of lookup tables with interpolation is another good way to do it.

Mike.

#### skyhawk

##### New Member
It's good to look at the range of values you need to consider. Try taking a look at this link:

If you restrict the data to realistic combinations of temperature and relative humidity, you can see that the range of temperatures that you need to handle is larger for low relative humidity than for high.

#### engineergirl27

##### New Member
Ok so looking at the website with the tables for heat index and wind chill, would it be possible to do a two dimensional look-up table. I am thinking it would be much easier,than those equations.

Elizabeth

#### Pommie

##### Well-Known Member
I did something similar to this a while back. I modified it to implement the first equation. Once you have your parameters in Temperature and RH you call GetHeat and it returns the value in variable Acc (2 bytes).
Code:
GetHeat		movfw	Temperature		;get Temperature
addlw	.35			;table starts at -35
call	Divide5			;table goes up in steps of 5
movwf	Acc			;each table entry is 24 words
clrf	Acc+1			;so we need to multiply by 24
bcf	STATUS,C
rlf	Acc,F
rlf	Acc+1,F
rlf	Acc,F			;*4
rlf	Acc+1,F
rlf	Acc,F			;*8
rlf	Acc+1,F
movfw	Acc
movwf	TempWord
movfw	Acc+1
movwf	TempWord+1		;keep a copy of Temperature*8
rlf	Acc,F			;*16
rlf	Acc+1,F
movfw	TempWord
btfsc	STATUS,C
incf	Acc+1,F
movfw	TempWord+1
movfw	RH			;get the relative humidity
call	Divide5			;table is in steps of 5
btfsc	STATUS,C
incf	Acc+1,F
movfw	Acc
bsf	STATUS,RP1		;bank 2
bcf	STATUS,RP1		;bank 0
movfw	Acc+1
bsf	STATUS,RP1		;bank 2
btfsc	STATUS,C
call	ReadFlash		;will return in bank 0
return

bsf	STATUS,RP0		;#3
nop				;required delay
nop
bcf	STATUS,RP0		;#2
movfw	EEDATA			;move into Acc
bcf	STATUS,RP1		;#0
movwf	Acc
bsf	STATUS,RP1		;#2
movfw	EEDATH
btfsc	EEDATH,5		;is it negative
iorlw	b'11000000'		;yes, so sign extend
bcf	STATUS,RP1		;#0
movwf	Acc+1
return

Divide5		clrf	TempWord		;returns W/5
Div5Loop	incf	TempWord,F
btfsc	STATUS,C
goto	Div5Loop
decf	TempWord,W
return

HeatTable
dw	-.122,-.27,.64,.151,.233,.311,.385,.454,.519,.579,.636,.687,.735,.778,.817,.851,.881,.907,.928,.945,.958,.0,.0,.0
dw	-.110,-.22,.62,.141,.217,.288,.356,.419,.478,.533,.584,.631,.674,.712,.747,.777,.803,.825,.844,.857,.867,.0,.0,.0
dw	-.98,-.17,.60,.133,.202,.267,.328,.386,.440,.489,.535,.577,.615,.650,.680,.707,.729,.748,.763,.774,.781,.0,.0,.0
dw	-.86,-.12,.58,.125,.187,.247,.302,.354,.403,.448,.489,.526,.560,.590,.617,.640,.659,.675,.687,.695,.700,.0,.0,.0
dw	-.75,-.7,.56,.117,.174,.228,.278,.325,.368,.408,.445,.478,.508,.534,.557,.577,.593,.606,.615,.621,.624,.0,.0,.0
dw	-.64,-.3,.55,.110,.161,.210,.255,.297,.335,.371,.403,.432,.459,.481,.501,.518,.531,.541,.548,.552,.552,.0,.0,.0
dw	-.53,.2,.54,.103,.150,.193,.233,.270,.305,.336,.364,.390,.412,.432,.448,.462,.473,.480,.485,.487,.486,.0,.0,.0
dw	-.42,.7,.54,.97,.139,.177,.213,.245,.276,.303,.328,.350,.369,.385,.399,.410,.418,.424,.427,.427,.424,.0,.0,.0
dw	-.32,.12,.53,.92,.128,.162,.194,.222,.249,.272,.294,.312,.329,.342,.353,.362,.368,.371,.372,.371,.367,.0,.0,.0
dw	-.23,.16,.53,.87,.119,.149,.176,.201,.224,.244,.262,.278,.291,.302,.311,.317,.321,.323,.323,.320,.314,.0,.0,.0
dw	-.13,.21,.53,.83,.111,.136,.160,.181,.201,.218,.233,.246,.257,.266,.272,.277,.279,.279,.277,.273,.267,.0,.0,.0
dw	-.4,.26,.53,.79,.103,.125,.145,.164,.180,.194,.206,.217,.225,.232,.237,.239,.240,.239,.236,.231,.224,.0,.0,.0
dw	.5,.30,.54,.76,.97,.115,.132,.147,.161,.172,.182,.191,.197,.202,.205,.206,.206,.203,.199,.194,.186,.0,.0,.0
dw	.13,.35,.55,.74,.91,.106,.120,.133,.144,.153,.161,.167,.172,.175,.176,.176,.175,.172,.167,.161,.153,.0,.0,.0
dw	.21,.39,.56,.72,.86,.98,.110,.120,.128,.136,.142,.146,.149,.151,.151,.150,.148,.144,.139,.133,.125,.0,.0,.0
dw	.29,.44,.57,.70,.81,.92,.101,.109,.115,.121,.125,.128,.130,.130,.130,.128,.125,.121,.116,.109,.102,.0,.0,.0
dw	.36,.48,.59,.69,.78,.86,.93,.99,.104,.108,.111,.113,.113,.113,.112,.110,.106,.102,.97,.90,.83,.0,.0,.0
dw	.43,.52,.61,.69,.76,.82,.87,.91,.95,.97,.99,.100,.100,.99,.97,.95,.91,.87,.82,.76,.69,.0,.0,.0
dw	.50,.57,.63,.69,.74,.78,.82,.85,.87,.89,.90,.90,.89,.88,.86,.84,.80,.76,.72,.66,.60,.0,.0,.0
dw	.56,.61,.66,.70,.73,.76,.79,.81,.82,.83,.83,.83,.82,.81,.79,.76,.73,.70,.66,.61,.56,.0,.0,.0
dw	.62,.65,.68,.71,.73,.75,.77,.78,.78,.79,.79,.78,.77,.76,.75,.73,.70,.67,.64,.61,.57,.0,.0,.0
dw	.68,.70,.71,.73,.74,.75,.76,.77,.77,.77,.77,.77,.76,.75,.74,.72,.71,.69,.67,.65,.62,.0,.0,.0
dw	.73,.74,.75,.75,.76,.77,.77,.77,.77,.78,.78,.78,.77,.77,.77,.76,.76,.75,.74,.73,.72,.0,.0,.0
dw	.78,.78,.78,.78,.79,.79,.79,.80,.80,.80,.81,.81,.82,.82,.83,.84,.84,.85,.86,.86,.87,.0,.0,.0
dw	.82,.82,.82,.82,.82,.82,.83,.84,.84,.85,.86,.88,.89,.91,.93,.95,.97,.99,.102,.104,.107,.0,.0,.0
dw	.87,.86,.86,.86,.86,.87,.88,.89,.91,.92,.95,.97,.100,.103,.106,.109,.113,.117,.122,.127,.132,.0,.0,.0
dw	.91,.90,.90,.91,.91,.93,.94,.96,.99,.102,.105,.109,.113,.118,.123,.128,.134,.140,.147,.154,.161,.0,.0,.0
dw	.94,.94,.95,.96,.97,.100,.102,.106,.109,.114,.118,.124,.129,.136,.143,.150,.158,.167,.176,.185,.195,.0,.0,.0
dw	.97,.98,.100,.102,.104,.108,.112,.116,.121,.127,.134,.141,.149,.157,.166,.176,.187,.198,.209,.221,.234,.0,.0,.0
dw	.100,.102,.105,.108,.112,.117,.122,.129,.136,.143,.152,.161,.171,.182,.194,.206,.219,.233,.247,.262,.278,.0,.0,.0
dw	.103,.106,.110,.115,.121,.127,.134,.143,.152,.162,.173,.184,.197,.210,.224,.239,.255,.272,.289,.308,.327,.0,.0,.0
dw	.105,.110,.116,.122,.130,.138,.148,.158,.170,.182,.196,.210,.225,.241,.258,.276,.295,.315,.336,.358,.380,.0,.0,.0
dw	.107,.114,.121,.130,.140,.151,.163,.176,.190,.205,.221,.238,.256,.275,.296,.317,.339,.362,.387,.412,.439,.0,.0,.0
dw	.108,.117,.128,.139,.151,.165,.179,.195,.212,.230,.249,.269,.291,.313,.337,.361,.387,.414,.442,.471,.502,.0,.0,.0
dw	.110,.121,.134,.148,.163,.180,.197,.216,.236,.257,.279,.303,.328,.354,.381,.410,.439,.470,.502,.535,.570,.0,.0,.0
dw	.110,.125,.141,.158,.176,.195,.216,.238,.262,.286,.312,.340,.368,.398,.429,.461,.495,.530,.566,.604,.642,.0,.0,.0

The table runs from -35°F to +140°F in steps of 5° and 0 to 100% RH in steps of 5. Note that I extended the table so each row contains 24 values to simplify the maths.

The table was generated with a little excel sheet that I wrote. I'll attach it. You should be able to generate the other table with it. To use it run the macro called "Convert" and the table data will be placed on the clipboard. I had to zip it as xls files aren't allowed.

The above code was written for a 16F88 but I'm pretty sure the 876 is the same. It's untested and so you may have to debug it.

Have fun.

Mike.

Last edited:

#### engineergirl27

##### New Member
Thanks for all your help, I am still extremely confused on the lookup table stuff, I wish that i only had to work on the programming for this. but that is not the case, I am also trying to get the paper and presentation slides done for this project which is due friday. do you guys know of any easier way to calculate wind chill, I have a simpler equation WC = Temp - Wind Speed/4, but this is for Celcius and km/hour, so I would need to the the conversions for that. which might be possibly, might but I don't know if I have enough time in the day to do that.

Elizabeth

Status
Not open for further replies.

Replies
1
Views
2K
Replies
9
Views
12K
Replies
9
Views
1K
Replies
3
Views
1K
Replies
8
Views
3K