Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Problem in smart house program using pic16f877a

Status
Not open for further replies.

marov

Newbie level 6
Joined
Dec 5, 2010
Messages
11
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,377
Hi guys,
I am working on a simple smart house project using pic16f877a.I used two ultrasonic sensors one on the outer and other inner. If outer give signal first so somebody enters and if the inner give signal first then somebody left. Also I am using a thermistor for calculating temperature. I wrote the code. It should be working, i simulated it using real Pic simulator and it worked, however using"Proteus" i had some errors. When I programmed the code on the pic i didn't get any response.

Here is the complete code:

unsigned int en,ex,t,k;
int i=0;
void main() {
ADCON1=0;

TRISA=0xFF;
TRISD=0;
PORTD=0;
TRISB=0;
PORTB=0;

ADC_Init();

while (1){
en=ADC_Read(0); //reading from external ultrasonic sensor
ex=ADC_Read(1);// reaing from internal ultrasonic sensor
t=ADC_Read(2);// reading form thermistor
k=ADC_Read(3);// pressure sensor

if( en> 300 && ex<50 ){// if the external sensor give signal, lamp is on, and counter for one person in incremented
PORTB.F2=1;
i=i+1;
Delay_ms(3000);}
if( ex> 300 && en<50 ) {/ if the internal sensor give signal, counter for one person in deccremented
if(i>0){
i=i-1;}
Delay_ms(3000);}

if(i==0){
PORTB.F2=0;}


if(i>=1 & k>128){ // if somebody is in the room, and pressure sensor is above a referance value
if (t>128){ // if t above reference value, switch on a cooler
PORTB.F4=0;
delay_ms(2000);
PORTB.F3=1;}
if( t>110 & t<128){
PORTB.F4=0;
PORTB.F3=0; }


if (t<110 ){ // if temp below reference temp turn on heater
PORTB.F3=0;delay_ms(2000);
PORTB.F4=1;} }


}}

I dont know if there is a problem in ADC
Guys i really appreciate your help
 

Here you are some points those might help:

1-
Code:
if(i>=1 & k>12)
Here, you are performing a bitwise AND between variable i and variable k. If you want to check that both conditions are true, use the && opertator.

2- Check your ADC by sending the data you read to the PC (Hyper Terminal) using UART. As I can see from your code, you are using MikroC, you have ready built in "Soft_UART" library.

3- If the ADC is working, check your sensors one by one.

Hope these help
 

Thanks man
But i need to know if there are other alternatives concerning the code or the circuit

---------- Post added at 13:23 ---------- Previous post was at 13:22 ----------

here is the circuit
22ez9v.jpg
 

First of all, I would recommend you to use a 220Ohm resistors between the microcontroller and the LEDs (D1, D2, D3) as this connection might damage the LEDs.
Second, where are your sensors? are you simulating them by these potentiometers? If yes, disconnect all the sensors except the "Outer Ultrasonic" and try to run your code.
You have to locate your problem first.
 

I connected the outer only, Proteus indicated an error in simulation telling that: Voltage reference of ADC yields a 0V range(V+=0 V-=0). It seems that the pic is considering one of the A ports as V+ reference (possible AN3).
However in the program i set ADCON1=0; which, according to the datasheet sets Vdd as V+ reference, and Vss as V- reference

---------- Post added at 14:41 ---------- Previous post was at 14:31 ----------

Thanks seadolphine2000 ,
The simulation has worked perfectly, the pic is considering AN3 as V- reference, so I edited the program and it worked.
Now i will try it in real life and i will come again.
Thanks again
 

Hi again
I wrote the program on the PIC using Micropro. Then i placed the PIC on the board but there was no results; No output at all even that Proteus simulated the program perfectly. I am using 4MHz crystal oscillator,with DC input 5.21V, pull up resistor for the MCLR.
I couldn't assume that the PIC is dead since i have 4 chips, and i don't think that the four are damaged. I know it's hard for u to discover the problem without looking to the circuit, but with your experience in the field, where could the error be???
 

OK, some points to be checked:
1- Check the oscillator configuration in MikroC
2- Write a very simple project that will turn on the three LEDs connected to portB. (By this you will know whether your controller is running or not)
3- Power your board and while powering, measure the supply voltage. For some reason, the voltage may drop and thus the controller is OFF. Also, measure the voltage on the MCLR pin. It must be 5V

These are the points I could come up with.
Good luck and keep me updated.
 

OK, some points to be checked:
1- Check the oscillator configuration in MikroC
2- Write a very simple project that will turn on the three LEDs connected to portB. (By this you will know whether your controller is running or not)
3- Power your board and while powering, measure the supply voltage. For some reason, the voltage may drop and thus the controller is OFF. Also, measure the voltage on the MCLR pin. It must be 5V

These are the points I could come up with.
Good luck and keep me updated.
 

Hi again seadolphine2000 , sorry for being late, but i doubt that the pic was damaged so i went to buy another one, it took me long time since no near stores sell pics.
1-In the oscillator configuration, I set the oscillator "XT", disabled the WDT,and the LVP.
2- When I got the new PIC, I tried blinking Leds ecery second and it worked. Then I tried a simple adc program and it worked
3_ I always check the pins you listed and they works normally.

I just wrote the main program on the Pic and and it didn't work, then i tried a simpler one and also it didn't.
Finally i plugged the pic to the programmer and Micropro strangely indicated that "the programmer doesn't recognize Pic16f877a". How I can solve that, please don't tell me that the pic is damaged again!!!!!!
 

OK, this means that your last trial made a problem either with programmer or with the PIC. To determine the cause do the following:
1- Before trying any new code, try all the PICs you have with programmer. Just check if the programmer can recognize the controllers. By this, you will make sure that the programmer is working fine and also, you will determine the working controllers.
2- Make sure that while programming you didn't lock the controller. I mean, you may have disabled the controller from being programmed again. This can be done by the "Fuse Bit" and once done, you can never program the controller again. So, make sure that this is not your case.
3- Assuming that the programmer is working, try the "LED Blinking" project on all the working controllers you have.
4- Revise the circuit connections, may be a short circuit had damaged the controller or something and, as you did, keep monitoring the power supply.
5- Assuming no problems with the past four points, check your code.

I hope this helps.
Good luck and feel free to ask any time.
 

hi marov
i agree with seadolphine2000 points
that r useful
i doubt that there is a problem in ur circuit that damage ur controller use 4.7k resister between power and MCLR pin
 

Hi
I divided the main proram into parts, For the ultrasonic sensors first:
unsigned int en,ex,t,k;
int i=0;
void main() {
ADCON1=0x00;
PORTA=0;
TRISA=0xFF;
TRISC=0;
PORTC=0;

ADC_Init();

while (1){
en=ADC_Read(0);
ex=ADC_Read(1);

if( en> 800 ){
PORTC.F4=1;
i++;
Delay_ms(6000);}
if(i==0){PORTC.F4=0;}
if(i>1){PORTC.F4=1;}

if(ex>800 ){
if(i>0){
i--;}
Delay_ms(6000);}
if(i==0){
PORTC.F4=0;
} if(i>0){PORTC.F4=1;}
}}

---------- Post added at 15:04 ---------- Previous post was at 15:01 ----------

If i connect the pic, the light will blink with voltage at AN0<800. The light will turn off an On peridodically without any change in the inputs!!!!
Only if I hold a 5V at AN0, the light will remain on, and only if I hold a 5V at AN1 the light will stay off!!!!

---------- Post added at 15:07 ---------- Previous post was at 15:04 ----------

If I connect the Pic, the light will start blinking On and oFF Peridically without changing the inputs!!!!!
Only if I hold the AN0 to 5V, the light will stay on, and only if i hold AN1 to 5V the light will stay off!!!!!!
 

Can you send me the datasheets of the Ultrasonic sensors you used?
I have a doubt that the problem may be in the sensors connections.
 

hi
i am just writing to thank everybody who has helped me since th pic has worked perfectly.
thank you
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top