CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

C code examples needed

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
Guest








C code examples needed
PostPosted: Tue Aug 28, 2007 4:56 am     Reply with quote

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: 1923
Location: Norman, OK

View user's profile Send private message

PostPosted: Tue Aug 28, 2007 5:55 am     Reply with quote

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








PostPosted: Tue Aug 28, 2007 3:42 pm     Reply with quote

that code has example that are in assembler and i was after ones in c.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Tue Aug 28, 2007 3:53 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Tue Aug 28, 2007 3:53 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Tue Aug 28, 2007 8:08 pm     Reply with quote

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

View user's profile Send private message Visit poster's website

PostPosted: Wed Aug 29, 2007 8:30 am     Reply with quote

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

View user's profile Send private message

PostPosted: Wed Aug 29, 2007 6:49 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Wed Aug 29, 2007 11:17 pm     Reply with quote

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. Very Happy Very Happy Very Happy Very Happy Very Happy Very Happy Very Happy Very Happy Very Happy Very Happy Very Happy
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Wed Aug 29, 2007 11:24 pm     Reply with quote

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

View user's profile Send private message Visit poster's website

PostPosted: Thu Aug 30, 2007 8:12 am     Reply with quote

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

View user's profile Send private message

PostPosted: Thu Aug 30, 2007 6:38 pm     Reply with quote

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 Very Happy [/code]
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
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