Sonsivri
 
*
Welcome, Guest. Please login or register.
Did you miss your activation email?
November 24, 2024, 06:01:58 18:01


Login with username, password and session length


Pages: [1]
Print
Author Topic: PIC Programing Question (16F688)  (Read 5443 times)
0 Members and 1 Guest are viewing this topic.
madmax
Newbie
*
Offline Offline

Posts: 15

Thank You
-Given: 10
-Receive: 1


« on: February 21, 2008, 07:03:12 19:03 »

I have just started experiementing with PIC's and have been using the 16F688. My first couple of experiements were simply making LED's blink and that worked pretty well. I then tried to use a button to alter the blink rate but don't beleive I am initilizing the pin with the button correctly. The picbasic pro code I used is below. Is there something in the initialization that I needed to do besides the TrisA statement?
Thanks!

trisA =%00000010  ; set pin RA1 to input, the rest of port A to output.
   
main:
high 0      ; set RA0 (pin 13) High
pause 1000
low 0
pause 1000
PinCheck:
If portA.1 then delay              ;If RA1 (pin12) is high goto delay
goto main
delay:
high 0
pause 250
low 0
pause 250
goto pincheck
end
Logged
hemlig
Translator
Active Member
***
Offline Offline

Posts: 168

Thank You
-Given: 26
-Receive: 50



« Reply #1 on: February 21, 2008, 10:24:08 22:24 »

Hi,

I think you must change the line:
"If portA.1 then delay              ;If RA1 (pin12) is high goto delay"
to
If portA.1 = 1 then goto delay              ;If RA1 (pin12) is high goto delay

Then it would look like this:

Code:
trisA =%00000010  ; set pin RA1 to input, the rest of port A to output.
   
main:
high 0      ; set RA0 (pin 13) High
pause 1000
low 0
pause 1000
PinCheck:
If portA.1 = 1 then goto delay              ;If RA1 (pin12) is high goto delay
goto main
delay:
high 0
pause 250
low 0
pause 250
goto pincheck
end

I have not tested your code so I don't know if it will compile or not.
But checking the state of the pin is essential.
Logged

If it work don't fix it!
madmax
Newbie
*
Offline Offline

Posts: 15

Thank You
-Given: 10
-Receive: 1


« Reply #2 on: February 22, 2008, 12:59:55 12:59 »

Thanks, I will give it a try! I am pretty sure I need to perform some initialization of the pins I want to use because of comparators and ADC that share the pins. I guess I should have started with a simpilier chip that didnt have these features. I will read the application notes and if I find the solution will post it.
-MadMax
Logged
bbarney
Moderator
Hero Member
*****
Offline Offline

Posts: 2429

Thank You
-Given: 405
-Receive: 545


Uhm? where did pickit put my mute button


« Reply #3 on: February 22, 2008, 05:01:25 17:01 »

some common problems are related here

Quote
PortA Doesn't Work
Parts of PortE do not work
Oh yes it does. It might need to be switched ON for Digital use though.
Whilst you weren't looking, Microchip have been busy stuffing the PICs full of goodies. Some of these goodies are ANALOG interface parts. A/D Convertors (eg 16F876) or Comparators (eg 16F628), or even BOTH (eg 16F877A and 12F675). Mostly (but not always), the Analog hardware is multiplexed with PortA (and PortE).

By default, on many PICs which have these Analog parts, when the PIC powers up, it defaults into Analog mode (the controlling Registers are usually set to zero) – which is why you can't do Digital I/O until you set that port into Digital mode.

If your PIC has Comparators…

Then the Comparators are most likely controlled by the CMCON Register. This is most common thing that is overlooked by people transferring programs over from one PIC to another (for example from a 16F84 to a 16F628).

CMCON=%00000111

Will normally switch the Analogue Comparator pins to Digital I/O mode. (CMCON=$07 or CMCON=7 will do the same thing). Go check with your PICs Datasheet to see what setting the CMCON Register to this value does.


If your PIC has A/D Convertors then…

The A/D Convertors are controlled usually by the ADCON1 Register (again check with your PICs datasheet – eg it’s ANSEL with the 12F series).

ADCON1=%00000111

Will normally switch the Analogue A/D pins to Digital I/O mode. (ADCON1=$07 or ADCON1=7 will do the same thing). Go check with your PICs Datasheet to see what setting the ADCON1 Register to this value does.


If your PIC has BOTH A/D Convertors and Comparators…

The you’ll need to set both ADCON1 and CMCON appropriately.

Example 1 - if you have a 12F like a 12F675, then

ANSEL=%00000000
CMCON=%00000111

Is the sequence for you.

Example 2 - if you have a 16F628 then all you need is...

CMCON=%00000111

Example 3 - if you have a 16F876 or 16F877 then this one is for you...

ADCON1=%00000111

ALWAYS check the correct Register settings for your own needs, by looking in the Comparator Section, and/or the A/D Converter Section in your PICs Datasheet.
Logged

Ever wonder why Kamikaze pilot's wore helmet's ?
madmax
Newbie
*
Offline Offline

Posts: 15

Thank You
-Given: 10
-Receive: 1


« Reply #4 on: February 22, 2008, 05:07:07 17:07 »

After reading the app notes and searching some forums I got my program to work by adding some initilization strings at the begining. I'm not sure if all of them are needed however most of them are required to get the pins to function as I want (digital I/O). Now that I am aware of the need for them I can experiement and determine how and when to best utilize them. The final code I used for this experiment is below:

  
    Ansel=%00000000           ;sets a/d pins to digital
    Cmcon0=%00000111          ; sets comparator pins to digital
    Adcon1=%00000111          ;sets a/d pins to digital
    Trisa=%11111110           ;sets all porta pins (except 0) to inputs
    

      
MAIN:  
PinCheck:
   if PortA.1 then delay
   High 0          ' Turn on LED connected to PORTB.0
   Pause 100
   Low 0           ' Turn off LED connected to PORTB.0
   Pause 100
      
   Goto main
  
   delay:
   high 0
   pause 1000
   low 0
   pause 1000
   goto pincheck  
   End

The program works as expected except that at present the program always enters the delay routeen and will use the main routeen when pin 1 is pulled low. I will examine the documents for pin one to see if there is an enternal pull-up resistor that needs to be switched off, otherwise I can correct the operation of the circuit by adding a pull-down resistor at pin 1 so that closing a switch forces pin 1 high. I know this is pretty basic stuff to most of you out there but hope that someone finds this post useful.

Posted on: February 22, 2008, 06:05:14 18:05 - Automerged

Thanks for the post! That forum content that you posted was one of the areas that I found and help set me on the right track. Thanks for your help! Cool
Logged
bbarney
Moderator
Hero Member
*****
Offline Offline

Posts: 2429

Thank You
-Given: 405
-Receive: 545


Uhm? where did pickit put my mute button


« Reply #5 on: February 22, 2008, 05:25:19 17:25 »

Those register setting's seem to screw up everybody when they first start and Microchip is always changing them so it's important to read the datasheet for every chip your using to save yourself some hiccup's Grin Grin
Logged

Ever wonder why Kamikaze pilot's wore helmet's ?
Pages: [1]
Print
Jump to:  


DISCLAIMER
WE DONT HOST ANY ILLEGAL FILES ON THE SERVER
USE CONTACT US TO REPORT ILLEGAL FILES
ADMINISTRATORS CANNOT BE HELD RESPONSIBLE FOR USERS POSTS AND LINKS

... Copyright © 2003-2999 Sonsivri.to ...
Powered by SMF 1.1.18 | SMF © 2006-2009, Simple Machines LLC | HarzeM Dilber MC