PDA

View Full Version : New to PBP and having problems!



Lauren Barta
- 7th October 2006, 21:41
Hi List Members,

I'm pretty new to Pic's and PicBasic Pro and I'm having some problems. I hope someone on this list may be able to enlighten me! I've purchased an MELabs X-1 to experiment with and to help me learn PicBasic Pro and MicroCode Studio. I'm using a PIC16F877A at 4 Mhz and this is what I'm trying to do with the code (shown below).

I need to read a voltage between 0 and 5 Volts and convert this input to a frequency (Squarewave) output. If the input voltage changes, the output frequency should also rapidly change.

Note:
There are only two different frequencies in the below program. Eventually there will be many more!

Any suggestions or help will be greatly appreciated!
Lauren

'************************************************* ***************
'* Name : UNTITLED.BAS *
'* Author : [select VIEW...EDITOR OPTIONS] *
'* Notice : Copyright (c) 2006 [select VIEW...EDITOR OPTIONS] *
'* : All Rights Reserved *
'* Date : 9/28/2006 *
'* Version : 1.0 *
'* Notes : *
'* : *
'************************************************* ***************
DEFINE LCD_DREG PORTD ' Define LCD registers and bits
DEFINE LCD_DBIT 4
DEFINE LCD_RSREG PORTE
DEFINE LCD_RSBIT 0
DEFINE LCD_EREG PORTE
DEFINE LCD_EBIT 1
A2D_VALUE VAR BYTE ' Create A2D_Value to store result

TRISA = %11111111 ' Set PORTA to all input
TRISB = %00000000 ' Set PORTB to all output
TRISD = %00000000
ADCON0 = %11000001 ' Configure and turn on A/D Module
ADCON1 = %00000010 ' Set PORTA analog and LEFT justify result
PAUSE 500 ' Wait 0.5 second for LCD startup

LOOP:
ADCON0.2 = 1 ' Start Conversion
NOTDONE:
IF ADCON0.2 = 1 THEN NOTDONE ' Wait for low on bit-2 of ADCON0, conversion
finished
A2D_VALUE = ADRESH ' Move high byte of result to A2D_Value
LCDOUT $FE, 1 ' Clear screen
LCDOUT "DEC VALUE= ", DEC A2D_VALUE," " ' Display 2 values
LCDOUT $FE, $C0, "BIN=", BIN8 A2D_VALUE," "
PORTD=A2D_VALUE ' displays value in bargraph
If A2D_VALUE = %00001111 THEN Freq1
If A2D_VALUE = %10000000 Then Freq2
PAUSE 100 ' Wait .1 second
GOTO LOOP ' Do it forever
Freq1:
HIGH 0
Pauseus 3600
LOW 0
Pauseus 3600
If A2D_VALUE <> %00001111 Then Loop 'This line is not working!
Goto Freq1

Freq2:
HIGH 0
Pauseus 1800
LOW 0
Pauseus 1800
If A2D_VALUE <> %10000000 Then Loop 'Nor is this line!
Goto Freq2

END

mister_e
- 7th October 2006, 23:58
you should use HPWM to produce the frequency, it's working smooth when your program jump here and there.

mikeb
- 12th October 2006, 03:20
Hi Lauren,

Do you still require assistance with your project ?

'mister e' is putting you on the right track. Try and use 'hardware' peripheral's
as much as you can to alleviate software latency issues. PBpro commands are great to get code off the ground quickly, but due to their inherent power, they generate quite a large amount of code to operate. Take a look at the 'asm' file, after compilation, and you will see what I mean.

meLAB's are a brilliant source for Microchip development stuff as you will see during your learning curve (none of us ever stop learning !).

Judging by the ADCON register configuration, it appears that you are using the internal A\D reference tied to the +5V logic rail and are most likely using the 'single turn' pot on the X1 board for your 0 - 5V signal. I imagine that there are two problems this project is suffereing from -

Circuit wise, there will be a lot of noise on the +5V rail and 'single turn' pot's are notoriously difficult to set with any accuracy.

Software wise, your code is looking for a 'literal' value (with the use of = and <>) in the 'IF_THEN' statements.

Bearing this in mind, the 'result register' only has to deviate by one least significant bit from the 'constant value' and the whole process will produce unexpected\unwanted results.

If you would like some sample code, please, DO NOT hesitate to ask.

Regards Mike B. Australia.

mikeb
- 12th October 2006, 03:49
Hi Lauren,

Forgot to point out the following in my last post -

Have a look at your Freq\n loops and see if you can spot a problem.

Freq1:
HIGH 0
Pauseus 3600
LOW 0
Pauseus 3600
If A2D_VALUE <> %00001111 Then Loop 'This line is not working!
Goto Freq1

If your code is getting here, then this is where it will stay, as it has strayed away from the 'main' loop where the A\D conversion was taking place. The 'result' register can never change from %00001111, consequently, the IF_THEN statement can never fail which will cause the program to cycle endlessly within this loop.

A technique I use to check program flow is to turn on\off an LED to signal when my code enters\exits various program paths. Invaluable to show where I've 'goofed'

If you are not already using 'Microcode Studio Plus' with PBPro I urge you to get it. There are some very clever people in this world and Mechanique are no exception.

Hope this helps.

Regards Mike B.