Author Topic: NEED URGENT HELP FOR C PROGRAMMING (PIC 16F877A)  (Read 5474 times)

0 Members and 1 Guest are viewing this topic.

Offline katherinaTopic starter

  • Jr. Member
  • **
  • Posts: 29
  • Helpful? 0
NEED URGENT HELP FOR C PROGRAMMING (PIC 16F877A)
« on: September 11, 2007, 02:15:11 AM »
HELP.... :(
I'M SO LOST NOW...
please some1 teach me how to do C programming for PIC 16F877A.
I'm doing a water level control, which:
1. the LED is ON when the water level is low (sensor for low water level is activated) and
2. the LED and BUZZER is ON when the water level is high (sensor for high water level is activated)....

This is all done using educational kit in my college lab...

It seems simple, but i don't even know the basic and the 1st step how to start do my programming, buy i do have some basic on C language.

please help me... thanx...

Offline Admin

  • Administrator
  • Supreme Robot
  • *****
  • Posts: 11,658
  • Helpful? 169
    • Society of Robots
Re: NEED URGENT HELP FOR C PROGRAMMING (PIC 16F877A)
« Reply #1 on: September 11, 2007, 07:42:55 AM »
I used the PIC16F877 on a lot of my older robots . . . you can find source code to help you here:
http://www.societyofrobots.com/robot_omni_wheel.shtml
http://www.societyofrobots.com/robot_sumo.shtml

What compiler are you using? This might help if you dont have one yet.

And what programmer are you using?

(You compile the code to a .hex file, then use a programmer to upload the file to the PIC)

Offline katherinaTopic starter

  • Jr. Member
  • **
  • Posts: 29
  • Helpful? 0
Re: NEED URGENT HELP FOR C PROGRAMMING (PIC 16F877A)
« Reply #2 on: September 25, 2007, 03:22:10 AM »
tq, im using PICC

Offline Admin

  • Administrator
  • Supreme Robot
  • *****
  • Posts: 11,658
  • Helpful? 169
    • Society of Robots
Re: NEED URGENT HELP FOR C PROGRAMMING (PIC 16F877A)
« Reply #3 on: September 25, 2007, 06:29:27 AM »
Where are you stuck at? Meaning, what do you have done, and what parts are you clueless about?

Have a program yet? What does this kit have done for you already? Did it come with sample code?

Offline creedcradle

  • Full Member
  • ***
  • Posts: 92
  • Helpful? 0
  • Physics Robotics
Re: NEED URGENT HELP FOR C PROGRAMMING (PIC 16F877A)
« Reply #4 on: September 25, 2007, 08:06:06 PM »
I guess what you have to do first is try to check the datasheet of PIC16F87XA ...

you are right , it is simple... but when you don't know where to start, it makes it diffifult..

so with us here, how can we give you ideas if we don't know where are you starting at?

give us more details  ;)
LEARN TO SHARE and SHARE TO LEARN!

Offline katherinaTopic starter

  • Jr. Member
  • **
  • Posts: 29
  • Helpful? 0
Re: NEED URGENT HELP FOR C PROGRAMMING (PIC 16F877A)
« Reply #5 on: September 27, 2007, 03:52:23 AM »
THANX... i will paste what i have done a bit later...
i dunno wat im doin...

Offline katherinaTopic starter

  • Jr. Member
  • **
  • Posts: 29
  • Helpful? 0
Re: NEED URGENT HELP FOR C PROGRAMMING (PIC 16F877A)
« Reply #6 on: October 02, 2007, 03:25:09 AM »
this is my program dat i got from a book, but how do i interface it?

im using photosensor....

n i dun think its complete....

Quote
#include <PIC16F877A.h>
void MSDelay (unsigned int);
#define Psensor PORTBbits.RB1
#define buzzer PORTCbits.RC7
void main (void)
     {
          TRISBbits.TRISB1 = 1;  //PORTB.1 as an input
          TRISCbits.TRISC7 = 0;  //make PORTC.7 an output
         
          while (Psensor ==1)
                {
                  buzzer = 0;
                  MSDelay (200) ;
                  buzzer = 1;
                  MSDelay (200) ;
                }
          while(1);              // stay here forever
         
          }
         
          void MSDelay (unsigned int itime)
          {
               unsigned int i;
               unsigned char j;
               for (i=0;i<itime;i++)
                   for (j=0;j<165;j++)
         

wats dat 165 for??/

Offline paulstreats

  • Supreme Robot
  • *****
  • Posts: 1,381
  • Helpful? 21
Re: NEED URGENT HELP FOR C PROGRAMMING (PIC 16F877A)
« Reply #7 on: October 02, 2007, 07:08:22 AM »
the 165 is clock dependant. In your book, does it say which crystal or oscillator frequency they are using? This is likely to be a different value if you are using 4mhz, 10mhz, 20 mhz crystal etc...

It is used to count us into ms timings.

Because this isnt a strict time dependant circuit or code, it doesnt really matter for you its probably best to just use what they say

(strictly speaking, you shouldnt need the timer delays anyway)

also this code will need editing for you, since there is only 1 sensor input for this. the code will only work if water is detected at the top level. and will halt at doing nothing forever otherwise.



Offline paulstreats

  • Supreme Robot
  • *****
  • Posts: 1,381
  • Helpful? 21
Re: NEED URGENT HELP FOR C PROGRAMMING (PIC 16F877A)
« Reply #8 on: October 02, 2007, 07:26:21 AM »
try this:
Code: [Select]
#include <PIC16F877A.h>
void MSDelay (unsigned int);
#define Psensorlow PORTBbits.RB1 //attach low sensor to pin b1
#define Psensorhigh PORTBbits.RB2 //attach high sensor to pin b2
#define led PORTCbits.RC6 //attach led to pin rc6
#define buzzer PORTCbits.RC7 //attach buzzer to pin c7
void main (void)
     {
          TRISBbits.TRISB1 = 1;  //PORTB.1 as an input
          TRISBbits.TRISB2 = 2; //PORTB.2 as an input
          TRISCbits.TRISC7 = 0;  //make PORTC.7 an output
          TRISCbits.TRISC6 = 0; //make PORTC.6 an output         

          while (1) //do this loop for ever
                {
                  if(Psensorlow == 1){ //if the low level sensor detects water
                          if(Psensorhigh == 1){ //if the high sensor is active
                          buzzer = 1; //turn on the buzzer
                          led=0; //make sure the led is off
                          MSDelay (200) ;//wait for 200 ms
                          buzzer = 0; //turn the buzzer off (this will make the buzzer intermittent)
                                           //or may be needed for pwm to the buzzer circuit?
                          MSDelay (200) ;//wait for 200 ms
                          } //end of if high sensor is active
                          if(Psensorhigh == 0){ //if high sensor is not activated
                          buzzer =0; //make sure the buzzer is off
                          led =1; //switch the led on
                          } //end of if high sensor is not active
                   }//end of if low level active                         
                  }//end of while loop
         
         
          }//end of main
         
          void MSDelay (unsigned int itime)
          {
               unsigned int i;
               unsigned char j;
               for (i=0;i<itime;i++){
                   for (j=0;j<165;j++);
               }
            }



this code can be optimized to work better, but this should be fine for you.
it is worth noting that different c compilers use slightly different jargon for when interfacing to TRIS and PORT etc.. on the pic.
if you can find out which c compiler you will be using, we can let you know. The above definitions wont work with them all.

Offline katherinaTopic starter

  • Jr. Member
  • **
  • Posts: 29
  • Helpful? 0
Re: NEED URGENT HELP FOR C PROGRAMMING (PIC 16F877A)
« Reply #9 on: October 09, 2007, 02:56:46 AM »
Quote
the code will only work if water is detected at the top level. and will halt at doing nothing forever otherwise.

i dun understand.. plz explain what do u mean by this?

tq for helping... 

i'm wondering why i cant compile the program in my compiler, even simple ones like LED flashing???

I'm using PCWH (PICC Compiler) and WIN PIC 800 to complile it to the PIC...

huhu... We've been trying to compile the program using different PCs and try to uninstall the compiler too. I'm wondering what's the problem...

plus, we have to present this very soon (in a week time)...   :o

we really need help...

Offline paulstreats

  • Supreme Robot
  • *****
  • Posts: 1,381
  • Helpful? 21
Re: NEED URGENT HELP FOR C PROGRAMMING (PIC 16F877A)
« Reply #10 on: October 09, 2007, 11:18:07 AM »
are you getting any error messages?

i think the ide you have uses the ccs c compiler, make sure the pin definitions are like:

#define Psensorlow RB1

and not

#define Psensorlow PORTBbits.RB1

also just use tris registers like:
TRISB1 = 0

and not

TRISBbits.TRISB1 = 0

Offline katherinaTopic starter

  • Jr. Member
  • **
  • Posts: 29
  • Helpful? 0
Re: NEED URGENT HELP FOR C PROGRAMMING (PIC 16F877A)
« Reply #11 on: October 23, 2007, 04:32:52 AM »
This is the program that i made:

Quote
#include <16f877A.h>
#use delay (clock = 20000000)
#fuses hs, noprotect, nowdt, nolvp

#byte PORTA = 5
#byte PORTB = 6
#byte PORTC = 7

main()
{
 set_tris_a(1);
 set_tris_b(0);
 set_tris_c(0);
 do{
              if (input(PIN_A1)==0)        // condition when switch 1 is pressed
              {
                    output_high(PIN_B1);   // buzzer on
                    portc = 0x1;           // led at Pin C0 on
                                        }
               else
              {       portc = 0xaa;        // led at port C on alternately
                      output_low(PIN_B1);  // buzzer off
                      output_low(PIN_C0);  // led at pin C0 off
                                         }
                                            }
 while (1);
}


how do i add interrupts to this program..?
I want all LED at port C to turn on when another switch is pressed?
How do i start do the interrupt routine?
I dun understand...
TQ

Offline katherinaTopic starter

  • Jr. Member
  • **
  • Posts: 29
  • Helpful? 0
Re: NEED URGENT HELP FOR C PROGRAMMING (PIC 16F877A) add Interrupt
« Reply #12 on: October 23, 2007, 08:07:42 PM »
the interrupt that i want to do is to blink the LED when the 'tank cover' is open.  I dun even know the basic how to insert interrupt to the program..

Offline Admin

  • Administrator
  • Supreme Robot
  • *****
  • Posts: 11,658
  • Helpful? 169
    • Society of Robots
Re: NEED URGENT HELP FOR C PROGRAMMING (PIC 16F877A)
« Reply #13 on: January 26, 2008, 09:46:37 AM »
did you ever get it to work?

 


Get Your Ad Here