CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

Variable corrupted when sharing in main/isr using C18

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
sugus
Guest







Variable corrupted when sharing in main/isr using C18
PostPosted: Thu Feb 23, 2006 5:42 am     Reply with quote

Hi guys,

I've got a problem with the "enc_a_value". I've written a simple code to test some functionalities of my design. My goal is to turn on a motor in forward direction for 500 encoder pulses. When it reaches that position, stop it and after a delay, turn it back in the reverse direction for 500 pulses again.

The problem is that when I clear "enc_a_value" on main, I can see that it is correctly cleared, but when it jumps again to the Servo Isr (where enc_a_value is updated), this variable takes it's old value again (500).

I'm using C18 v.2.30 demo and MPLAB 7.30. I tried to disable interrupts when updating the variable in main, and adding "-v" to compiler settings, but still not works.

I will be grateful with any suggest.

Regards.


#include <p18cxxx.h>
#include <math.h>
#include <timers.h>
#include <pwm.h>

#typedef unsigned int PWM;

volatile float j = 0;
volatile unsigned int enc_a_value = 0;
volatile unsigned int enc_b_value = 0;
volatile unsigned int encoder_tmp = 0;


void interrupt_at_high_vector(void);
void servo_isr(void);
void delay(void);
void init_timers(void);
void config_ports(void);
void azimuth_motor_forward(PWM azimuth_duty);
void azimuth_motor_reverse(PWM azimuth_duty);
void elevation_motor_forward(PWM elevation_duty);
void elevation_motor_reverse(PWM elevation_duty);
void azimuth_motor_fast_stop(void);
void elevation_motor_fast_stop(void);



#pragma config WDT = OFF

#pragma code high_vector = 0x08
void interrupt_at_high_vector(void)

{
_asm
goto servo_isr
_endasm
}
#pragma code
#pragma interrupt servo_isr
void servo_isr(void){

PIR1bits.TMR1IF = 0; // resets Timer1 interruption flag
WriteTimer1(0xACAF); // TMR1 start counting value

enc_a_value = TMR3L; // Encoder A value refresh from
encoder_tmp = TMR3H; // Timer 3 asynchronous counter
encoder_tmp = (encoder_tmp << 8); //
enc_a_value = encoder_tmp + enc_a_value; //

enc_b_value = TMR0L; // Encoder A value refresh from
encoder_tmp = TMR0H; // Timer 0 asynchronous counter
encoder_tmp = (encoder_tmp << 8); //
enc_b_value = encoder_tmp + enc_b_value; //
}

void delay (void)
{
double i;


for (i = 0; i < 1000; i++)
{
j = sin(rand());
}

}

void config_ports (void)
{

TRISA = 111111;
TRISB = 11001000;
TRISC = 00000001;
TRISD = 00000011;
TRISE = 00000000;

}

void init_timers (void)
{

OpenTimer0(TIMER_INT_OFF & T0_16BIT & T0_SOURCE_EXT & T0_EDGE_RISE); // ENCODER B CONFIG
OpenTimer1(TIMER_INT_ON & T1_16BIT_RW & T1_SOURCE_INT & T1_PS_1_1 & T1_OSC1EN_OFF & T1_SYNC_EXT_OFF ); // SERVO
OpenTimer2(TIMER_INT_OFF & T2_PS_1_4 & T2_POST_1_1); // PWM
OpenTimer3(TIMER_INT_OFF & T3_16BIT_RW & T3_SOURCE_EXT & T3_PS_1_1 & T3_SYNC_EXT_ON & T1_CCP1_T3_CCP2); // ENCODER A CONFIG
WriteTimer1(0xACAF);
}

void azimuth_motor_forward(PWM azimuth_duty)
{
SetDCPWM1 (azimuth_duty); // Sets PWM1 duty cycle
PORTBbits.RB0 = 0;
PORTBbits.RB1 = 1; // TMR1 start counting value
}

void azimuth_motor_reverse (PWM azimuth_duty)
{
SetDCPWM1 (azimuth_duty); // Sets PWM1 duty cycle
PORTBbits.RB0 = 1;
PORTBbits.RB1 = 0;
}

void elevation_motor_forward (PWM elevation_duty)
{
SetDCEPWM1 (elevation_duty); // Sets PWM2 duty cycle
PORTEbits.RE0 = 0;
PORTEbits.RE1 = 1;
}


void elevation_motor_reverse (PWM elevation_duty)
{
SetDCEPWM1 (elevation_duty); // Sets PWM2 duty cycle
PORTEbits.RE0 = 1;
PORTEbits.RE1 = 0;
}

void azimuth_motor_fast_stop (void)
{
PORTBbits.RB0 = 1;
PORTBbits.RB1 = 1;
}

void elevation_motor_fast_stop (void)
{
PORTEbits.RE0 = 1;
PORTEbits.RE1 = 1;
}

void main (void)
{

_asm
MOVLW 0X0A //
MOVWF 0x0FC1, 0 // configure ADcon1 peripheral
_endasm

config_ports();
init_timers();
INTCONbits.GIE = 1; // Enables Global Interrupt bit
INTCONbits.PEIE = 1; // Enables Peripheral Interrupt bit

OpenPWM1 (0x28); // Sets PWM1 period
OpenEPWM1 (0x28); // Sets PWM2 period


while(1){

while (enc_a_value < 0x500){
azimuth_motor_forward(0x50);
elevation_motor_forward(0x50);
INTCONbits.GIE = 0; // Disables Global Interrupt bit
PORTCbits.RC7 = 1;
PORTCbits.RC6 = 0;
INTCONbits.GIE = 1; // Enables Global Interrupt bit
}
azimuth_motor_fast_stop();
elevation_motor_fast_stop();
INTCONbits.GIE = 0; // Disables Global Interrupt bit
enc_a_value = 0;
enc_b_value = 0;
INTCONbits.GIE = 1; // Enables Global Interrupt bit

delay();

while (enc_a_value < 0x500){
azimuth_motor_reverse(0x50);
elevation_motor_reverse(0x50);
PORTCbits.RC7 = 1;
PORTCbits.RC6 = 1;
}
azimuth_motor_fast_stop();
elevation_motor_fast_stop();
INTCONbits.GIE = 0; // Disables Global Interrupt bit
enc_a_value = 0;
enc_b_value = 0;
INTCONbits.GIE = 1; // Enables Global Interrupt bit
delay();

///////////////////////////////


while (enc_a_value < 0x500){
azimuth_motor_forward(0xE5);
elevation_motor_forward(0xE5);
PORTCbits.RC7 = 1;
PORTCbits.RC6 = 0;
}
azimuth_motor_fast_stop();
elevation_motor_fast_stop();
INTCONbits.GIE = 0; // Disables Global Interrupt bit
enc_a_value = 0;
enc_b_value = 0;
INTCONbits.GIE = 1; // Enables Global Interrupt bit
delay();

while (enc_a_value < 0x500){
azimuth_motor_reverse(0xE5);
elevation_motor_reverse(0xE5);
PORTCbits.RC7 = 0;
PORTCbits.RC6 = 1;
}

azimuth_motor_fast_stop();
elevation_motor_fast_stop();
INTCONbits.GIE = 0; // Disables Global Interrupt bit
enc_a_value = 0;
enc_b_value = 0;
INTCONbits.GIE = 1; // Enables Global Interrupt bit
delay();
} //end while(1)
}//end main()
Ttelmah
Guest







PostPosted: Thu Feb 23, 2006 10:38 am     Reply with quote

This is _not_ a C18 forum, but a CCS forum. Rewrite it in CCS C, and somebody here may well be able to help you, but if you want to ask a C18 question, you'd better go onto one of the C18 forums...

Best Wishes
treitmey



Joined: 23 Jan 2004
Posts: 1094
Location: Appleton,WI USA

View user's profile Send private message Visit poster's website

PostPosted: Thu Feb 23, 2006 11:04 am     Reply with quote

I have a question about a 55' triumph TR2 long door.

How do you tune the SU carbs?


My attempt at dry humor Very Happy
Duh. The big CCS at the top of the forum didn't give him a clue?
sugus
Guest







PostPosted: Fri Feb 24, 2006 5:40 am     Reply with quote

Thanks both for your humor.

If some day CCS provides a real 18-family compiler, I will seriously think about using it.
ccsc very good
Guest







pic18 ok... hhh
PostPosted: Fri Feb 24, 2006 6:30 am     Reply with quote

you're blind

it's only discrepancy in ability
dyeatman



Joined: 06 Sep 2003
Posts: 1933
Location: Norman, OK

View user's profile Send private message

PostPosted: Fri Feb 24, 2006 6:47 am     Reply with quote

I (and many others here) have used the "real" CCS family compiler in many commercial/Industrial projects and it has done the job. Bugs are present in every compiler INCLUDING C18. Please don't come here as a C18 user and down the CCS product. If you don't have CCS and/or nothing good to say, how about going to the C18 forum with your C18 questions and comments. I don't go to the C18 forum and down them....
Guest








PostPosted: Fri Feb 24, 2006 8:31 am     Reply with quote

dyeatman wrote:
I (and many others here) have used the "real" CCS family compiler in many commercial/Industrial projects and it has done the job. Bugs are present in every compiler INCLUDING C18. Please don't come here as a C18 user and down the CCS product. If you don't have CCS and/or nothing good to say, how about going to the C18 forum with your C18 questions and comments. I don't go to the C18 forum and down them....


I have also used CCS in many projects too. It did the job, with some bugs (especially when using I2c and timer interrupts together, at high speed). But when upgrading at 18 family, I miss some functionalities, regarding CAN bus for example. If I don't have much good things to say it's maybe because my english is quite limited. My intention is far from down this forum, so I apologize for posting a C18 code. I'm sure you will overcome the hurt.
Guest








PostPosted: Fri Feb 24, 2006 9:43 am     Reply with quote

treitmey wrote:
I have a question about a 55' triumph TR2 long door.

How do you tune the SU carbs?


My attempt at dry humor Very Happy
Duh. The big CCS at the top of the forum didn't give him a clue?


The best thing you can do with your Triumph is bring it to a scrapyard. A pick-up will be ideal for you. That allows you to drive closely with Lakewood's cows.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
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