|
|
View previous topic :: View next topic |
Author |
Message |
Wolfman
Joined: 25 Dec 2011 Posts: 2
|
PIC16F876 if I/O |
Posted: Sun Dec 25, 2011 2:18 pm |
|
|
Hello guys , I am starting to learn to use a PIC16F876 in order to make simple input Output commands combined with if and else if.
I am very new in C and I assume that you will see this immediately.
I want to do a very simple task:
If Pin A1 is high
it should switch PIN B1 B2 B3 and B4 to high.
If Pin A1 is low
it should switch PIN C1.
I used the following commands:
Code: |
#if defined(__PCM__)
#include <16F876.h>
#fuses HS,NOWDT,NOPROTECT,NOLVP
#use delay(clock=20000000)
#elif defined(__PCH__)
#include <18F452.h>
#fuses HS,NOWDT,NOPROTECT,NOLVP
#use delay(clock=20000000)
#endif
void main () {
if (input(PIN_A1))
output_high(PIN_B1);
output_high(PIN_B2);
output_high(PIN_B3);
output_high(PIN_B4);
if (!input(PIN_A1));
output_high(PIN_C1);
}
|
It would be very helpful for me to learn this if someone finds a problem inside this code!
Thank you so much |
|
|
SherpaDoug
Joined: 07 Sep 2003 Posts: 1640 Location: Cape Cod Mass USA
|
|
Posted: Sun Dec 25, 2011 2:47 pm |
|
|
This is not the best forum to learn elementary C programming, but here are a couple of things I notice:
1) This code will run through ONCE in a few microseconds and then the PIC will go to sleep. I suspect you want the code to run continuously. There are many ways to do this but a common one is to use a while(1) loop which loops as long as 1 is true...forever.
2) You set pins high, but you never set them low. Once set high they will stay high forever.
3) Rather than two if() statements I would use if and else. If you later change one of the if()s and forget to change the other, your code may get into a third state you had not anticipated where neither or both ifs are true.
4) Get yourself a real paper book on C programming. The C Programming Language by Kernighan & Ritchie ISBN 0-13-110362-8 is my favorite. _________________ The search for better is endless. Instead simply find very good and get the job done. |
|
|
Wolfman
Joined: 25 Dec 2011 Posts: 2
|
Will be done |
Posted: Sun Dec 25, 2011 2:54 pm |
|
|
Hello, thanks for the perfect answer, thats more than enough to get started for me! I will also order this book!
Thank you again for this answer, helps me quite a bit
I also recognized that somebody changed the color of my text and form of it! Thanks too
This should be a working code now:
Code: |
#if defined(__PCM__)
#include <16F876.h>
#fuses HS,NOWDT,NOPROTECT,NOLVP
#use delay(clock=20000000)
#elif defined(__PCH__)
#include <18F452.h>
#fuses HS,NOWDT,NOPROTECT,NOLVP
#use delay(clock=20000000)
#endif
void main () {
while(1)
if (input(PIN_A1))
{ output_high(PIN_B1);
output_high(PIN_B2);
output_high(PIN_B3);
output_high(PIN_B4);
}
else
{
output_high(PIN_C1);
}
}
|
|
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9228 Location: Greensville,Ontario
|
|
Posted: Sun Dec 25, 2011 4:10 pm |
|
|
Make life easier for yourself...
1) Since you're using the 876 PIC get rid of the #if defined...... and just use the 876 'setup ' code.
2) While you have your project open, press F11, and the CCS on screen help file will open. Keep it open! Very, very handy 'at your fingertips' reference on how CCS C is used.
3) Have a look at the examples that CCS supplies in the examples folder. You see how most things can be made to work.
4) When you 'revise' code, copy the old program to another file, make changes, test then save it. Repeat this often. Yes, you may have pgm1.c, pgm2.c, pgm45.c but you'll have a good record of what works and what doesn't. Just changing the same file over and over again will confuse you as a simple 'misplaced bracket' can cause you hours of grief !
5) Always add 'errors' to the use rs232(...) options. It'll keep the hardware UART from 'magically stopping'... |
|
|
SherpaDoug
Joined: 07 Sep 2003 Posts: 1640 Location: Cape Cod Mass USA
|
|
Posted: Sun Dec 25, 2011 10:17 pm |
|
|
That is a start but your while(1) needs brackets {} to define what code to loop.
Code: | while (1) {
put code to loop here
} |
Also you still never set the pins low, so once set high they will stay high forever. _________________ The search for better is endless. Instead simply find very good and get the job done. |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|