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 support@ccsinfo.com

Problem with interruptions PIC 16F628a + PCM compiler v4.068

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



Joined: 09 Aug 2011
Posts: 6

View user's profile Send private message

Problem with interruptions PIC 16F628a + PCM compiler v4.068
PostPosted: Tue Aug 09, 2011 2:27 am     Reply with quote

My project is to receive data with serial communication from VB.net. This data will be used as a delay to switch on and to switch off 3 leds !! So the algorithm is as follows:
In the PC we have 3 buttons !! When the user launch the PC-Interface he will push the"CONNECT" button (the vb will send "1" to the pic), the pic will send the currents values of the delays (a1,b1,a2,b2) after receiving this delays !!
The user have the choice between two button !! the first !! pushing the "CONFIGURATION MODE" in VB interface !! (VB will send "2" to the pic will send "F" or "C" according to the state of the switch connected to RB0 when the pic send "C" so it's ok !! We can send new delays ... after typing the new data the user press "SEND" a message will appear to confirm his choice (VB.net will send the new delay and pic will response will the same data ) so after user's confirmation the pic will save the new delays in his EEPROM.
Also it exist another button "FUNCTIONAL MODE" but this function is not important. So my problem is when I send Minutes the pic will not be able to manage the interruptions !! So I think that the problem is in the 'minute' function and I have doubts about the int_ext interruption.
Please if someone can help me I will be very grateful Smile) this is my code :
Code:

#include <16F628A.h>

#FUSES NOWDT                    //No Watch Dog Timer
#FUSES LP                       //Low power osc < 200 khz
#FUSES NOPUT                    //No Power Up Timer
#FUSES NOPROTECT                //Code not protected from reading
#FUSES NOBROWNOUT               //No brownout reset
#FUSES NOMCLR                   //Master Clear pin used for I/O
#FUSES NOLVP                    //No low voltage prgming, B3(PIC16) or B5(PIC18) used for I/O
#FUSES NOCPD                    //No EE protection

#use delay(clock=4000000)
#use rs232(baud=9600,parity=N,xmit=PIN_A3,rcv=PIN_A2,bits=8)

#include <stdlib.h>
#include <string.h>

#use delay (clock=4000000)

#use rs232 (baud=9600, xmit=pin_b2, rcv=pin_b1, parity=N, stop=1)

#define so 10   // définir les adresse mémoir EEPROM qu'on va les utiliser
#define mo 15
#define sf 20
#define mf 25

unsigned char send[5],c[5],etat[5],select[5],chaine1[20],chaine2[20],str1[20],str2[20];  // déclaration d'une chaine de caractères d'une taille maximale 20
unsigned int16 s=0,x1=0,y1=0,x2=0,y2=0,a1=0,b1=0,a2=0,b2=0;                          // déclaration d'un entier non signé de longueur 16 bits 
unsigned int1 k=0;         
unsigned int z=0,w,x=0,j,i=0;

#int_ext
void ext_isr()
{
   //DISABLE_INTERRUPTS(INT_ext);
   if(!input(pin_b0)){
   output_low(pin_b4);
   output_low(pin_b5);
   output_low(pin_b6);
   output_low(pin_b7);
   output_high(pin_a2);
   output_low(pin_a1);}
}

#int_timer0
void timer(){
   z=0;
   if(kbhit()){
   gets(select);
   x=atoi(select);
   switch(x){
   case 1:{
   printf("%lu\r",a1);          // time of LED lighting in seconds
   printf("%lu\r",b1);          // time of LED lighting in minutes
   printf("%lu\r",a2);          // time of LED switched off in seconds
   printf("%lu\r",b2);          // time of LED switched off in minutes
   break;
   }
   case 2:{
   j=2;
   strcpy(c,"2");               
   if(input(pin_b0))             
   printf("%s\r","F");
   while(input(pin_b0)&& j==2)  // boucle pour passer au mode config si on l'est pas deja   
   {
   //delay_ms(1000);
   gets(c);
   j=atoi(c);
   output_high(pin_a1);
   output_low(pin_a2);
   if(input(pin_b0)&& j!=8)
   printf("%s\r","F");
   }
   if(j!=8){
   printf("%s\r","C");
   output_high(pin_a2);
   output_low(pin_a1);
   output_low(pin_b4);         
   output_low(pin_b5);
   output_low(pin_b6);
   output_low(pin_b7);}
   while(z==0 && j!=8 ){
   gets(send);
   printf("%s\r",send);
   s=atol(send);
   if(s==4){
   gets(chaine1);               // acquisition des nouveaux valeurs
   gets(str1);
   gets(chaine2);
   gets(str2);
   x1=atol(chaine1);            // conversion chaine -> entier long
   y1=atol(str1);
   x2=atol(chaine2);
   y2=atol(str2);
   printf("%s\r",chaine1);      // renvoyer les valeurs reçu pour une confirmation de l'utilisateur
   printf("%s\r",str1);
   printf("%s\r",chaine2);
   printf("%s\r",str2);
   gets(select);
   w=atoi(select);
   if (w==1) {
   a1=x1;
   a2=x2;
   b1=y1;
   b2=y2;
   write_eeprom(so,a1);
   write_eeprom(mo,b1);
   write_eeprom(sf,a2);
   write_eeprom(mf,b2);
   a1=a1*1000;
   a2=a2*1000;
   z=1;
   }
   else
   {
   z=0;
   }
   }
   else
   {
   z=1;
   }
   }
   break;
   }
   case 3:{
   j=3;
   strcpy(c,"3");               
   if(input(pin_b0)==0)             
   printf("%s\r","C");
   while(input(pin_b0)==0 && j==3)  // boucle pour passer au mode config si on l'est pas deja   
   {
   gets(c);
   j=atoi(c);
   if((input(pin_b0)==0) && j!=9 )
   printf("%s\r","C");
   }
   if (j!= 9)
   printf("%s\r","F");
   break;
   }
}
   }
}

void minute (int16 m){    // procédure qui fait un durée d'attente de m minutes
   while(m>0){
   delay_ms(60000);
   m--;}
}

void main()
{
   EXT_INT_EDGE(H_TO_L);
   setup_timer_0(RTCC_INTERNAL | RTCC_DIV_256);
   setup_timer_1(T1_DISABLED);
   setup_timer_2(T2_DISABLED,0,1);
   setup_comparator(NC_NC_NC_NC);
   setup_vref(FALSE);
//Setup_Oscillator parameter not selected from Intr Oscillotar Config tab
   set_timer0(60);
   // TODO: USER CODE!!
   enable_interrupts(GLOBAL);// autorisé tout les intérruptions
   a1=read_eeprom(so);
   b1=read_eeprom(mo);
   a2=read_eeprom(sf);
   b2=read_eeprom(mf);
   a1=a1*1000;
   a2=a2*1000;
   while(1)
   {
   enable_interrupts(INT_EXT);
   enable_interrupts(int_timer0);
   if((input(pin_b0))){
   output_high(pin_b4);
   output_high(pin_b5);
   output_high(pin_b6);
   output_high(pin_b7);
   output_high(pin_a1);
   output_low(pin_a2);
   delay_ms(a1);
   minute(b1);
   output_high(pin_a1);
   output_low(pin_a2);
   output_low(pin_b4);       
   output_low(pin_b5);
   output_low(pin_b6);
   output_low(pin_b7);
   delay_ms(a2);
   minute(b2);
   }     
   else
   {
   output_high(pin_a2);
   output_low(pin_a1);
   }
   }
}
Ttelmah



Joined: 11 Mar 2010
Posts: 19327

View user's profile Send private message

PostPosted: Tue Aug 09, 2011 4:18 am     Reply with quote

Your chip has a hardware UART. Use this for your serial. Otherwise you have not got a hope.
Keep interrupt handlers _SHORT_. You _must_ get out again quickly, or something will suffer. You have perhaps 1000* as much code in the timer interrupt as is acceptable, with things that themselves use a lot of arithmetic, and take a long time.
On wake up, INTEDG, defaults to '1', which means that INT_EXT triggers when pin B0, goes from low to high. You then test B0 in the interrupt routine, and change various pins, if it is low. This will only happen, if B0, has gone high for less than perhaps 30uSec.
Don't keep re-enabling the interrupts in the loop.

Best Wishes
raflab



Joined: 09 Aug 2011
Posts: 6

View user's profile Send private message

PostPosted: Tue Aug 09, 2011 4:45 am     Reply with quote

Thx for the fast response Smile) So I will replace the timer0 with int_rda interruption ?? and the treatment of the interruption must be as quickly as it possible !! But even if I do that the problem of the "Minute" function still appear. When I do data acquisition from the VB.net interface and I use that data in 'minute' the PIC can't manage the interruption like he's doing when the delay is only in second :(
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