Stuck with my code

Status
Not open for further replies.

mini

Member
Hi All,
I am newbie in this forum, I had problem and stuck with these code, could someone here can help me to solve it my problem, below are my code :

On above code, the button up and down doesn't work properly and reading of the power watt un-correct, LCD appears unstable.

I hope someone want to help me.

Mercy,
Mini.
 
Are the up/down buttons actual buttons or some other digital inputs?
If real buttons, you need to "debounce" the inputs before processing logic. With real buttons, there is contact bounce, and the PIC is so fast, it will see the contact going on/off all within a millisecond or two as you press the button. So, your "while up" or "while dn" will fail with contact bounce.
Usually with real buttons, I put in a small delay, like 30mS, then verify the button is still in the original state before processing the rest of the code. Without debounce, your code may trigger an "up" and a "down" all within the same time that you are actually still pressing the button.
If the code seems to work while you hold the button down steady, then it could be a different issue.
If the "buttons" are digital signals from another source, then debounce is not an issue, but signal noise could be if the wire lengths are long.

Follow good power supply filtering at the PIC, make sure you have the proper bypass capacitors filtering the VDD to VSS.

I also noticed in your code, your first "If da" does not seem to have a proper endif, as you seem to go to a "While da" and a "While NOT da" all within the same block. Is this correct?
Print your code out on paper, then mark and check every "if" with every "endif"

Code:
If da Then 'press Enter
Lcdcmdout LcdClear 'LCD Clear
Lcdout "Frequency Tuning" 'Write on display

While da 'ENTER
WaitMs 20
Wend

WaitMs 100 'Wait for
'--------------------------Read L&H byte-------------------------
Read 10, freq.LB
Read 11, freq.HB
'----------Frequency devided---------
fshow = freq / 20
fmod = freq Mod 20
fmod = fmod * 5
'-------------------------------------------------------------
Lcdcmdout LcdHome 'Prepare for LCD Out
If fmod < 10 Then
Lcdout "-------", #fshow, ".0", #fmod, "Mhz " 'Write on LCD
Else
Lcdout "-------", #fshow, ".", #fmod, "Mhz " 'Write on LCD
Endif
'--------------------------------------------------------------------------------------------------
While Not da 'If not enter
tm = 0 'status 0
.
.
.
 
Last edited:

Or if you want to get really really lazy, you can just ignore any states changes within a certain amount of time after an initial state change, but that's the barest bones form of debounce.
 
Also, your line:
Define LCD_COMMANDUS = 5000 'delay after LCDCMDOUT, default value is 5000
may be forcing a word into a byte variable. I do not see LCD_COMMANDUS declared as a word.
 
Or if you want to get really really lazy, you can just ignore any states changes within a certain amount of time after an initial state change, but that's the barest bones form of debounce.
I avoid bounce by only reading the keys 50 times per second. So I guess I ignore state changes for 20mS. Works for me. In fact, I'd go as far as saying there is no better way.

Mike.
 
I avoid bounce by only reading the keys 50 times per second. So I guess I ignore state changes for 20mS. Works for me. In fact, I'd go as far as saying there is no better way.

Mike.
Really no better way? Because the reason I say it is the barest bones method is that it is vulnerable to induced noise since every initial state change will register as valid, regardless of whether it is stable or not. Whereas checking that the state remains stable at least twice separated by a time interval reduces or eliminates that scenario.
 
If you have a key that bounces for longer than 20mS then I suggest you stop using paper clips and drawing pins. Therefore, when you read it and it's bouncing (and happens to read open), it will have stopped bouncing next time you read it. Try it - you will never see any bounce and it's nice and responsive. I've had buttons (in cars!!) that I can press and release and it doesn't register because some programmer used too long a delay between checking states.

Mike.
 
Is this responding to my comment about induced noise? Because your response doesn't address that concern. The induced transient doesn't need to last for 20ms. It just needs to last for an instant and hit at the same time the MCU is checking.
 
If your circuit is susceptible to induced noise then you need to redesign your circuit - no amount of programming will fix that.

Mike.
 
If your circuit is susceptible to induced noise then you need to redesign your circuit - no amount of programming will fix that.

Mike.
Not always. Depends on where it's operating and the length of cables involved.
 
Hi All Master,
Thank you for your advice according my issue.

So far I already tried to refer on your suggestion, but I am still getting stuck.
I tried to disable Endif and activated another Endif it better the LCD now not running some character.

So if you wish to tried this code, you find my attached files, and please share it back.

On the attached there are Oshonsoft and Proteus.

Mercy.
Mini.
 

Attachments

  • Oshonsoft Transmissor.zip
    44.8 KB · Views: 228
mini, in you ZIP code, you seem to have commented out the flbyte in one part of the code:

Code:

'send 1101_1101_1001_0101-Word b-> > saa1057 flbyte = %10010101 <--------------- COMMENTED OUT !

fhbyte = %11011101
da = 0
ck = 0
ce = 0
WaitUs 5
ce = 1
ck = 1
WaitUs 5
ck = 0

For tm = 1 To 8
da = fhbyte.7
WaitUs 5
ck = 1
WaitUs 5
ck = 0
fhbyte = ShiftLeft(fhbyte, 1)
Next tm

For tm = 1 To 8
da = flbyte.7 <----------------------------- NOT DEFINED as %10010101
WaitUs 5
ck = 1
WaitUs 5
ck = 0
flbyte = ShiftLeft(flbyte, 1)
Next tm
 
Hi Sagor1,
Could you please revised that code attachment, I am confused if not do any changes inside the code.

Mercy,
Mini.
 
Just saying, in the code you posted, the line:

'send 1101_1101_1001_0101-Word b-> > saa1057 flbyte = %10010101

Has your flbyte as part of a comment line, it will not compile.

Should read as two lines:

'send 1101_1101_1001_0101-Word b-> > saa1057
flbyte = %10010101
...
etc...
 
Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…