View previous topic :: View next topic |
Author |
Message |
Guest
|
C code examples needed |
Posted: Tue Aug 28, 2007 4:56 am |
|
|
i have tried writing some code but it is not worikng so have been looking for some c code example for a pic16f84a or pic16f628a chip but cant find any.
does anyone know where i could find any examples just simple beginners stuff. i tryed seaching google and the forum but no luck. thanks for any help |
|
|
dyeatman
Joined: 06 Sep 2003 Posts: 1934 Location: Norman, OK
|
|
Posted: Tue Aug 28, 2007 5:55 am |
|
|
One source is the examples in the PICC\EXAMPLES directory. Your Google
apparently is broken because I just Googled for 16F84 and found a number
of tutorials and lots of other example code. This looks like a good one:
http://www.mstracey.btinternet.co.uk/pictutorial/picmain.htm |
|
|
Guest
|
|
Posted: Tue Aug 28, 2007 3:42 pm |
|
|
that code has example that are in assembler and i was after ones in c. |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Tue Aug 28, 2007 3:53 pm |
|
|
To use Google to search for CCS code, search for a keyword or other
directive that's unique to CCS. For example "#use delay" is only used
by CCS. Try the following search strings in Google. (including the quotes).
Quote: | 16F84A "use delay" |
Quote: | 16F628A "use delay" |
A lot of the code that you find won't be very useful, but maybe some
of it will help. You would probably find more if you substituted a
more commonly used PIC, such as 16F877 or 16F877A. |
|
|
jecottrell
Joined: 16 Jan 2005 Posts: 559 Location: Tucson, AZ
|
|
Posted: Tue Aug 28, 2007 3:53 pm |
|
|
Broad questions don't usually elicit much of a response. Try narrowing it down for us.
Are you using the CCS compiler? (What version?)
Post some of your code that isn't working. (Don't be afraid... I find the more butchered my examples that I post, the more help I get.)
What exactly are you trying to do with your code? Flash and LED?
What hardware are you using to test your code?
Sign up. That will allow you to edit posts, etc. It also shows the pros around here that you're not just a drive by poster....
Good luck,
John |
|
|
umka
Joined: 28 Aug 2007 Posts: 99 Location: New Zealand
|
|
Posted: Tue Aug 28, 2007 8:08 pm |
|
|
ok here is the code i was trying to use so when there is a high on porta the corisponding port b would light an led. i am just after really simple stuff as i am new to pic and ccs compiler and just feeling my way around for now. i still cant really find anything useful code on the net for the chips i have i may just order a 16f877a. would that be a good chip to get?
code for led
#include <16f84a.h>
#fuses xt,nowdt,put,noprotect
#byte PORTA =0x05
#byte PORTB =0x06
#byte TRISA =0x85
#byte TRISB =0x86
void main(void)
{
TRISB=0x00; // make portb all outputs
TRISA=0x1f; // make porta all inputs
while(1) // always
{
PORTB=PORTA;
}
}
cheers mike |
|
|
treitmey
Joined: 23 Jan 2004 Posts: 1094 Location: Appleton,WI USA
|
|
Posted: Wed Aug 29, 2007 8:30 am |
|
|
Yes the 16f877a is a good chip. I use it in 80% of my projects.
- - -
Alot of times its better not to use fast_io.
You were trying to use it without the fast_io directive.
you also don't have a #use delay(clock) statement
Will this work?? I tested it with a 16F877 on PIC DEM 2 plus with 10MhZ
Code: |
#include <16F84a.H>
#fuses xt,nowdt,put,noprotect
#use delay(clock=10000000,RESTART_WDT)
//#use rs232(baud=19200,xmit=PIN_B3,invert,stream=debug)
#use rs232(xmit=PIN_C6,rcv=PIN_C7,enable=PIN_C5,baud=19200,stream=XSTREAM)
#case
#zero_ram
void main(void)
{
setup_adc_ports(NO_ANALOGS);
while(1) // always
{
output_b(input_a());
}
} |
|
|
|
umka
Joined: 28 Aug 2007 Posts: 99 Location: New Zealand
|
|
Posted: Wed Aug 29, 2007 6:49 pm |
|
|
the above code does not compile sorry treitmey. i have changed the code some more to try and make it even simpler.
#include <16f84a.h>
#fuses xt,nowdt,put,noprotect
#use delay(clock=4000000)
void main(void)
{
while(1) // always
{
if (pin_b2 == 1)
output_high(pin_a2);
else if (pin_b2 == 0)
output_low(pin_a2);
}
}
this compiles and loads but doesnt do anything when i try to use it on a breadbord. |
|
|
umka
Joined: 28 Aug 2007 Posts: 99 Location: New Zealand
|
|
Posted: Wed Aug 29, 2007 11:17 pm |
|
|
ok i have some action.
heres the code for the blinking led
Code: | #include <16f84a.h>
#fuses xt,nowdt,put,noprotect
#use delay(clock=4000000)
void main(void)
{
SET_TRIS_a( 0x00 );
while(1)
{
output_high(pin_a1);
delay_ms( 500 );
output_low(pin_a1);
delay_ms( 500 );
}
} |
this feels sooooooooooooo good. stoked its working now for some more complicated stuff. |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Wed Aug 29, 2007 11:24 pm |
|
|
One tip. You don't need to set the TRIS. Because you're using the CCS
output_low() and output_high() functions, the compiler will set the TRIS
for you. This is the standard operating mode of the compiler. You can
specify fast i/o mode with a #use statement, and then use a set_tris_a()
statement, but it's not necessary. It's easier to leave the compiler in
standard i/o mode and let it handle setting the TRIS. |
|
|
treitmey
Joined: 23 Jan 2004 Posts: 1094 Location: Appleton,WI USA
|
|
Posted: Thu Aug 30, 2007 8:12 am |
|
|
Should have known better then to try and just switch 16F877 to 16F84A
try this
Code: | #include <16F84a.H>
#fuses xt,nowdt,put,noprotect
#use delay(clock=10000000,RESTART_WDT)
//#use rs232(baud=19200,xmit=PIN_B3,invert,stream=debug)
//#use rs232(xmit=PIN_C6,rcv=PIN_C7,enable=PIN_C5,baud=19200,stream=XSTREAM)
#case
#zero_ram
void main(void)
{
//setup_adc_ports(NO_ANALOGS);
while(1) // always
{
output_b(input_a());
}
} |
|
|
|
umka
Joined: 28 Aug 2007 Posts: 99 Location: New Zealand
|
|
Posted: Thu Aug 30, 2007 6:38 pm |
|
|
cool cheers for the help PCM programmer and treitmey
i have the switchs working now had too add a line to the code to get it working properly so they could be toggled.
Code: | #include <16F84a.H>
#fuses xt,nowdt,put,noprotect
#use delay(clock=10000000,RESTART_WDT)
void main(void)
{
while(1)
{
port_a_pullups(true);
output_b(input_a());
}
} |
once again thanks guys [/code] |
|
|
|