 |
 |
| View previous topic :: View next topic |
| Author |
Message |
Douglas Kennedy
Joined: 07 Sep 2003 Posts: 755 Location: Florida
|
|
Posted: Mon Apr 28, 2008 11:16 am |
|
|
| It is possible to switch PIC's from one device to another and have code run successfully. It is never without risk especially if the code is not well understood or not well written. If there is also a lack of curiosity in reading up on the differences between the PICs then frustration frequently occurs. This frustration is often vented by pasting code on this board with a cry for help hoping others will go into the weeds to find errors. A constructive approach is to break down the code on the first PIC into sections that can be independently tested. Then one section at a time is ported to the new PIC and tested. When all sections are tested in at least someway then and only then are all the sections tested together. Now if the coder doesn't understand the original code the ability to break the code down maybe absent. If this is true then it should dissuade the coder from attempting to port it. It is possible to come up with a best selling novel by cutting and pasting from newspapers but it is more likely it will be frustrating gibberish even to the person doing it. |
|
 |
kein
Joined: 23 Jul 2007 Posts: 103
|
| Interrupt not working |
Posted: Tue Apr 29, 2008 3:35 am |
|
|
What is wrong with the following code? The interrupt is not working at all...
Nothing is working here
| Code: | #include <18F4550.h>
#device adc=10
#FUSES HSPLL, PLL5, CPUDIV1, NOWDT, PUT, BROWNOUT, NOLVP // 20 MHz xtal
#use delay(clock=48000000)
#use rs232(baud=9600,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8)
// Multiply the following values x 10 ms to get the delay times,
// since each timer tick is 10 ms.
#define LED_TIMER_TICKS 50 // 500 ms
#define RTCC_PRELOAD (256 - 196)
// GLOBALS
unsigned int16 dc;
char gc_LED_timer;
char gc_old_button_status;
/*Function definition section*/
void ADLX330ACCELBlinkLED(void) ;
void Adxl330TIMER0_isr(void);
void Adxl330AD_isr(void);
void Adxl330Accelcheck_LEDs(void);
void main()
{
set_tris_b(0x17); // TRISB = 00010111; RB4,RB2,RB1 and RB0
setup_timer_0(RTCC_INTERNAL|RTCC_DIV_256|RTCC_8_bit);
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);
set_timer0(RTCC_PRELOAD); // 256-(.01/((4*256)/20000000))
enable_interrupts(INT_TIMER0);
enable_interrupts(GLOBAL);
for(;;)
{
Adxl330Accelcheck_LEDs();
}
}
void Adxl330TIMER0_isr(void)
{
set_timer0(RTCC_PRELOAD); // 256-(.01/((4*256)/20000000))
// Decrement any timers that are running.
if(gc_LED_timer)
gc_LED_timer--;
//Now handle serial transmission of buffered character
}
void Adxl330Accelcheck_LEDs(void)
{
if(gc_LED_timer)
return;
else
gc_LED_timer = LED_TIMER_TICKS;
ADLX330ACCELBlinkLED();
}
void ADLX330ACCELBlinkLED(void)
{
static int visits;
visits++;
if(visits & 1)
output_low(PIN_B5);
else
output_high(PIN_B5);
}
|
|
|
 |
Ttelmah Guest
|
|
Posted: Tue Apr 29, 2008 4:51 am |
|
|
Big thing wrong with code as posted. Where is the interrupt handler?. Enabling an interrupt (and the global), without the handler present, will hang the chip....
Best Wishes |
|
 |
kein
Joined: 23 Jul 2007 Posts: 103
|
| The chips Hangs! |
Posted: Tue Apr 29, 2008 5:16 am |
|
|
Thanks for your help. Here I've corrected my silly mistakes but still the chip hangs.!!
| Code: | //--------------------------------------------------------
// 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 Adxl330TIMER0_isr(void)
{
set_timer0(RTCC_PRELOAD); // 256-(.01/((4*256)/20000000))
// Decrement any timers that are running.
if(gc_LED_timer)
gc_LED_timer--;
//Now handle serial transmission of buffered character
} |  |
|
 |
kein
Joined: 23 Jul 2007 Posts: 103
|
| Guys help please the interrupt is not working!! |
Posted: Tue Apr 29, 2008 10:15 pm |
|
|
WHAT IS WRONG WITH MY CODE, THE INTERRUPT IS NOT WORKING AT ALL. mY PIC16F4450 SEEMS TO BE HANG FOR NO REASON.
kIND REGARDS
s.kEIN
| Code: | #include <18F4550.h>
#device adc=10
#FUSES HSPLL, PLL5, CPUDIV1, NOWDT, PUT, BROWNOUT, NOLVP // 20 MHz xtal
#use delay(clock=48000000)
#use rs232(baud=9600,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8)
// Multiply the following values x 10 ms to get the delay times,
// since each timer tick is 10 ms.
#define LED_TIMER_TICKS 50 // 500 ms
#define RTCC_PRELOAD (256 - 196)
// GLOBALS
unsigned int16 dc;
char gc_LED_timer;
char gc_old_button_status;
/*Function definition section*/
void ADLX330ACCELBlinkLED(void) ;
void Adxl330TIMER0_isr(void);
void Adxl330AD_isr(void);
void Adxl330Accelcheck_LEDs(void);
void main()
{
set_tris_b(0x17); // TRISB = 00010111; RB4,RB2,RB1 and RB0
setup_timer_0(RTCC_INTERNAL|RTCC_DIV_256|RTCC_8_bit);
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);
set_timer0(RTCC_PRELOAD); // 256-(.01/((4*256)/20000000))
enable_interrupts(INT_TIMER0);
enable_interrupts(GLOBAL);
for(;;)
{
Adxl330Accelcheck_LEDs();
}
}
#int_TIMER0
void Adxl330TIMER0_isr(void)
{
set_timer0(RTCC_PRELOAD); // 256-(.01/((4*256)/20000000))
// Decrement any timers that are running.
if(gc_LED_timer)
gc_LED_timer--;
//Now handle serial transmission of buffered character
}
void Adxl330Accelcheck_LEDs(void)
{
if(gc_LED_timer)
return;
else
gc_LED_timer = LED_TIMER_TICKS;
ADLX330ACCELBlinkLED();
}
void ADLX330ACCELBlinkLED(void)
{
static int visits;
visits++;
if(visits & 1)
output_low(PIN_B5);
else
output_high(PIN_B5);
} |
|
|
 |
RLScott
Joined: 10 Jul 2007 Posts: 465
|
| Re: Guys help please the interrupt is not working!! |
Posted: Wed Apr 30, 2008 4:36 am |
|
|
| kein wrote: |
| Code: |
void Adxl330Accelcheck_LEDs(void)
{
if(gc_LED_timer)
return;
else
gc_LED_timer = LED_TIMER_TICKS;
ADLX330ACCELBlinkLED();
}
|
|
The ADLX330ACCELBlinkLED(); is indented as if you meant it to be called only when gc_LED_timer goes down to zero. But you left off the "{" and "}", so that it is really blinking the LED very fast all the time. I think you meant:
| Code: |
void Adxl330Accelcheck_LEDs(void)
{
if(gc_LED_timer)
return;
else
{
gc_LED_timer = LED_TIMER_TICKS;
ADLX330ACCELBlinkLED();
}
}
|
Robert Scott
Real-Time Specialties |
|
 |
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Wed Apr 30, 2008 12:09 pm |
|
|
| Quote: | | the interrupt is not working |
It works on my board. I compiled your code with vs. 4.071 and it
blinks the LED. It blinks at a rate of 2.4 Hz.
Your calculation for the RTCC_PRELOAD is wrong. Your calculation
is done as if you had a 20 MHz oscillator. In fact, if I change the
fuses to a simple HS, without all the divisors, and change the
#use delay to 20 MHz, it works perfectly. It blinks at a 1 Hz rate.
Basically, PICs (and all electronics) don't work unless you pay
attention to detail. |
|
 |
kein
Joined: 23 Jul 2007 Posts: 103
|
| Thanks |
Posted: Sun May 04, 2008 2:00 am |
|
|
Thanks guys! I realised that oring the 0x08 as shown below turns on timer0. I don't know why! | Code: | | setup_timer_0(0x08|RTCC_INTERNAL|RTCC_DIV_256); |
Secondly what is the correct formula for calculating the reload time? I'm confused between the following two formulae:
| Code: |
1) RL = 255 - (t*Fosc)/(4 * Prescaler).. t===>time
2) RL = 255 - (255 * t* Fosc)/(4 * Prescaler).. t==>time
3) RL = 65536 - ( t*Fosc)/(4 * 256* Prescaler)..
4) RL = 65536 - ( 65536*t*Fosc)/(4 * 256* Prescaler)... |
which are the right ones for timer0? |
|
 |
Ttelmah Guest
|
|
Posted: Sun May 04, 2008 2:12 am |
|
|
The top one should use 256, not 255.
Both!.....
It depends on the PIC. Most have an 8 bit timer0, so count from 0 to 255, and then go to 0 again (256 counts), so would use the top one. Some though, have timer0, programmable to work as a 16 bit timer, and then it counts from 0 to 65535, and loops back to 0, giving the 65536 counts.
Best Wishes |
|
 |
kein
Joined: 23 Jul 2007 Posts: 103
|
| Thanks guys |
Posted: Mon May 05, 2008 12:58 am |
|
|
thanks guys
Last edited by kein on Mon May 05, 2008 6:43 pm; edited 1 time in total |
|
 |
kein
Joined: 23 Jul 2007 Posts: 103
|
| What is wrong with my code? |
Posted: Mon May 05, 2008 6:42 pm |
|
|
Thanks Tlemah, but this posses a new problem. If I wanted my timer0(set to 8bit) to interrupt every 10ms(100hz). The above equation won't work as illustrated below:
| Code: | 1) RL = 256 - (t*Fosc)/(4 * Prescaler)
= 256 - (48*10^6*0.01)/(4*256)
= 256 - 469
= -213
2) RL = 256 - (256 * t* Fosc)/(4 * Prescaler).. t==>time
= 256 - (256*48*10^6*0.01)/(4*256)
= 256 - 120000
= -119744 |
How do you deal with situation like this ?
secondly, I got a new problem! The following code keeps on printing the adc values to terminal without stopping...
What I wanted to do is to use #int_timer0 to interrupts every 5ms and blink the led if button is not pressed.If a button is pressed the timeout has expired, interrupt 2 is enabled (#int_timer2). #int_timer2 enables #ADC interrupts and also continuously restarting adc conversion. The ADC interrupt fills the buffer with data until the buffer is full. When the buffer is full, both ADC and Timer2 interrupts are disabled and a display function is called to display the data on hyper terminal. Once the 1024 samples are send to the computer, the display function should exit. But it doesn't. It keeps on printing the values without stopping when count reaches 1024!!
What should I do? All I want is when a button is pressed, timer2 interrupt is enabled which in turn enables ADC interrupt and the data is sampled every 10 ms. When buffer is full, the interrupts(ADC & timer2) should be disabled and buffer values send to the computer..
Kind regards
| Code: |
#include <18F4550.h>
#device adc=10
#FUSES HSPLL, PLL5, CPUDIV1, NOWDT, PUT, BROWNOUT, NOLVP // 20 MHz xtal
#use delay(clock=48000000)
#use rs232(baud=9600,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8)
#include "LCD4X20.C"
#define RTCC_PRELOAD (65536 -60000) // 65536 -60000)=(65536 - (65536*0.005*48*10^6)/(4*256*256))
#define RTCC_ON 0X08
#define MAX_SIZE 1024
#define LED_TIMER_RLOAD 50 // 250ms
#define TMR0_TIMEOUT_TICKS; 200 // 1000ms reload time out
#define ADC_CNT_TIMERS 10 //10ms
typedef struct ADC_SAMPLES
{
int1 flag;
unsigned int16 Indx;
int Buf[MAX_SIZE];
} SAMPLES;
char LED_timer_on;
char timeout;
char adc_cnt_on ;
//Functions definition
void check_LED();
void DispData();
void TIMER0_isr(void);
void TIMER2_isr(void);
void AD_isr(void);
//.............main .....
void main()
{
data.flag = data.Indx = 0;
timeout = TMR0_TIMEOUT_TICKS;
adc_cnt_on = ADC_CNT_TIMERS;
LED_timer_on=LED_TIMER_RLOAD;
set_tris_b(0x17); // TRISB = 00010111; RB4,RB2,RB1 and RB0
lcd_init(); //
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_timer_0(RTCC_ON|RTCC_INTERNAL|RTCC_DIV_256);
set_timer0(RTCC_PRELOAD); /* 256-(.01/((4*256)/20000000)) */
enable_interrupts(INT_TIMER0);
setup_timer_2(T2_DIV_BY_16,47,16); // (48000000*0.001/(4*16*16)) interrupts every 1ms
enable_interrupts(GLOBAL); /*This enable global interrupts*/
for(;;)
{
//check_LED();
DispData();
}
}
/*Timer0 interrupts every 5ms*/
#int_TIMER0
void TIMER0_isr(void)
{
set_timer0(RTCC_PRELOAD);
if(input(PIN_B4)==1) //check if PUSHBUTTON IS NOT PRESSED
{
if(LED_timer_on-- == 0)
{
LED_timer_on=LED_TIMER_RLOAD;
output_low(PIN_B5);
}
else
{
output_high(PIN_B5);
}
}
else
{ if ((--timeout==0) && input(PIN_B4)==0) //if button pressed and timeout is 0
{
timeout = TMR0_TIMEOUT_TICKS; //reload time out
enable_interrupts(INT_TIMER2);
enable_interrupts(INT_AD);
}
}
}
/*Timer2 interrupts every 1ms and keep on restarting adc*/
#int_TIMER2
void TIMER2_isr(void)
{
if(--adc_cnt_on==0)
{
adc_cnt_on = ADC_CNT_TIMERS;
read_adc(ADC_START_ONLY);
}
}
/*Read adc results into a buffer*/
#int_AD
void AD_isr(void)
{
if(data.Indx < MAX_SIZE)
data.Buf [data.Indx++] = (read_adc(ADC_READ_ONLY) - 0X266);
if(data.Indx == MAX_SIZE - 1)
{
data.flag = 1;
data.Indx = 0;
disable_interrupts(INT_TIMER2); //INT_TIMER2 interrupt OFF
disable_interrupts(INT_AD); // INT_AD interrupt OFF
}
}
//display the adjusted adc values on hyperterminal
void DispData(void)
{
unsigned int16 x;
if( data.flag)
{
for(x = 0; x < MAX_SIZE; x++)
printf("%04Lu,\t%02d\r\n",x, data.Buf[x]);
data.flag == 0;
}
else{ lcd_gotoxy(1,4);
lcd_putc("Recording,Complete...");
}
}
|
|
|
 |
Ttelmah Guest
|
|
Posted: Tue May 06, 2008 2:39 am |
|
|
Use Timer2, or change your crystal frequency.
Best Wishes |
|
 |
Matro Guest
|
|
Posted: Tue May 06, 2008 2:48 am |
|
|
Just a remark.
In a previous post, you said :
| Quote: |
WHAT IS WRONG WITH MY CODE, THE INTERRUPT IS NOT WORKING AT ALL. mY PIC16F4450 SEEMS TO BE HANG FOR NO REASON.
|
PIC 16F4450 doesn't exist but I guess that you're talking about PIC 18F4450.
The problem I see is that your code begins with :
| Code: |
#include <18F4550.h>
|
That is absolutely not corresponding to the correct chip.
It should be :
| Code: |
#include <18F4450.h>
|
Where is the mistake?
Matro. |
|
 |
kein
Joined: 23 Jul 2007 Posts: 103
|
| Sorry that was a typo! |
Posted: Tue May 06, 2008 2:59 am |
|
|
Sorry that was a typo! My chip is pic18F4550 and therefore I had the correct header:
| Code: | | #include <18F4550.h> |
One of my problem is that printf() seems to be running forever instead of stopping once (x++ ==1024)
| Code: | //display the adjusted adc values on hyperterminal
void DispData(void)
{
unsigned int16 x;
if( data.flag)
{
for(x = 0; x < MAX_SIZE; x++)
printf("%04Lu,\t%02d\r\n",x, data.Buf[x]);
data.flag == 0;
}
else{ lcd_gotoxy(1,4);
lcd_putc("Recording,Complete...");
}
} |
I wanted this code to exit when( x++==MAX_SIZE) but it's not doing it.
Kind regards |
|
 |
RLScott
Joined: 10 Jul 2007 Posts: 465
|
| Re: Sorry that was a typo! |
Posted: Tue May 06, 2008 6:46 am |
|
|
| kein wrote: |
| Code: |
for(x = 0; x < MAX_SIZE; x++)
printf("%04Lu,\t%02d\r\n",x, data.Buf[x]);
|
I wanted this code to exit when( x++==MAX_SIZE) but it's not doing it.
Kind regards |
Do you mean that the printf() is displaying values of x higher than 1024? Are you sure MAX_SIZE is still 1024?
Robert Scott
Real-Time Specialties |
|
 |
|
|
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
|