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
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