Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Need help in C programming

Status
Not open for further replies.

ycho

New Member
Hi,

I am connected a missing pulse detector to pin2(RA0) of a PIC16f877a.
Basically the missing pulse detector will always given high output, and change to low output if sense missing pulse.It will change back to high after a short period. I need to write a program when the output change to low, the LCD will display number = 1. And the number will increase by one every time the output change to low. Below is the program i wrote, but it doesn't work.
Code:
#include <16f877a.h>
#use delay(clock=20000000)
#fuses hs,noprotect,nowdt,nolvp
#define use_portb_lcd TRUE 
#include <lcd.c>
#define PWR_LED Pin_A5
#define IR1 Pin_A0

void main()
{  
   int interrupt = 0;
    lcd_init();

     {
   if (input(IR1) == True)  /this code is used for check H or L using a LED
     output_high(PWR_LED);
     else
     output_low(PWR_LED);
     
      lcd_putc("\fAutomatic Room");
      lcd_putc("\nLight Controller");
      delay_ms(5000);
      lcd_putc("\f");
      
     
      lcd_gotoxy(1,1);
      lcd_putc("People in = \n");
      
      if(input(IR1)==0)   / if the input is low
      {
         interrupt+=1;         increment by one
         lcd_gotoxy(13,1);   display on first row,13th column
         lcd_putc(interrupt);
      }
   }while(1);
}


if (input(IR1) == True) /this code is used for check High or Low using a LED
output_high(PWR_LED);
else
output_low(PWR_LED);
the problem i facing is the above code couldn't work if combine with the LCD code. It could work if the code compile separately.
the next problem is below code didn't show any response in the LCD.
if(input(IR1)==0) / if the input is low
{
interrupt+=1; increment by one
lcd_gotoxy(13,1); display on first row,13th column
lcd_putc(interrupt);
}
}while(1);

Thanks for the help
 
You must have graphical LCD ?

13 it row the 1 would at the start of row 13

1,1 is line one first digit

13,1 would be Line 13 first digit.

And the LCD will right off screen
 
Last edited:
Do you know why those 2 code cannot work when combine together?

do you seen any problem regarding the code for counting number of low input?
 
Start with what kind of LCD your using get that working then add the rest.

And what compiler your using.

Your code doesn't set PORTA.0 to input. so it will always read high
 
Last edited:
I am using 2*16 LCD. I already test it and it works fine.
I am using PCW.
how to set PORTA.0 to input?
set_tris_a(0b00000001);?
 
You need that, and to turn off the ANALOG-TO-DIGITAL
CONVERTER ADCON1 = 0x06

I haven't used PCW
 
Last edited:
i added
ADCON1 = 0x06
but there is an error, Undefined identifier ADCON1.
how to fix it?
do you mean i need to include both of the code below?
set_tris_a(0b00000001);
ADCON1 = 0x06;
 
Some thing has to tell it to turn off the ADC and set your pin to input. Now how you do that I'm not sure.

I haven't used PCW I have it as a demo but never tried it I have use C18 and hi-tech and mickroC and Boost

But not PCW
 
You set it to input like this sample

Code:
while ( !input(PIN_B1) );

// waits for B1 to go high

 

if( input(PIN_A0) )

   printf("A0 is now high\r\n");

 

int16 i=PIN_B1;

while(!i);

//waits for B1 to go high

This how you turn adc off
ADC_OFF

Code:
////////////////////////////////////////////////////////////////// ADC
// ADC Functions: SETUP_ADC(), SETUP_ADC_PORTS() (aka SETUP_PORT_A),
//                SET_ADC_CHANNEL(), READ_ADC()
// Constants used for SETUP_ADC() are:
#define ADC_OFF                 0              // ADC Off
#define ADC_CLOCK_DIV_2   0x10000
#define ADC_CLOCK_DIV_4    0x4000
#define ADC_CLOCK_DIV_8    0x0040
#define ADC_CLOCK_DIV_16   0x4040
#define ADC_CLOCK_DIV_32   0x0080
#define ADC_CLOCK_DIV_64   0x4080
#define ADC_CLOCK_INTERNAL 0x00c0              // Internal 2-6us
 
Last edited:
Status
Not open for further replies.

Latest threads

Back
Top