Maker Pro
Maker Pro

USART interruption

belhouss13

Jan 27, 2016
8
Joined
Jan 27, 2016
Messages
8
Hi,
Firstly sorry for my English.
I discover pic programing with PIC18F4520
I would like to make an USART receive interruption, but I have a problem, my code is:



int Bf3_Gestion_Etat_Bouton(void) // gestion allumage led
{
if (PortA.RA0 == 0)
{
LATA.LATA4=1;
return 1;
}
else
{
LATA.LATA4=0;
return 0;
}
}

int Ash3_Gestion_Etat_Bouton(void) // gestion allumage led
{
if (PortA.RA1 == 0)
{
LATA.LATA5=1;
return 1;
}
else
{
LATA.LATA5=0;
return 0;
}
}

int Ph3_Gestion_Etat_Bouton(void) // gestion allumage led
{
if (PortA.RA2 == 0)
{
LATE.LATE0=1;
return 1;
}
else
{
LATE.LATE0=0;
return 0;
}
}

int Aux_Gestion_Etat_Bouton(void) // gestion allumage led
{
if (PortA.RA3 == 0)
{
LATE.LATE1=1;
return 1;
}
else
{
LATE.LATE1=0;
return 0;
}
}

int BouttonControl(void) // gestion allumage led
{
int a,b,c,d,sum;

if(PORTA.RA0 == 0)a=1;
else a=0;

if(PORTA.RA1 == 0)b=10;
else b=0;

if(PORTA.RA2 == 0)c=100;
else c=0;

if(PORTA.RA3 == 0)d=1000;
else d=0;

sum= a+b+c+d;
return sum;

}


void Interrupt() iv 0x000008 ics ICS_AUTO {

PIE1.RCIE=0;

LATE.RE2 = 1 ; // to turn on indicator led
delay_ms(1000);

PIR1.RCIF=0;
RCSTA.RCIE=1;

}



void main() {
int a, b, c, d ;
char *text = "v";


ADCON0 = 0b00111100;
ADCON1 = 0b00001111;
TRISA = 0b11001111;
LATA = 0b00000000;
TRISB = 0b11000000;
TRISC = 0b10010100;
TRISD = 0b11111111;
TRISE = 0b000;
LATE = 0b000;
LATC = 0b01000011;

UART1_Init(9600);
delay_ms(500) ;


OSCCON.SCS1 =0;
OSCCON.SCS0= 0;
PIE1.RCIE= 1;
PIE1.TXIE= 0;
RCON.IPEN= 0;
INTCON.GIE= 1;
INTCON.PEIE= 1;

TXSTA.TXEN= 1;

RCSTA.SPEN = 1;
RCSTA.CREN= 1;
RCSTA.CREN= 1;


while(1)
{
LATE.RE2 = 0 ; // led d'indication interruption


a= Bf3_Gestion_Etat_Bouton();
b= Ash3_Gestion_Etat_Bouton();
c= Ph3_Gestion_Etat_Bouton();
d= Aux_Gestion_Etat_Bouton();

if(a == 1)
UART1_Write_Text(text);
}

}

when I execute the code, the interruption occurs correctly, the led turn on, after the interruption turns off correctly and the routine in the while continues its correct functioning. But it is impossible to contact another interruption. I read the datasheet and think that I understand the functioning of the interruption procedure, but it seems that I forget something. if someone can help me it will be cool.
 

belhouss13

Jan 27, 2016
8
Joined
Jan 27, 2016
Messages
8
modification :

int Bf3_Gestion_Etat_Bouton(void) // gestion allumage led
{
if (PortA.RA0 == 0)
{
LATA.LATA4=1;
return 1;
}
else
{
LATA.LATA4=0;
return 0;
}
}

int Ash3_Gestion_Etat_Bouton(void) // gestion allumage led
{
if (PortA.RA1 == 0)
{
LATA.LATA5=1;
return 1;
}
else
{
LATA.LATA5=0;
return 0;
}
}

int Ph3_Gestion_Etat_Bouton(void) // gestion allumage led
{
if (PortA.RA2 == 0)
{
LATE.LATE0=1;
return 1;
}
else
{
LATE.LATE0=0;
return 0;
}
}

int Aux_Gestion_Etat_Bouton(void) // gestion allumage led
{
if (PortA.RA3 == 0)
{
LATE.LATE1=1;
return 1;
}
else
{
LATE.LATE1=0;
return 0;
}
}

int BouttonControl(void) // gestion allumage led
{
int a,b,c,d,sum;

if(PORTA.RA0 == 0)a=1;
else a=0;

if(PORTA.RA1 == 0)b=10;
else b=0;

if(PORTA.RA2 == 0)c=100;
else c=0;

if(PORTA.RA3 == 0)d=1000;
else d=0;

sum= a+b+c+d;
return sum;

}


void Interrupt() iv 0x000008 ics ICS_AUTO {

PIE1.RCIE=0;

LATE.RE2 = 1 ; // to turn on indicator led
delay_ms(10);

PIR1.RCIF=0;
PIE1.RCIE=1;

}



void main() {
int a, b, c, d ;
char *text = "v";


ADCON0 = 0b00111100;
ADCON1 = 0b00001111;
TRISA = 0b11001111;
LATA = 0b00000000;
TRISB = 0b11000000;
TRISC = 0b10010100;
TRISD = 0b11111111;
TRISE = 0b000;
LATE = 0b000;
LATC = 0b01000011;

UART1_Init(9600);
delay_ms(500) ;


OSCCON.SCS1 =0;
OSCCON.SCS0= 0;
PIE1.RCIE= 1;
PIE1.TXIE= 0;
RCON.IPEN= 0;
INTCON.GIE= 1;
INTCON.PEIE= 1;

TXSTA.TXEN= 1;

RCSTA.SPEN = 1;
RCSTA.CREN= 1;
RCSTA.CREN= 1;


while(1)
{
LATE.RE2 = 0 ; // led d'indication interruption


a= Bf3_Gestion_Etat_Bouton();
b= Ash3_Gestion_Etat_Bouton();
c= Ph3_Gestion_Etat_Bouton();
d= Aux_Gestion_Etat_Bouton();

if(a == 1)
UART1_Write_Text(text);
}

}
 

belhouss13

Jan 27, 2016
8
Joined
Jan 27, 2016
Messages
8
Hi Amar,

no I don't. the program stay on the interruption: i try to check all the day where the problem come from but no solution
 

Amar Dhore

Dec 2, 2015
129
Joined
Dec 2, 2015
Messages
129
I can help you but Could you explain a little bit what you are trying to do?
 

Amar Dhore

Dec 2, 2015
129
Joined
Dec 2, 2015
Messages
129
when I execute the code, the interruption occurs correctly, the led turn on, after the interruption turns off correctly and the routine in the while continues its correct functioning. //I understood this

But it is impossible to contact another interruption. //what do you mean by this???????????????
Are you saying that if you can have another interrupt routine besides USART?
 

belhouss13

Jan 27, 2016
8
Joined
Jan 27, 2016
Messages
8
Hi Amar,

I find a solution. the problem was because RCIF is a flag bit only readable. To clear It, it was necessary to read the data, it was for thiat the program stayd on the interrupt routine. Thank you very much Amar it's very sympatic.
Where do you come from?
 

Amar Dhore

Dec 2, 2015
129
Joined
Dec 2, 2015
Messages
129
Yes you are right. RCIF flag will be set as long as there is data in your receive reg. @Belhouss I am from USA.
 

belhouss13

Jan 27, 2016
8
Joined
Jan 27, 2016
Messages
8
Hi amar,

nice to meet you:
I have a question I think that you have the response: I have an Digital / analogique converteur that i need commande with an edge signal, to genrate this signal with a pic i must use timer with interruption? If it´s right do it existe other solution?
 

Amar Dhore

Dec 2, 2015
129
Joined
Dec 2, 2015
Messages
129
if you need a constant duty cycle, yes you can use a timer with an interrupt to generate a square signal or other option is, you can generate PWM (google). I think that's what you asking, right?
 
Top