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.

compiled code not working properly

Status
Not open for further replies.
PIC Simulator IDE, PIC 12F683.
When I use the simulator on this simple test, the only pin that goes high is GP0, the last output in the series. So I reversed the order of the outputs, ie., GP0=1, GP1=1, etc. Again, the only output to go high is the last in the series, ie., GP4.
Can someone please explain why only one output goes high while the others remain low?
TRISIO = %00001000
GPIO = %00000000
GP4 = 1
GP2 = 1
GP1 = 1
GP0 = 1
End
 
My guess is they're set with BSF command which reads port, sets one bit, then writes back. If you don't configure pins as digital (on most PICs they're analog by default), this command will always read zero, so only the last bit get set. You need to configure all these pins as digital.
 
My guess is they're set with BSF command which reads port, sets one bit, then writes back. If you don't configure pins as digital (on most PICs they're analog by default), this command will always read zero, so only the last bit get set. You need to configure all these pins as digital.
You know, I've been tearing my hair out all day on this. Thank you so much for sorting it out. Works like a dream.
 
Setting the pins to digital might work under most circumstances, but I believe that best practice is to avoid using read-modify-write operations on ports. In the higher end uC's you have the latch registers specifically for this purpose, if you can spare the RAM and tolerate the minor performance hit I would suggest setting up variables for each port, performing your manipulations on them, and then writing them to PORTx.
 
Setting the pins to digital might work under most circumstances, but I believe that best practice is to avoid using read-modify-write operations on ports. In the higher end uC's you have the latch registers specifically for this purpose, if you can spare the RAM and tolerate the minor performance hit I would suggest setting up variables for each port, performing your manipulations on them, and then writing them to PORTx.

I'm a novice at this game, heydonms, and so I imagine I've completely misinterpreted your input, 'cos my attempt doesn't work; it produces the same result as my original code. It might be cheeky of me to ask, but could you very kindly provide an example of what you mean, using the same micro type and ports that I used.
 
you could use AllDigital command to all I/Os to digital (turning off ADC and Comprator functions):

Code:
AllDigital
TRISIO = %00001000
GPIO = %00000000
GP4 = 1
GP2 = 1
GP1 = 1
GP0 = 1
End
 
I'm a novice at this game, heydonms, and so I imagine I've completely misinterpreted your input, 'cos my attempt doesn't work; it produces the same result as my original code. It might be cheeky of me to ask, but could you very kindly provide an example of what you mean, using the same micro type and ports that I used.

I've never used the 12F series and I don't even recognise the language you are using (% as a prefix of a binary constant? some Pascal variation or something?) so I'm not going to try to give a working example but the general idea is that the BSF and BCF operations (as well as a few others) read the value, change the relevant bit and then write it back but the value that is read isn't the value that you wrote to the port, it is the actual digital value present on the port. Normally these should be the same but there are situations where they can be different (e.g. a very low impedance path between the pin and ground will pull it low even if you set it high).

Say you wanted to set pin 3 of port A high and pin 4 low, instead of doing:

BSF PORTA, 3
BCF PORTA, 4

You should do:

BSF MyPORTA, 3
BCF MyPORTA, 4
MOVF MyPORTA, W
MOVWF PORTA

Because MyPORTA is just a normal variable, the data you read from it will always be equal to what you wrote to it, then every time you change MyPORTA you copy the new value into the real PORTA.

If you want to give it a shot, and if you get stuck post your code here we can have a look at it.

EDIT: Oops, didn't realise PIC Simulator had a BASIC compiler built in, so I guess that is your language. Anyway, I'm not familiar with their syntax, so I'm still not going to try to write code for it.
 
Last edited:
you could use AllDigital command to all I/Os to digital (turning off ADC and Comprator functions):

Code:
AllDigital
TRISIO = %00001000
GPIO = %00000000
GP4 = 1
GP2 = 1
GP1 = 1
GP0 = 1
End
Thanks. Actually, NorthGuy in this thread provided the same solution, and of course it works fine. However, heydonms in the same thread suggests this may not be the best approach and that it is preferable to, well, please read his response above. My interpretation of his suggestion is as shown , and doesn't work. So I believe I misinterpreted him. I'm hoping he will provide an example of what he means in terms of the micro I'm using, the PIC 12F683:
TRISIO = %000001000
GPIO = %00000000
Dim i As Bit
Dim j As Bit
Dim k As Bit
Dim m As Bit
i = 1
j = 1
k = 1
m = 1
GP4 = i
GP2 = j
GP1 = k
GP0 = m
End
 
TRISIO = %000001000
GPIO = %00000000
Dim i As Bit
Dim j As Bit
Dim k As Bit
Dim m As Bit
i = 1
j = 1
k = 1
m = 1
GP4 = i
GP2 = j
GP1 = k
GP0 = m
End

You are still setting each of the bits individually. I don't know how to do it in their flavour of BASIC, but you need to write to the entire GP register in one go rather than manipulating individual bits. Something like:

Dim MyGP as byte
MyGP.0 = 1
MyGP.1 = 1
MyGP.2 = 1
MyGP.3 = 1

GP = MyGP

I've just made up the syntax so don't just copy that code, you need to go find out how to manipulate the bits in a byte.
 
I get you. TRISIO sets the register mode (zeros set the pins to outputs, 1's to inputs); GPIO sets the pins High or Low directly in one go. Works ok. Thanks a lot.


TRISIO = %000001000
GPIO = %00010110
End
 
I get you. TRISIO sets the register mode (zeros set the pins to outputs, 1's to inputs); GPIO sets the pins High or Low directly in one go. Works ok. Thanks a lot.


TRISIO = %000001000
GPIO = %00010110
End

Sorry, heydonms, I forgot to quote you. this of course was a response to your thoughts about addressing the GP register directly.
 
Food for thought...
The method heydoms recomended is the proper way to do it when you are working with assembly or directly with the registers. But part of the reason to use higher level languages is to get a level of abstraction from the registers.

Using ALL DIGITAL provides a great level of abstraction. Using gommands of OUTPUT HIGH and OUTPUT LOW also provide you a level of abstraction. Not saying there is a good way or a bad way. Just pointing that there are different ways to skin the same cat.
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top