Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

[SOLVED] writing a variable on output using RB0 interrupt

Status
Not open for further replies.

kappa_am

Full Member level 6
Joined
Jul 16, 2012
Messages
331
Helped
19
Reputation
38
Reaction score
19
Trophy points
1,298
Location
Vancouver
Activity points
3,859
Hi all,
I would like to write a variable which is computed in main function to PORTC using RB0 interrupt. But I can not write a value in PORTC in ISR loop.
I would be grateful if some one help me about this. an example is appreciated.
I am working with MIKROC for PICs.


Thank you.
 

could you please provide me with a example? I have checked all possible error rising aspects, but still no result.
 

Show your code first. There are very good help available in MikroC where volatile principle explained.
 

Code:
volatile unsigned char i; 

void main()
{
  TRISC = 0; // To configure PORTC as output port
  OPTION_REG.INTEDG = 1; // Set Rising Edge Trigger for INT
  INTCON.GIE = 1; // Enable The Global Interrupt
  INTCON.INTE = 1; // Enable INT
  while(1)
  {
    PORTC =i; //Set some value at PortC
  }
}

void interrupt() //  ISR
{
 INTCON.INTF=0; // Clear the interrupt 0 flag
  i++;

}
 

I can not write a value in PORTC in ISR loop.
Why, particularly?
Which problem do you think to solve by writing to port SFR indirectly?
 

Yes you can
and also if you think there is problem then use one flag raise the flag in ISR
and check the flag in main loop and zero it again.
 

hello

What PIC family
if 18F use LATC instead PORTC ...to write value into the PORT


show us your code...
 

Hi,
thank you all for taking time and reply.
The code is as below.
the code is just composed for examination; just for checking what is going wrong in process.
by this code PIC have to convert nine analog channels to digital ( channel AN2 and AN3 are used as reference voltage for A/D converter) then do other computations. in this code I just want to have the result of the A/D conversion on PORTC by external interrupt. pin RB7 and RB6 are used for channel selection ( in this code I just try 4 first results). the variable rep is shown correct on PORTD but A/D results is get not selected correctly. I mean when I put RB7=0 and RB6=0 the output on PORTC is 00110001 ( about 1v). and AN0 is on output when I put RB7=0 and RB6=1. all other results are shifted in this way AN1 in on output when rep=2 etc. why?

Thank you.

Code:
unsigned int ADRES;
unsigned char ADL_byte, ADH_byte, cap, cap2;
unsigned short OUTM[4];
unsigned int ADR;
//const unsigned short REFC[11] = {64, 128, 0, 0 , 192, 64, 128, 192, 64, 128, 192} ;
void interrupt() {
unsigned short rep;
if (INTCON.INT0IF){
INTCON.INTF=0;
rep=0;
rep=(PORTB.B7<<1)|PORTB.B6;
PORTC= OUTM[rep];
PORTD=rep;}}
unsigned int ADREAD(unsigned char channel){
   if (channel>10) channel=0;
   GO_DONE_BIT=1;
   while(GO_DONE_BIT);
   ADL_byte=ADRESL;
   ADH_byte=ADRESH;
   // ADR=(ADH_byte<<8)| ADL_byte;
   ADR=ADH_byte;
   if(channel.B3 != 0){
    channel.B3=0;
    ADCON0=0x83 | (channel<<3); }
   else
   { ADCON0=0x81 | (channel<<3);}
   return ADR;}
void main() {
  OPTION_REG= 0xc7;
    CMCON=7;
  ADCON0=0x81;
  ADCON1=0x04;
  ADCON2=0x10;    //0x30
  INTCON=0x90;
  TRISA=0xFF;
  TRISB=0xFF;
  TRISC=0x00;
  PORTC=0x01;
  TRISD=0x00;
  PORTD=0x01;
  delay_ms(4000);
  PORTC=0x00;
  PORTD=0x00;
  while(1)
  {cap2=0  ;
    for (cap=0; cap<4; cap++)
    { if (cap==2) cap2=cap2+2;
      ADRES=ADREAD(cap2);
     OUTM[cap] = ADRES;
       cap2+=1;
       delay_ms(1);
      }
    } }

P.S: I use PIC 16F777
 
Last edited:

Please zip and post the mikroC project files so that it can be modified.
 

hi all,
I have checked it. the output is ok when there is other computation than A/D conversion. for example I gave constant value to OUTM[4]; I have correct output. I mean when rep=1, the second value is on output and... . the n I examine main loop by obtaining OUTM by some mathematical operation, it is ok too. I am sure there is a problem in function of A/D conversion that add 1 to cap value, but I cannot figure out what is it. I have attached the file to this post. I would be grateful if you take a look.

Thank you.
 

Attachments

  • ADCtest.zip
    17.9 KB · Views: 53

hello,


PB is choose of the ADC channel..

simply write
Code:
 ADCON0=0x81 | (channel<<3);}

to have bit1 always zero ..

and choose chanel
before convert
 

to use other computation time to charge capacitor of A/D converter for next step, I choose next channel right after the conversion of the present channel. all of the lines are ok!
I have found the error
Code:
  {cap2=0  ;
have to turn to
Code:
  {cap2=1  ;
because channel 2 and 3 are used for reference voltage. since I charge capacitor of A/D converter in previous step, in turn in loop instead of channel 4, channel 3 will be chosen, while it is used for reference voltage.

Thank you all for your care.
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top