 |
 |
| View previous topic :: View next topic |
| Author |
Message |
RLScott
Joined: 10 Jul 2007 Posts: 465
|
| Re: Thanks Ttelmah |
Posted: Wed Apr 23, 2008 5:45 am |
|
|
| kein wrote: | ...It same behaviour as printf() far much and very slow. If I used inside interrupt subroutine, nothing is print out of the terminal. Secondly if I used inside main(0, performance is really poor!
I don't know what I'm doing wrong?...
|
One problem for poor performance in main() is that you are spending time inside the blinking LED function. The comment in your code says something about a LED state machine. But you are not using a state machine. If you were using a state machine, then the LED blinking would take very little time. A state machine has no delays. It just decides what to do and does it and returns. I suggest a state machine something like this:
| Code: |
void ADLX330ACCELBlinkLED(unsigned char on, unsigned char off, unsigned char tm)
{
static int visits;
visits++;
if(visits & 1)
output_low(PIN_B5);
else
output_high(PIN_B5);
} |
This will toggle the LED one each call, but will not do any delays that hurt performance. If this makes the LED blink too fast, then check some other bit of "visits".
But the biggest problem is the way you poll for TXIF to send out characters. You do this only 100 times per second. That means you can only send out 100 characters per second. But at 9600 baud, you could be sending out about 1000 characters per second. So the output stream is going to go 10 times slower this way. That will certainly make the performance look poor. As it is right now, your bputc() function is going to be discarding the oldest characters if printf() is called too often. And printf() is now called at a rate that depends on how much time is used in blinking LEDs. Since the oldest characters in your TX buffer are being constantly thrown away, it is no wonder that the printout looks confusing.
To make sure you are not thowing away characters, you need to stop calling printf() until the previous bunch of characters has been all sent out. Do something like this in main():
| Code: |
// This is the main loop. You put your "tasks" here.
while(1)
{
int16 local_data = 0L, cnt =0;
DispData();
BlinkLed();
if (count==0)
{
disable_interrupts(GLOBAL);
local_data=data;
cnt = dc;
enable_interrupts(GLOBAL);
printf(bputc,"%0Ld,%0Ld\n\r", cnt, local_data);
}
}
} |
This will ensure that you will not try to print a new reading unless the previous reading has already been printed out.
By the way, it does no good to set TXIF=0 as you are doing. TXIF cannot be cleared in software. It is cleared automatically when you write to TXREG. But this is a small matter and only wastes one instruction.
Robert Scott
Real-Time Specialties |
|
 |
Ttelmah Guest
|
|
Posted: Wed Apr 23, 2008 7:39 am |
|
|
Never _ever_ have an 'enable_interrupts(GLOBAL)', inside an interrupt handler. This physically _must not_ be done on the PIC. It overrides the hardware protection built into the chip to prevent interrupt re-entrancy, and will make code not work...
The disable/enable round the reading of the 'data' value, is because if you read it in the 'main', what happens if the value changes in an interrupt after _half_ the value is read?. This cannot happen inside an interrupt.
Get rid of INT_AD. You don't need it. The whole point of reading the ADC inside the timer, is to allow this to be removed, and get rid of the large overhead this involves.
As RLScott says, the whole point, is to just do things that must occur at repeatable intervals inside the ISR (read the ADC, update your timers, and possibly send one character to the serial), do these as fast as possible, and get back to doing the 'main' work (printing messages etc.), in the main code.
Best Wishes |
|
 |
kein
Joined: 23 Jul 2007 Posts: 103
|
| Thanks guys for your help |
Posted: Fri Apr 25, 2008 9:46 pm |
|
|
Thanks guys for your help but this method does not run faster either than printf()!What am i doing wrong here? Below is the code. It's quite frustrating that this is not working as expected!
| Code: | //#include "\\bee-student\ughome2\n4849892\PICC Projects\Test.h"
#include <16F877A.h>
#device *=16
#device adc=10
#FUSES NOWDT, HS, NOPUT, NOPROTECT, NODEBUG, NOBROWNOUT, NOLVP, NOCPD, WRT_50%
#use delay(clock=20000000)
#use rs232(baud=115200,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8)
//----------------------------------------------------------------------
// DEFINES
//
// With a 4 MHz oscillator, a RTCC pre-scaler of 256, and a RTCC
// preload of 39, we get an rtcc interrupt rate of 100.16 Hz (Approx.
// every 10 ms).
// This will be our "tick" clock that we use for various event timers.
#define RTCC_PRELOAD (256 - 196)
#define TXBUFF_SIZE (32) //set to suit your worst case
#define incr(x) if(++x==TXBUFF_SIZE)x=0
// Multiply the following values x 10 ms to get the delay times,
// since each timer tick is 10 ms.
#define BUTTONS_TIMER_TICKS 4 // 40 ms
#define IRDA_TIMER_TICKS 10 // 100 ms
#define LED_TIMER_TICKS 50 // 500 ms
#define UART_TIMER_TICKS 100 // 1000 ms
#byte PORTB = 0x06 // PortB lives in File 6 (data bus)
#byte PORTC = 0x07 // PortC lives in File 7 (address bus A12 to A8, RX, TX & R/W)
#byte PORTD = 0x08 // PortD lives in File 8 (address bus A0 to A7)
#byte PORTE = 0x09 // PortE lives in File 9 (used for DIP switches)
#use fast_io(B) // Fast access to PortB (don't fiddle with TRISB)
#use fast_io(C) // Fast access to PortB (don't fiddle with TRISB)
#use fast_io(D) // Fast access to PortD (don't fiddle with TRISD)
#use fast_io(E) // Fast access to PortE (don't fiddle with TRISE)
#include <stdio.h>
#include <24256.C>
#include <LCD4X20.C>
#include <stdlib.h>
#bit TXIF=0xC.4
#BIT ADFM = 0xFC0.7
#byte timer0low = 0xfd6
#byte TXREG=0x19
// GLOBALS
char gc_buttons_timer;
char gc_IRDA_timer;
char gc_LED_timer;
char gc_uart_timer;
unsigned int16 dc;
unsigned int16 data;
int1 flag;
int8 TXBuff[TXBUFF_SIZE];
char gc_old_button_status;
int8 in=0,out=0,count=0; //use separate counter for speed
void TIMER0_isr(void);
void Adxl330Accelcheck_buttons(void);
void bputc(char chr);
void Adxl330DispData(void);
void ADLX330ACCELBlinkLED(void);
void Adxl330Accelcheck_IRDA(void);
void Adxl330Accelcheck_uart(void);
void Adxl330Accelcheck_LEDs(void);
void main()
{
unsigned int16 result;
data = 0L;
result= 0L;
flag = 0;
lcd_init();
init_ext_eeprom();
set_tris_b(0x17); // TRISB = 00010111; RB4,RB2,RB1 and RB0 TTL Inputs
port_b_pullups(FALSE); // Don't use internal pull up resistors
setup_adc_ports(AN0_AN1_AN2_AN4_AN5_VSS_VREF);
setup_adc(ADC_CLOCK_DIV_64);
//setup_timer_0( RTCC_DIV_256 ); //Timer0 enabled with a 245 prescaler
setup_timer_0(RTCC_INTERNAL|RTCC_8_BIT|RTCC_DIV_256);
set_adc_channel(4); // select the required channel //
delay_us(10);
ADFM = 1;
lcd_gotoxy(1,1);// Type some characters and backspace over them.AccelBuff
lcd_putc(" X Y Z");
set_timer0(RTCC_PRELOAD);
enable_interrupts(INT_TIMER0);
//enable_interrupts(INT_AD);
enable_interrupts(GLOBAL);
dc = 0L;
flag = 0;
lcd_gotoxy(1,2);// Type some characters and backspace over them.AccelBuff
lcd_putc(" This is a test...");
// TODO: USER CODE!!
// This is the main loop. You put your "tasks" here.
while(1)
{
Adxl330DispData();
Adxl330Accelcheck_IRDA();
Adxl330Accelcheck_LEDs();
Adxl330Accelcheck_uart();
Adxl330Accelcheck_buttons();
}
}
void ADLX330ACCELBlinkLED(void)
{
static int visits;
visits++;
if(visits & 1)
output_low(PIN_B5);
else
output_high(PIN_B5);
}
//==============================
// If the button debounce delay has not counted down yet,
// then just return. If it has counted down to 0,
// then reload the timer and execute the rest of
// this function.
// The timer tick is set for a 10 ms period, so let's
// say a suitable debounce time would be 20 ms or more.
// So we'll set CHECK_BUTTONS_TICKS = 2.
// So this function will execute approximately every 2 ticks,
// or 20 ms.
void Adxl330Accelcheck_buttons(void
{
char new_status;
static unsigned int8 c=0;
//static unsigned int8 k =0;
if(gc_buttons_timer)
return;
else
gc_buttons_timer = BUTTONS_TIMER_TICKS;
new_status = input(PIN_B4); // Read the buttons
// Have the switches changed ?
if(new_status != gc_old_button_status)
{
char buff[4];
// If so, do something.
lcd_gotoxy(1,3);// Type some characters and backspace over them.AccelBuff
printf(lcd_putc,"%u",c);
c++;
}
// Save button status for next time.
gc_old_button_status = new_status;
}
void Adxl330DispData(void)
{
int16 local_data = 0L, cnt =0;
if (count==0)
{
disable_interrupts(GLOBAL);
local_data=(int16)(data - 0x266);
cnt = dc;
enable_interrupts(GLOBAL);
printf(bputc,"%0Ld,%0Ld\n\r", cnt, local_data);
}
}
//--------------------------------------------------------
void Adxl330Accelcheck_LEDs(void)
{
if(gc_LED_timer)
return;
else
gc_LED_timer = LED_TIMER_TICKS;
// Put your LED blinking state machine here.
ADLX330ACCELBlinkLED();
}
//--------------------------------------------------------
void Adxl330Accelcheck_IRDA()
{
if(gc_IRDA_timer)
return;
else
gc_IRDA_timer = IRDA_TIMER_TICKS;
// Handle the IRDA here.
}
//--------------------------------------------------------
void Adxl330Accelcheck_uart(void)
{
if(gc_uart_timer)
return;
else
gc_uart_timer = UART_TIMER_TICKS;
}
void bputc(char chr)
{
TXBuff[in] = chr;
incr(in);
DISABLE_INTERRUPTS(INT_TIMER0);
if (++count==TXBUFF_SIZE)
{
--count;
incr(out);
//throw away oldest character if overflow
}
ENABLE_INTERRUPTS(INT_TIMER0);
}
//--------------------------------------------------------
// The rtcc interrupt occurs when the rtcc rolls over from FF to 00.
// I have programmed it to interrupt at a 100 Hz rate.
//
// RTCC interrupt rate = Fosc / (4 * rtcc pre-scaler * rtcc pre-load)
//
// = 20 MHz / (4 * 256 * 196)
//
// = 100.16 Hz
//
// This gives us a timer tick approx. every 10 ms (9.98 ms actually).
#int_TIMER0
void TIMER0_isr(void)
{
timer0low = timer0low + RTCC_PRELOAD;
//Get rid of the INT_AD
data=(read_adc(ADC_READ_ONLY)); //read last ADC
read_adc(ADC_START_ONLY); //start the next conversion
dc++;
// Decrement any timers that are running.
if(gc_buttons_timer)
gc_buttons_timer--;
if(gc_IRDA_timer)
gc_IRDA_timer--;
if(gc_LED_timer)
gc_LED_timer--;
if(gc_uart_timer)
gc_uart_timer--;
//Now handle serial transmission of buffered character
if (count!=0) {
//A character is waiting
if (TXIF) {
//The output buffer has space
TXREG=TXBuff[out];
incr(out);
count--;
//TXIF=0;
}
}
//Now 'key' is that if a character is waiting in the output buffer
//it checks to see if the hardware can take a character. If it can,
//just one character is sent.
//The 'send' overhead is then very small indeed in the timer interrupt.
} |
|
|
 |
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Fri Apr 25, 2008 11:44 pm |
|
|
| Quote: |
#include <16F877A.h>
#bit TXIF=0xC.4
#BIT ADFM = 0xFC0.7
#byte timer0low = 0xfd6
#byte TXREG=0x19
|
I didn't look at your whole program, but what's wrong with this here ?
This is spottable just by inspection. It's an immediate catch. |
|
 |
kein
Joined: 23 Jul 2007 Posts: 103
|
| H PCM programmer |
Posted: Sat Apr 26, 2008 12:01 am |
|
|
Hi PCM Programmer, with regards to the following:
| Quote: | #bit TXIF=0xC.4
#BIT ADFM = 0xFC0.7
#byte timer0low = 0xfd6
#byte TXREG=0x19 |
I was advised from the previous posts that I should include them. The quesion is what do I do with? Should I get rid of them excluding #include <16F877A.h> ? I'm confused>> |
|
 |
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Sat Apr 26, 2008 12:08 am |
|
|
Look in the PIC data sheet at this table:
| Quote: | | FIGURE 2-3: PIC16F877A/876A REGISTER FILE MAP |
Then compare it to a similar table in an 18F series PIC.
Also, it's not just the issue of those lines being included in your program.
They are used in your program, and some of them are wrong. |
|
 |
kein
Joined: 23 Jul 2007 Posts: 103
|
| I'm currently using pic16f877a not pic18Fxxx |
Posted: Sat Apr 26, 2008 12:29 am |
|
|
I'm currently using pic16f877a not pic18Fxxx. I've checked datasheet and couldn't even see the values.Here is what I can see
:pic16f877a....>Timer0`.. File Address 01h and also 101h
SSPBUF ..file Adress 13h
pic18f4450....>Timer0L File Address FCFh and also FD7h
SPBRG ..file Adress FAFh
. Maybe I should just remove them! The problem is I'll not be able to use bputc() function which is suppose to be faster...
I'm very confused and stressed with this ... |
|
 |
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Sat Apr 26, 2008 1:06 am |
|
|
Set them to the correct address for the PIC that you're using.
You need to do this yourself. If you learn it by yourself, you'll always
remember it and never make this mistake again. |
|
 |
RLScott
Joined: 10 Jul 2007 Posts: 465
|
| Re: Thanks guys for your help |
Posted: Sat Apr 26, 2008 5:12 am |
|
|
| kein wrote: | Thanks guys for your help but this method does not run faster either than printf()!What am i doing wrong here? Below is the code. It's quite frustrating that this is not working as expected!
|
What do you want to run faster? Your serial output speed is still limited to 100 characters per second because of the way to check TXIF in the timer ISR. If you want the output stream to run at a full 9600 baud with no pauses inbetween characters, then you will have to process that output stream some other way. Here are some choices:
1. Increase the speed of the timer ISR to 1000 interrupts per second rather than 100.
...or...
2. Check TXIF within your main loop rather than in the timer ISR:
| Code: |
// This is the main loop. You put your "tasks" here.
while(1)
{
Adxl330DispData();
Adxl330Accelcheck_IRDA();
Adxl330Accelcheck_LEDs();
Adxl330Accelcheck_uart();
Adxl330Accelcheck_buttons();
//Now handle serial transmission of buffered character
if (count!=0) {
//A character is waiting
if (TXIF) {
//The output buffer has space
TXREG=TXBuff[out];
incr(out);
count--;
//TXIF=0;
}
}
|
This will be better as long as all the other functions in this loop ( Adxl330DispData(), Adxl330Accelcheck_IRDA(), Axl330Accelcheck_LEDs(), Adxl330Accelcheck_uart(), Adxl330Accelcheck_buttons()) are fast so that you can get around the loop more than 100 times per second.
Robert Scott
Real-Time Specialties |
|
 |
kein
Joined: 23 Jul 2007 Posts: 103
|
| #byte timer0low = 0x01 // 0x101/0xfd6 |
Posted: Sun Apr 27, 2008 4:39 am |
|
|
Thanks a lot guys for your help. I appreciate it...
PCM Programmer! Thanks a lot for your advice and help... Below is how I configure and correct the mistakes. I'm abit confused about TMR0 because it's found in two addresses i.e. 0x01 and 0x101.# | Quote: | | byte timer0low = 0x01 // 0x101/0xfd6 | which one do I set here?
| Code: | #bit TXIF=0xC.4
#BIT ADFM =0x09F.7 // 0xFC0.7
#byte timer0low = 0x01 // 0x101/0xfd6
#byte TXREG=0x19 |
|
|
 |
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Sun Apr 27, 2008 2:22 pm |
|
|
| Quote: |
I'm a bit confused about TMR0 because it's found in two addresses
i.e. 0x01 and 0x101.
|
In the 16F-series PICs, RAM is organized in "banks". To allow quick
access to certain commonly used registers, the chip designers put them
in more than one bank. In your case, just use the lowest address for
Timer0. |
|
 |
kein
Joined: 23 Jul 2007 Posts: 103
|
| Thank you |
Posted: Sun Apr 27, 2008 7:01 pm |
|
|
| Thanks PCM Programmer, I'll try that now.... |
|
 |
kein
Joined: 23 Jul 2007 Posts: 103
|
| Nothing works now |
Posted: Mon Apr 28, 2008 2:01 am |
|
|
I've migrated to pic18f4450 coz of its huge ram but nothing works now.What is really wrong?LCD should print something!!
Here is the code:
| Code: | #include <18F4550.h>
#device adc=10
#FUSES NOWDT, WDT128, HSPLL, NOPROTECT, BROWNOUT, BORV20, NOPUT, NOCPD, STVREN, NODEBUG, NOLVP, NOWRT, NOWRTD, IESO, FCMEN, PBADEN, NOWRTC, NOWRTB, NOEBTR, NOEBTRB, NOCPB, MCLR, LPT1OSC, NOXINST, PLL12, CPUDIV4, USBDIV, VREGEN, ICPRT
#use delay(clock=20000000)
#byte PORTB = 0x06 // PortB lives in File 6 (data bus)
#byte PORTC = 0x07 // PortC lives in File 7 (address bus A12 to A8, RX, TX & R/W)
#byte PORTD = 0x08 // PortD lives in File 8 (address bus A0 to A7)
#byte PORTE = 0x09 // PortE lives in File 9 (used for DIP switches)
#use fast_io(B) // Fast access to PortB (don't fiddle with TRISB)
#use fast_io(C) // Fast access to PortB (don't fiddle with TRISB)
#use fast_io(D) // Fast access to PortD (don't fiddle with TRISD)
#use fast_io(E) // Fast access to PortE (don't fiddle with TRISE)
#define EEPROM_SCL PIN_C3
#define EEPROM_SDA PIN_C4
#use rs232(baud=115200,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8)
#use i2c(Master,Fast,sda=PIN_C4,scl=PIN_C3)
// Multiply the following values x 10 ms to get the delay times,
// since each timer tick is 10 ms.
#define BUTTONS_TIMER_TICKS 4 // 40 ms
#define IRDA_TIMER_TICKS 10 // 100 ms
#define LED_TIMER_TICKS 50 // 500 ms
#define UART_TIMER_TICKS 100 // 1000 ms
//----------------------------------------------------------------------
// DEFINES
//
// With a 4 MHz oscillator, a RTCC pre-scaler of 256, and a RTCC
// preload of 39, we get an rtcc interrupt rate of 100.16 Hz (Approx.
// every 10 ms).
// This will be our "tick" clock that we use for various event timers.
#define RTCC_PRELOAD (256 - 196)
#include <24256.C>
#include "Adxl330ProjAdcfunctions.c"
#include <LCD4X20.C>
#byte Adxl330_TMR0 = 0xfd6
/*Global varaible declaration section*/
struct Mystructs
{
int data[1024];
} Buff;
// GLOBALS
unsigned int16 dc;
char gc_buttons_timer;
char gc_IRDA_timer;
char gc_LED_timer;
char gc_uart_timer;
char gc_old_button_status;
/*Function definition section*/
void Adxl330TIMER0_isr(void);
void Adxl330AD_isr(void);
void Adxl330_DispData(void);
void ADLX330ACCELBlinkLED(void);
VOID Adxl330Accelcheck_LEDs(void);
void main()
{
//Adxl330IOInit();
set_tris_b(0x17); // TRISB = 00010111; RB4,RB2,RB1 and RB0 TTL Inputs
lcd_init();
init_ext_eeprom();
setup_timer_0(RTCC_INTERNAL|RTCC_8_BIT|RTCC_DIV_256);
setup_adc_ports(AN0_TO_AN2|VSS_VDD);
setup_adc(ADC_CLOCK_DIV_64);
set_adc_channel(0); // select the required channel //
delay_us(10);
//setup_adc(ADC_OFF);
setup_psp(PSP_DISABLED);
setup_wdt(WDT_OFF);
//setup_timer_0(RTCC_INTERNAL|RTCC_DIV_8);
enable_interrupts(INT_TIMER0);
enable_interrupts(INT_AD);
enable_interrupts(GLOBAL);
lcd_gotoxy(1,1);// Type some characters and backspace over them.AccelBuff
lcd_putc(" X Y Z");
dc = 0;
//Setup_Oscillator parameter not selected from Intr Oscillator Config tab
// TODO: USER CODE!!
for(;;)
{
Adxl330_DispData();
Adxl330Accelcheck_LEDs();
}
}
void Adxl330TIMER0_isr(void)
{
Adxl330_TMR0 = Adxl330_TMR0 + RTCC_PRELOAD;
read_adc(ADC_START_ONLY); //start the next conversion
// Decrement any timers that are running.
if(gc_buttons_timer)
gc_buttons_timer--;
if(gc_IRDA_timer)
gc_IRDA_timer--;
if(gc_LED_timer)
gc_LED_timer--;
if(gc_uart_timer)
gc_uart_timer--;
//Now handle serial transmission of buffered character
}
#int_AD
void Adxl330AD_isr(void)
{
if(dc < 1024)
Buff.data[dc] = (read_adc(ADC_READ_ONLY)- 0x266); //read last ADC
dc++;
}
void Adxl330_DispData(void)
{
char new_status;
new_status = input(PIN_B4); // Read the buttons
// Have the switches changed ?
if(new_status != gc_old_button_status)
{
//SWITCH MUST BE RELEASED BEFORE DATA IS ALL SENT
unsigned int16 x; /*x is a temporary counter variable*/
printf("%s,\t %s \n","Time", "Acceleration");
for( x = 0; x < 1024; x++)
{
/*print one set of data into one line on the spreadsheet*/
printf("%04Lu,\t%02d", x, Buff.data[x]);
}
}
// Save button status for next time.
gc_old_button_status = new_status;
}
//--------------------------------------------------------
void Adxl330Accelcheck_LEDs(void)
{
if(gc_LED_timer)
return;
else
gc_LED_timer = LED_TIMER_TICKS;
// Put your LED blinking state machine here.
ADLX330ACCELBlinkLED();
lcd_gotoxy(1,3);// Type some characters and backspace over them.AccelBuff
printf(lcd_putc,"%02u",gc_LED_timer);
//c++;
}
void ADLX330ACCELBlinkLED(void)
{
static int visits;
visits++;
if(visits & 1)
output_low(PIN_B5);
else
output_high(PIN_B5);
} |
|
|
 |
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Mon Apr 28, 2008 2:07 am |
|
|
| Quote: | | I've migrated to pic18f4450 but nothing works now. What is wrong? |
At a minimum, the same thing as last time. |
|
 |
kein
Joined: 23 Jul 2007 Posts: 103
|
| PCM programmer you are indeed a Guru |
Posted: Mon Apr 28, 2008 3:11 am |
|
|
PCM programmer you are indeed a programming Guru...
My port address for the lcd was wrong. |
|
 |
|
|
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
|