|
|
View previous topic :: View next topic |
How was this code? |
Not bad for a newbie. |
|
0% |
[ 0 ] |
Really bad, even for a newbie. |
|
100% |
[ 1 ] |
|
Total Votes : 1 |
|
Author |
Message |
quepsis
Joined: 25 Jun 2014 Posts: 1
|
16F877A Working Slow( Newbie to CCS and circuits in general) |
Posted: Wed Jun 25, 2014 6:46 am |
|
|
Hello everyone. '' I've designed a circuit which drives LCD(16x2), uses thermometer(18B20), uses a 3-wired Fan Motor and calculates the RPM of that said motor at the same time(At the. However, when I program this into 16F877A, it runs slow. I mean really slow. I was wondering if it was code-related(It may also be the design of the circuit). I know it is sloppy and very amateur coding, but anyways, if you could help me; that would be grand'' . (I have collected these through various sources)
Code of the thermometer
Code: |
ds1820.h
float ds1820_read()
{
int8 busy=0, temp1, temp2;
signed int16 temp3;
float result;
onewire_reset();
onewire_write(0xCC);
onewire_write(0x44);
while (busy == 0)
busy = onewire_read();
onewire_reset();
onewire_write(0xCC);
onewire_write(0xBE);
temp1 = onewire_read();
temp2 = onewire_read();
temp3 = make16(temp2, temp1);
result = (float) temp3 / 16.0; //Calculation for DS18B20 with 0.1 deg C resolution
// delay_ms(200);
return(result);
} |
My PWM controller code for the thermometer
Code: |
1wire.h
/***********************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 );
} |
my main code
Code: |
#include <16f877A.h> // Kullanylacak denetleyicinin ba?lyk dosyasy tanytylyyor.
//***********Denetleyici konfigürasyon ayarlary************
//#fuses XT,NOWDT,NOPROTECT,NOBROWNOUT,NOLVP,NOPUT,NOWRT,NODEBUG,NOCPD
#use delay(clock=20000000) // Gecikme fonksiyonu için kullanylan osilatör frekansy belirtiliyor
#define LCD_DATA_PORT getenv("SFR:PORTD")
#define LCD_ENABLE_PIN PIN_E2 ////
#define LCD_RS_PIN PIN_E0 ////
#define LCD_RW_PIN PIN_E1 ////
#define LCD_DATA4 PIN_D4 ////
#define LCD_DATA5 PIN_D5 ////
#define LCD_DATA6 PIN_D6 ////
#define LCD_DATA7 PIN_D7
#define ONE_WIRE_PIN PIN_B0
#include<1wire.c>
#include<lcd.c>
#include<ds1820.c>
int8 capture_rising_edge;
int8 got_pulse_width;
int16 ccp_delta;
#int_ccp1
void ccp1_isr(void)
{
static int16 t1_rising_edge;
if(capture_rising_edge)
{
setup_ccp1(CCP_CAPTURE_FE);
capture_rising_edge = FALSE;
t1_rising_edge = CCP_1;
}
else
{
setup_ccp1(CCP_CAPTURE_RE);
capture_rising_edge = TRUE;
ccp_delta = CCP_1 - t1_rising_edge;
got_pulse_width = TRUE;
}
}
void main ()
{
int xo = 50;
float pulse_width_ms;
int16 local_ccp_delta;
float temperature;
set_tris_d(0xFF);
lcd_init();
output_low(PIN_C2);
setup_ccp2(CCP_PWM);
setup_timer_2(T2_DIV_BY_16, 100, 1); // Setting the CCP2 for PWM (yellow wire of the fan)
set_pwm2_duty(xo);
got_pulse_width = FALSE;
capture_rising_edge = TRUE;
setup_ccp1(CCP_CAPTURE_RE);
setup_timer_1(T1_INTERNAL | T1_DIV_BY_2 ); //(Reading the frequency)
enable_interrupts(INT_CCP1);
enable_interrupts(GLOBAL);
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){
if(got_pulse_width)
{
lcd_gotoxy(1,1);
disable_interrupts(GLOBAL);
local_ccp_delta = ccp_delta;
enable_interrupts(GLOBAL);
pulse_width_ms = local_ccp_delta / 1250.0;
printf(lcd_putc,"%lfRpm ", /*(1000.0/pulse_width_ms),*/(15000.0/pulse_width_ms)); // I calculated Frequency * 60/4 = RPM
got_pulse_width = FALSE;
}
lcd_gotoxy(1,2);
temperature = ds1820_read();
printf(lcd_putc,"Temp:%.2f",temperature);
if(temperature>=40){
xo=75; //Changing the duty cycle when the temperature rises
}
if(temperature<=30){
xo=25;
}
}
}
| |
|
|
asmboy
Joined: 20 Nov 2007 Posts: 2128 Location: albany ny
|
|
Posted: Wed Jun 25, 2014 6:54 am |
|
|
can you show your schematic? |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9221 Location: Greensville,Ontario
|
|
Posted: Wed Jun 25, 2014 9:36 am |
|
|
hmm... really slow....
1) have you run a '1Hz LED' program in your PIC? Does it run at 1Hz?
2) floating point math is super slooooooow !!! Use integer math, scaled integers to really speed things up !
3) break it down a bit....and just read the temperature and display,does that work 100%?
hth
jay |
|
|
MikeP
Joined: 07 Sep 2003 Posts: 49
|
|
Posted: Thu Jun 26, 2014 11:08 pm |
|
|
Code: | #use delay(clock=20000000) |
I see that your #fuse line is commented out I don't think the clock option will set the HS fuse
The clock option in #use delay should only be used with xtal or int option to set clock if you want to set oscillator multiplication or division or when your fuses are set correctly and you don't want the compiler messing with the fuses
Since your clock is 20m I am going to assume you are using a 20MHZ crystal because if I recall right the 16f877A does not have a internal clock that fast.
Try this
Code: | #fuses NOWDT, NOLVP
#use delay(xtal=20000000) |
The above code will set the clock fuses correctly but there are others you should be setting at a minimum. Others may point out others but off the top of my head |
|
|
|
|
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
|