Sonsivri

Electronics => Projects => Topic started by: sadman on September 21, 2013, 10:21:18 10:21



Title: 8 bit baniary counter
Post by: sadman on September 21, 2013, 10:21:18 10:21
hi

i have ported a c code to basic for 8 bit binary counter but my code is not working as original one can some one help me to fix this i have uploaded both original and ported code here with simulation file

regards
sadman





Title: Re: 8 bit baniary counter
Post by: Catcatcat on September 21, 2013, 03:53:18 15:53
Of course will not work, read here http://catcatcat.d-lan.dp.ua/en/skachat/primeryi-postroeniya-koda-programm-dlya-pic-kontrollerov/tsifrovoy-vvod-dannyih/ (http://catcatcat.d-lan.dp.ua/en/skachat/primeryi-postroeniya-koda-programm-dlya-pic-kontrollerov/tsifrovoy-vvod-dannyih/) if Google release translation understand, there is an example.


Title: Re: 8 bit baniary counter
Post by: robotai on September 23, 2013, 03:50:32 03:50
In C code, we use "{..}"  to form program block.

In Basic code, "If" should has related "End If" to form program control block. Obviously you missed it.


Title: Re: 8 bit baniary counter
Post by: sadman on September 23, 2013, 05:18:09 05:18
In C code, we use "{..}"  to form program block.

In Basic code, "If" should has related "End If" to form program control block. Obviously you missed it.

can you please provide a example


Title: Re: 8 bit baniary counter
Post by: robotai on September 23, 2013, 05:59:21 05:59
Well, not sure what example you want? If you don't understand the syntax, maybe you need to study the BASIC language from the beginning.

For part of your code, it should looks like
Code:
If up=1 Then
  bcount=bcount+1
  DelayMS 100
  If bcount=0 Then bcount=255
End If
If dn=1 Then
  bcount=bcount-1
  DelayMS 100
  If bcount=255 Then bcount=0
End If

You didn't port the debounce part from C code exactly. Maybe you are using your way.


Title: Re: 8 bit baniary counter
Post by: ero on September 23, 2013, 06:12:46 06:12
Here is the working code;
Code:
Device = 16F628
Xtal = 4
Config INTRC_OSC_NOCLKOUT, WDT_OFF, PWRTE_ON, BODEN_OFF, LVP_OFF, CP_OFF, MCLRE_OFF ;On-chip RC-oscillator
All_Digital TRUE

CMCON=7

TRISB=0
TRISA=%00000011
Symbol up=PORTA.0
Symbol dn=PORTA.1
 
Dim bcount As Byte

bcount=0
PORTB=0
DelayMS 300

start:
If up=1 Then
bcount=bcount+1
If bcount=0 Then  bcount=255
PORTB=bcount
WHILE UP=1:WEND
DelayMS 50
endif

If dn=1 Then
 bcount=bcount-1
 If bcount=255 Then bcount=0
 PORTB=bcount
 while dn=1:wend
DelayMS 50
endif

GoTo start
End

ero


Title: Re: 8 bit baniary counter
Post by: sadman on September 23, 2013, 07:20:03 07:20
Well, not sure what example you want? If you don't understand the syntax, maybe you need to study the BASIC language from the beginning.

For part of your code, it should looks like
Code:
If up=1 Then
  bcount=bcount+1
  DelayMS 100
  If bcount=0 Then bcount=255
End If
If dn=1 Then
  bcount=bcount-1
  DelayMS 100
  If bcount=255 Then bcount=0
End If

You didn't port the debounce part from C code exactly. Maybe you are using your way.


how i can add debounce part


Title: Re: 8 bit baniary counter
Post by: robotai on September 23, 2013, 08:01:12 08:01
I think ero has ported it for you on previous reply.

For better mechanism, you can refer to
http://www.microchip.com/forums/m265696.aspx
This is much like what I have usually used.


Title: Re: 8 bit baniary counter
Post by: sadman on September 24, 2013, 05:35:04 05:35
thank to all friends for the support finally i get it working as required
 
sadman