Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

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 :
'--------------PLL IC PIC16F819--------------------------------
'Declare LCD 16x2
Define LCD_BITS = 4
Define LCD_DREG = PORTB
Define LCD_DBIT = 0
Define LCD_RSREG = PORTA 'For lcd rs
Define LCD_RSBIT = 2
Define LCD_EREG = PORTA 'For lcd e
Define LCD_EBIT = 3
Define LCD_RWREG = 0 'LCD RW
Define LCD_RWBIT = 0 'RWBIT

'---------------------Delay for LCD -----------------
Define LCD_COMMANDUS = 5000 'delay after LCDCMDOUT, default value is 5000
Define LCD_DATAUS = 50 'delay after LCDOUT, default value is 50
Define LCD_INITMS = 100 'delay used by LCDINIT, default value is 100
'---------------------Declare PORT ce, ck, da to Connection to Pin SAA1057--------------------------
Symbol ce = PORTB.5 'IC PLL SAA1057 CE,CK,DA
Symbol ck = PORTB.4
Symbol da = PORTB.6

Symbol tx_en = PORTB.7 'TX output to drive VDD to ON the power supply stable
Symbol dn = PORTA.0 'Button Decrease Frequency or Power
Symbol up = PORTA.1 'Button Increase Frequency or Power
Symbol lock = PORTA.4 'Send data IC PLL for LOCK

'---------------------Declaration -------------------------------------
Dim freq As Word
Dim f5 As Word
Dim fshow As Word
Dim fmod As Byte
Dim fhbyte As Byte
Dim flbyte As Byte
Dim change As Bit
Dim tm As Byte
Dim t2 As Byte
Dim vu As Byte
Dim power_1 As Word 'NAME "Power" I GOT ERROR HERE WHAT IS THAT AN ERROR
Dim power_show As Byte
Dim power_mod As Byte

ADCON1 = 0x06

TRISB = %01000000 'PORTB 6 output, other are input

TRISA = %11110011 'PORTA 2&3 output, other are input
'-----PLL data status----------------------------------------------------------------------
ce = 0 'CE=0 ck = 0 'CK=0 da = 0 'DA=0
tx_en = 0 'Status TX OFF

Lcdinit 'Initialisation of LCD
Lcdcmdout LcdClear 'Clear LCD

Lcdout " FM PLL 5 W " 'Write on display
WaitMs 3000 'Waiting for 3 second

change = 1 'Change to up

'------Write on EEPROM ???????? 10-11-----------
Read 10, freq.LB
Read 11, freq.HB

'----------Set frequency 108.0 MHz =1080 2=2160-----------
If freq > 2160 Then 'if more than 108 Mhz
freq = 1750 'then frequency 87.5 MHz
Write 10, freq.LB 'Write Byte low
Write 11, freq.HB 'Write Byte high
Endif

If freq < 1750 Then 'if frequency less than 87.50
freq = 2160 'then frequency 108 MHz
Write 10, freq.LB 'Write Byte low
Write 11, freq.HB 'Write Byte high
Endif
'--------------------------------------------------------------------------------------------------
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

'--------------Here are Up button---------------------------------

If up Then
freq = freq + 1
While up

tm = tm + 4

If tm = 64 Then freq = freq + 1
If tm = 128 Then freq = freq + 1
If tm = 192 Then freq = freq + 1
If tm = 0 Then freq = freq + 1
If freq > 2160 Then freq = 1750

fshow = freq / 20
fmod = freq Mod 20
fmod = fmod * 5

Lcdcmdout LcdHome
If fmod < 10 Then
Lcdout "-------", #fshow, ".0", #fmod, "Mhz "
Else

Endif
Wend
Endif
Lcdout "-------", #fshow, ".", #fmod, "Mhz "

'----------Here are down button-------------------------

If dn Then freq = freq - 1
While dn

tm = tm + 4

If tm = 64 Then freq = freq - 1
If tm = 128 Then freq = freq - 1
If tm = 192 Then freq = freq - 1
If tm = 0 Then freq = freq - 1

If freq < 1750 Then freq = 2160

fshow = freq / 20
fmod = freq Mod 20
fmod = fmod * 5

Lcdcmdout LcdHome
If fmod < 10 Then
Lcdout "-------", #fshow, ".0", #fmod, "Mhz "
Else

Endif
Wend
Endif

Lcdout "-------", #fshow, ".", #fmod, "Mhz "
Wend 'When ENTER Write EEPROM 10-11

Write 10, freq.LB
Write 11, freq.HB
change = 1
WaitMs 1000
'Endif ' NAME "Endif" I GOT ERROR HERE
'---------------------------------------------------------------------------------------------
TRISB = %00000000 'All PORTB output
'----------------------------make a loop -------------------------------------------------
loop:
If change Then 'IC PLL
'---------------------------Read EEPROM ---------------------------------------------
Read 10, freq.LB
Read 11, freq.HB
'------------------------------------------------------------------------------------------------
fshow = freq / 20
fmod = freq Mod 20
fmod = fmod * 5
Lcdcmdout LcdHome
If fmod < 10 Then
Lcdout "-------", #fshow, ".0", #fmod, "Mhz "
Else
Lcdout "-------", #fshow, ".", #fmod, "Mhz "
Endif
'------------------ IC PLL SAA1057--------------------------------------------------------
f5 = freq * 5
fhbyte = f5.HB
flbyte = f5.LB
'-------------------IC PLL --------------------------------------------------------------------
For t2 = 1 To 2
da = 0
ck = 0
ce = 0
WaitUs 5
ce = 1
ck = 1
WaitUs 5
ck = 0
'---------------------------------------Byte ---------------------------------------------------
For tm = 1 To 8
da = fhbyte.7
WaitUs 5
ck = 1
WaitUs 5
ck = 0
fhbyte = ShiftLeft(fhbyte, 1)
Next tm
'------------------------------------Byte-------------------------------------------------
For tm = 1 To 8
da = flbyte.7
WaitUs 5
ck = 1
WaitUs 5
ck = 0
flbyte = ShiftLeft(flbyte, 1)
Next tm
'--------------------------------Data status is 0----------------------------
da = 0
ce = 0
WaitUs 5
ck = 1
WaitUs 5
ck = 0
Next t2
'------------Option IC PLL SAA----------------
'send 1101_1101_1001_0101-Word b-> > saa1057 flbyte = %10010101

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
WaitUs 5
ck = 1
WaitUs 5
ck = 0
flbyte = ShiftLeft(flbyte, 1)
Next tm

da = 0
ce = 0
WaitUs 5
ck = 1
WaitUs 5
ck = 0
'-------------------------------------------------------------------------------------------------
change = 0
WaitMs 1000
tx_en = 1 'If ready (TX ON) Endif
'------------------------------------------------------------------------------------------------
ADCON1 = 0x00
'------------LCD----------------------------
'------------Calculation for a VU ----------------------------
Adcin 0, vu
vu = vu / 2
If vu > 14 Then vu = 14
'--------------------------------------------------------------
Adcin 1, power_1 'NAME "Power" GET ERROR ---> RF Power Output
'-------Calculation for RF Watt-----------------------
power_1 = power_1 * power_1
power_1 = power_1 / 50
power_show = power_1 / 10
power_mod = power_1 Mod 10
'-------------------------------------------------------------------------------------------------
ADCON1 = 0x06
Lcdcmdout LcdLine2Home
For tm = 0 To vu
Lcdout ">"
Next tm
Lcdout "I "
'-------------------Status LOCK---------------------
If Not lock Then
WaitMs 3000
'--------LOCK ------------
If Not lock Then
Lcdcmdout LcdHome
Lcdout "U" 'I GOT ERROR HERE
change = 1
WaitMs 500
Endif
Endif
'-----------------------------Reading for WATT-----------------------------------------------
Lcdcmdout LcdHome
Lcdout "= "
Lcdcmdout LcdHome
Lcdout "=", #power_show, ".", #power_mod, "w"
WaitMs 100
Goto loop 'looping
Endif
'---------------------------------End of 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:
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.

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.
 
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: 322
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.

Latest threads

New Articles From Microcontroller Tips

Back
Top