PDA

View Full Version : 16F877a - Please Help



tarexpanda
- 3rd April 2007, 07:59
Hi~ this is my first time using a microcontroller, let alone programming. Somehow, I ended up being the programmer for a group project, and I desperately need some help. I am programming a pic16f877a with PicBasic Pro with an input coming from a digital temperature sensor (tmp121) and an output to a transmitter (txm-433-lr). If the temperature coming in from the sensor is greater than 85 degrees farenheit, I want an pulse signal to be sent out to make a car honk and '1' to be sent to an alarm device via the transmitter. I've spent a long time looking over other examples and tried to piece things together the best I could, but I'm afraid that I am utterly horrible at this.

Thanks in advance to anybody that helps! Here is the code I have so far:

' read tmp121 1-wire temp sensor
' if temp > 85 degrees farenheit, send signal to transmitter

DEFINE LOADER_USED 1
DEFINE OSC 20

temp VAR WORD 'storage for temp
ftemp VAR WORD ' holds converted temp
samples VAR WORD ' hold number of samples

TRISD = %00000000 ' output portd
TRISC = %00000000 ' output portc

car_honk var PORTD.6 ' pin 29 to start car honk
panic_alarm var PORTD.7 ' pin 30
transmit_datain var PORTC.6 ' pin 25 to transmitter

ADCON1 = 7 ' set PORTA to digital
PAUSE 500 ' wait .5 seconds

loop:
FOR sample = 1 TO 20 ' take 20 samples
SERIN PORTA.0 temp ' here I don't know how to read in the digital temperature from the sensor
samples = samples + temp
PAUSE 500 ' wait about .5 sec per loop
NEXT sample
temp = samples/20 ' take average
ftemp = (180/100 * temp) + 32 ' convert to farenheit

IF ftemp > 85 THEN set_alarm

samples = 0 ' clear sample accumulator

GOTO loop ' do it forever

set_alarm:
PULSOUT car_honk, 500000 ' send pulse 1 sec long
HIGH panic_alarm
HIGH transmit_datain
PAUSE 10000 ' can I use PAUSE to make the set_alarm last for 10 seconds?
RETURN

END

mackrackit
- 3rd April 2007, 08:28
Need to have - include "modedefs.bas" - near the start of the program for serial to work.

Get rid of Sample after NEXT.

IF ftemp > 85 THEN set_alarm change to
IF ftemp > 85 THEN GOSUB set_alarm (look in the manual)

Look in the manual for how SERIN works.

Take things piece by piece. Try reading the temperature to hyper-terminal first to make sure that is working, the add more functions after that.