Controller isn't acting on input data

mikroC, mikroBasic and mikroPascal PRO for Microchip’s 8-bit PIC MCUs.
Post Reply
Author
Message
mikrocdaniel
Posts: 1
Joined: 28 Jan 2022 14:21

Controller isn't acting on input data

#1 Post by mikrocdaniel » 28 Jan 2022 14:57

Hello.

Im busy with a schoolproject, a lift that is controlled by two controller (master/slave)
When i reading de sensordata and acting on it to stop the motor, some time it will and when i write more code it doesn't anymore. plz help!

I2C is working and can confirm this with my scope, if i press a button on the slave is start de lift on the master.
Only the sensor in the master doesn't stop de lift in the master.

i'm using the PIC16F877A for the master and slave operations

im aware that there is a lot of declarations that are not been used, im at the stage of trail & error and cant figure it out! :twisted:

Here is my code:

unsigned short storing;
unsigned short LiftPositieStap;

unsigned short Rdata = 0;
unsigned short Wdata = 0;

unsigned int T_Slave_I2C = 0;
unsigned int T_Lift_BasisPositie = 0;

unsigned short read;

unsigned short portd_shadow;
unsigned short portc_shadow;
unsigned short slave_shadow;

unsigned char ii = 0;

void init(){
//Poort setup, 0=output, 1=input, <76543210>
adcon0 = 0;
adcon1 = 0x07; //config to digital io
TRISA = 0B11111111;
PORTA = 0;
TRISB = 0B11111111;
PORTB = 0;
TRISC = 0B00000000;
PORTC = 0;
TRISD = 0B11111111;
TRISE = 0B00000111;
PORTE = 0;
}

void InitTimer0(){
OPTION_REG = 0x82;
TMR0 = 6;
INTCON = 0xA0;
}

void Interrupt(){
//Timer overflow 1ms
if (TMR0IF_bit){
TMR0IF_bit = 0;
TMR0 = 6;
//Timers
T_Slave_I2C++;
T_Lift_BasisPositie++;
}
}

int SlaveData(unsigned char adres, unsigned char send){
unsigned char tempRData;
I2C1_Start();
I2C1_Wr(adres);
I2C1_Wr(send);
I2C1_Repeated_Start();
I2C1_Wr(adres+1);
tempRData = I2C1_Rd(0);
I2C1_Stop();
return tempRData;
}

void main(){
init();
I2C1_Init(100000);
InitTimer0();
PWM1_Init(5000);
PWM1_Set_Duty(200);
PWM1_Start();

storing = 1;
while(1){
//--------------------------------
//Input poorten lezen
portd_shadow = portd;
slave_shadow = RData;
WData = ~portd_shadow;
//--------------------------------
if(T_Slave_I2C >= 50){
RData = SlaveData(0x40, WData);
T_Slave_I2C = 0;
}

//Startup: bring lift to start position
if(storing == 1){
LiftPositieStap = 0;
if(slave_shadow.f0 == 1 & portd_shadow.f1 == 1 & LiftPositieStap == 0){ //RData.F0 == 1 &
portc_shadow = 0b00000110;
LiftPositieStap = 1;
}
if(~portd_shadow.f1 == 1 & LiftPositieStap == 1){
portc_shadow = 0b00000000;
}


/*
if(slave_shadow.f1 == 1){
portc_shadow = 0b00000101;
}

if(~portd_shadow.f2 == 1){
portc_shadow = 0b00000000;
storing = 0;
}
*/


}

else{
//normal operation
}

//OUTPUT Data
PORTC = portc_shadow;
}

if i start using the code that has bolds it doesn't act on my sensor anymore to stop the lift

sgssn
Posts: 35
Joined: 13 Sep 2021 16:24

Re: Controller isn't acting on input data

#2 Post by sgssn » 01 Feb 2022 10:02

Hi
first: All data, that is modified in an ISR should be declared as volatile. For example your
T_Slave_I2C;
T_Lift_BasisPositie;

So you should write:
volatile unsigned int T_Slave_I2C = 0;
volatile unsigned int T_Lift_BasisPositie = 0;

Than, i do not recognise your sensors. How do you react on them?

Besides: a further point: Do you really use data types int and short at school?
I don'tknow your compiler, but for clearance it is better to use int8_t, uint8_t, int16_t, uint16_t... to be aware, what limits you have.


regards
Gerhard

Post Reply

Return to “PIC PRO Compilers”