PDA

View Full Version : I Need Help.. I want to generate controlled pulses



alabbadi
- 10th November 2007, 16:04
Hey all.. how are you.. hope ur doin well..

my problem z dat i wana generate pulses with specific frequency... the code should take interruption from the user (keypad) so that if he/she presses pad1, frequency changes to 1 hz.. if he/she presses pad2, then frequency changes to 10 hz, 3 gives 50 hz, 4 gives 100 hz.... i need it as soon as possible... and thnx for your help and support :)

Ioannis
- 16th November 2007, 08:12
Since you posted on the Wish list, this could be a pious hope!

Also don not expect some one to write your code and give it to your plate. We have bussiness to take care off!

Try something and sure we will give a hand.

Ioannis

mister_e
- 16th November 2007, 13:32
what's a frequency.. it's just some hig and low pulse of a certain length.

Now you should know how to calculate the frequency period? This gives you some info about how much time the pulse will be high, then low

Easy to use Keypad routine, just plug it into your code and enjoy!

Matrix Keypad routine - Read this
http://www.picbasic.co.uk/forum/showthread.php?t=3250

But download the latest version bellow
http://www.picbasic.co.uk/forum/attachment.php?attachmentid=1742&d=1181850086

alabbadi
- 16th November 2007, 23:39
here is my code:


===============================================
Define LOADER_USED 1

' Define LCD connections
Define LCD_DREG PORTD
Define LCD_DBIT 4
Define LCD_RSREG PORTE
Define LCD_RSBIT 0
Define LCD_EREG PORTE
Define LCD_EBIT 1


' Define program variables
col Var Byte ' Keypad column
row Var Byte ' Keypad row
key Var Byte ' Key value
fre Var byte ' half pulse time
LED0 var PORTD.0 ' Alias PORTD.0 to LED

OPTION_REG = $7f ' Enable PORTB pullups
'OPTION_REG.7 = 0 ' Enable PORTB pullups

ADCON1 = 7 ' Make PORTA and PORTE digital
Low PORTE.2 ' LCD R/W low (write)

Pause 100 ' Wait for LCD to start

Lcdout $fe, 1, "Choose Your Frequency" ' Display sign on message

'On Interrupt Goto myint ' Define interrupt handler
'INTCON = %10010000 ' Enable INTE interrupt

loop1: Gosub getkey ' Get a key from the keypad

'On Interrupt Goto myint ' Define interrupt handler
'INTCON = 144 ' Enable INTE interrupt

loop3: if key = 1 then
Lcdout $fe, 1, "1 Hz"
fre = 500
gosub loop2
endif

loop4: if key = 2 then
Lcdout $fe, 1, "10 Hz"
fre = 50
gosub loop2
endif

loop5: if key = 3 then
Lcdout $fe, 1, "50 Hz"
fre = 80
gosub loop2
endif

loop6: if key = 4 then
Lcdout $fe, 1, "100 Hz"
fre = 40
gosub loop2
endif

'disable interrupt

loop2: High LED0 ' Turn on LED connected to PORTD.0
Pause fre ' Delay for "fre" seconds

lOW LED0
Pause fre ' Delay for "fre" seconds

'gosub getkey
'goto loop1


'if key = 1 then
'goto loop3
'endif

'if key = 2 then
'goto loop4
'endif

'if key = 3 then
'goto loop5
'endif

'if key = 4 then
'goto loop6
'endif

;Disable interrupt ' No interrupts past this point
'myint: 'gosub getkey
'goto loop1
'INTCON.1 = 0 ' Clear interrupt flag
'Resume ' Return to main program
'Enable interrupt

goto loop2

'goto loop1

'enable interrupt

Return ' Go back to loop and blink LED forever


' Subroutine to get a key from keypad
getkey:
Pause 500 ' Debounce

getkeyu:
' Wait for all keys up
PORTB = 0 ' All output pins low
TRISB = $f0 ' Bottom 4 pins out, top 4 pins in
If ((PORTB >> 4) != $f) Then getkeyu ' If any keys down, loop

Pause 50 ' Debounce

getkeyp:
' Wait for keypress
For col = 0 To 3 ' 4 columns in keypad
PORTB = 0 ' All output pins low
TRISB = (dcd col) ^ $ff ' Set one column pin to output
row = PORTB >> 4 ' Read row
If row != $f Then gotkey ' If any keydown, exit
Next col

Goto getkeyp ' No keys down, go look again

gotkey: ' Change row and column to key number 1 - 16

key = (col * 4) + (ncd (row ^ $f))
Return ' Subroutine over

End
=================================================


1) the problem is that by using this code, generated pulses cud not change using the interrupt code.. which i cudnt fix!!!! this is where i need help...
{at the begining, i need the code to check for the pressed keypad and loop as the corresponding frequency... then the code should check for any interruption from the user if he/she pressed any key.. if he pressed any other key, the frequency should change accordingly,,, if not it should cont looping to generate the previous frequency}


2) another thing is that i need ur help in understanding the concept of working of the keypad connection diagram (when we press the keypad, does it give 1 or 0)

alabbadi
- 16th November 2007, 23:45
i jst wud like 2 add one more thing which is that im using PIC16F877A microcontroller with its "LABX1, learning board" which means that keypads r already there installed and ready to use...

Ioannis
- 17th November 2007, 12:22
I don't know about the LABX1 board, so cannot help you on that.

About the other matter, as stated, and if I understood it correctly, you need to have a clean frequency at the output while checking the keyboard with PBP interrupts?

I think it is not going to work as expected, since the PBP interrupts are slow and will introduce jitter at the output.

I believe it would be easier to use just four inputs and 4 buttons and check these while you have the output High or low and you are waiting for the respective time to exprire.

Like this:
loop:
High output
gosub check_buttons
pause xx msec (you have to find the best value here)
low output
pause yy msec (you have to find the best value here)
goto loop

check_buttons
if but1=0 then
xx=value1:yy=value1a
return
endif
if but2=0 then
xx=value2:yy=value2a
return
endif
if but3=0 then
xx=value3:yy=value3a
return
endif
if but4=0 then
xx=value4:yy=value4a
return
endif
return

Now you have to find by trial and error the most accurate values for the value1 through value4a

P.S. Experienced programmers might laugh at the above example, but I think you have to start with something simple before jump into interrupts. If you really need interrupts, consider Darrel Taylors Instant Interrupts. The definite solution.

Ioannis

isaac
- 18th November 2007, 10:38
i totally agreed with u Ioannis

isaac
- 18th November 2007, 10:42
1 hz.. if he/she presses pad2, then frequency changes to 10 hz, 3 gives 50 hz, 4 gives 100

f=1/T so you can find T for each of your freqs
that would then be pause xx etc

Isaac