A question regarding PIC programing in C

Thread Starter

Kmelhem

Joined Aug 31, 2010
15
sorry for being [qoute]pigheaded[/qoute] :p it all works now, but still i need help with interrupts because i dont want my program to keep scanning input and setting the bits, i just want it to set bits on input change, with the stimulus i was able to create my own interrupt on port c, what i had in mind is connect port c with port a, thus it will change with it, and the program was supposed to drive it low after changing bit on port b, but yet, program cant set input ports.
still searching for resources
 

Thread Starter

Kmelhem

Joined Aug 31, 2010
15
LOL nvm guys, it was a dumb idea, now am doing smth else, ill post it when am done, supposed to be enough for me and generate a software interrupt, which is basically saving the last input in a variable, and keep comparing current input with last one, jump to function if its not equal and sets PORTB bits, then save the last input again.
hope it works
 

Thread Starter

Kmelhem

Joined Aug 31, 2010
15
Rich (BB code):
#include <htc.h>
int portval=0;
void delay()
{
int i=0;
for (i=0;i < 20;i++) {
i++;
}
}
void dojob(void){
portval = PORTA;
switch (PORTA) {
case 0x01:
RB0=0; 
delay();
break;
case 0x02:
RB0=1;
delay();
break;
case 0x03:
RB1=0;
delay();
break;
case 0x04:
RB1=1;
delay();
break;
case 0x05:
RB2=0;
delay();
break;
case 0x06:
RB2=1;
delay();
break;
case 0x07:
RB3=0;
delay();
break;
case 0x08:
RB3=1;
delay();
break;
default :
delay();
break;
}
}
void main () {
  ADCON0=0;
  ADCON1 = 0b00000110;
  TRISA =0b11111111;
  TRISB =0b00000000;
  PORTB=0;
 while(1) {
if (PORTA != portval) dojob() ;
}
  
}
thats basically it, but the prob, is that i cant toggle the same pin of PORTB using the same input with this code. but i only need those combinations, so am kinda ok.
thx everybody
 
Rich (BB code):
#include <16F877A.H>
#define value    PORTA&0b00001111 
void init(void)
  {
  TRISA =0b00001111;
  TRISB =0b00000000;
  PORTA =0;
  PORTB =0;
  }
 
void main () {
 void init(void);
 
 while(true) {
 
  switch (value){
         case: 0b00000001
         If (PORTBbits.RB0==0)
         {
            PORTBbits.RB0=1;
         else
            PORTBbits.RB0=0;
         }
         break;
         case: 0b00000010
         If (PORTBbits.RB1==0){
              PORTBbits.RB1=1;
         else
              PORTBbits.RB1=0;
         }
  .....................ect
 

DonQ

Joined May 6, 2009
321
Rich (BB code):
  switch (value){
         case: 0b00000001
         If (PORTBbits.RB0==0)
         {
            PORTBbits.RB0=1;
         else
            PORTBbits.RB0=0;
         }
         break;
What does your compiler say about code like that?
 

Thread Starter

Kmelhem

Joined Aug 31, 2010
15
My compiler doesnt accept (PORTAbits.RA2), it accepts (RA2) directly.
and besides, vantusaonho, i tried ur solution way before u even post it, the problem is that it will keep toggling the output bits, since it is in an infinite loop, and i need some kind of interrupt for it to work will.
but in my solution, on input change, it will do what it is supposed to, but then if i enter the same input nothing will happen, thus i cant use same input to toggle output, so a certain input makes a pin high, and another input makes it low. anywayz ty for ur help.
 
1. You need to learn what Interrupts are and how they function. (Hint: They are PORTB Interrupts on that chip.) Read through the Datasheet to get an idea of how they work with your specific chip. Also, read through this FAQ: http://www.htsoft.com/support/faqs.php#faq49

2. You will have to program in some type of "debouncing". That may not mean much with a simulator, but when you work with real switches it will be extremely important.
 

Thread Starter

Kmelhem

Joined Aug 31, 2010
15
Am gonna first try this on a real pic, if it worked it will be perfect for me.
Anywayz here it is.
The ouput pins of the pic will be connected to relays and the input of this pic will be connected to the output of a DTMF IC which is connected to the speaker of a mobile device, another mobile will be sending DTMF keys to that mobile, DTMF circuit will analyze them to certain outputs, accordingly the pic will turn the relays on and off.
 

t06afre

Joined May 11, 2009
5,934
Is this a school project by the way, or is it something you do just for fun. Both are OK so to speak. Your project is doable. But it has some loose ends. Like have selected the DTMF decoder yet. Also you should also be aware of the fact that many DTMF decoders have a pin that gives out a pulse then the output has been updated. This may be useful in your application. For your own sake. You should write down a detailed description describing what to do. This example is taken from the book
Beginning C: From Novice to Professional, Fourth Edition, By Ivory Horton
Rich (BB code):
If the weather is good, I will go out in the yard. And if it’s cool enough, I will sit in the sun. Else I will sit in the shade. Else I will stay indoors. I will then drink some lemonade.​

Then take a look at the DTMF decoder chip and the PIC and make some case scenarios about which pins/ports to use. I guess you have done this already. Split your problem into blocks. Then you can go back to the MPLAB and and do some programming. Do not try to solve the whole program problem in one session. But solve separate blocks. Start with the blocks that you feel most comfortable with. Then as you gain skills you can take the harder ones. As a tip, perhaps the if else structure is not the best. Take a look at the switch break structure.​
 

Thread Starter

Kmelhem

Joined Aug 31, 2010
15
Thx alot t06afre... you have been of a great help to me :)
i havent bought the chips yet, going to in couple of days, but i wanted to see if i can make such a program before buying the chips, koz it will need some tweeking when the chips r bought.
and btw it is a school project, i hope it works koz it has a second part, which is a GPS system :S

Edit :- and btw i have used the switch structure in the program after having alot of trouble with the if structure
 
Last edited:
Top