Please help about sensors as input in PIC16F877A

Status
Not open for further replies.

UyAb

Junior Member level 2
Joined
Mar 6, 2013
Messages
20
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Location
Tacloban City, Philippines
Visit site
Activity points
1,416
I have an IR sensor..
I want it to be an input to my microcontroller..
How would I write the codes if my sensor is in PortA and my output is in PortC?
 

Compiler? Sensor - Analog - Digital? If analog, connect it to Analog channel (PORTA), setting ADC, and read ADC.

For example (this is for PIC16F1827):

main(){

//initiate ADC:

Setting PORT A bits as Analog Inputs (if Sensor Outputs connected to RA0, RA1 AND RA2):

TRISA = 0b00000111;

// setting ANSEL
ANSELA = 0b00000111;

init_ADC();

int val_X_Initial = read_ADC(0); //Read Channel 0 (AN0/RA0)
int val_Y_Initial = read_ADC(1); //Read Channel 1 (AN1/RA1)
int val_Z_Initial = read_ADC(2); //Read Channel 2 (AN2/RA2)

}
void init_ADC(){

ADCS0 = 1; // ADCS<1:0>: A/D Conversion Clock Select bits when ADCS2 = 0;
ADCS1 = 0; // 01 = FOSC/8

ADCON1 = 0b10100000; // bit 0 - 3 -> undefined
// bit 4-5 -> 10 -> voltage ref is +Vref and Vss (VCFG0& VCFG1)
// bit 6 -> 0 -> A/D clock divide by 2 is disabled (ADCS2 = 0)
// bit 7 -> 1 -> ADFM -> result format -> right justified

ADON = 1; // Turn A/D on

__delay_us(20); // delay 20 usec to settle A/D acquisition

}

unsigned int read_ADC(unsigned char channel_ADC){

__delay_us(20); // time interval required between two Consecutive ADC channel reads

if (channel_ADC == 0){ // X - direction
CHS2 = 0; // CHS<2:0>: (000) Analog Channel Select bits; Channel 0 (RA0/AN0)
CHS1 = 0;
CHS0 = 0;
}

if (channel_ADC == 1){ // Y - direction
CHS2 = 0; // CHS<2:0>: (001) Analog Channel Select bits; Channel 1 (RA1/AN1)
CHS1 = 0;
CHS0 = 1;
}

if (channel_ADC == 2){ // Z - direction
CHS2 = 0; // CHS<2:0>: (010) Analog Channel Select bits; Channel 2 (RA2/AN2)
CHS1 = 1;
CHS0 = 0;
}

ADGO = 1; // Start conversion

while ( ADGO ); // wait for ADGO to go off signalling end of conversion

//return (ADRESH);
return (((unsigned int)ADRESH)<<8)|(ADRESL); // 10 bit resolution - 1024 steps
}

Hope this help.
 

i"m using MicroC 8.1.
But I can't understand the codings.
by d way here is my codes..
can you help me finish this?
one of my problem is..the timer goes like this..1:23,1:22,1:21,1:20,1:13,1:12..
it always goes back to it's initial value..
Code:
char message1[]="INSERT BARCODE";
char message2[]="WELCOME USER";
char message3[]="Enter Time(Hr/s)";
char message4[]="CLASSROOM";
char message5[]="POWER CONTROL";
char message6[]="Press * or #";
char message7[]="*:Start #:Cancel";
unsigned short kp;
char code[3],code1[1];
short i=0,h=0,m2=0,m1=0;
unsigned int delay=60000;
void enter_hours()
{
     kp=0; //reset key code variable
       do
       kp= Keypad_Released();  //store key code in variable
       while (!kp);     //prepare value for output,transform key to it's ASCII value
       switch(kp)
       {
        case 1: kp=49;break; //1
        case 2: kp=50;break; //2
        case 3: kp=51;break; //3
        case 5: kp=52;break; //4
        case 6: kp=53;break; //5
        case 7: kp=54;break; //6
        case 9: kp=55;break; //7
        case 10: kp=56;break;//8
        case 11: kp=57;break;//9
        case 14: kp=48;break;//0
       }
     code[i]=kp;
     Lcd_Chr(2,i+1,code[i]);
     Lcd_Chr_Cp(':');
     i++;
}
void enter_min10s()
{
     kp=0; //reset key code variable
       do
       kp= Keypad_Released();  //store key code in variable
       while (!kp);     //prepare value for output,transform key to it's ASCII value
       switch(kp)
       {
        case 1: kp=49;break; //1
        case 2: kp=50;break; //2
        case 3: kp=51;break; //3
        case 5: kp=52;break; //4
        case 6: kp=53;break; //5
        case 14: kp=48;break;//0
       }
     code[i]=kp;
     Lcd_Chr(2,3,code[i]);
     i++;
}
void enter_min1s()
{
     kp=0; //reset key code variable
       do
       kp= Keypad_Released();  //store key code in variable
       while (!kp);     //prepare value for output,transform key to it's ASCII value
       switch(kp)
       {
        case 1: kp=49;break; //1
        case 2: kp=50;break; //2
        case 3: kp=51;break; //3
        case 5: kp=52;break; //4
        case 6: kp=53;break; //5
        case 7: kp=54;break; //6
        case 9: kp=55;break; //7
        case 10: kp=56;break;//8
        case 11: kp=57;break;//9
        case 14: kp=48;break;//0
       }
     code[i]=kp;
     Lcd_Chr(2,4,code[i]);
     i++;
}
void select()
{
     kp=0; //reset key code variable
       do
       kp= Keypad_Released();  //store key code in variable
       while (!kp);     //prepare value for output,transform key to it's ASCII value
       switch(kp)
       {
        case 13: kp=42;break;//*
        case 15: kp=35;break;//#
       }
     code1[i]=kp;
}
void input_time()
{
 Lcd_Cmd(Lcd_Clear);
   Lcd_Out(1,1,message3);
   enter_hours();
   enter_min10s();
   enter_min1s();
   delay_ms(500);
}
void display_time()
{
 Lcd_Chr(2,8,h+48);
 Lcd_Chr(2,9,':');
 Lcd_Chr(2,10,m2+48);
 Lcd_Chr(2,11,m1+48);
}
void buzzer()
{PORTC.F7=1;
delay_ms(10000);
}
void countdown_timer()
{
 Lcd_Cmd(Lcd_Clear);
 Lcd_Out(1,1,"Time Left:");
 Lcd_Out(2,14,"H:M");
 for(h=code[0]-48;h>-1;h--)
 {display_time();
   for(m2=code[1]-48;m2>-1;m2--)
   {display_time();
     for(m1=code[2]-48;m1>-1;m1--)
     {display_time();
      delay_ms(1000);
     }
   }
 }
}

void main()
{ADCON1=1;
 TRISA=1;
 TRISC=0;
 PORTA=0;
 PORTC=0;
 Keypad_Init(&PORTD);
 Lcd_Init(&PORTB);
 
 Preload:
 Lcd_Cmd(Lcd_Clear);
 Lcd_Cmd(Lcd_Cursor_Off);
 Lcd_Out(1,2,message1);
 delay_ms(3000);
 {
  do
   {PORTA=0;
    if(PORTA.F2==1&&PORTA.F3==1&&PORTA.F4==1)goto Start
   }while(1);
 }

 Start:
 Lcd_Cmd(Lcd_clear);
 Lcd_Out(1,2,message2);
 delay_ms(2000);
 input_time();
 Lcd_Cmd(Lcd_clear);
 Lcd_Out(1,1,message6);
 Lcd_Out(2,1,message7);
 {
  do
  {
   i=0;
   select();
   if(code1[0]==42)goto Start1
   if(code1[0]==35)goto Start
  }while(1);
 }

 Start1:
 Lcd_Cmd(Lcd_Clear);
 Lcd_Out(1,4,message4);
 Lcd_Out(2,2,message5);
 delay_ms(1500);
 countdown_timer();
 buzzer();
}
 

IR sensor output is high 5v or low 0v (digital).

**broken link removed**
 


Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
unsigned int seconds = 0;
unsigned char strSec[5];
 
void main() {
 
    while(1) {
 
        //Decrement seconds
        seconds--;
        strSec[0] = seconds/100 + 48;
        strSec[1] = (seconds/10)%10 + 48;
        strSec[2] = (seconds/1)%10 + 48;
        strSec[3] = '\0';
        //send strSec to lcd display function as argument.  
 
 
    }
 
}

 
Reactions: UyAb

    UyAb

    Points: 2
    Helpful Answer Positive Rating
please tell me about a good book to learn pic16877a programming, i am good in programming of 8051 but dont know about pic16f877a
 

Status
Not open for further replies.

Similar threads

Cookies are required to use this site. You must accept them to continue using the site. Learn more…