Jump to content

Baixue

EstablishedMember
  • Content Count

    16
  • Joined

  • Last visited

Community Reputation

0 Neutral

About Baixue

  • Rank
    Newbrie
  1. I've been trying to fix the getc() block function for the past week. But it still doesn't work as I want it to be. So I decide to use two PICs for this, one for IOC,one for USART. Hope it will work this way. Thank you so much for all the help!!!! Cheers, Baixue
  2. Hi Reynard, I think the problem must be like the quote says. But I still want to check the USART interrupt feature, maybe~~I accidently enable the USART interrupt somewhere, so it stays in interrupt routine. By the way, I am using a IR sensor as the source of IOC interrupt instead of a switch, so there are not much noise. And also I am wondering if I connect the IR sensor to one of the I/O ports instead of IOC port, and check every 10ms to see if there is a change on the I/O port, that might work, isn't it? Thanks a lot! Cheers, Baixue
  3. This time I can see the output from Rs232 so it gives a better view of where the programs stops. everytime I start to run the program, it works well for the first time it get interrupted, the output shown is like this: start start start start end end start start........... . But when I give it a second interrupt ,it shows like this : start start end end start(it stops here, nothing shows up), when I give it a interrupt again, it only shows : end end. And it continues to show "end end" , never shows "start" again , which means it never goes back to main. So why it alwasy works well for the very first interrupt?(goes to interrupt routine and then go back to main) ,but after that, it is screwed up. My code is here Thank you a lot! #include <system.h> #pragma DATA 0x2007, _HS_OSC & _WDT_OFF #pragma CLOCK_FREQ 4000000 bool IR_event=false; unsigned char ss; void initi_status() { baudctl=0x00;//set the baud rate control register set_bit(rcsta,7);//set SPEN bit,serial port enabled spbrg = 207;//set the baud rate as 1200, brgh=1 ansel =0x00; // the fllowing two used to turn off analouge modle anselh =0x00; trisb=0x11; //congigure the RX/TX port clear_bit(txsta,6); // 8 bit transmission set_bit(txsta,5); // enable the transmission clear_bit(txsta,4); // asychronous model set_bit(txsta,2); // high speed baud rate clear_bit(rcsta,6);// 8 bits reception set_bit(rcsta,4);// enable continous receiving once asynchronous is selected trisc=0x00; portc=0x00; } void putc(char c) { txreg=c; // put the input into txreg register while ((txsta&0x02) == 0){ } //check if transmission is done or not } void put(char *string) { do { char s = *string; if( s == 0 ) // end of string break; putc( s ); // Send the string refered by pointer string++; } while( 1 ); } char getc(void) { while (pir1.5==0) { }// checked if the receiving is done return rcreg; } void main() { initi_status(); trisc=0x00; //configure portc as output portc=0x00;//initialize portc trisb=0xff; // configure portb as input, we are going to use portb.6&4 as IOC,save .7& 5 as RX/TX iocb=0x10; //enable portb.6 & 4 IOC set_bit(intcon,7);//enable all interrupts set_bit(intcon,6);//enable all peripheral interrupts clear_bit(intcon,5);//disable the timer0 interrupt clear_bit(intcon,4);// disable the RA2/INT exteranl interrupt set_bit(intcon,3);//enable the interrupt(RABIE) ansel =0x00; // the fllowing two used to turn off analouge modle anselh =0x00; while(1) { delay_s(1); portc=0x00; put("start "); if(IR_event) { IR_event=false; ss=getc(); } } } void interrupt() { IR_event=true; portc=0x01; unsigned char v=portb;//read portb clear_bit(intcon,0);// clear the interrupt flag(RABIF) put("end! "); }
  4. I am trying to implement IOC and USART feature together on PIC16f690. It suppose to work like this: Once the IOC happens, it should go to interrupt routine and light up the led. And then when it goes back to while loop in main, PIC will receive a character using USART (in my code:unsinged char ss=getc()). But it works very weird like this: before any IOC happens, it stays in while loop in main(which is right), but after one interrupt occurs, it stays in the interrupt routine,never goes back to while loop in main. But if I comment out " unsinged char ss=getc() " (the receive code), the IOC works perfectly. So I think it's the reception cause the problem. I just don't understand how come USART could screw up IOC. Can anyone help me?? I will really really appreicate any help!!! Regards, Baixue #include <system.h> #pragma DATA 0x2007, _HS_OSC & _WDT_OFF // Use portb.4 as IOC port; portb.7 & 5 are USART pins bool IR_event=false; unsigned char ss; void initi_status() { baudctl=0x00;//set the baud rate control register set_bit(rcsta,7);//set SPEN bit,serial port enabled spbrg = 103;//set the baud rate as 2400, brgh=1 ansel =0x00; // the fllowing two used to turn off analouge modle anselh =0x00; trisb=0x11; //congigure the RX/TX port clear_bit(txsta,6); // 8 bit transmission set_bit(txsta,5); // enable the transmission clear_bit(txsta,4); // asychronous model set_bit(txsta,2); // high speed baud rate clear_bit(rcsta,6);// 8 bits reception set_bit(rcsta,4);// enable continous receiving once asynchronous is selected trisc=0x00; portc=0x00; } char getc(void) { while (pir1.5==0) { }// checked if the receiving is done return rcreg; } void main() { initi_status(); trisc=0x00; //configure portc as output portc=0x00;//initialize portc trisb=0xff; // configure portb as input, we are going to use portb.6&4 as IOC,save .7& 5 as RX/TX iocb=0x10; //enable portb. 4 IOC set_bit(intcon,7);//enable all interrupts set_bit(intcon,6);//enable all peripheral interrupts clear_bit(intcon,5);//disable the timer0 interrupt clear_bit(intcon,4);// disable the RA2/INT exteranl interrupt set_bit(intcon,3);//enable the interrupt(RABIE) while(1) { delay_ms(200); portc=0x00; while(IR_event) { delay_ms(300); IR_event=false; [size="6"]ss=getc();[/size] // this reception code screws up the IOC,but how come? why? } } } void interrupt() { IR_event=true; portc=0x01;//light up led unsigned char rr=portb;//read portb to clear the interrupt clear_bit(intcon,0);// clear the interrupt flag(RABIF) }
  5. Thank you so much techie and Reynard!! It works very well right now!!! To techie: Your way works good for two different kinds of interrupt, like one IOC, one Timer0 interrupt, but it won't work on two IOC, cause all the IOC share the same interrupt flag, you won't be able to tell which port just get interrupted if there is only one interrupt flag used by all the IOC ports. But thank you for this idea! To Reynard: I did wha you said and it works perfectly now! Thank you so much! Here is my code, just in case the other people needs to look at it. Thank you so much you guys! #include <system.h> #pragma DATA 0x2007, _HS_OSC & _WDT_OFF bit c6,c4,p6,p4; void main() { trisc=0x00; //configure portc as output portc=0x00;//initialize portc trisb=0xff; // configure portb as input, we are going to use portb.6&4 as IOC,save .7& 5 as RX/TX iocb=0x50; //enable portb.6 & 4 IOC set_bit(intcon,7);//enable all interrupts set_bit(intcon,6);//enable all peripheral interrupts clear_bit(intcon,5);//disable the timer0 interrupt clear_bit(intcon,4);// disable the RA2/INT exteranl interrupt set_bit(intcon,3);//enable the interrupt(RABIE) ansel =0x00; // the fllowing two used to turn off analouge modle anselh =0x00; while(1) { delay_ms(200); portc=0x00; } void interrupt() { c6=portb.6; //put the current value of portb.6 into bit c6 c4=portb.4; //put the current value of portb.4 into bit c4 if(c6^p6) // if the current value is different with previous { if(c6==0)// this used to filter out the falling edge portc=0x01;//light on led#1 } else if(c4^p4) // if the current value is different with previous { if(c4==0) portc=0x08; ;//light on led#4 } portb=0xff;//write to portb to clear the interrupt clear_bit(intcon,0);// clear the interrupt flag(RABIF) p6=portb.6; //put the current value into p6, it will be the previous value for the next round p4=portb.4; }
  6. I still couldn't figure out what's wrong with my code, I decide to give up working on porta. I think I just will let the IOC work on portb,cause it works perfectly on any port of portb. Shrug~~ Thank you so much for your help though,AlexR! Regards, Baixue
  7. I need it here(at line 10): ioca=0x20;//enable porta.5 IOC Could it be any other reason? Thanks a lot!
  8. I wonder if there is a way to do multiple interrupt on PIC16F. Like what I am doing is set two ports as IOC, if the first port get interrupted, light up led 1, and then if the second port get interrupt, light up led 2. But since there is only one function interrupt(), I don't know how can the program go to two different routines and do difference things. Thank you for any answer!!
  9. I am still dealing with IOC on the low pin count demo board, which has a PIC16f690 on it. I wrote the IOC for portb and it works perfectly, however, it just doesn't work on porta (I use porta.5 as IOC pin in my case), which is really frustrating and confusing. Could some one take a look at my code and see what the problem would be? Appreciate a lot!! #include <system.h> #pragma DATA 0x2007, _HS_OSC & _WDT_OFF void main() { trisc=0x00; //configure portc as output portc=0x00;//initialize portc trisa=0xff;//configure porta as input, use porta.5 as IOC ioca=0x20;//enable porta.5 IOC set_bit(intcon,7);//enable all interrupts set_bit(intcon,6);//enable all peripheral interrupts clear_bit(intcon,5);//disable the timer0 interrupt clear_bit(intcon,4);// disable the RA2/INT exteranl interrupt set_bit(intcon,3);//enable the interrupt(RABIE) ansel =0x00; // the fllowing two used to turn off analouge modle anselh =0x00; while(1) { delay_ms(200); portc=0x00; } } void interrupt() { portc=0x01; // when interrupt happens, light up a led porta=0xff;//write to porta to clear the interrupt clear_bit(intcon,0);// clear the interrupt flag(RABIF) }
  10. I did it! Sorry to bother you guys with this topic Just ignore this . Thank you anyway!
  11. I did the IOC on pic16f690 and it works. But it gives interrupt on both edges, how can I filter out on of the edges(either falling or rising)? Thanks a lot!! Regards, Baixue
  12. Thank you so much Reynard!!!!! That make a lot sense!!!
  13. Yeah! I got it work now! Thank you so much Reynard! And thank you all guys! But I wonder do ANSEL and ANSELH have to be treated when you want to make the ports deal with digital signals? Because when I first learn microcontroller to make the port as digital output, I didn't deal with ANSELH stuff and it worked. Thank you!! Regards, Baixue
  14. The hardware I use is Low Pin Count Demo Board. I didn't put on any extra stuff on the board and the board is very simple, just has a pic16f690 and four leds on it. I tested the my Timer0 interrupt code today(the one I posted), it still doesn't work
×
×
  • Create New...