|
|
View previous topic :: View next topic |
Author |
Message |
Keith1130
Joined: 31 Oct 2009 Posts: 6
|
Question about how to output high to a PIN in PIC18F2480 |
Posted: Sat Oct 31, 2009 3:42 pm |
|
|
I want to use communicate the temperature sensor DS1822 with PIC18F2480 to output a signal to two LEDs. When the temperature is lower than 30 degree, the first LED lights. Otherwise, the second LED lights.
I used the code from the link below and did some modifications:
http://www.ccsinfo.com/forum/viewtopic.php?t=28425
However, neither of the LEDs lights when I tested the hardware. Can any one help me to figure out what is going wrong?
Here's my code:
Code: |
/***********************1Wire Class***********************/
/*Description: This class handles all communication */
/* between the processor and the 1wire */
/* sensors.
/*********************************************************/
/*-------1-wire definitions-------*/
#define ONE_WIRE_PIN PIN_B0
/*******************1-wire communication functions********************/
void onewire_reset() // OK if just using a single permanently connected device
{
output_low(ONE_WIRE_PIN);
delay_us( 500 ); // pull 1-wire low for reset pulse
output_float(ONE_WIRE_PIN); // float 1-wire high
delay_us( 500 ); // wait-out remaining initialisation window.
output_float(ONE_WIRE_PIN);
}
/*********************** onewire_write() ********************************/
/*This function writes a byte to the sensor.*/
/* */
/*Parameters: byte - the byte to be written to the 1-wire */
/*Returns: */
/*********************************************************************/
void onewire_write(int data)
{
int count;
for (count=0; count<8; ++count)
{
output_low(ONE_WIRE_PIN);
delay_us( 2 ); // pull 1-wire low to initiate write time-slot.
output_bit(ONE_WIRE_PIN, shift_right(&data,1,0)); // set output bit on 1-wire
delay_us( 60 ); // wait until end of write slot.
output_float(ONE_WIRE_PIN); // set 1-wire high again,
delay_us( 2 ); // for more than 1us minimum.
}
}
/*********************** read1wire() *********************************/
/*This function reads the 8 -bit data via the 1-wire sensor. */
/* */
/*Parameters: */
/*Returns: 8-bit (1-byte) data from sensor */
/*********************************************************************/
int onewire_read()
{
int count, data;
for (count=0; count<8; ++count)
{
output_low(ONE_WIRE_PIN);
delay_us( 2 ); // pull 1-wire low to initiate read time-slot.
output_float(ONE_WIRE_PIN); // now let 1-wire float high,
delay_us( 8 ); // let device state stabilise,
shift_right(&data,1,input(ONE_WIRE_PIN)); // and load result.
delay_us( 120 ); // wait until end of read slot.
}
return( data );
}
// main source file : DS1822.c
#include <18F2480.h>
#device *=16
// internal osilator used
#FUSES NOWDT,INTRC, NOPUT, NOPROTECT, NOBROWNOUT, NOMCLR, NOLVP, NOCPD//for 16f628
#use delay(clock=4000000)
#USE RS232(BAUD=9600, XMIT=PIN_C6, RCV=PIN_C7)
#include "1wire.c"
int8 tH,tL,Conf;
float ds1822_read()
{
int8 busy=0, temp1, temp2;
signed int16 temp3;
float result;
onewire_reset();
onewire_write(0xCC);
onewire_write(0x44);
delay_ms(200);
while (busy == 0)
busy = onewire_read();
onewire_reset();
onewire_write(0xCC);
onewire_write(0xBE);
temp1 = onewire_read();
temp2 = onewire_read();
tH=onewire_read();
tL=onewire_read();
Conf=onewire_read();
temp3 = make16(temp2, temp1);
//result = (float) temp3 / 2.0; // 0.5 deg C resolution
result = (float) temp3 / 16.0; //0.1 deg C resolution
delay_ms(200);
return(result);
}
void main()
{
float temperature;
setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);
setup_timer_1(T1_DISABLED);
setup_timer_2(T2_DISABLED,0,1);
//for 10 bit resolution mod
onewire_write(0xCC);
onewire_write(0x4E);
onewire_write(125);
onewire_write(-55); //this should be done for proper working of DS18B20
onewire_write(127);
onewire_reset();
onewire_write(0xCC);
onewire_write(0x48);
delay_ms(15);
while (1)
{
temperature = ds1822_read();
if (temperature < 30){
output_high(PIN_A0);
output_low(PIN_A0);
}
else{
output_low(PIN_A0);
output_high(PIN_A1);
}
}
}
|
Thank you very much. |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Sat Oct 31, 2009 10:53 pm |
|
|
Quote: | while (1)
{
temperature = ds1822_read();
if (temperature < 30){
output_high(PIN_A0);
output_low(PIN_A0);
}
else{
output_low(PIN_A0);
output_high(PIN_A1);
}
}
}
When the temperature is lower than 30 degree, the first LED lights. Otherwise, the second LED lights.
However, neither of the LEDs lights when I tested the hardware.
|
Look closely at your code for the "temperature < 30" case. |
|
|
Keith1130
Joined: 31 Oct 2009 Posts: 6
|
|
Posted: Fri Nov 06, 2009 6:24 pm |
|
|
I was trying to compile the code in MPLAB after i changed
Code: |
while (1)
{
temperature = ds1822_read();
if (temperature < 30){
output_high(PIN_A0);
output_low(PIN_A0);
}
else{
output_low(PIN_A0);
output_high(PIN_A1);
}
}
|
to
Code: | while (1)
{
temperature = ds1822_read();
if (temperature < 30){
output_high(PIN_A0);
output_low(PIN_A1);
}
else{
output_low(PIN_A0);
output_high(PIN_A1);
}
}
|
However, the compilation results gave me 6 errors:
Quote: |
Clean: Deleting intermediary and output files.
Clean: Deleted file "1wire.ESYM".
Clean: Deleted file "W:\PIC_C\temp_mplab\1wire.o".
Clean: Deleted file "DS1822.ESYM".
Clean Warning: File "W:\PIC_C\temp_mplab\DS1822.o" doesn't exist.
Clean: Deleted file "1wire.ERR".
Clean: Deleted file "DS1822.ERR".
Clean: Done.
Executing: "C:\Program files\Picc\CCSC.exe" +FH "1wire.c" +EXPORT +DF +LN +T +A +M +Z +Y=9 +EA
W:\PIC_C\temp_mplab\1wire.o ===> 0 Errors, 0 Warnings.
Executing: "C:\Program files\Picc\CCSC.exe" +FH "DS1822.c" +EXPORT +DF +LN +T +A +M +Z +Y=9 +EA
*** Error 23 "C:\PROGRA~1\PICC\devices\18F2480.h" Line 2(8,9): Can not change device type this far into the code
*** Error 48 "C:\PROGRA~1\PICC\devices\18F2480.h" Line 173(1,31): Expecting a (
*** Error 48 "C:\PROGRA~1\PICC\devices\18F2480.h" Line 173(6,11): Expecting a (
*** Error 23 "W:\PIC_C\temp_mplab\1wire.c" Line 7(8,9): Can not change device type this far into the code
*** Error 43 "W:\PIC_C\temp_mplab\1wire.c" Line 7(9,11): Expecting a declaration
*** Error 43 "W:\PIC_C\temp_mplab\1wire.c" Line 7(11,13): Expecting a declaration
W:\PIC_C\temp_mplab\DS1822.o ===> 6 Errors, 0 Warnings.
Halting build on first failure as requested.
BUILD FAILED: Fri Nov 06 17:56:45 2009
|
I am not sure what is wrong with the code since it also output three error for 18F2480.h which I never edit. Besides, I saw people said the code works fine for DS18B20 but I am using DS1822. I checked the datasheets for both of them. The operations are actually the same. Will it be a problem if I use the code for DS1822? |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Fri Nov 06, 2009 7:20 pm |
|
|
Take the 1 wire code and save it as the 1wire.c file. The 1 wire code is
the code that is above this line in the source code above:
Code: | // main source file : DS1822.c |
Save the remainder of the code in your main source file.
Put them both in the same directory. Compile the main source file.
I don't really quite believe you changed only one line and it failed to
compile. I believe you also changed something else.
It's possible that you added "1wire.c" to the "Source Files" list in MPLAB.
Don't do that. Just have the main source file be in the Source files list. |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|