PDA

View Full Version : 16F876A Pulsin issues



Bobbo_ZA
- 30th March 2010, 07:40
Hi All,

After my successful Glow igniter project I have now moved onto trying to make an all in one setup / testing unit using a 16F876A and a Hitachi LCD module.

I have been able to output standard strings to the LCD (Startup instructions, menu etc) however I am now looking to get started with the servo and Receiver side of the project.

What I am looking to do is read in the pulsin value from the Rx and display it on the screen (sounds simple as I used this for the glow project), but this seems to not be working on the 16F.

What I get on the screen with the code below (stripped out the menu structures for testing) is the word "Pulse" with the space and then a strange alphanumeric single character which varies depending on the stick position of the transmitter (Clearly its picking up something).

My first though is that I'm using WORD to store the incoming value, however this has worked previously with the 12F PIC i used for the glow project.



@ DEVICE PIC16F876A,HS_OSC,LVP_OFF,WDT_OFF,PROTECT_OFF

DEFINE OSC 20 '20 MHz Osc
DEFINE LCD_COMMANDUS 2000
TRISB = %00000001 'Set PortB.0 as input for Rx

Rx Var PORTB.0 'Rx input
servo var PORTB.1 'Servo output

P VAR word 'Pulse Input

pause 2000 ' Allow LCD to init

Main:
PulsIn Rx,1,P 'Read in Rx input
pulsout servo, P 'output to a servo connected on B.1
Toggle PORTB.5
LCDOUT $FE, 1, "Pulse"," ", P 'Output to LCD
LCDOUT $FE, $C0," ", P 'Testing on the second line
goto main:

End



Any ideas why my "P" does not output the correct value to the LCD? I would expect something between 100-200 as a numeric value.

Could it be that the 20Mhz crystal is too fast as this would give me a 2us resolution? (I noticed that the servo was moving in really tiny increments)

Many Thanks
Rob

Melanie
- 30th March 2010, 07:51
P is a NUMBER not a displayable ASCII value.

If you want to display the numeric value on the LCD, it needs to be converted. Prefix it with either a hash # in the form

LCDOUT $FE, 1, "Pulse"," ", #P

or with the word DEC in the form

LCDOUT $FE, 1, "Pulse"," ", DEC P

PS, you could of course include the extra SPACE on the end of your word "Pulse " like...

LCDOUT $FE, 1, "Pulse ", DEC P

Bobbo_ZA
- 30th March 2010, 08:54
Hi Melanie,

Firstly, wow, thank you, :-) all the reading up I was doing last night and I cant believe I missed that, :-), makes sense and I'm sure it will work (will have to wait till I get home from work to test it - note to self, bring board to work)

Another probably small issue I have is the pic does not seem to be driving the servo correctly, in the 12F I could simply do a pulsin on one pin and pulsout to another pin which would control the servo no problem (Planing to use this as a failsafe - want to read in a switch channel and depending on the position I'll output either direct to the servo or via another pic or Arduino for navigation), however now with the 16F the servo moves in one direction but extremely slowly, like 1mm every 5 minutes, its almost as if the resolution is so high its moving in tiny steps, could this be caused by the 20Mhz osc?

Thanks again
Rob

Melanie
- 30th March 2010, 09:53
The value of P (or other timing) may well reveal why.

Acetronics2
- 30th March 2010, 10:02
Hi, Bobbo

Try this




...

PAUSE 2000
LCDOUT $FE, 1, "Pulse"," "

Main:
PulsIn Rx,1,P 'Read in Rx input
LOW servo
pulsout servo, P 'output to a servo connected on B.1
Toggle PORTB.5
LCDOUT $FE, $80+7, # P 'Output to LCD
LCDOUT $FE, $C0+1,# P 'Testing on the second line
goto main:

...



Servo will follow the input signal ...

Alain

Bobbo_ZA
- 30th March 2010, 10:30
I feel like such a rookie, :-) I should have look at more of my previous code, :-)



pulsin gr,1,g
pulsin rx,1,P
low throttle
pulsout throttle, P


Thanks exactly what I had in a previous project, :-)

Can't wait to get home now, :-)

Will post pics once I have it programed and working, hope to build up a nice multi function test unit - once thing im still investigating is how to measure the battery voltage of a 4 cell flight pack (nicad or nimh) with a 250ma load, im using the units 5.02V regulated supply as a reference but not quite sure if I could just give the flight pack's V to the ADC through a resistor and work from there? 4 Cell fully charged will give me 5.6V and I dont know what the ADC does when the voltage is higher than the reference V, also could the pic handle the extra .6V?

On a side note I'm still hunting down a case for my LM35 project, finally got some ally tubing to house the sensor, just need to find a nice case for the PIC and water proof it, :-) (will post pics etc as well once complete)

Kind Regards
rob

Acetronics2
- 30th March 2010, 12:32
Hi, Rob

Consider all inputs are clamped to Vcc + .6 v ... ( take care: clamps diodes are very Weeeeeeak ! )

so, the best is not to have more than Vcc ... or use strong external clamp diodes ( 1 N4148 i.e )

but analog inputs can rise to Vref + 2v ... not forgetting Vcc+.6v is an absolute maximum ...

Alain

Bobbo_ZA
- 30th March 2010, 12:50
Would adding a bigger load to the pack solve the problem? A bigger load should pull the cells down to 1.2v per cell to give me a max of 4.8V on a fully charged pack? perhaps a 500ma load?

Acetronics2
- 30th March 2010, 13:14
Bad Idea ...

just use a voltage divider in the input ...

1/2 is the easiest to find ( 2 equal 1% resistors ) ...
2/3 is still easy ( 1 k and 2k resistors @ 1%)

Installing a load that can draw 300 - 400 mA on demand is rather a good thing ...

But think regulator doesn't output EXACT 5.000 v ... so measure it and enter value as a parameter for calculations ...

Alain

Bobbo_ZA
- 30th March 2010, 20:21
I have an idiot grin on my face, :-) im making some really good progress, I have the pulse in now displaying on the screen (strange from a JR Rx Im getting a range from 560 - 990 from the pulsin) and have the ability to jump to a servo test (sweep from side to side) at the push of a button, :-)

My ADC is sort of working, with the voltage divider I'm getting 2.54V on my DMM, however if I display the ADC input Im only seeing 2 and not the .54 (obviously this will be multiplied by 2 once i get the decimals). I have tried:



lcdout $FE, $C0+8, dec volt

and

lcdout $FE, $C0+8, # volt



but still only get the first digit. Is this due to the ADC VAR being a "WORD"



Volt Var WORD
.
.
.
ADCIN 4, volt
.
.
.


My Vref from the regulator is 5.00V on my DMM

Not sure im getting this whole ADC concept, I;ve read another one of Melanie's posts and from what I can work out, my RAW ADC in variable should be as follows:

if my Vref = 5.00 then if my ADCIN was exactly 5.00V the ADC result would give me 1024, however I am giving a ADC of 2.54 I should get a value in the variable of 520? however I am displaying a single digit "2" on the LCD with the code above, not sure how to do the math on this.

Bobbo_ZA
- 30th March 2010, 21:05
Ah, ok, finally, im a git, :-)

Im now reading a 520 adc value with the battery on and the DMM measuring 2.54 (Needs to be multiplied by 2 to reverse the voltage divider effect).

Err, something not right, im missing something with the math:



ADCIN 4, volt ' Read AN4 into temp variable
Vin = (5*volt/1024) ' Convert to voltage assuming 5.00 = 1024

lcdout $FE, $C0+8, dec Vin,".",dec2 Vin,"V"



pulling out many hairs, :-( my biggest issue is that even in school my maths was not great, spose thats why Excel was invented, for people like me, :-)

Bobbo_ZA
- 30th March 2010, 22:30
just not getting the decimal places now, :-(

Latest attempt



ADCIN 4, volt ' Read AN4 into temp variable
volt = volt * 2
Vin = (5*volt/1023)
Vind = (volt // 1000) / 10 'millivolts?

lcdout $FE, $C0+8, dec Vin,".",dec2 Vind,"V"


Time to pack it in for the night, work tomorrow, fairly good progress, just the decimal points to figure out.

Kind Regards
Rob

Bobbo_ZA
- 31st March 2010, 09:21
Am I on the right track?

the "//" operator will store the remainder in the chosen Var, thus:



ADCIN 4, volt ' Read AN4 into temp variable
volt = volt * 2 'Correct the ADCin for the Voltage divider?
FVolt = (5*volt/1023)
MVolt = (5*volt // 1023) 'millivolts?

lcdout $FE, $C0+8, dec FVolt,".",dec4 MVolt,"V"


Sorry for getting overly complex with this but trying to understand how it works, I have been able to simulate what I need to do in excel, just not sure if the above will work in PBP:

D1 = Vref @ 5.00
D5 = Adc Corrected @ 1021
D6 = 10 bit ADC @ 1024

D14 = Whole number (Formula =TRUNC(D9,0), result = 4
D15 = Remainder (Formula =(MOD((D5*D1)/D6,1)*1000)), result = 990

I could then LCDout D14, ".", D15 in theory if the formula in PBP equaled the excel result.

not sure though, still a bit confused

Cheers
Rob

Acetronics2
- 31st March 2010, 09:27
just not getting the decimal places now, :-(

Latest attempt



ADCIN 4, volt ' Read AN4 into temp variable
volt = volt * 2
Vin = (5*volt/1023)
Vind = (volt // 1000) / 10 'millivolts?

lcdout $FE, $C0+8, dec Vin,".",dec2 Vind,"V"


Time to pack it in for the night, work tomorrow, fairly good progress, just the decimal points to figure out.

Kind Regards
Rob

Ok Rob,

What about :




ADCIN 4, volt ' Read AN4 into temp variable

volt = volt * 2 * 5

Volt = 100*volt ' See manual for DIV32 !!!
Vind = DIV32 1024 ' Here we have result in 1/100 Volts ...

Vin = Vind/100 ' Here we get the integer part ...
Vind = Vind // 100 ' Here we get the decimal part ...

lcdout $FE, $C0+8, dec Vin,".",dec2 Vind,"V"



that's all !

Bobbo_ZA
- 31st March 2010, 10:46
Cant believe it can be that simple, i was trying to over complicate things, :-)

Will try it again tonight, :-)

Achievements So far:
1. Hitachi LCD interfaced to 16F876A
2. Read input from RC Receiver
3. Drive a Servo from the PIC
4. Display pulsin values on the LCD
5. Measure Flight Pack voltage - with load on demand

Still to do:
1. Menu system for each function
2. separate components into sub functions
3. Move off Breadboard and build into an enclosure of some sort.

List of proposed features:
1. Rc Flight pack battery tester
2. Servo Tester - with Sweep and Centering
3. Current monitor - planing to add either SI210709-300T (50A) or ACS750
4. Receiver Pulse monitor for other PIC projects
5. Thermometer - Although I think I'm all out of AD inputs
6. Sure Im going to come across some more features to add to it, :-) any ideas?

Bobbo_ZA
- 1st April 2010, 14:28
Hi Ace, All,

its working, :-) cant believe it was that simple, :-)

going to start working on the next phase of the implementation this weekend.

Hope everyone has a good weekend, :-)

Cheers
Rob